3 # Copyright (c) 2019 AT&T Intellectual Property.
4 # Copyright (c) 2019 Nokia.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 #############################
20 # Simple cli for xapp manager
22 # In addition to standard shell tools, requires packages "curl" and
23 # "yajl-tools" (the second provides json_reformat on Ubuntu; on Red Hat-style
24 # distributions install "yajl" instead).
30 usage: $myname [-h host] [-p port] [-v] command params...
31 - command is one of deploy, undeploy, status, subscriptions, health, help
32 - (abbreviations dep, undep, stat, subs, heal allowed)
33 - Parameters of the commands that may have parameters:
34 -- deploy: name of the xapp to deploy
35 -- undeploy: name of the xapp to undeploy
37 ---- No parameters: Lists information about all deployed xapps
38 ---- xapp name as parameter: Prints information about the given xapp
39 ---- xapp name and instance: Lists information about the given instance only
40 -- subscriptions is followed by sub-command list, add, delete, or modify
41 ---(abbreviations del and mod for delete and modify are allowed):
42 ---- list without parameters lists all subscriptions
43 ---- list with subscription id prints that subscription
44 ---- add URL eventType maxRetry retryTimer
45 ------- URL is the URL to notify
46 ------- eventType one of created,deleted,all
47 ------- maxRetry and retryTimer are positive decimal numbers
48 ---- modify id URL eventType maxRetry retryTimer
49 ------- id is the subscription id (find out with the list command)
50 --------the rest of the parameters are like in add
52 ------- id is the subscription id to delete (find out with the list command)
53 - Default values for host and port can be set in environment
54 - variables APPMGR_HOST and APPMGR_PORT
55 - Option -v sets verbose mode.
65 # Check for environment override
66 if [ "x$APPMGR_HOST" != "x" ]; then
69 if [ "x$APPMGR_PORT" != "x" ]; then
73 # Proper shell option parsing:
74 while getopts "h:p:v" flag
76 # Curiously, getopts does not guard against an argument-requiring option
77 # eating the next option. It also does not handle the -- convention.
78 # Here is how to fix that.
79 if [ "X$OPTARG" = 'X--' ]; then
80 break # Explicit end of options
82 if expr -- "$OPTARG" : '-.*' > /dev/null ; then
83 echo $myname: Option -$flag has no required value, or value begins with -,
84 echo - which is disallowed.
96 echo $myname: Bad option letter or required option argument missing.
102 # Get rid of the option part
105 if [ $verbose = 1 ]; then
133 if [ "x$1" = "x" ]; then
134 echo "$myname: Missing command"
136 echo "$myname: Unrecognized command $1"
143 if [ $verbose = 1 ]; then
144 echo "Command $cmd params=$2"
147 errfile=`mktemp /tmp/appmgr_e.XXXXXXXXXX`
148 resultfile=`mktemp /tmp/appmgr_r.XXXXXXXXXX`
149 # Variable status used for the return value of the whole script.
152 # Helper for command execution:
153 # Do a rest call with "curl": $1 = method, $2 = path (without host and port
154 # which come from variables), $3 data to POST if needed
155 # returns 0 if OK, and any returned data is in $resultfile
156 # else 1, and error message from curl is in $errfile, which is printed
157 # before returning the 1.
158 # Also sets $status to the return value.
160 # On curl options: --silent --show-error disables progress bar, but allows
161 # error messages. --connect-timeout 20 limits waiting for connection to
162 # 20 seconds. In practice connection will succeed almost immediately,
163 # or in the case of wrong address not at all.
167 if [ "x$3" != "x" ]; then
170 if curl --silent --show-error --connect-timeout 20 --header "Content-Type: application/json" -X $1 -o $resultfile "http://${host}:${port}$2" $data 2> $errfile ;then
180 rm -f $errfile $resultfile
183 # Execute command ($cmd guaranteed to be valid)
184 # Assumes the API currently implemented.
185 # Functions for each command below (except health which is so simple).
188 base_xapps=$base/xapps
189 base_health=$base/health
190 base_subs=$base/subscriptions
193 if [ "x$1" != "x" ]; then
194 if rest POST $base_xapps \{\"name\":\"$1\"\} ; then
195 json_reformat < $resultfile
198 echo Error: expected the name of xapp to deploy
207 if [ "x$1" != "x" ]; then
208 urlpath="$urlpath/$1"
209 if rest DELETE $urlpath; then
210 # Currently xapp_manager returns an empty result if
211 # undeploy is succesfull. Don't reformat file if empty.
212 if [ -s $resultfile ]; then
213 json_reformat < $resultfile
219 echo Error: expected the name of xapp to undeploy
228 if [ "x$1" != "x" ]; then
229 urlpath="$urlpath/$1"
231 if [ "x$2" != "x" ]; then
232 urlpath="$urlpath/instances/$2"
234 if rest GET $urlpath; then
235 json_reformat < $resultfile
239 # This is a bit more complex. $1 is sub-command: list, add, delete, modify
241 # Validate the subscription data that follows a subscription add or modify
242 # subcommand. $1=URL, $2=eventType, $3=maxRetries, $4=retryTimer
243 # URL must look like URL, event type must be one of created deleted all,
244 # maxRetries and retryTimer must be non-negative numbers.
245 # If errors, sets variable status=1 and prints errors, else leaves
248 validate_subscription() {
249 if ! expr "$1" : "^http://.*" \| "$1" : "^https://.*" >/dev/null; then
250 echo "$myname: bad URL $1"
253 if ! [ "$2" = created -o "$2" = deleted -o "$2" = all ]; then
254 echo "$myname: unrecognized event $2"
257 if ! expr "$3" : "^[0-9][0-9]*$" >/dev/null; then
258 echo "$myname: invalid maximum retries count $3"
261 if ! expr "$4" : "^[0-9][0-9]*$" >/dev/null; then
262 echo "$myname: invalid retry time $4"
272 if [ "x$2" != "x" ]; then
273 urlpath="$urlpath/$2"
275 if rest GET $urlpath; then
276 json_reformat < $resultfile
282 validate_subscription "$2" "$3" "$4" "$5"
283 if [ $status = 0 ]; then
284 if rest POST $urlpath \{\"targetUrl\":\"$2\",\"eventType\":\"$3\",\"maxRetries\":$4,\"retryTimer\":$5\} ; then
285 json_reformat < $resultfile
292 if [ "x$2" != "x" ]; then
293 urlpath="$urlpath/$2"
295 echo "$myname: Subscription id required"
298 if [ $status = 0 ]; then
299 if rest DELETE $urlpath; then
300 # Currently xapp_manager returns an empty result if
301 # delete is succesfull. Don't reformat file if empty.
302 if [ -s $resultfile ]; then
303 json_reformat < $resultfile
305 echo "Subscription $2 deleted"
313 if [ "x$2" != "x" ]; then
314 urlpath="$urlpath/$2"
316 echo "$myname: Subscription id required"
319 if [ $status = 0 ]; then
320 validate_subscription "$3" "$4" "$5" "$6"
321 if [ $status = 0 ]; then
322 if rest PUT $urlpath \{\"targetUrl\":\"$3\",\"eventType\":\"$4\",\"maxRetries\":$5,\"retryTimer\":$6\} ; then
323 json_reformat < $resultfile
331 echo "$myname: unrecognized subscriptions subcommand $1"
347 do_subscriptions "$2" "$3" "$4" "$5" "$6" "$7"
350 if rest GET $base_health ; then
360 # An Emacs hack to set the indentation style of this file