blob: 7068802bebae81ad12c107447fc147585e316352 [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
Thomas Vachuska510419f2018-06-28 17:05:09 -07008[ -f $ONOS_TAR ] || (echo "$ONOS_TAR not found" && exit 1)
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -07009
Ray Milkeyd84f89b2018-08-17 14:54:17 -070010
11function killServer() {
12 echo "Killing ONOS server..."
13 ps -e | grep apache.karaf.main.Main | grep -v grep | cut -d\ -f1 | xargs kill -9 &>/dev/null
14}
15
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070016# Kill any running instances
Ray Milkeyd84f89b2018-08-17 14:54:17 -070017killServer
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070018
Thomas Vachuska510419f2018-06-28 17:05:09 -070019ONOS_DIR=/tmp/$(tar tf $ONOS_TAR | head -n 1 | cut -d/ -f1)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070020ONOS_MD5=$ONOS_DIR/CHECKSUM
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070021
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070022# Extract MD5 of the ONOS tar file and of the previous installation, if one exists
Ray Milkey2fa4f182016-11-07 10:14:10 -080023md5cmd=''
Ray Milkey0bb987e2016-11-11 15:02:51 -080024md5cmdprm=''
Ray Milkey2fa4f182016-11-07 10:14:10 -080025case "$OSTYPE" in
Ray Milkey0bb987e2016-11-11 15:02:51 -080026 darwin*) md5cmd='md5' ; md5cmdprm='-q' ;;
27 *) md5cmd='md5sum';;
Ray Milkey2fa4f182016-11-07 10:14:10 -080028esac
29
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070030oldMD5=$(cat $ONOS_MD5 2>/dev/null)
Ray Milkey0bb987e2016-11-11 15:02:51 -080031newMD5=$(${md5cmd} ${md5cmdprm} $ONOS_TAR)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070032
33# Search for the "clean" argument anywhere on the command-line
34echo "$@" | egrep -q "\bclean\b" && clean=true || unset clean
35
36set -e # Do not tolerate any errors from this point onward
37
38# If the previous installation does not exist, or if the ONOS tar changed,
39# or if the user asked for clean run, start from scratch.
Thomas Vachuska9152b152016-08-24 14:05:09 -070040if [ ! -d $ONOS_DIR -o "$oldMD5" != "$newMD5" -o -d $ONOS_DIR -a -n "$clean" ]; then
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070041 echo "Running clean installation..."
42
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070043 # Blitz previously unrolled onos- directory
44 rm -fr $ONOS_DIR
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070045
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070046 # Unroll new image from the specified tar file
Thomas Vachuska510419f2018-06-28 17:05:09 -070047 [ -f $ONOS_TAR ] && tar zxf $ONOS_TAR -C /tmp
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070048
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070049 # Write out this installation's MD5 checksum
50 echo "$newMD5" > $ONOS_MD5
51
Thomas Vachuska397befc2016-11-17 12:41:19 -080052 # Run using the secure SSH client
Thomas Vachuska397befc2016-11-17 12:41:19 -080053 [ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q
Thomas Vachuska510419f2018-06-28 17:05:09 -070054 $ONOS_DIR/bin/onos-user-key $(id -un) "$(cut -d\ -f2 ~/.ssh/id_rsa.pub)"
55 $ONOS_DIR/bin/onos-user-password onos rocks
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070056
57 # Create config/cluster.json (cluster metadata)
58 IP=${ONOS_IP:-127.0.0.1}
59 echo "Creating local cluster configs for IP $IP..."
Thomas Vachuska510419f2018-06-28 17:05:09 -070060 [ -d $ONOS_DIR/config ] || mkdir -p $ONOS_DIR/config
61 cat > $ONOS_DIR/config/cluster.json <<-EOF
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070062 {
63 "name": "default",
Jordan Halterman19c123a2018-07-30 13:57:19 -070064 "node": {
65 "id": "$IP",
66 "ip": "$IP",
67 "port": 9876
68 }
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070069 }
Madan Jampani590160a2016-06-14 16:49:44 -070070EOF
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070071
Charles Chancbe8dd72017-06-27 18:25:05 -070072 # Copy config files
Thomas Vachuskaf34554f2018-07-31 17:09:36 -070073 find ${ONOS_ROOT:-.}/tools/package/config -maxdepth 1 -name \*.json -exec cp {} $ONOS_HOME/config/ \;
Charles Chancbe8dd72017-06-27 18:25:05 -070074
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070075else
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070076 # Otherwise, run using the previous installation
77 echo "Running previous installation..."
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070078fi
Madan Jampani590160a2016-06-14 16:49:44 -070079
Thomas Vachuska510419f2018-06-28 17:05:09 -070080# Change into the ONOS home directory
81cd $ONOS_DIR
82export ONOS_HOME=$PWD
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084# FIXME
85#export HOME=/Users/tom
86cp $HOME/Downloads/reflectasm-1.11.7.jar \
87 $ONOS_DIR/apache-karaf-4.2.1/system/com/esotericsoftware/reflectasm/1.11.7/reflectasm-1.11.7.jar
88
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070089# Start ONOS as a server, but include any specified options
90./bin/onos-service server "$@" &>onos.log &
91echo "$!" > /tmp/onos.pid
92
93# Hang-on a bit and then start tailing the ONOS log output
Charles Chan7cad3252016-11-16 10:41:07 -080094MAX_RETRY=30
Charles Chanf9335af2016-05-05 21:05:47 -070095echo "Waiting for karaf.log"
Charles Chan7cad3252016-11-16 10:41:07 -080096until [ $MAX_RETRY -le 0 ]; do
Charles Chanf9335af2016-05-05 21:05:47 -070097 KARAF_LOG=$(find $ONOS_HOME -type f -name karaf.log)
98 if [ $KARAF_LOG ]; then
Thomas Vachuskafdb47552016-11-17 13:31:57 -080099 trap killServer INT
Charles Chanf9335af2016-05-05 21:05:47 -0700100 tail -f $KARAF_LOG
101 return
102 fi
Charles Chan7cad3252016-11-16 10:41:07 -0800103 MAX_RETRY=$[$MAX_RETRY-1]
Charles Chanf9335af2016-05-05 21:05:47 -0700104 sleep 1
105done
Madan Jampani590160a2016-06-14 16:49:44 -0700106echo "Fail to open karaf.log"
Thomas Vachuskafdb47552016-11-17 13:31:57 -0800107killServer