blob: 6fce6e8d033e0bedf7f664407b696e232061c3ae [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=
Carmelo Cascone395b2312019-06-18 17:34:16 -07007JDK_TAR=
Carmelo Cascone3dcb7fd2019-06-26 14:08:36 -07008ABSOLUTE_JAVABASE=
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -07009
Thomas Vachuska510419f2018-06-28 17:05:09 -070010[ -f $ONOS_TAR ] || (echo "$ONOS_TAR not found" && exit 1)
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070011
Ray Milkeyd84f89b2018-08-17 14:54:17 -070012
13function killServer() {
14 echo "Killing ONOS server..."
Andrea Campanella7730c552018-12-06 11:37:39 -080015 ps -ef | grep apache.karaf.main.Main | grep -v grep | awk '{print $2}' | xargs kill -9 &>/dev/null
Ray Milkeyd84f89b2018-08-17 14:54:17 -070016}
17
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070018# Kill any running instances
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019killServer
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070020
Thomas Vachuska510419f2018-06-28 17:05:09 -070021ONOS_DIR=/tmp/$(tar tf $ONOS_TAR | head -n 1 | cut -d/ -f1)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070022ONOS_MD5=$ONOS_DIR/CHECKSUM
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070023
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070024# Extract MD5 of the ONOS tar file and of the previous installation, if one exists
Ray Milkey2fa4f182016-11-07 10:14:10 -080025md5cmd=''
Ray Milkey0bb987e2016-11-11 15:02:51 -080026md5cmdprm=''
Ray Milkey2fa4f182016-11-07 10:14:10 -080027case "$OSTYPE" in
Ray Milkey0bb987e2016-11-11 15:02:51 -080028 darwin*) md5cmd='md5' ; md5cmdprm='-q' ;;
29 *) md5cmd='md5sum';;
Ray Milkey2fa4f182016-11-07 10:14:10 -080030esac
31
Carmelo Cascone3dcb7fd2019-06-26 14:08:36 -070032# Either use the given ABSOLUTE_JAVABASE as JAVA_HOME, or extract the passed
33# JDK_TAR and use that.
34if [[ -d ${ABSOLUTE_JAVABASE} ]]; then
35 export JAVA_HOME=${ABSOLUTE_JAVABASE}
36elif [[ -f ${JDK_TAR} ]]; then
Carmelo Cascone395b2312019-06-18 17:34:16 -070037 JDK_DIR=$ONOS_DIR-jdk
38 JDK_MD5=$JDK_DIR/CHECKSUM
39
40 oldJdkMD5=$(cat $JDK_MD5 2>/dev/null)
41 newJdkMD5=$(${md5cmd} ${md5cmdprm} $JDK_TAR)
42
43 if [[ ! -d ${JDK_DIR} || "$oldJdkMD5" != "$newJdkMD5" ]]; then
44 echo "Unpacking JDK in ${JDK_DIR} (from $JDK_TAR)..."
45 # Unroll new JDK from the specified tar file
46 rm -fr $JDK_DIR
47 mkdir $JDK_DIR
48 tar zxf $JDK_TAR -C $JDK_DIR --strip-components 1
49 echo "$newJdkMD5" > $JDK_MD5
50 else
51 echo "Using JDK in ${JDK_DIR}..."
52 fi
53 # Use the extracted JDK as our new JAVA_HOME
54 export JAVA_HOME=${JDK_DIR}
55fi
56
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070057oldMD5=$(cat $ONOS_MD5 2>/dev/null)
Ray Milkey0bb987e2016-11-11 15:02:51 -080058newMD5=$(${md5cmd} ${md5cmdprm} $ONOS_TAR)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070059
60# Search for the "clean" argument anywhere on the command-line
61echo "$@" | egrep -q "\bclean\b" && clean=true || unset clean
62
63set -e # Do not tolerate any errors from this point onward
64
65# If the previous installation does not exist, or if the ONOS tar changed,
66# or if the user asked for clean run, start from scratch.
Thomas Vachuska9152b152016-08-24 14:05:09 -070067if [ ! -d $ONOS_DIR -o "$oldMD5" != "$newMD5" -o -d $ONOS_DIR -a -n "$clean" ]; then
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070068 echo "Running clean installation..."
69
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070070 # Blitz previously unrolled onos- directory
71 rm -fr $ONOS_DIR
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070072
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070073 # Unroll new image from the specified tar file
Thomas Vachuska510419f2018-06-28 17:05:09 -070074 [ -f $ONOS_TAR ] && tar zxf $ONOS_TAR -C /tmp
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070075
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070076 # Write out this installation's MD5 checksum
77 echo "$newMD5" > $ONOS_MD5
78
Thomas Vachuska397befc2016-11-17 12:41:19 -080079 # Run using the secure SSH client
Thomas Vachuska397befc2016-11-17 12:41:19 -080080 [ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q
Thomas Vachuska510419f2018-06-28 17:05:09 -070081 $ONOS_DIR/bin/onos-user-key $(id -un) "$(cut -d\ -f2 ~/.ssh/id_rsa.pub)"
82 $ONOS_DIR/bin/onos-user-password onos rocks
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070083
84 # Create config/cluster.json (cluster metadata)
85 IP=${ONOS_IP:-127.0.0.1}
86 echo "Creating local cluster configs for IP $IP..."
Thomas Vachuska510419f2018-06-28 17:05:09 -070087 [ -d $ONOS_DIR/config ] || mkdir -p $ONOS_DIR/config
88 cat > $ONOS_DIR/config/cluster.json <<-EOF
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070089 {
Samuel Jero31e16f52018-09-21 10:34:28 -040090 "name": "default-$RANDOM",
Jordan Halterman19c123a2018-07-30 13:57:19 -070091 "node": {
92 "id": "$IP",
93 "ip": "$IP",
94 "port": 9876
Samuel Jero31e16f52018-09-21 10:34:28 -040095 },
96 "clusterSecret": "$RANDOM"
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070097 }
Madan Jampani590160a2016-06-14 16:49:44 -070098EOF
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070099
Charles Chancbe8dd72017-06-27 18:25:05 -0700100 # Copy config files
Thomas Szyrkowiec6316e432019-02-27 15:51:06 +0100101 find ${ONOS_ROOT:-.}/tools/package/config -maxdepth 1 -name \*.json -exec cp {} $ONOS_DIR/config/ \;
Charles Chancbe8dd72017-06-27 18:25:05 -0700102
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -0700103else
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -0700104 # Otherwise, run using the previous installation
105 echo "Running previous installation..."
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -0700106fi
Madan Jampani590160a2016-06-14 16:49:44 -0700107
Thomas Vachuska510419f2018-06-28 17:05:09 -0700108# Change into the ONOS home directory
109cd $ONOS_DIR
110export ONOS_HOME=$PWD
111
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -0700112# Start ONOS as a server, but include any specified options
113./bin/onos-service server "$@" &>onos.log &
114echo "$!" > /tmp/onos.pid
115
116# Hang-on a bit and then start tailing the ONOS log output
Charles Chan7cad3252016-11-16 10:41:07 -0800117MAX_RETRY=30
Charles Chanf9335af2016-05-05 21:05:47 -0700118echo "Waiting for karaf.log"
Charles Chan7cad3252016-11-16 10:41:07 -0800119until [ $MAX_RETRY -le 0 ]; do
Charles Chanf9335af2016-05-05 21:05:47 -0700120 KARAF_LOG=$(find $ONOS_HOME -type f -name karaf.log)
121 if [ $KARAF_LOG ]; then
Thomas Vachuskafdb47552016-11-17 13:31:57 -0800122 trap killServer INT
Charles Chanf9335af2016-05-05 21:05:47 -0700123 tail -f $KARAF_LOG
124 return
125 fi
Charles Chan7cad3252016-11-16 10:41:07 -0800126 MAX_RETRY=$[$MAX_RETRY-1]
Charles Chanf9335af2016-05-05 21:05:47 -0700127 sleep 1
128done
Madan Jampani590160a2016-06-14 16:49:44 -0700129echo "Fail to open karaf.log"
Thomas Vachuskafdb47552016-11-17 13:31:57 -0800130killServer