blob: e89800688ac7970aadba37a0175a3e909febf56f [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=
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -07008
Thomas Vachuska510419f2018-06-28 17:05:09 -07009[ -f $ONOS_TAR ] || (echo "$ONOS_TAR not found" && exit 1)
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070010
Ray Milkeyd84f89b2018-08-17 14:54:17 -070011
12function killServer() {
13 echo "Killing ONOS server..."
Andrea Campanella7730c552018-12-06 11:37:39 -080014 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 -070015}
16
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070017# Kill any running instances
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018killServer
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070019
Thomas Vachuska510419f2018-06-28 17:05:09 -070020ONOS_DIR=/tmp/$(tar tf $ONOS_TAR | head -n 1 | cut -d/ -f1)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070021ONOS_MD5=$ONOS_DIR/CHECKSUM
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070022
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070023# Extract MD5 of the ONOS tar file and of the previous installation, if one exists
Ray Milkey2fa4f182016-11-07 10:14:10 -080024md5cmd=''
Ray Milkey0bb987e2016-11-11 15:02:51 -080025md5cmdprm=''
Ray Milkey2fa4f182016-11-07 10:14:10 -080026case "$OSTYPE" in
Ray Milkey0bb987e2016-11-11 15:02:51 -080027 darwin*) md5cmd='md5' ; md5cmdprm='-q' ;;
28 *) md5cmd='md5sum';;
Ray Milkey2fa4f182016-11-07 10:14:10 -080029esac
30
Carmelo Cascone395b2312019-06-18 17:34:16 -070031# Use provided JDK as JAVA_HOME if given
32if [[ -f ${JDK_TAR} ]]; then
33 JDK_DIR=$ONOS_DIR-jdk
34 JDK_MD5=$JDK_DIR/CHECKSUM
35
36 oldJdkMD5=$(cat $JDK_MD5 2>/dev/null)
37 newJdkMD5=$(${md5cmd} ${md5cmdprm} $JDK_TAR)
38
39 if [[ ! -d ${JDK_DIR} || "$oldJdkMD5" != "$newJdkMD5" ]]; then
40 echo "Unpacking JDK in ${JDK_DIR} (from $JDK_TAR)..."
41 # Unroll new JDK from the specified tar file
42 rm -fr $JDK_DIR
43 mkdir $JDK_DIR
44 tar zxf $JDK_TAR -C $JDK_DIR --strip-components 1
45 echo "$newJdkMD5" > $JDK_MD5
46 else
47 echo "Using JDK in ${JDK_DIR}..."
48 fi
49 # Use the extracted JDK as our new JAVA_HOME
50 export JAVA_HOME=${JDK_DIR}
51fi
52
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070053oldMD5=$(cat $ONOS_MD5 2>/dev/null)
Ray Milkey0bb987e2016-11-11 15:02:51 -080054newMD5=$(${md5cmd} ${md5cmdprm} $ONOS_TAR)
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070055
56# Search for the "clean" argument anywhere on the command-line
57echo "$@" | egrep -q "\bclean\b" && clean=true || unset clean
58
59set -e # Do not tolerate any errors from this point onward
60
61# If the previous installation does not exist, or if the ONOS tar changed,
62# or if the user asked for clean run, start from scratch.
Thomas Vachuska9152b152016-08-24 14:05:09 -070063if [ ! -d $ONOS_DIR -o "$oldMD5" != "$newMD5" -o -d $ONOS_DIR -a -n "$clean" ]; then
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070064 echo "Running clean installation..."
65
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070066 # Blitz previously unrolled onos- directory
67 rm -fr $ONOS_DIR
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070068
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070069 # Unroll new image from the specified tar file
Thomas Vachuska510419f2018-06-28 17:05:09 -070070 [ -f $ONOS_TAR ] && tar zxf $ONOS_TAR -C /tmp
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -070071
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070072 # Write out this installation's MD5 checksum
73 echo "$newMD5" > $ONOS_MD5
74
Thomas Vachuska397befc2016-11-17 12:41:19 -080075 # Run using the secure SSH client
Thomas Vachuska397befc2016-11-17 12:41:19 -080076 [ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q
Thomas Vachuska510419f2018-06-28 17:05:09 -070077 $ONOS_DIR/bin/onos-user-key $(id -un) "$(cut -d\ -f2 ~/.ssh/id_rsa.pub)"
78 $ONOS_DIR/bin/onos-user-password onos rocks
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070079
80 # Create config/cluster.json (cluster metadata)
81 IP=${ONOS_IP:-127.0.0.1}
82 echo "Creating local cluster configs for IP $IP..."
Thomas Vachuska510419f2018-06-28 17:05:09 -070083 [ -d $ONOS_DIR/config ] || mkdir -p $ONOS_DIR/config
84 cat > $ONOS_DIR/config/cluster.json <<-EOF
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070085 {
Samuel Jero31e16f52018-09-21 10:34:28 -040086 "name": "default-$RANDOM",
Jordan Halterman19c123a2018-07-30 13:57:19 -070087 "node": {
88 "id": "$IP",
89 "ip": "$IP",
90 "port": 9876
Samuel Jero31e16f52018-09-21 10:34:28 -040091 },
92 "clusterSecret": "$RANDOM"
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070093 }
Madan Jampani590160a2016-06-14 16:49:44 -070094EOF
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -070095
Charles Chancbe8dd72017-06-27 18:25:05 -070096 # Copy config files
Thomas Szyrkowiec6316e432019-02-27 15:51:06 +010097 find ${ONOS_ROOT:-.}/tools/package/config -maxdepth 1 -name \*.json -exec cp {} $ONOS_DIR/config/ \;
Charles Chancbe8dd72017-06-27 18:25:05 -070098
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -070099else
Thomas Vachuskaa3029cf2016-08-23 16:47:46 -0700100 # Otherwise, run using the previous installation
101 echo "Running previous installation..."
Thomas Vachuskae4abf5b22016-08-15 14:04:46 -0700102fi
Madan Jampani590160a2016-06-14 16:49:44 -0700103
Thomas Vachuska510419f2018-06-28 17:05:09 -0700104# Change into the ONOS home directory
105cd $ONOS_DIR
106export ONOS_HOME=$PWD
107
Thomas Vachuska5f54c6f2016-05-04 19:19:51 -0700108# Start ONOS as a server, but include any specified options
109./bin/onos-service server "$@" &>onos.log &
110echo "$!" > /tmp/onos.pid
111
112# Hang-on a bit and then start tailing the ONOS log output
Charles Chan7cad3252016-11-16 10:41:07 -0800113MAX_RETRY=30
Charles Chanf9335af2016-05-05 21:05:47 -0700114echo "Waiting for karaf.log"
Charles Chan7cad3252016-11-16 10:41:07 -0800115until [ $MAX_RETRY -le 0 ]; do
Charles Chanf9335af2016-05-05 21:05:47 -0700116 KARAF_LOG=$(find $ONOS_HOME -type f -name karaf.log)
117 if [ $KARAF_LOG ]; then
Thomas Vachuskafdb47552016-11-17 13:31:57 -0800118 trap killServer INT
Charles Chanf9335af2016-05-05 21:05:47 -0700119 tail -f $KARAF_LOG
120 return
121 fi
Charles Chan7cad3252016-11-16 10:41:07 -0800122 MAX_RETRY=$[$MAX_RETRY-1]
Charles Chanf9335af2016-05-05 21:05:47 -0700123 sleep 1
124done
Madan Jampani590160a2016-06-14 16:49:44 -0700125echo "Fail to open karaf.log"
Thomas Vachuskafdb47552016-11-17 13:31:57 -0800126killServer