blob: 1d82c1de3e28597380a0ced4936f4b014b5ed5a5 [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
Thomas Vachuska5630c612015-03-24 12:24:12 -070023# Clean the previous Karaf directory if requested and if it exists.
24if [ "$1" = "clean" ]; then
25 shift
26 [ -d $KARAF_ROOT ] && rm -fr $KARAF_ROOT $STAGE/apps $STAGE/config
27fi
28
Thomas Vachuskaba082b82015-05-20 13:47:38 -070029ONOS_IP=${ONOS_IP:-127.0.0.1}
Thomas Vachuskafcd61142015-04-23 13:59:08 -070030IP="${1:-$ONOS_IP}"
31
32# If IP was not given, nor configured attempt to use ONOS_NIC env. variable
33if [ -z "$IP" -a -n "$ONOS_NIC" ]; then
Thomas Vachuska60fc8422015-04-23 16:18:34 -070034 IP=$(validateIp $ONOS_NIC)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070035 [ -z "$IP" ] && echo "No adapter with IP matching $ONOS_NIC found!"
36else
37 # Otherwise, verify that the IP address given exists among the adapters.
38 saveIp=$IP
Thomas Vachuska60fc8422015-04-23 16:18:34 -070039 IP=$(validateIp $IP)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070040 [ -z "$IP" ] && echo "No adapter with IP $saveIp found!"
41fi
42
43# If IP is still not surmised or if usage was requested, show usage and IPs.
44if [ -z "$IP" -o "$1" = "-?" -o "$1" = "-h" -o "$1" = "--help" ]; then
Thomas Vachuska5630c612015-03-24 12:24:12 -070045 echo "usage: $(basename $0) [clean] <ip-address>"
46 echo "Available IP addresses are:"
Thomas Vachuska60fc8422015-04-23 16:18:34 -070047 validateIp .
Thomas Vachuska5630c612015-03-24 12:24:12 -070048 exit 1
49fi
50
Thomas Vachuska5630c612015-03-24 12:24:12 -070051SUBNET="$(echo $IP | cut -d. -f1-3)"
52
53# Bail on any errors
54set -e
55
56# Check if Apache Karaf is already installed.
57if [ ! -d $KARAF_ROOT ]; then
58 # Check if Apache Karaf bits are available and if not, fetch them.
59 if [ ! -f $KARAF_TAR ]; then
60 echo "Downloading $KARAF_TAR..."
61 curl -sL http://downloads.onosproject.org/third-party/apache-karaf-$KARAF_VERSION.tar.gz > $KARAF_TAR
62 fi
63 [ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ] && \
64 echo "Apache Karaf bits $KARAF_ZIP or $KARAF_TAR not found" && exit 1
65
66 echo "Unpacking $KARAF_TAR to $STAGE..."
67 mkdir -p $STAGE
68 cd $STAGE
69 tar zxf $KARAF_TAR
70 rm -rf $KARAF_ROOT/demos
71fi
72
73if ! grep -q "/onos-features/" $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
74 # Patch the Apache Karaf distribution file to add ONOS features repository
75 echo "Adding ONOS feature repository..."
76 perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
77 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
78fi
79
80if ! grep -q ",onos-api," $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
81 # Patch the Apache Karaf distribution file to load default ONOS boot features
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070082 export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
Thomas Vachuska5630c612015-03-24 12:24:12 -070083 echo "Adding ONOS boot features $BOOT_FEATURES..."
84 perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
85 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
86fi
87
88if [ ! -f $KARAF_ROOT/lib/onos-branding-$ONOS_POM_VERSION.jar ]; then
89 # Patch the Apache Karaf distribution with ONOS branding bundle
90 echo "Branding as ONOS..."
91 rm -f $KARAF_ROOT/lib/onos-branding-*.jar
92 cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-$ONOS_POM_VERSION.jar \
93 $KARAF_ROOT/lib
94fi
95
Thomas Vachuska9b171c52015-04-28 12:01:07 -070096echo "Creating local cluster configs for IP $IP..."
97[ -d $STAGE/config ] || mkdir -p $STAGE/config
98cat > $STAGE/config/cluster.json <<EOF
Thomas Vachuska5630c612015-03-24 12:24:12 -070099 { "ipPrefix": "$SUBNET.*",
100 "nodes":[ { "id": "$IP", "ip": "$IP", "tcpPort": 9876 }]}
101EOF
102
Thomas Vachuska9b171c52015-04-28 12:01:07 -0700103cat > $STAGE/config/tablets.json <<EOF
Thomas Vachuska69bf7c12015-05-20 13:59:22 -0700104 { "nodes": [ { "ip": "$IP", "id": "$IP", "tcpPort": 9876 }],
105 "partitions": { "p1": [ { "ip": "$IP", "id": "$IP", "tcpPort": 9876 }]}}
Thomas Vachuska5630c612015-03-24 12:24:12 -0700106EOF
Thomas Vachuska5630c612015-03-24 12:24:12 -0700107
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700108echo "Staging builtin apps..."
109rm -fr $STAGE/apps
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700110onos-stage-apps $STAGE/apps $KARAF_ROOT/system
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700111
Thomas Vachuskafcd61142015-04-23 13:59:08 -0700112ACTIVE_APPS=${ONOS_APPS:-drivers,openflow}
113echo "Customizing apps to be auto-activated: $ACTIVE_APPS..."
114for app in ${ACTIVE_APPS//,/ }; do
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700115 touch $STAGE/apps/org.onosproject.$app/active
116done