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