Zack Williams | 2b94629 | 2016-08-22 15:32:29 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ----------------------------------------------------------------------------- |
| 3 | # Tool to manage ONOS applications using REST API. |
| 4 | # ----------------------------------------------------------------------------- |
| 5 | |
| 6 | node=${1:-$OCI} |
| 7 | cmd=${2:-list} |
| 8 | app=${3} |
| 9 | |
| 10 | export URL=http://$node:8181/onos/v1/applications |
| 11 | export HDR="-HContent-Type:application/octet-stream" |
| 12 | export HAJ="-HContent-Type:application/json" |
| 13 | export curl="curl -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS" |
| 14 | |
| 15 | # Prints usage help |
| 16 | function usage { |
| 17 | echo "usage: onos-app <node-ip> list" >&2 |
| 18 | echo " onos-app <node-ip> {install|install!} <app-file>" >&2 |
| 19 | echo " onos-app <node-ip> {reinstall|reinstall!} [<app-name>] <app-file>" >&2 |
| 20 | echo " onos-app <node-ip> {activate|deactivate|uninstall} <app-name>" >&2 |
| 21 | exit 1 |
| 22 | } |
| 23 | |
| 24 | # Extract app name from the specified *.oar file |
| 25 | function appName { |
| 26 | aux=/tmp/aux$$.jar |
| 27 | cp $1 $aux |
| 28 | pushd /tmp >/dev/null |
| 29 | jar xf $aux app.xml && grep name= app.xml | cut -d\" -f2 |
| 30 | rm -f $aux /tmp/app.xml |
| 31 | popd >/dev/null |
| 32 | } |
| 33 | |
| 34 | [ -z $node -o "$node" = "-h" -o "$node" = "--help" -o "$node" = "-?" ] && usage |
| 35 | |
| 36 | case $cmd in |
| 37 | list) $curl -X GET $URL;; |
| 38 | installUrl!|installUrl) |
| 39 | activate="false" |
| 40 | [ $cmd = "installUrl!" ] && activate="true" |
| 41 | [ $# -lt 3 ] && usage |
| 42 | appurl=$3 |
| 43 | $curl -X POST $HAJ -d '{"url" : "'"$appurl"'", "activate" : "'$activate'" }' $URL |
| 44 | ;; |
| 45 | install!|install) |
| 46 | [ $cmd = "install!" ] && activate="?activate=true" |
| 47 | [ $# -lt 3 -o ! -f $app ] && usage |
| 48 | $curl -X POST $HDR $URL$activate --data-binary @$app |
| 49 | ;; |
| 50 | |
| 51 | reinstall!|reinstall) |
| 52 | [ $cmd = "reinstall!" ] && activate="?activate=true" |
| 53 | [ $# -lt 4 -a ! -f "$3" ] && usage |
| 54 | [ $# -eq 4 -a ! -f "$4" ] && usage |
| 55 | oar=$4 |
| 56 | [ $# -lt 4 ] && oar=$3 && app=$(appName $oar) |
| 57 | $curl -X DELETE $URL/$app |
| 58 | $curl -X POST $HDR $URL$activate --data-binary @$oar |
| 59 | ;; |
| 60 | |
| 61 | uninstall) |
| 62 | [ $# -lt 3 ] && usage |
| 63 | $curl -X DELETE $URL/$app |
| 64 | ;; |
| 65 | activate) |
| 66 | [ $# -lt 3 ] && usage |
| 67 | $curl -X POST $URL/$app/active |
| 68 | ;; |
| 69 | deactivate) |
| 70 | [ $# -lt 3 ] && usage |
| 71 | $curl -X DELETE $URL/$app/active |
| 72 | ;; |
| 73 | |
| 74 | *) usage;; |
| 75 | esac |
| 76 | |
| 77 | |
| 78 | status=$? |
| 79 | echo # new line for prompt |
| 80 | exit $status |