RIFT OSM R1 Initial Submission
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / scripts / rwlaunchpad
1 #!/bin/bash
2
3 # Script details
4 SCRIPTNAME=`basename $0`
5 SCRIPT=$0
6 SCRIPT_ARGS=${@}
7
8 # Initialize some of the variables
9 if [ "$RIFT_LP_ADDR" = "" ]; then
10   RIFT_LP_ADDR="localhost"
11 fi
12 PKGS=()
13 INSTANTIATE=0
14 DESC_ID=""
15 NS_NAME=""
16 LOGGING=0
17 RIFT_LP_PKG_UPLOAD_URL="https://${RIFT_LP_ADDR}:4567/api/upload"
18
19 ######################################################################
20 #  Function:usage                                                    #
21 #           Prints usage                                             #
22 ######################################################################
23 function usage() {
24   cat <<EOF
25   usage $SCRIPTNAME [-h] [-r launchpad-ip][-u upload-package][-i ns-service-name [-d descriptor-id]][-l]
26        -h : show this message
27        -r : launchpad ip address  -  defaults to RIFT_LP_ADDR enviroment variable
28        -u : upload package with the package name specified
29        -i : Instantiate a network service with network service name
30        -d : Instantiate a network service with the specified descriptor
31        -l : Log to file
32 EOF
33 }
34
35 ######################################################################
36 #  Function:validate_args                                            #
37 #           Validates the passed arguments                           #
38 ######################################################################
39 function validate_args () {
40   if [ "$RIFT_LP_ADDR" = "" ]; then
41     echo "RIFT LP address must be specified - set RIFT_LP_ADDR or specify -l option"
42     usage
43     exit 1
44   fi
45   if [ "${#PKGS[@]}" -eq 0 -a "${INSTANTIATE}" -eq 0 ]; then
46     echo "One of -u or -i option must be specified"
47     usage
48     exit 1
49   fi
50   if [ "${INSTANTIATE}" -eq 1 ]; then
51     if [ "${NS_NAME}" = "" -o "${DESC_ID}" = "" ]; then
52       echo "Must specify both descriptor id and ns service name when -i is specified"
53       usage
54       exit 1
55     fi
56   fi
57 }
58
59 ######################################################################
60 #  Function:upload_pacakage                                          #
61 #           Uploads a package with the passed argument               #
62 ######################################################################
63 function upload_package() {
64   if [ -z "$1" ]; then
65     echo "upload_package: package name should be passed in as an argument"
66     usage
67     exit 1
68   fi
69   PACKAGE=$1
70   curl --insecure -F "descriptor=@${PACKAGE}" ${RIFT_LP_PKG_UPLOAD_URL}
71 }
72
73 ######################################################################
74 #  Function:instantiate_ns                                           #
75 #           Instantiates a netork service                            #
76 ######################################################################
77 function instantiate_ns() {
78   echo "instantiate_ns need implementation"
79 }
80
81
82 while getopts ":hl:r:u:i:n:" OPTION
83 do
84     case $OPTION in
85         h)
86             usage
87             exit 1
88             ;;
89         r)
90             RIFT_LP_ADDR=$OPTARG
91             RIFT_LP_PKG_UPLOAD_URL="https://${RIFT_LP_ADDR}:4567/api/upload"
92             ;;
93         u)
94             PKGS+=($OPTARG)
95             ;;
96         i)
97             INSTANTIATE=1
98             NS_NAME=$OPTARG
99             ;;
100         n)
101             DESC_ID=$OPTARG
102             ;;
103         l)
104             LOGGING=1
105             ;;
106         *)
107             usage
108             exit 1
109             ;;
110     esac
111 done
112
113 shift $((OPTIND-1))
114
115 validate_args
116
117 if [ $LOGGING -eq 1 ]; then
118     LOGDIR="/tmp"
119     LOGFILE="$LOGDIR/$SCRIPTNAME-$DATE.log"
120     echo "Logging to file $LOGFILE"
121
122     # Redirect stdout ( > ) and stderr to file
123     # and store the STDOUT and STDERR for later use
124     exec 3>&1
125     exec 4>&2
126     exec >$LOGFILE
127     exec 2>&1
128 fi
129
130 echo "Started at $DATE"
131
132 # Iterate through the packages and upload them
133 for PKG in "${PKGS[@]}"
134 do
135   echo "Uploading package $PKG"
136   upload_package $PKG
137   echo ""
138 done
139
140 if [ "${INSTANTIATE}" -eq 1 ]; then
141   instantiate_ns $DESC_ID
142 fi
143
144 echo "Ended at $DATE"