blob: 5e17d035ad21d08172c87c1d0c1292ae2e615f27 [file] [log] [blame]
Thomas Vachuska5630c612015-03-24 12:24:12 -07001#!/bin/bash
2# -----------------------------------------------------------------------------
3# Downloads and sets-up Apache Karaf as a basis for running ONOS locally
4# as a single-instance.
Thomas Vachuskafcd61142015-04-23 13:59:08 -07005#
6# Note that this in no way impacts the method for running ONOS remotely.
7# For that, one should use onos-package and onos-install tools.
Thomas Vachuska5630c612015-03-24 12:24:12 -07008# -----------------------------------------------------------------------------
9
10[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
11. $ONOS_ROOT/tools/build/envDefaults
12
13# TODO: consider putting this under ~/Applications/onos/apache-karaf-...
14export KARAF_ROOT=${KARAF_ROOT:-~/Applications/apache-karaf-$KARAF_VERSION}
15export STAGE=$(dirname $KARAF_ROOT)
16
Thomas Vachuska60fc8422015-04-23 16:18:34 -070017# Validates the specified IP regular expression against existing adapters.
18# Excludes local-loopback.
19function validateIp {
Thomas Vachuskaba082b82015-05-20 13:47:38 -070020 ifconfig | awk '{ print $2}' | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | grep $1
Thomas Vachuska60fc8422015-04-23 16:18:34 -070021}
22
Brian O'Connorbcfc0542015-12-09 22:26:45 -080023# Parse optional arguments
24while [[ $# > 0 ]]; do
25case $1 in
26 clean)
Brian O'Connor72e74bf2015-12-11 13:56:50 -080027 echo "Removing existing karaf, apps, and config directories..."
Brian O'Connorbcfc0542015-12-09 22:26:45 -080028 CLEAN="true"
29 [ -d $KARAF_ROOT ] && rm -fr $KARAF_ROOT $STAGE/apps $STAGE/config
30 ;;
31 secure)
32 SECURE="true"
33 ;;
34 *)
35 break
36 ;;
37esac
38shift
39done
Thomas Vachuska5630c612015-03-24 12:24:12 -070040
Thomas Vachuskaba082b82015-05-20 13:47:38 -070041ONOS_IP=${ONOS_IP:-127.0.0.1}
Thomas Vachuskafcd61142015-04-23 13:59:08 -070042IP="${1:-$ONOS_IP}"
43
44# If IP was not given, nor configured attempt to use ONOS_NIC env. variable
45if [ -z "$IP" -a -n "$ONOS_NIC" ]; then
Thomas Vachuska60fc8422015-04-23 16:18:34 -070046 IP=$(validateIp $ONOS_NIC)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070047 [ -z "$IP" ] && echo "No adapter with IP matching $ONOS_NIC found!"
48else
49 # Otherwise, verify that the IP address given exists among the adapters.
50 saveIp=$IP
Thomas Vachuska60fc8422015-04-23 16:18:34 -070051 IP=$(validateIp $IP)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070052 [ -z "$IP" ] && echo "No adapter with IP $saveIp found!"
53fi
54
55# If IP is still not surmised or if usage was requested, show usage and IPs.
56if [ -z "$IP" -o "$1" = "-?" -o "$1" = "-h" -o "$1" = "--help" ]; then
Brian O'Connorbcfc0542015-12-09 22:26:45 -080057 echo "usage: $(basename $0) [clean] [secure] <ip-address>"
Thomas Vachuska5630c612015-03-24 12:24:12 -070058 echo "Available IP addresses are:"
Thomas Vachuska60fc8422015-04-23 16:18:34 -070059 validateIp .
Thomas Vachuska5630c612015-03-24 12:24:12 -070060 exit 1
61fi
62
Thomas Vachuska5630c612015-03-24 12:24:12 -070063SUBNET="$(echo $IP | cut -d. -f1-3)"
64
65# Bail on any errors
66set -e
67
68# Check if Apache Karaf is already installed.
69if [ ! -d $KARAF_ROOT ]; then
70 # Check if Apache Karaf bits are available and if not, fetch them.
71 if [ ! -f $KARAF_TAR ]; then
72 echo "Downloading $KARAF_TAR..."
HIGUCHI Yuta9fe56ce2015-11-20 10:24:01 -080073 curl -sL http://downloads.onosproject.org/third-party/apache-karaf-$KARAF_VERSION.tar.gz --create-dirs -o $KARAF_TAR
Thomas Vachuska5630c612015-03-24 12:24:12 -070074 fi
75 [ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ] && \
76 echo "Apache Karaf bits $KARAF_ZIP or $KARAF_TAR not found" && exit 1
77
78 echo "Unpacking $KARAF_TAR to $STAGE..."
79 mkdir -p $STAGE
80 cd $STAGE
81 tar zxf $KARAF_TAR
82 rm -rf $KARAF_ROOT/demos
83fi
84
Brian O'Connorbcfc0542015-12-09 22:26:45 -080085if [ "$SECURE" = "true" ]; then
86 echo "Enabling security mode ONOS..."
87
88 # SM-ONOS step 1: downgrade felix config admin
89 FELIX_CFG_ADMIN=${FELIX_CFG_ADMIN:-~/Downloads/org.apache.felix.configadmin-1.6.0.jar}
90 if [ ! -f $FELIX_CFG_ADMIN ]; then
91 echo "Downloading $FELIX_CFG_ADMIN..."
92 curl -sL http://archive.apache.org/dist/felix/org.apache.felix.configadmin-1.6.0.jar > $FELIX_CFG_ADMIN
93 fi
94 [ ! -f $FELIX_CFG_ADMIN ] && \
95 echo "Felix config admin not found: $FELIX_CFG_ADMIN" && exit 1
96
97 mkdir -p $KARAF_ROOT/system/org/apache/felix/org.apache.felix.configadmin/1.6.0
98 cp $FELIX_CFG_ADMIN $KARAF_ROOT/system/org/apache/felix/org.apache.felix.configadmin/1.6.0
99 perl -pi.old -e "s|org.apache.felix.configadmin/1.8.0|org.apache.felix.configadmin/1.6.0|g" \
100 $KARAF_ROOT/etc/startup.properties
101
102 # SM-ONOS step 2: stage ONOS Felix framework security (will get downloaded on demand); end
103
104 # SM-ONOS step 3.1: configure karaf
105 perl -pi.old -e "s|#java.security.policy|java.security.policy|" \
106 $KARAF_ROOT/etc/system.properties
107 perl -pi.old -e "s|#org.osgi.framework.security|org.osgi.framework.security|" \
108 $KARAF_ROOT/etc/system.properties
109fi
110
Thomas Vachuska5630c612015-03-24 12:24:12 -0700111if ! grep -q "/onos-features/" $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
112 # Patch the Apache Karaf distribution file to add ONOS features repository
113 echo "Adding ONOS feature repository..."
114 perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
115 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
116fi
117
118if ! grep -q ",onos-api," $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
119 # Patch the Apache Karaf distribution file to load default ONOS boot features
Thomas Vachuskabf916ea2015-05-20 18:24:34 -0700120 export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
Brian O'Connorbcfc0542015-12-09 22:26:45 -0800121 if [ "$SECURE" = "true" ]; then
122 # SM-ONOS Step 3.2: add onos-security to featuresBoot
123 export BOOT_FEATURES="onos-security,$BOOT_FEATURES"
124 fi
Thomas Vachuska5630c612015-03-24 12:24:12 -0700125 echo "Adding ONOS boot features $BOOT_FEATURES..."
126 perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
127 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
128fi
129
130if [ ! -f $KARAF_ROOT/lib/onos-branding-$ONOS_POM_VERSION.jar ]; then
131 # Patch the Apache Karaf distribution with ONOS branding bundle
132 echo "Branding as ONOS..."
133 rm -f $KARAF_ROOT/lib/onos-branding-*.jar
134 cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-$ONOS_POM_VERSION.jar \
135 $KARAF_ROOT/lib
136fi
137
Thomas Vachuska9b171c52015-04-28 12:01:07 -0700138echo "Creating local cluster configs for IP $IP..."
139[ -d $STAGE/config ] || mkdir -p $STAGE/config
140cat > $STAGE/config/cluster.json <<EOF
Madan Jampani596f2662015-10-23 14:35:23 -0700141{
142 "name": "default",
143 "nodes": [ {"id": "$IP", "ip": "$IP", "port": 9876 } ],
144 "partitions": [ { "name": "p1", "members": [ "$IP" ] } ]
145}
Thomas Vachuska5630c612015-03-24 12:24:12 -0700146EOF
Thomas Vachuska5630c612015-03-24 12:24:12 -0700147
Jonathan Hart2aa40682015-08-03 10:53:51 -0700148if [ "$CLEAN" = "true" ]; then
149 echo "Copying package configs..."
150 cp -r $ONOS_ROOT/tools/package/etc/* $KARAF_ROOT/etc/
151 cp -r $ONOS_ROOT/tools/package/config/* $STAGE/config/
152fi
153
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700154echo "Staging builtin apps..."
155rm -fr $STAGE/apps
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700156onos-stage-apps $STAGE/apps $KARAF_ROOT/system
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700157
Thomas Vachuskafcd61142015-04-23 13:59:08 -0700158ACTIVE_APPS=${ONOS_APPS:-drivers,openflow}
159echo "Customizing apps to be auto-activated: $ACTIVE_APPS..."
160for app in ${ACTIVE_APPS//,/ }; do
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700161 touch $STAGE/apps/org.onosproject.$app/active
162done