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