blob: e0886cf0c1fb43cabf0151140196306dcaa4e8cc [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 {
20 [ $(uname) = "Darwin" ] && \
21 ifconfig | grep 'inet ' | cut -d\ -f2 | grep -v "127\.0\.0\.1" | grep $1 || \
22 ifconfig | grep 'inet addr:' | cut -d: -f2 | cut -d\ -f1 | grep -v "127\.0\.0\.1" | grep $1
23}
24
Thomas Vachuska5630c612015-03-24 12:24:12 -070025# Clean the previous Karaf directory if requested and if it exists.
26if [ "$1" = "clean" ]; then
27 shift
28 [ -d $KARAF_ROOT ] && rm -fr $KARAF_ROOT $STAGE/apps $STAGE/config
29fi
30
Thomas Vachuskafcd61142015-04-23 13:59:08 -070031IP="${1:-$ONOS_IP}"
32
33# If IP was not given, nor configured attempt to use ONOS_NIC env. variable
34if [ -z "$IP" -a -n "$ONOS_NIC" ]; then
Thomas Vachuska60fc8422015-04-23 16:18:34 -070035 IP=$(validateIp $ONOS_NIC)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070036 [ -z "$IP" ] && echo "No adapter with IP matching $ONOS_NIC found!"
37else
38 # Otherwise, verify that the IP address given exists among the adapters.
39 saveIp=$IP
Thomas Vachuska60fc8422015-04-23 16:18:34 -070040 IP=$(validateIp $IP)
Thomas Vachuskafcd61142015-04-23 13:59:08 -070041 [ -z "$IP" ] && echo "No adapter with IP $saveIp found!"
42fi
43
44# If IP is still not surmised or if usage was requested, show usage and IPs.
45if [ -z "$IP" -o "$1" = "-?" -o "$1" = "-h" -o "$1" = "--help" ]; then
Thomas Vachuska5630c612015-03-24 12:24:12 -070046 echo "usage: $(basename $0) [clean] <ip-address>"
47 echo "Available IP addresses are:"
Thomas Vachuska60fc8422015-04-23 16:18:34 -070048 validateIp .
Thomas Vachuska5630c612015-03-24 12:24:12 -070049 exit 1
50fi
51
Thomas Vachuska5630c612015-03-24 12:24:12 -070052SUBNET="$(echo $IP | cut -d. -f1-3)"
53
54# Bail on any errors
55set -e
56
57# Check if Apache Karaf is already installed.
58if [ ! -d $KARAF_ROOT ]; then
59 # Check if Apache Karaf bits are available and if not, fetch them.
60 if [ ! -f $KARAF_TAR ]; then
61 echo "Downloading $KARAF_TAR..."
62 curl -sL http://downloads.onosproject.org/third-party/apache-karaf-$KARAF_VERSION.tar.gz > $KARAF_TAR
63 fi
64 [ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ] && \
65 echo "Apache Karaf bits $KARAF_ZIP or $KARAF_TAR not found" && exit 1
66
67 echo "Unpacking $KARAF_TAR to $STAGE..."
68 mkdir -p $STAGE
69 cd $STAGE
70 tar zxf $KARAF_TAR
71 rm -rf $KARAF_ROOT/demos
72fi
73
74if ! grep -q "/onos-features/" $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
75 # Patch the Apache Karaf distribution file to add ONOS features repository
76 echo "Adding ONOS feature repository..."
77 perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
78 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
79fi
80
81if ! grep -q ",onos-api," $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
82 # Patch the Apache Karaf distribution file to load default ONOS boot features
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070083 export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-cli,onos-rest,onos-gui"
Thomas Vachuska5630c612015-03-24 12:24:12 -070084 echo "Adding ONOS boot features $BOOT_FEATURES..."
85 perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
86 $KARAF_ROOT/etc/org.apache.karaf.features.cfg
87fi
88
89if [ ! -f $KARAF_ROOT/lib/onos-branding-$ONOS_POM_VERSION.jar ]; then
90 # Patch the Apache Karaf distribution with ONOS branding bundle
91 echo "Branding as ONOS..."
92 rm -f $KARAF_ROOT/lib/onos-branding-*.jar
93 cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-$ONOS_POM_VERSION.jar \
94 $KARAF_ROOT/lib
95fi
96
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070097if [ ! -d $STAGE/config ]; then
Thomas Vachuska5630c612015-03-24 12:24:12 -070098 echo "Creating local cluster configs for IP $IP..."
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070099 mkdir -p $STAGE/config
Thomas Vachuska5630c612015-03-24 12:24:12 -0700100 cat > $STAGE/config/cluster.json <<EOF
101 { "ipPrefix": "$SUBNET.*",
102 "nodes":[ { "id": "$IP", "ip": "$IP", "tcpPort": 9876 }]}
103EOF
104
105 cat > $STAGE/config/tablets.json <<EOF
106 { "nodes": [ { "ip": "$IP", "id": "$IP", "tcpPort": 7238 }],
107 "partitions": { "p1": [ { "ip": "$IP", "id": "$IP", "tcpPort": 7238 }]}}
108EOF
109fi
110
Thomas Vachuskafcd61142015-04-23 13:59:08 -0700111echo "Setting up hazelcast.xml for subnet $SUBNET.*..."
Thomas Vachuska5630c612015-03-24 12:24:12 -0700112cp $ONOS_ROOT/tools/package/etc/hazelcast.xml $KARAF_ROOT/etc/hazelcast.xml
113perl -pi.old -e "s/192.168.56/$SUBNET/" $KARAF_ROOT/etc/hazelcast.xml
114perl -pi.old -e "s/ <name>onos</ <name>$IP</" $KARAF_ROOT/etc/hazelcast.xml
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700115
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700116echo "Staging builtin apps..."
117rm -fr $STAGE/apps
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -0700118onos-stage-apps $STAGE/apps $KARAF_ROOT/system
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700119
Thomas Vachuskafcd61142015-04-23 13:59:08 -0700120ACTIVE_APPS=${ONOS_APPS:-drivers,openflow}
121echo "Customizing apps to be auto-activated: $ACTIVE_APPS..."
122for app in ${ACTIVE_APPS//,/ }; do
Thomas Vachuskac4cb1002015-03-29 10:28:26 -0700123 touch $STAGE/apps/org.onosproject.$app/active
124done