blob: 3f369a3320312d4a97ac1cb901dbe62e445c8858 [file] [log] [blame]
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -07001#!/bin/bash
2# -----------------------------------------------------------------------------
3# Runs ONOS from distributable onos.tar.gz
4# -----------------------------------------------------------------------------
5
6ONOS_TAR=
7
8cd /tmp
9
10# Kill any running instances
11[ -f /tmp/onos.pid ] && kill -9 $(cat /tmp/onos.pid) &>/dev/null
12
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070013ONOS_DIR=$(tar tf $ONOS_TAR | head -n 1 | cut -d/ -f1)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070014ONOS_MD5=$ONOS_DIR/CHECKSUM
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070015
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070016# Extract MD5 of the ONOS tar file and of the previous installation, if one exists
Ray Milkey2fa4f182016-11-07 10:14:10 -080017md5cmd=''
Ray Milkey0bb987e2016-11-11 15:02:51 -080018md5cmdprm=''
Ray Milkey2fa4f182016-11-07 10:14:10 -080019case "$OSTYPE" in
Ray Milkey0bb987e2016-11-11 15:02:51 -080020 darwin*) md5cmd='md5' ; md5cmdprm='-q' ;;
21 *) md5cmd='md5sum';;
Ray Milkey2fa4f182016-11-07 10:14:10 -080022esac
23
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070024oldMD5=$(cat $ONOS_MD5 2>/dev/null)
Ray Milkey0bb987e2016-11-11 15:02:51 -080025newMD5=$(${md5cmd} ${md5cmdprm} $ONOS_TAR)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070026
27# Search for the "clean" argument anywhere on the command-line
28echo "$@" | egrep -q "\bclean\b" && clean=true || unset clean
29
30set -e # Do not tolerate any errors from this point onward
31
32# If the previous installation does not exist, or if the ONOS tar changed,
33# or if the user asked for clean run, start from scratch.
Thomas Vachuska9152b152016-08-24 14:05:09 -070034if [ ! -d $ONOS_DIR -o "$oldMD5" != "$newMD5" -o -d $ONOS_DIR -a -n "$clean" ]; then
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070035 echo "Running clean installation..."
36
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070037 # Blitz previously unrolled onos- directory
38 rm -fr $ONOS_DIR
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070039
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070040 # Unroll new image from the specified tar file
41 [ -f $ONOS_TAR ] && tar zxf $ONOS_TAR
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070042
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070043 # Write out this installation's MD5 checksum
44 echo "$newMD5" > $ONOS_MD5
45
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070046 # Change into the ONOS home directory
47 cd $ONOS_DIR
48 export ONOS_HOME=$PWD
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070049
Thomas Vachuska397befc2016-11-17 12:41:19 -080050 # Run using the secure SSH client
Thomas Vachuska397befc2016-11-17 12:41:19 -080051 [ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q
52 $ONOS_HOME/bin/onos-user-key $(id -un) "$(cut -d\ -f2 ~/.ssh/id_rsa.pub)"
Thomas Vachuska5af2e4f2016-12-16 12:07:33 -080053 $ONOS_HOME/bin/onos-user-password onos rocks
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070054
55 # Create config/cluster.json (cluster metadata)
56 IP=${ONOS_IP:-127.0.0.1}
57 echo "Creating local cluster configs for IP $IP..."
58 [ -d $ONOS_HOME/config ] || mkdir -p $ONOS_HOME/config
59 cat > $ONOS_HOME/config/cluster.json <<-EOF
60 {
61 "name": "default",
62 "nodes": [ {"id": "$IP", "ip": "$IP", "port": 9876 } ],
63 "partitions": [ { "id": 1, "members": [ "$IP" ] } ]
64 }
Madan Jampani590160a2016-06-14 16:49:44 -070065EOF
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070066
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070067else
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070068 # Otherwise, run using the previous installation
69 echo "Running previous installation..."
70
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070071 # Change into the ONOS home directory
72 cd $ONOS_DIR
73 export ONOS_HOME=$PWD
74fi
Madan Jampani590160a2016-06-14 16:49:44 -070075
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070076# Start ONOS as a server, but include any specified options
77./bin/onos-service server "$@" &>onos.log &
78echo "$!" > /tmp/onos.pid
79
Thomas Vachuskafdb47552016-11-17 13:31:57 -080080function killServer() {
81 echo "Killing ONOS server..."
Thomas Vachuska5af2e4f2016-12-16 12:07:33 -080082 cat /tmp/onos.pid | xargs kill -9
Thomas Vachuskafdb47552016-11-17 13:31:57 -080083}
84
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070085# Hang-on a bit and then start tailing the ONOS log output
Charles Chan7cad3252016-11-16 10:41:07 -080086MAX_RETRY=30
Charles Chanf9335af2016-05-05 21:05:47 -070087echo "Waiting for karaf.log"
Charles Chan7cad3252016-11-16 10:41:07 -080088until [ $MAX_RETRY -le 0 ]; do
Charles Chanf9335af2016-05-05 21:05:47 -070089 KARAF_LOG=$(find $ONOS_HOME -type f -name karaf.log)
90 if [ $KARAF_LOG ]; then
Thomas Vachuskafdb47552016-11-17 13:31:57 -080091 trap killServer INT
Charles Chanf9335af2016-05-05 21:05:47 -070092 tail -f $KARAF_LOG
93 return
94 fi
Charles Chan7cad3252016-11-16 10:41:07 -080095 MAX_RETRY=$[$MAX_RETRY-1]
Charles Chanf9335af2016-05-05 21:05:47 -070096 sleep 1
97done
Madan Jampani590160a2016-06-14 16:49:44 -070098echo "Fail to open karaf.log"
Thomas Vachuskafdb47552016-11-17 13:31:57 -080099killServer