blob: 1c30c42536f5c16444ffd72b22ce1a32611b0104 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001#!/bin/bash
Ray Milkey5c0d8f92017-06-09 12:26:31 -07002
3#
Brian O'Connora09fe5b2017-08-03 21:12:30 -07004# Copyright 2015-present Open Networking Foundation
Ray Milkey5c0d8f92017-06-09 12:26:31 -07005#
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 Vachuska02aeb032015-01-06 22:36:30 -080019# -----------------------------------------------------------------------------
20# Tool to manage ONOS applications using REST API.
21# -----------------------------------------------------------------------------
22
Ray Milkey5c0d8f92017-06-09 12:26:31 -070023# 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.
26ONOS_HOME=${ONOS_HOME:-$(cd $(dirname $0)/.. >/dev/null 2>&1 && pwd)}
27ONOS_WEB_USER=${ONOS_WEB_USER:-onos} # ONOS WEB User defaults to 'onos'
28ONOS_WEB_PASS=${ONOS_WEB_PASS:-rocks} # ONOS WEB Password defaults to 'rocks'
29
30. ${ONOS_HOME}/bin/_find-node
31
32node=$(find_node $1)
Thomas Vachuska02aeb032015-01-06 22:36:30 -080033cmd=${2:-list}
34app=${3}
35
36export URL=http://$node:8181/onos/v1/applications
37export HDR="-HContent-Type:application/octet-stream"
alshabib20a070b2016-06-03 14:44:05 -070038export HAJ="-HContent-Type:application/json"
jaegonkim1f5fbe52016-08-31 06:57:02 -070039export curl="curl -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS --noproxy localhost "
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040
Thomas Vachuska6723a482015-05-01 13:13:23 -070041# Prints usage help
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070042function usage {
Thomas Vachuska94117802015-04-17 10:27:18 -070043 echo "usage: onos-app <node-ip> list" >&2
44 echo " onos-app <node-ip> {install|install!} <app-file>" >&2
Thomas Vachuska6723a482015-05-01 13:13:23 -070045 echo " onos-app <node-ip> {reinstall|reinstall!} [<app-name>] <app-file>" >&2
Thomas Vachuska94117802015-04-17 10:27:18 -070046 echo " onos-app <node-ip> {activate|deactivate|uninstall} <app-name>" >&2
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070047 exit 1
48}
49
Thomas Vachuska6723a482015-05-01 13:13:23 -070050# Extract app name from the specified *.oar file
51function 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 Vachuska94117802015-04-17 10:27:18 -070060[ -z $node -o "$node" = "-h" -o "$node" = "--help" -o "$node" = "-?" ] && usage
61
Thomas Vachuska02aeb032015-01-06 22:36:30 -080062case $cmd in
63 list) $curl -X GET $URL;;
alshabib20a070b2016-06-03 14:44:05 -070064 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 Vachuska6723a482015-05-01 13:13:23 -070071 install!|install)
72 [ $cmd = "install!" ] && activate="?activate=true"
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070073 [ $# -lt 3 -o ! -f $app ] && usage
Thomas Vachuska6723a482015-05-01 13:13:23 -070074 $curl -X POST $HDR $URL$activate --data-binary @$app
75 ;;
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070076
Thomas Vachuska6723a482015-05-01 13:13:23 -070077 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 Vachuskac4cb1002015-03-29 10:28:26 -070083 $curl -X DELETE $URL/$app
Thomas Vachuska6723a482015-05-01 13:13:23 -070084 $curl -X POST $HDR $URL$activate --data-binary @$oar
85 ;;
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070086
87 uninstall)
88 [ $# -lt 3 ] && usage
Thomas Vachuska6723a482015-05-01 13:13:23 -070089 $curl -X DELETE $URL/$app
90 ;;
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070091 activate)
92 [ $# -lt 3 ] && usage
Thomas Vachuska6723a482015-05-01 13:13:23 -070093 $curl -X POST $URL/$app/active
94 ;;
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070095 deactivate)
96 [ $# -lt 3 ] && usage
Thomas Vachuska6723a482015-05-01 13:13:23 -070097 $curl -X DELETE $URL/$app/active
98 ;;
Thomas Vachuskac4cb1002015-03-29 10:28:26 -070099
100 *) usage;;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800101esac
Brian O'Connor3938f612015-04-14 15:01:32 -0700102
Thomas Vachuska16501b02015-08-24 16:58:32 -0700103
104status=$?
Thomas Vachuska94117802015-04-17 10:27:18 -0700105echo # new line for prompt
Thomas Vachuska16501b02015-08-24 16:58:32 -0700106exit $status