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