blob: edb2f336aeb69f58ab702f7b9cb221dce38daa78 [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
Thomas Vachuskabc833672016-01-04 15:12:50 -080025 case $1 in
Brian O'Connorbcfc0542015-12-09 22:26:45 -080026 clean)
Thomas Vachuskabc833672016-01-04 15:12:50 -080027 CLEAN="true";;
Brian O'Connorbcfc0542015-12-09 22:26:45 -080028 secure)
Thomas Vachuskabc833672016-01-04 15:12:50 -080029 SECURE="true";;
Brian O'Connorbcfc0542015-12-09 22:26:45 -080030 *)
Thomas Vachuskabc833672016-01-04 15:12:50 -080031 break;;
32 esac
33 shift
Brian O'Connorbcfc0542015-12-09 22:26:45 -080034done
Thomas Vachuska5630c612015-03-24 12:24:12 -070035
Thomas Vachuskaba082b82015-05-20 13:47:38 -070036ONOS_IP=${ONOS_IP:-127.0.0.1}
Thomas Vachuskafcd61142015-04-23 13:59:08 -070037IP="${1:-$ONOS_IP}"
38
Thomas Vachuskabc833672016-01-04 15:12:50 -080039# If the installed version does not line-up with ONOS_POM_VERSION force clean install
40if [ ! -f $KARAF_ROOT/etc/org.apache.karaf.features.cfg ] || \
41 ! grep -q "mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features" \
42 $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
43 echo "Existing ONOS Karaf uses version different from $ONOS_POM_VERSION; forcing clean install..."
44 CLEAN="true"
45fi
46
47# If clean option was specified, wipe-out existing installation
48if [ "$CLEAN" = "true" ]; then
Jian Licc752fa2016-03-25 18:15:22 -070049 echo "Removing existing ONOS Karaf, apps, data and config directories..."
50 [ -d $KARAF_ROOT ] && rm -fr $KARAF_ROOT $STAGE/apps $STAGE/data $STAGE/config
Thomas Vachuskabc833672016-01-04 15:12:50 -080051fi
52
Thomas Vachuskafcd61142015-04-23 13:59:08 -070053# If IP was not given, nor configured attempt to use ONOS_NIC env. variable
54if [ -z "$IP" -a -n "$ONOS_NIC" ]; then
Thomas Vachuska60fc8422015-04-23 16:18:34 -070055 IP=$(validateIp $ONOS_NIC)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070056 [ -z "$IP" ] && echo "No adapter with IP matching $ONOS_NIC found!"
57else
58 # Otherwise, verify that the IP address given exists among the adapters.
59 saveIp=$IP
Thomas Vachuska60fc8422015-04-23 16:18:34 -070060 IP=$(validateIp $IP)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070061 [ -z "$IP" ] && echo "No adapter with IP $saveIp found!"
62fi
63
64# If IP is still not surmised or if usage was requested, show usage and IPs.
65if [ -z "$IP" -o "$1" = "-?" -o "$1" = "-h" -o "$1" = "--help" ]; then
Brian O'Connorbcfc0542015-12-09 22:26:45 -080066 echo "usage: $(basename $0) [clean] [secure] <ip-address>"
Thomas Vachuska5630c612015-03-24 12:24:12 -070067 echo "Available IP addresses are:"
Thomas Vachuska60fc8422015-04-23 16:18:34 -070068 validateIp .
Thomas Vachuska5630c612015-03-24 12:24:12 -070069 exit 1
70fi
71
Thomas Vachuska5630c612015-03-24 12:24:12 -070072SUBNET="$(echo $IP | cut -d. -f1-3)"
73
74# Bail on any errors
75set -e
76
77# Check if Apache Karaf is already installed.
78if [ ! -d $KARAF_ROOT ]; then
79 # Check if Apache Karaf bits are available and if not, fetch them.
80 if [ ! -f $KARAF_TAR ]; then
81 echo "Downloading $KARAF_TAR..."
HIGUCHI Yuta9fe56ce2015-11-20 10:24:01 -080082 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 -070083 fi
84 [ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ] && \
85 echo "Apache Karaf bits $KARAF_ZIP or $KARAF_TAR not found" && exit 1
86
87 echo "Unpacking $KARAF_TAR to $STAGE..."
88 mkdir -p $STAGE
89 cd $STAGE
90 tar zxf $KARAF_TAR
91 rm -rf $KARAF_ROOT/demos
92fi
93
Brian O'Connorbcfc0542015-12-09 22:26:45 -080094if [ "$SECURE" = "true" ]; then
95 echo "Enabling security mode ONOS..."
96
97 # SM-ONOS step 1: downgrade felix config admin
98 FELIX_CFG_ADMIN=${FELIX_CFG_ADMIN:-~/Downloads/org.apache.felix.configadmin-1.6.0.jar}
99 if [ ! -f $FELIX_CFG_ADMIN ]; then
100 echo "Downloading $FELIX_CFG_ADMIN..."
101 curl -sL http://archive.apache.org/dist/felix/org.apache.felix.configadmin-1.6.0.jar > $FELIX_CFG_ADMIN
102 fi
103 [ ! -f $FELIX_CFG_ADMIN ] && \
104 echo "Felix config admin not found: $FELIX_CFG_ADMIN" && exit 1
105
106 mkdir -p $KARAF_ROOT/system/org/apache/felix/org.apache.felix.configadmin/1.6.0
107 cp $FELIX_CFG_ADMIN $KARAF_ROOT/system/org/apache/felix/org.apache.felix.configadmin/1.6.0
108 perl -pi.old -e "s|org.apache.felix.configadmin/1.8.0|org.apache.felix.configadmin/1.6.0|g" \
109 $KARAF_ROOT/etc/startup.properties
110
111 # SM-ONOS step 2: stage ONOS Felix framework security (will get downloaded on demand); end
112
113 # SM-ONOS step 3.1: configure karaf
114 perl -pi.old -e "s|#java.security.policy|java.security.policy|" \
115 $KARAF_ROOT/etc/system.properties
116 perl -pi.old -e "s|#org.osgi.framework.security|org.osgi.framework.security|" \
117 $KARAF_ROOT/etc/system.properties
118fi
119
Thomas Vachuska5630c612015-03-24 12:24:12 -0700120if ! grep -q "/onos-features/" $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
121 # Patch the Apache Karaf distribution file to add ONOS features repository
122 echo "Adding ONOS feature repository..."
123 perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
124 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
125fi
126
127if ! grep -q ",onos-api," $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
128 # Patch the Apache Karaf distribution file to load default ONOS boot features
Thomas Vachuskabf916ea2015-05-20 18:24:34 -0700129 export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
Brian O'Connorbcfc0542015-12-09 22:26:45 -0800130 if [ "$SECURE" = "true" ]; then
131 # SM-ONOS Step 3.2: add onos-security to featuresBoot
132 export BOOT_FEATURES="onos-security,$BOOT_FEATURES"
133 fi
Thomas Vachuska5630c612015-03-24 12:24:12 -0700134 echo "Adding ONOS boot features $BOOT_FEATURES..."
135 perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
136 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
137fi
138
139if [ ! -f $KARAF_ROOT/lib/onos-branding-$ONOS_POM_VERSION.jar ]; then
140 # Patch the Apache Karaf distribution with ONOS branding bundle
141 echo "Branding as ONOS..."
142 rm -f $KARAF_ROOT/lib/onos-branding-*.jar
143 cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-$ONOS_POM_VERSION.jar \
144 $KARAF_ROOT/lib
145fi
146
Thomas Vachuska9b171c52015-04-28 12:01:07 -0700147echo "Creating local cluster configs for IP $IP..."
148[ -d $STAGE/config ] || mkdir -p $STAGE/config
149cat > $STAGE/config/cluster.json <<EOF
Madan Jampani596f2662015-10-23 14:35:23 -0700150{
151 "name": "default",
152 "nodes": [ {"id": "$IP", "ip": "$IP", "port": 9876 } ],
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800153 "partitions": [ { "id": 0, "members": [ "$IP" ] }, { "id": 1, "members": [ "$IP" ] } ]
Madan Jampani596f2662015-10-23 14:35:23 -0700154}
Thomas Vachuska5630c612015-03-24 12:24:12 -0700155EOF
Thomas Vachuska5630c612015-03-24 12:24:12 -0700156
Jonathan Hart2aa40682015-08-03 10:53:51 -0700157if [ "$CLEAN" = "true" ]; then
158 echo "Copying package configs..."
159 cp -r $ONOS_ROOT/tools/package/etc/* $KARAF_ROOT/etc/
160 cp -r $ONOS_ROOT/tools/package/config/* $STAGE/config/
161fi
162
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700163echo "Staging builtin apps..."
164rm -fr $STAGE/apps
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700165onos-stage-apps $STAGE/apps $KARAF_ROOT/system
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700166
Thomas Vachuskafcd61142015-04-23 13:59:08 -0700167ACTIVE_APPS=${ONOS_APPS:-drivers,openflow}
168echo "Customizing apps to be auto-activated: $ACTIVE_APPS..."
169for app in ${ACTIVE_APPS//,/ }; do
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700170 touch $STAGE/apps/org.onosproject.$app/active
171done