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 | # ----------------------------------------------------------------------------- |
Thomas Vachuska | 4bd4bf1 | 2018-04-16 14:02:18 -0700 | [diff] [blame] | 22 | function usage { |
| 23 | echo "usage: onos-app [options] <node-ip> list" >&2 |
| 24 | echo " onos-app [options] <node-ip> {install|install!} <app-file>" >&2 |
| 25 | echo " onos-app [options] <node-ip> {reinstall|reinstall!} [<app-name>] <app-file>" >&2 |
| 26 | echo " onos-app [options] <node-ip> {activate|deactivate|uninstall} <app-name>" >&2 |
| 27 | echo "" |
| 28 | echo "options: [-P port] [-u user] [-p password] [-v]" |
| 29 | exit 1 |
| 30 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 31 | |
Thomas Vachuska | 4bd4bf1 | 2018-04-16 14:02:18 -0700 | [diff] [blame] | 32 | . $(dirname $0)/_rest-port |
Thomas Vachuska | 7f2a356 | 2018-02-28 10:02:16 -0800 | [diff] [blame] | 33 | . $(dirname $0)/_find-node |
Ray Milkey | 5c0d8f9 | 2017-06-09 12:26:31 -0700 | [diff] [blame] | 34 | |
| 35 | node=$(find_node $1) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 36 | cmd=${2:-list} |
| 37 | app=${3} |
| 38 | |
Thomas Vachuska | 4bd4bf1 | 2018-04-16 14:02:18 -0700 | [diff] [blame] | 39 | export URL=http://$node:$port/onos/v1/applications |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | export HDR="-HContent-Type:application/octet-stream" |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 41 | export HAJ="-HContent-Type:application/json" |
Thomas Vachuska | 4bd4bf1 | 2018-04-16 14:02:18 -0700 | [diff] [blame] | 42 | export curl="curl $fail -sSL --user $user:$password --noproxy ${node} " |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 43 | |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 44 | |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 45 | # Extract app name from the specified *.oar file |
| 46 | function appName { |
| 47 | aux=/tmp/aux$$.jar |
| 48 | cp $1 $aux |
| 49 | pushd /tmp >/dev/null |
| 50 | jar xf $aux app.xml && grep name= app.xml | cut -d\" -f2 |
| 51 | rm -f $aux /tmp/app.xml |
| 52 | popd >/dev/null |
| 53 | } |
| 54 | |
Thomas Vachuska | 9411780 | 2015-04-17 10:27:18 -0700 | [diff] [blame] | 55 | [ -z $node -o "$node" = "-h" -o "$node" = "--help" -o "$node" = "-?" ] && usage |
| 56 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 57 | case $cmd in |
| 58 | list) $curl -X GET $URL;; |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 59 | installUrl!|installUrl) |
| 60 | activate="false" |
| 61 | [ $cmd = "installUrl!" ] && activate="true" |
| 62 | [ $# -lt 3 ] && usage |
| 63 | appurl=$3 |
| 64 | $curl -X POST $HAJ -d '{"url" : "'"$appurl"'", "activate" : "'$activate'" }' $URL |
| 65 | ;; |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 66 | install!|install) |
| 67 | [ $cmd = "install!" ] && activate="?activate=true" |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 68 | [ $# -lt 3 -o ! -f $app ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 69 | $curl -X POST $HDR $URL$activate --data-binary @$app |
| 70 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 71 | |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 72 | reinstall!|reinstall) |
| 73 | [ $cmd = "reinstall!" ] && activate="?activate=true" |
| 74 | [ $# -lt 4 -a ! -f "$3" ] && usage |
| 75 | [ $# -eq 4 -a ! -f "$4" ] && usage |
| 76 | oar=$4 |
| 77 | [ $# -lt 4 ] && oar=$3 && app=$(appName $oar) |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 78 | $curl -X DELETE $URL/$app |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 79 | $curl -X POST $HDR $URL$activate --data-binary @$oar |
| 80 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 81 | |
| 82 | uninstall) |
| 83 | [ $# -lt 3 ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 84 | $curl -X DELETE $URL/$app |
| 85 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 86 | activate) |
| 87 | [ $# -lt 3 ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 88 | $curl -X POST $URL/$app/active |
| 89 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 90 | deactivate) |
| 91 | [ $# -lt 3 ] && usage |
Thomas Vachuska | 6723a48 | 2015-05-01 13:13:23 -0700 | [diff] [blame] | 92 | $curl -X DELETE $URL/$app/active |
| 93 | ;; |
Thomas Vachuska | c4cb100 | 2015-03-29 10:28:26 -0700 | [diff] [blame] | 94 | |
| 95 | *) usage;; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 96 | esac |
Brian O'Connor | 3938f61 | 2015-04-14 15:01:32 -0700 | [diff] [blame] | 97 | |
Thomas Vachuska | 16501b0 | 2015-08-24 16:58:32 -0700 | [diff] [blame] | 98 | |
| 99 | status=$? |
Thomas Vachuska | 9411780 | 2015-04-17 10:27:18 -0700 | [diff] [blame] | 100 | echo # new line for prompt |
Thomas Vachuska | 16501b0 | 2015-08-24 16:58:32 -0700 | [diff] [blame] | 101 | exit $status |