Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Ray Milkey | 5c0d8f9 | 2017-06-09 12:26:31 -0700 | [diff] [blame] | 2 | |
| 3 | # |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 4 | # Copyright 2015-present Open Networking Foundation |
Ray Milkey | 5c0d8f9 | 2017-06-09 12:26:31 -0700 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 19 | # ----------------------------------------------------------------------------- |
| 20 | # Tool to manage ONOS applications using REST API. |
| 21 | # ----------------------------------------------------------------------------- |
| 22 | |
Ray Milkey | 5c0d8f9 | 2017-06-09 12:26:31 -0700 | [diff] [blame] | 23 | # If ONOS_HOME is set, respect its value. |
| 24 | # If ONOS_HOME is not set (e.g. in the init or service environment), |
| 25 | # set it based on this script's path. |
| 26 | ONOS_HOME=${ONOS_HOME:-$(cd $(dirname $0)/.. >/dev/null 2>&1 && pwd)} |
| 27 | ONOS_WEB_USER=${ONOS_WEB_USER:-onos} # ONOS WEB User defaults to 'onos' |
| 28 | ONOS_WEB_PASS=${ONOS_WEB_PASS:-rocks} # ONOS WEB Password defaults to 'rocks' |
| 29 | |
| 30 | . ${ONOS_HOME}/bin/_find-node |
| 31 | |
| 32 | node=$(find_node $1) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 33 | cmd=${2:-list} |
| 34 | app=${3} |
| 35 | |
| 36 | export URL=http://$node:8181/onos/v1/applications |
| 37 | export HDR="-HContent-Type:application/octet-stream" |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 38 | export HAJ="-HContent-Type:application/json" |
jaegonkim | 1f5fbe5 | 2016-08-31 06:57:02 -0700 | [diff] [blame] | 39 | export curl="curl -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS --noproxy localhost " |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 41 | # Prints usage help |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 42 | function usage { |
Thomas Vachuska | 9411780 | 2015-04-17 10:27:18 -0700 | [diff] [blame] | 43 | echo "usage: onos-app <node-ip> list" >&2 |
| 44 | echo " onos-app <node-ip> {install|install!} <app-file>" >&2 |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 45 | echo " onos-app <node-ip> {reinstall|reinstall!} [<app-name>] <app-file>" >&2 |
Thomas Vachuska | 9411780 | 2015-04-17 10:27:18 -0700 | [diff] [blame] | 46 | echo " onos-app <node-ip> {activate|deactivate|uninstall} <app-name>" >&2 |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 47 | exit 1 |
| 48 | } |
| 49 | |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 50 | # Extract app name from the specified *.oar file |
| 51 | function appName { |
| 52 | aux=/tmp/aux$$.jar |
| 53 | cp $1 $aux |
| 54 | pushd /tmp >/dev/null |
| 55 | jar xf $aux app.xml && grep name= app.xml | cut -d\" -f2 |
| 56 | rm -f $aux /tmp/app.xml |
| 57 | popd >/dev/null |
| 58 | } |
| 59 | |
Thomas Vachuska | 9411780 | 2015-04-17 10:27:18 -0700 | [diff] [blame] | 60 | [ -z $node -o "$node" = "-h" -o "$node" = "--help" -o "$node" = "-?" ] && usage |
| 61 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 62 | case $cmd in |
| 63 | list) $curl -X GET $URL;; |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 64 | installUrl!|installUrl) |
| 65 | activate="false" |
| 66 | [ $cmd = "installUrl!" ] && activate="true" |
| 67 | [ $# -lt 3 ] && usage |
| 68 | appurl=$3 |
| 69 | $curl -X POST $HAJ -d '{"url" : "'"$appurl"'", "activate" : "'$activate'" }' $URL |
| 70 | ;; |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 71 | install!|install) |
| 72 | [ $cmd = "install!" ] && activate="?activate=true" |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 73 | [ $# -lt 3 -o ! -f $app ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 74 | $curl -X POST $HDR $URL$activate --data-binary @$app |
| 75 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 76 | |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 77 | reinstall!|reinstall) |
| 78 | [ $cmd = "reinstall!" ] && activate="?activate=true" |
| 79 | [ $# -lt 4 -a ! -f "$3" ] && usage |
| 80 | [ $# -eq 4 -a ! -f "$4" ] && usage |
| 81 | oar=$4 |
| 82 | [ $# -lt 4 ] && oar=$3 && app=$(appName $oar) |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 83 | $curl -X DELETE $URL/$app |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 84 | $curl -X POST $HDR $URL$activate --data-binary @$oar |
| 85 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 86 | |
| 87 | uninstall) |
| 88 | [ $# -lt 3 ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 89 | $curl -X DELETE $URL/$app |
| 90 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 91 | activate) |
| 92 | [ $# -lt 3 ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 93 | $curl -X POST $URL/$app/active |
| 94 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 95 | deactivate) |
| 96 | [ $# -lt 3 ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 97 | $curl -X DELETE $URL/$app/active |
| 98 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 99 | |
| 100 | *) usage;; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 101 | esac |
Brian O'Connor | 3938f61 | 2015-04-14 15:01:32 -0700 | [diff] [blame] | 102 | |
Thomas Vachuska | 16501b0 | 2015-08-24 16:58:32 -0700 | [diff] [blame] | 103 | |
| 104 | status=$? |
Thomas Vachuska | 9411780 | 2015-04-17 10:27:18 -0700 | [diff] [blame] | 105 | echo # new line for prompt |
Thomas Vachuska | 16501b0 | 2015-08-24 16:58:32 -0700 | [diff] [blame] | 106 | exit $status |