blob: 5ff7279510f34260bed9dc8ca5b1ef61e31ae5e3 [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 {
Ayaka Koshibe98c31652016-05-26 11:48:47 -070020 ifconfig | sed -ne 's:inet[^6][ adr:]*\([0-9.]*\).*:\1: p' | 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
Changhoon Yoona68e6f92016-05-02 15:36:43 +0900108 perl -pi.old -e "s|^(.*org.apache.felix.configadmin.*)|mvn\\\\:org.apache.felix/org.apache.felix.configadmin/1.6.0 = 10|" \
Brian O'Connorbcfc0542015-12-09 22:26:45 -0800109 $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..."
Brian O'Connorfa5e0762016-04-08 15:51:34 -0700123 perl -pi.old -e "s|^(featuresRepositories=.*)|featuresRepositories=mvn:org.apache.karaf.features/standard/$KARAF_VERSION/xml/features,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
Thomas Vachuska5630c612015-03-24 12:24:12 -0700124 $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
Brian O'Connorfa5e0762016-04-08 15:51:34 -0700129 BOOT_FEATURES="standard,ssh,webconsole"
Brian O'Connorbcfc0542015-12-09 22:26:45 -0800130 if [ "$SECURE" = "true" ]; then
131 # SM-ONOS Step 3.2: add onos-security to featuresBoot
Brian O'Connorfa5e0762016-04-08 15:51:34 -0700132 BOOT_FEATURES="$BOOT_FEATURES,onos-security"
Brian O'Connorbcfc0542015-12-09 22:26:45 -0800133 fi
Brian O'Connorfa5e0762016-04-08 15:51:34 -0700134 BOOT_FEATURES="$BOOT_FEATURES,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
Thomas Vachuska5630c612015-03-24 12:24:12 -0700135 echo "Adding ONOS boot features $BOOT_FEATURES..."
Brian O'Connorfa5e0762016-04-08 15:51:34 -0700136 perl -pi.old -e "s|^(featuresBoot=.*)|featuresBoot=$BOOT_FEATURES|" \
Thomas Vachuska5630c612015-03-24 12:24:12 -0700137 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
138fi
139
140if [ ! -f $KARAF_ROOT/lib/onos-branding-$ONOS_POM_VERSION.jar ]; then
141 # Patch the Apache Karaf distribution with ONOS branding bundle
142 echo "Branding as ONOS..."
143 rm -f $KARAF_ROOT/lib/onos-branding-*.jar
144 cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-$ONOS_POM_VERSION.jar \
145 $KARAF_ROOT/lib
146fi
147
Thomas Vachuska9b171c52015-04-28 12:01:07 -0700148echo "Creating local cluster configs for IP $IP..."
149[ -d $STAGE/config ] || mkdir -p $STAGE/config
150cat > $STAGE/config/cluster.json <<EOF
Madan Jampani596f2662015-10-23 14:35:23 -0700151{
152 "name": "default",
153 "nodes": [ {"id": "$IP", "ip": "$IP", "port": 9876 } ],
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800154 "partitions": [ { "id": 0, "members": [ "$IP" ] }, { "id": 1, "members": [ "$IP" ] } ]
Madan Jampani596f2662015-10-23 14:35:23 -0700155}
Thomas Vachuska5630c612015-03-24 12:24:12 -0700156EOF
Thomas Vachuska5630c612015-03-24 12:24:12 -0700157
Jonathan Hart2aa40682015-08-03 10:53:51 -0700158if [ "$CLEAN" = "true" ]; then
159 echo "Copying package configs..."
160 cp -r $ONOS_ROOT/tools/package/etc/* $KARAF_ROOT/etc/
161 cp -r $ONOS_ROOT/tools/package/config/* $STAGE/config/
162fi
163
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700164echo "Staging builtin apps..."
165rm -fr $STAGE/apps
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700166onos-stage-apps $STAGE/apps $KARAF_ROOT/system
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700167
Thomas Vachuskafcd61142015-04-23 13:59:08 -0700168ACTIVE_APPS=${ONOS_APPS:-drivers,openflow}
169echo "Customizing apps to be auto-activated: $ACTIVE_APPS..."
170for app in ${ACTIVE_APPS//,/ }; do
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700171 touch $STAGE/apps/org.onosproject.$app/active
172done