update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / examples / ping_pong_ns / stand_up_ping_pong
1 #!/bin/bash
2
3 # arguments
4 if [ -z $1 ] 
5 then 
6     echo "must supply ip for launchpad"
7     exit -1
8 else
9     lp_ip=${1}
10 fi
11
12 username=$(whoami)
13
14 # make sure we're in a rift shell
15 if [ -z $RIFT_ROOT ] 
16 then 
17     echo "must be in a rift-shell"
18     exit -1
19 fi
20
21 # make sure the system is up
22 system_is_up() {
23     response=$(curl --silent --insecure \
24                     -o /dev/null \
25                     --write-out "%{http_code}"\
26                     --user admin:admin \
27                     https://${lp_ip}:8008/api/config/launchpad-config \
28                     --request GET \
29             )
30
31    if [ ${response} -eq 200 ]
32    then
33        return 0
34    else
35        if [ ${response} -eq 404 ]
36        then
37            # not running a launchpad!
38            echo "riftware is running on ${lp_ip} but there is no launchpad-config"
39            exit -1
40        fi
41        return 1
42    fi
43 }
44
45 #echo -n "wait for system"
46 #while ! system_is_up
47 #do
48 #    echo -n "."
49 #    sleep 5s
50 #done
51
52 echo "system is up"
53
54 # setup the openstack account
55 echo -n "adding account"
56
57 post_json_to_rwrest() {
58    if [ -z "$1" ]
59    then
60      echo "must supply url"
61      exit -1
62    else
63        url=$1
64    fi
65
66    if [ -z "$2" ]
67    then
68      echo "must supply payload"
69      exit -1
70    else
71        payload="${2}"
72    fi
73
74    response=$(curl --silent --insecure \
75                    --header "content-type:application/vnd.yang.data+json" \
76                    --header "Accept:application/vnd.yang.data+json" \
77                    --user admin:admin \
78                    https://${lp_ip}:8008${url} \
79                    --request POST --data "${payload}" \
80            )
81
82     added_account=$(echo "${response}" | grep -e \"success|ok\" | wc -l)
83     already_exists=$(echo "${response}" | grep \"data-exists\" | wc -l)
84     success=$((added_account + already_exists))
85 }
86
87 account_payload=" {
88   \"account\": [
89     {
90       \"name\": \"OS\",
91       \"account-type\": \"openstack\",
92       \"openstack\": {
93         \"auth_url\": \"http://engstack.eng.riftio.com:5000/v3/\",
94         \"secret\": \"mypasswd\",
95         \"key\": \"${username}_automation\",
96         \"tenant\": \"${username}_automation\",
97         \"mgmt-network\": \"private\"
98       }
99     }
100   ]
101 }"
102
103 post_json_to_rwrest "/api/config/cloud/account" "${account_payload}"
104
105 if [ ${success} -ne 1 ];
106 then
107     echo -en "\r" # clear pending line
108     echo "failed to add cloud account:"
109     echo ${response}
110     exit 0
111 else
112     echo " success"
113 fi
114
115 # onboard descriptors
116 cd $RIFT_BUILD/modules/core/mano/src/core_mano-build/examples/ping_pong_ns
117
118 wait_for_package() {
119    if [ -z "$1" ]
120    then
121      echo "must supply transaction id to wait for"
122      exit -1
123    fi
124    
125    project="default"
126    response=$(curl --silent --insecure https://${lp_ip}:8008/api/operational/project/${project}/create-jobs/job/${transaction_id})
127    transaction_state=$(echo ${response} | awk -F "status" '{print $2}' | awk '{print $2}')
128    transaction_state=${transaction_state:1:-2}
129    
130    if [ ${transaction_state} == "pending" ];
131    then
132        return 0
133    else
134        return 1
135    fi
136 }
137
138 upload_package() {
139    if [ -z "$1" ]
140    then
141      echo "must supply package to upload"
142      exit -1
143    else
144        package=$1
145    fi
146
147    echo -n "upload ${package} package"
148
149    response=$(curl --silent --insecure -F "descriptor=@${package}" https://${lp_ip}:4567/api/upload)
150    transaction_id=$(echo ${response} | awk '{print $2}')
151    transaction_id=${transaction_id:1:-2}
152
153    while wait_for_package transaction_id
154    do
155        echo -n "."
156        sleep 1s
157    done
158    if [ ${transaction_state} == "failure" ];
159    then
160        echo "failed"
161        status=1
162    else
163        echo "success"
164        status=0
165    fi
166
167 }
168
169 upload_package "ping_vnfd.tar.gz"
170 ping_status=${status}
171 upload_package "pong_vnfd.tar.gz"
172 pong_status=${status}
173
174 success=$((ping_status + pong_status))
175
176 if [ ${success} -ne 0 ];
177 then
178     echo -en "\r" # clear pending line
179     echo "cannot on-board nsd when a vnfd fails"
180     exit -1
181 fi
182
183 upload_package "ping_pong_nsd.tar.gz"
184 if [ ${status} -ne 0 ];
185 then
186     echo -en "\r" # clear pending line
187     echo "failed to on-board nsd"
188     exit -1
189 fi
190
191 # instantiate ping_pong_nsd
192 echo "instantiate ping pong nsd"
193
194 tag=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1)
195
196 tmpdir="/tmp/${tag}"
197 mkdir ${tmpdir}
198
199 tar -xf ping_pong_nsd.tar.gz -C ${tmpdir}
200
201 nsdfile="${tmpdir}/ping_pong_nsd/ping_pong_nsd.yaml"
202
203 nsd_id=$(cat ${nsdfile} | grep "nsd:id" | head -n1 | awk '{print $2}')
204
205 rm -r ${tmpdir}
206
207 nsr_id=$(cat /proc/sys/kernel/random/uuid)
208 nsd_payload="{
209     \"nsr\":[
210         {
211             \"id\":\"${nsr_id}\",
212             \"nsd-ref\":\"${nsd_id}\",
213             \"name\":\"${username}-${tag}-ping-pong-nsd\",
214             \"short-name\":\"pingpong\",
215             \"description\":\"ping pong nsd instantiated by ${username} with tag ${tag}\",
216             \"admin-status\":\"ENABLED\",
217             \"cloud-account\":\"OS\"
218         }
219     ]
220 }"
221
222 post_json_to_rwrest "/api/config/ns-instance-config" "${nsd_payload}"
223
224 if [ ${success} -ne 1 ];
225 then
226     echo -en "\r" # clear pending line
227     echo "failed to instantiate nsd:"
228     echo ${response}
229     exit -1
230 else
231     echo " success"
232 fi
233