Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ----------------------------------------------------------------------------- |
| 3 | # Runs ONOS from distributable onos.tar.gz |
| 4 | # ----------------------------------------------------------------------------- |
| 5 | |
| 6 | ONOS_TAR= |
Carmelo Cascone | 395b231 | 2019-06-18 17:34:16 -0700 | [diff] [blame^] | 7 | JDK_TAR= |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 8 | |
Thomas Vachuska | 510419f | 2018-06-28 17:05:09 -0700 | [diff] [blame] | 9 | [ -f $ONOS_TAR ] || (echo "$ONOS_TAR not found" && exit 1) |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 10 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 11 | |
| 12 | function killServer() { |
| 13 | echo "Killing ONOS server..." |
Andrea Campanella | 7730c55 | 2018-12-06 11:37:39 -0800 | [diff] [blame] | 14 | ps -ef | grep apache.karaf.main.Main | grep -v grep | awk '{print $2}' | xargs kill -9 &>/dev/null |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 17 | # Kill any running instances |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | killServer |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 19 | |
Thomas Vachuska | 510419f | 2018-06-28 17:05:09 -0700 | [diff] [blame] | 20 | ONOS_DIR=/tmp/$(tar tf $ONOS_TAR | head -n 1 | cut -d/ -f1) |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 21 | ONOS_MD5=$ONOS_DIR/CHECKSUM |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 22 | |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 23 | # Extract MD5 of the ONOS tar file and of the previous installation, if one exists |
Ray Milkey | 2fa4f18 | 2016-11-07 10:14:10 -0800 | [diff] [blame] | 24 | md5cmd='' |
Ray Milkey | 0bb987e | 2016-11-11 15:02:51 -0800 | [diff] [blame] | 25 | md5cmdprm='' |
Ray Milkey | 2fa4f18 | 2016-11-07 10:14:10 -0800 | [diff] [blame] | 26 | case "$OSTYPE" in |
Ray Milkey | 0bb987e | 2016-11-11 15:02:51 -0800 | [diff] [blame] | 27 | darwin*) md5cmd='md5' ; md5cmdprm='-q' ;; |
| 28 | *) md5cmd='md5sum';; |
Ray Milkey | 2fa4f18 | 2016-11-07 10:14:10 -0800 | [diff] [blame] | 29 | esac |
| 30 | |
Carmelo Cascone | 395b231 | 2019-06-18 17:34:16 -0700 | [diff] [blame^] | 31 | # Use provided JDK as JAVA_HOME if given |
| 32 | if [[ -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} |
| 51 | fi |
| 52 | |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 53 | oldMD5=$(cat $ONOS_MD5 2>/dev/null) |
Ray Milkey | 0bb987e | 2016-11-11 15:02:51 -0800 | [diff] [blame] | 54 | newMD5=$(${md5cmd} ${md5cmdprm} $ONOS_TAR) |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 55 | |
| 56 | # Search for the "clean" argument anywhere on the command-line |
| 57 | echo "$@" | egrep -q "\bclean\b" && clean=true || unset clean |
| 58 | |
| 59 | set -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 Vachuska | 9152b15 | 2016-08-24 14:05:09 -0700 | [diff] [blame] | 63 | if [ ! -d $ONOS_DIR -o "$oldMD5" != "$newMD5" -o -d $ONOS_DIR -a -n "$clean" ]; then |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 64 | echo "Running clean installation..." |
| 65 | |
Thomas Vachuska | e4abf5b2 | 2016-08-15 14:04:46 -0700 | [diff] [blame] | 66 | # Blitz previously unrolled onos- directory |
| 67 | rm -fr $ONOS_DIR |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 68 | |
Thomas Vachuska | e4abf5b2 | 2016-08-15 14:04:46 -0700 | [diff] [blame] | 69 | # Unroll new image from the specified tar file |
Thomas Vachuska | 510419f | 2018-06-28 17:05:09 -0700 | [diff] [blame] | 70 | [ -f $ONOS_TAR ] && tar zxf $ONOS_TAR -C /tmp |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 71 | |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 72 | # Write out this installation's MD5 checksum |
| 73 | echo "$newMD5" > $ONOS_MD5 |
| 74 | |
Thomas Vachuska | 397befc | 2016-11-17 12:41:19 -0800 | [diff] [blame] | 75 | # Run using the secure SSH client |
Thomas Vachuska | 397befc | 2016-11-17 12:41:19 -0800 | [diff] [blame] | 76 | [ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q |
Thomas Vachuska | 510419f | 2018-06-28 17:05:09 -0700 | [diff] [blame] | 77 | $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 Vachuska | e4abf5b2 | 2016-08-15 14:04:46 -0700 | [diff] [blame] | 79 | |
| 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 Vachuska | 510419f | 2018-06-28 17:05:09 -0700 | [diff] [blame] | 83 | [ -d $ONOS_DIR/config ] || mkdir -p $ONOS_DIR/config |
| 84 | cat > $ONOS_DIR/config/cluster.json <<-EOF |
Thomas Vachuska | e4abf5b2 | 2016-08-15 14:04:46 -0700 | [diff] [blame] | 85 | { |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 86 | "name": "default-$RANDOM", |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 87 | "node": { |
| 88 | "id": "$IP", |
| 89 | "ip": "$IP", |
| 90 | "port": 9876 |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 91 | }, |
| 92 | "clusterSecret": "$RANDOM" |
Thomas Vachuska | e4abf5b2 | 2016-08-15 14:04:46 -0700 | [diff] [blame] | 93 | } |
Madan Jampani | 590160a | 2016-06-14 16:49:44 -0700 | [diff] [blame] | 94 | EOF |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 95 | |
Charles Chan | cbe8dd7 | 2017-06-27 18:25:05 -0700 | [diff] [blame] | 96 | # Copy config files |
Thomas Szyrkowiec | 6316e43 | 2019-02-27 15:51:06 +0100 | [diff] [blame] | 97 | find ${ONOS_ROOT:-.}/tools/package/config -maxdepth 1 -name \*.json -exec cp {} $ONOS_DIR/config/ \; |
Charles Chan | cbe8dd7 | 2017-06-27 18:25:05 -0700 | [diff] [blame] | 98 | |
Thomas Vachuska | e4abf5b2 | 2016-08-15 14:04:46 -0700 | [diff] [blame] | 99 | else |
Thomas Vachuska | a3029cf | 2016-08-23 16:47:46 -0700 | [diff] [blame] | 100 | # Otherwise, run using the previous installation |
| 101 | echo "Running previous installation..." |
Thomas Vachuska | e4abf5b2 | 2016-08-15 14:04:46 -0700 | [diff] [blame] | 102 | fi |
Madan Jampani | 590160a | 2016-06-14 16:49:44 -0700 | [diff] [blame] | 103 | |
Thomas Vachuska | 510419f | 2018-06-28 17:05:09 -0700 | [diff] [blame] | 104 | # Change into the ONOS home directory |
| 105 | cd $ONOS_DIR |
| 106 | export ONOS_HOME=$PWD |
| 107 | |
Thomas Vachuska | 5f54c6f | 2016-05-04 19:19:51 -0700 | [diff] [blame] | 108 | # Start ONOS as a server, but include any specified options |
| 109 | ./bin/onos-service server "$@" &>onos.log & |
| 110 | echo "$!" > /tmp/onos.pid |
| 111 | |
| 112 | # Hang-on a bit and then start tailing the ONOS log output |
Charles Chan | 7cad325 | 2016-11-16 10:41:07 -0800 | [diff] [blame] | 113 | MAX_RETRY=30 |
Charles Chan | f9335af | 2016-05-05 21:05:47 -0700 | [diff] [blame] | 114 | echo "Waiting for karaf.log" |
Charles Chan | 7cad325 | 2016-11-16 10:41:07 -0800 | [diff] [blame] | 115 | until [ $MAX_RETRY -le 0 ]; do |
Charles Chan | f9335af | 2016-05-05 21:05:47 -0700 | [diff] [blame] | 116 | KARAF_LOG=$(find $ONOS_HOME -type f -name karaf.log) |
| 117 | if [ $KARAF_LOG ]; then |
Thomas Vachuska | fdb4755 | 2016-11-17 13:31:57 -0800 | [diff] [blame] | 118 | trap killServer INT |
Charles Chan | f9335af | 2016-05-05 21:05:47 -0700 | [diff] [blame] | 119 | tail -f $KARAF_LOG |
| 120 | return |
| 121 | fi |
Charles Chan | 7cad325 | 2016-11-16 10:41:07 -0800 | [diff] [blame] | 122 | MAX_RETRY=$[$MAX_RETRY-1] |
Charles Chan | f9335af | 2016-05-05 21:05:47 -0700 | [diff] [blame] | 123 | sleep 1 |
| 124 | done |
Madan Jampani | 590160a | 2016-06-14 16:49:44 -0700 | [diff] [blame] | 125 | echo "Fail to open karaf.log" |
Thomas Vachuska | fdb4755 | 2016-11-17 13:31:57 -0800 | [diff] [blame] | 126 | killServer |