Send both ubuntu versions to Robot VM
[demo.git] / boot / dcae2_vm_init.sh
1 #!/bin/bash
2 #############################################################################
3 #
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
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 #        http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #############################################################################
18
19
20 # prepare a curl command
21 # parameters: URL METHOD CURLOPTIONS EXTRA_HEADERS_AS_A_STRING AUTH_AS_USER:PASS DATA
22 assemble_curl_command()
23 {
24     local URL="$1"
25     local METHOD="$2"
26     local CURLOPTIONS="$3"
27     local EXTRA_HEADERS="$4"
28     local AUTH="$5"
29     local DATA="$6"
30     local CMD=''
31     if [ ! -z "$METHOD" ]; then
32         CMD="curl $CURLOPTIONS $METHOD"
33     else
34         CMD="curl $CURLOPTIONS -X GET"
35     fi
36     if [ ! -z "$EXTRA_HEADERS" ]; then
37         CMD="$CMD $EXTRA_HEADERS"
38     fi
39     if [ ! -z "$AUTH" ]; then
40         CMD="$CMD $AUTH"
41     fi
42     if [ ! -z "$DATA" ]; then
43         CMD="$CMD $DATA"
44     fi
45     CMD="$CMD $URL"
46     echo "$CMD"
47 }
48
49
50 # Make a rest API call
51 # parameters: URL METHOD expected_response_code EXTRA_HEADERS_AS_A_STRING AUTH_AS_USER:PASS DATA
52 call_api_for_response_code()
53 {
54     local CURLOPTIONS='-kIso /dev/null -w "%{http_code}"'
55     read -r CMDF <<-END
56 $(assemble_curl_command "$1" "$2" "$CURLOPTIONS" "$4" "$5" "$6")
57 END
58     eval "$CMDF";
59 }
60 call_api_for_response_body()
61 {
62     local CURLOPTIONS='-ksb'
63     read -r CMDF <<-END
64 $(assemble_curl_command "$1" "$2" "$CURLOPTIONS" "$4" "$5" "$6")
65 END
66     eval "$CMDF"
67 }
68 call_api_for_response_header()
69 {  
70     local CURLOPTIONS='-ks -o /dev/null -D -'
71     read -r CMDF <<-END
72 $(assemble_curl_command "$1" "$2" "$CURLOPTIONS" "$4" "$5" "$6")
73 END
74     eval "$CMDF"
75 }
76 call_api_for_verbose()
77
78     local CURLOPTIONS='-kIv'
79     read -r CMDF <<-END
80 $(assemble_curl_command "$1" "$2" "$CURLOPTIONS" "$4" "$5" "$6")
81 END
82     eval "$CMDF"
83     #local TFILE=$(mktemp /tmp/curlcmd.XXXXXXXXX)
84     #echo $CMD > $TFILE
85     #eval $(cat $TFILE)
86     #rm -f $TFILE
87 }
88
89
90 # Wait till a web service API return specified response code
91 # parameters: URL METHOD EXPECTED_RESP_CODE EXTRA_HEADERS_AS_A_STRING AUTH_AS_USER:PASS DATA
92 wait_for_api()
93 {
94     local RESP="$3" 
95     local ACTUALRESP
96     ACTUALRESP=$(call_api_for_response_code "$1" "$2" "$3" "$4" "$5" "$6")
97     while [ "$ACTUALRESP" != "$RESP" ]; do
98         echo "RESP CODE $ACTUALRESP, not as expected RESP CODE $RESP @ $(date)."
99         sleep 30
100         ACTUALRESP=$(call_api_for_response_code "$1" "$2" "$3" "$4" "$5" "$6")
101     done
102     echo "RESP CODE $ACTUALRESP, matches with expected RESP CODE $RESP."
103 }
104
105 # Wait till a TCP port is open
106 # parameters: HOST PORT
107 wait_for_tcp_port()
108 {  
109     local DEST="$1"
110     local PORT="$2"
111     while ! nc -z -w 1 "$DEST" "$PORT"; do
112         sleep 4
113         echo '.'
114     done
115 }
116
117
118
119
120 wait_for_aai_ready()
121 {
122     # wait till A&AI up and ready
123     local AAIHOST
124     AAIHOST=$(cat /opt/config/aai1_ip_addr.txt)
125     local AAIURL="https://$AAIHOST:8443/aai/v11/examples/cloud-regions"
126     local AAIMETHOD='-X GET'
127     local AAIRESP='200'
128     local AAIHEADERS='-H "X-FromAppId: AAI-Temp-Tool" -H "X-TransactionId: AAI-Temp-Tool" -H "Real-Time: true" -H "Content-Type: application/json" -H "Accept: application/json"'
129     local AAIAUTH='-u AAI:AAI'
130     local AAIDATA=''
131     echo "===> Waiting for A&AI to get ready for getting $AAIRESP from $AAIURL @ $(date)"
132     wait_for_api "$AAIURL" "$AAIMETHOD" "$AAIRESP" "$AAIHEADERS" "$AAIAUTH" "$AAIDATA"
133     echo "===> A&AI ready @ $(date)"
134 }
135
136
137
138 wait_for_multicloud_ready()
139 {
140     # wait till MultiCloud up and ready
141     local MCHOST
142     MCHOST=$(cat /opt/config/openo_ip_addr.txt)
143     local MCURL="http://$MCHOST:9005/api/multicloud-titanium_cloud/v0/swagger.json"
144     local MCMETHOD='-X GET'
145     local MCRESP='200'
146     local MCHEADERS='-H "Real-Time: true" -H "Content-Type: application/json" -H "Accept: application/json"'
147     local MCAUTH=''
148     local MCDATA=''
149     echo "===> Waiting for MultiCloud to get ready for getting $MCRESP from $MCURL @ $(date)"
150     wait_for_api "$MCURL" "$MCMETHOD" "$MCRESP" "$MCHEADERS" "$MCAUTH" "$MCDATA"
151     echo "===> MultiCloud ready @ $(date)"
152 }
153
154 register_multicloud_pod25dns_with_aai()
155 {
156     # Register MultiCloud with A&AI
157     local CLOUD_OWNER='pod25dns'
158     local CLOUD_VERSION='titanium_cloud'
159     local CLOUD_REGION
160     local CLOUD_ENV
161     local CLOUD_IDENTITY_URL
162     local DNSAAS_SERVICE_URL
163     local DNSAAS_USERNAME='demo'
164     local DNSAAS_PASSWORD='onapdemo'
165
166     CLOUD_REGION="$(cat /opt/config/dnsaas_region.txt)"
167     CLOUD_ENV="$(cat /opt/config/cloud_env.txt)"
168     MCIP="$(cat /opt/config/openo_ip_addr.txt)"
169     CLOUD_IDENTITY_URL="http://${MCIP}/api/multicloud-titanium_cloud/v0/${CLOUD_OWNER}_${CLOUD_REGION}/identity/v2.0"
170
171     local RESPCODE
172     DNSAAS_SERVICE_URL="$(cat /opt/config/dnsaas_keystone_url.txt)"
173     # a tenant of the same name must be set up on the Deisgnate providing OpenStack
174     DNSAAS_TENANT_NAME="$(cat /opt/config/dnsaas_tenant_name.txt)"
175     cat >"/tmp/${CLOUD_OWNER}_${CLOUD_REGION}.json" <<EOL
176 {
177     "cloud-owner" : "$CLOUD_OWNER",
178     "cloud-region-id" : "$CLOUD_REGION",
179     "cloud-region-version" : "$CLOUD_VERSION",
180     "cloud-type" : "$CLOUD_ENV",
181     "cloud-zone" : "cloud zone",
182     "complex-name" : "complex name2",
183     "identity-url": "$CLOUD_IDENTITY_URL",
184     "owner-defined-type" : "owner-defined-type",
185     "sriov-automation" : false,
186     "esr-system-info-list" : {
187         "esr-system-info" : [
188             {
189                 "esr-system-info-id": "532ac032-e996-41f2-84ed-9c7a1766eb30",
190                 "cloud-domain": "Default",
191                 "default-tenant" : "$DNSAAS_TENANT_NAME",
192                 "user-name" : "$DNSAAS_USERNAME",
193                 "password" : "$DNSAAS_PASSWORD",
194                 "service-url" : "$DNSAAS_SERVICE_URL",
195                 "ssl-cacert": "example-ssl-cacert-val-75021",
196                 "ssl-insecure": true,
197                 "system-name": "example-system-name-val-29071",
198                 "system-type": "VIM",
199                 "ip-address": "example-ip-address-val-44432",
200                 "port": "example-port-val-93235",
201                 "type": "example-type-val-85255",
202                 "protocal": "example-protocal-val-52954",
203                 "vendor": "example-vendor-val-94515",
204                 "version": "example-version-val-71880"
205             }
206         ]
207     }
208 }
209 EOL
210
211
212     local REGHOST
213     local REGURL
214     local REGMETHOD='-X PUT'
215     local REGHEADERS='-H "X-FromAppId: AAI-Temp-Tool" -H "X-TransactionId: AAI-Temp-Tool" -H "Real-Time: true" -H "Content-Type: application/json" -H "Accept: application/json"'
216     local REGRESP='201'
217     local REGAUTH='-u AAI:AAI'
218     local REGDATA
219
220     REGHOST="$(cat /opt/config/aai1_ip_addr.txt)"
221     REGURL="https://$REGHOST:8443/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/$CLOUD_OWNER/$CLOUD_REGION"
222     REGDATA="-T /tmp/${CLOUD_OWNER}_${CLOUD_REGION}.json"
223     echo "Register MultiCloud with A&AI owner $CLOUD_OWNER"
224     RESP=$(call_api_for_response_code "$REGURL" "$REGMETHOD" "$REGRESP" "$REGHEADERS" "$REGAUTH" "$REGDATA")
225     echo "RESP CODE: $RESP"
226 }
227
228
229 register_multicloud_pod25_with_aai()
230
231     # Register MultiCloud with A&AI
232     local CLOUD_OWNER='pod25'
233     local CLOUD_VERSION='titanium_cloud'
234     local CLOUD_REGION
235     local DNSAAS_CLOUD_REGION
236     local CLOUD_ENV
237     local MCIP
238     local CLOUD_IDENTITY_URL
239     local KEYSTONE_URL
240     local USERNAME
241     local PASSWORD
242     local TENANT_NAME
243
244     CLOUD_REGION="$(cat /opt/config/openstack_region.txt)"
245     DNSAAS_CLOUD_REGION="$(cat /opt/config/dnsaas_region.txt)"
246     CLOUD_ENV="$(cat /opt/config/cloud_env.txt)"
247     MCIP="$(cat /opt/config/openo_ip_addr.txt)"
248     CLOUD_IDENTITY_URL="http://${MCIP}/api/multicloud-titanium_cloud/v0/${CLOUD_OWNER}_${CLOUD_REGION}/identity/v2.0"
249     KEYSTONE_URL="$(cat /opt/config/openstack_keystone_url.txt)"
250     if [[ "$KEYSTONE_URL" == */v3 ]]; then
251         echo "$KEYSTONE_URL"
252     elif [[ "$KEYSTONE_URL" == */v2.0 ]]; then
253         echo "$KEYSTONE_URL"
254     else
255         KEYSTONE_URL="${KEYSTONE_URL}/v3"
256         echo "$KEYSTONE_URL"
257     fi
258     USERNAME="$(cat /opt/config/openstack_user.txt)"
259     PASSWORD="$(cat /opt/config/openstack_password.txt)"
260     TENANT_NAME="$(cat /opt/config/tenant_name.txt)"
261     cat >"/tmp/${CLOUD_OWNER}_${CLOUD_REGION}.json" <<EOL
262 {
263     "cloud-owner" : "$CLOUD_OWNER",
264     "cloud-region-id" : "$CLOUD_REGION",
265     "cloud-region-version" : "$CLOUD_VERSION",
266     "cloud-type" : "$CLOUD_ENV",
267     "cloud-zone" : "cloud zone",
268     "complex-name" : "complex name",
269     "identity-url": "$CLOUD_IDENTITY_URL",
270     "owner-defined-type" : "owner-defined-type",
271     "sriov-automation" : false,
272     "cloud-extra-info" : "{\"epa-caps\":{\"huge_page\":\"true\",\"cpu_pinning\":\"true\",\"cpu_thread_policy\":\"true\",\"numa_aware\":\"true\",\"sriov\":\"true\",\"dpdk_vswitch\":\"true\",\"rdt\":\"false\",\"numa_locality_pci\":\"true\"},\"dns-delegate\":{\"cloud-owner\":\"pod25dns\",\"cloud-region-id\":\"${DNSAAS_CLOUD_REGION}\"}}",
273     "esr-system-info-list" : {
274         "esr-system-info" : [
275             {
276                 "esr-system-info-id": "432ac032-e996-41f2-84ed-9c7a1766eb29",
277                 "cloud-domain": "Default",
278                 "default-tenant" : "$TENANT_NAME",
279                 "user-name" : "$USERNAME",
280                 "password" : "$PASSWORD",
281                 "service-url" : "$KEYSTONE_URL",
282                 "ssl-cacert": "example-ssl-cacert-val-75021",
283                 "ssl-insecure": true,
284                 "system-name": "example-system-name-val-29070",
285                 "system-type": "VIM",
286                 "ip-address": "example-ip-address-val-44431",
287                 "port": "example-port-val-93234",
288                 "type": "example-type-val-85254",
289                 "protocal": "example-protocal-val-52954",
290                 "vendor": "example-vendor-val-94515",
291                 "version": "example-version-val-71880"
292             }
293         ]
294     }
295 }
296 EOL
297  
298     local REGHOST
299     local REGURL
300     local REGMETHOD='-X PUT'
301     local REGHEADERS='-H "X-FromAppId: AAI-Temp-Tool" -H "X-TransactionId: AAI-Temp-Tool" -H "Real-Time: true" -H "Content-Type: application/json" -H "Accept: application/json"'
302     local REGRESP='201'
303     local REGAUTH='-u AAI:AAI'
304     local REGDATA
305  
306     REGHOST="$(cat /opt/config/aai1_ip_addr.txt)"
307     REGURL="https://$REGHOST:8443/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/$CLOUD_OWNER/$CLOUD_REGION"
308     REGDATA="-T /tmp/${CLOUD_OWNER}_${CLOUD_REGION}.json"
309     echo "Register MultiCloud with A&AI owner $CLOUD_OWNER"
310     RESP=$(call_api_for_response_code "$REGURL" "$REGMETHOD" "$REGRESP" "$REGHEADERS" "$REGAUTH" "$REGDATA")
311     echo "RESP CODE: $RESP"
312 }
313
314
315
316 verify_multicloud_registration() 
317 {
318     local CLOUD_OWNER='pod25'
319     local CLOUD_REGION
320     local CLOUD_VERSION='titanium_cloud'
321     local CLOUD_ENV
322     local REGHOST
323     local REGURL
324     local REGMETHOD='-X GET'
325     local REGHEADERS='-H "X-FromAppId: AAI-Temp-Tool" -H "X-TransactionId: AAI-Temp-Tool" -H "Real-Time: true" -H "Content-Type: application/json" -H "Accept: application/json"'
326     local REGRESP='200'
327     local REGAUTH='-u AAI:AAI'
328     local REGDATA=''
329     local RESPCODE
330
331     CLOUD_REGION="$(cat /opt/config/openstack_region.txt)"
332     CLOUD_ENV="$(cat /opt/config/cloud_env.txt)"
333     REGHOST="$(cat /opt/config/aai1_ip_addr.txt)"
334
335     REGURL="https://$REGHOST:8443/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/${CLOUD_OWNER}/${CLOUD_REGION}?depth=all"
336     # Verify MultiCloud with A&AI
337     RESPCODE=$(call_api_for_response_code "$REGURL" "$REGMETHOD" "$REGRESP" "$REGHEADERS" "$REGAUTH" "$REGDATA")
338     echo "Register MultiCloud with A&AI owner $CLOUD_OWNER verify response code: $RESPCODE"
339
340     CLOUD_OWNER='pod25dns'
341     REGURL="https://$REGHOST:8443/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/${CLOUD_OWNER}/${CLOUD_REGION}?depth=all"
342     RESPCODE=$(call_api_for_response_code "$REGURL" "$REGMETHOD" "$REGRESP" "$REGHEADERS" "$REGAUTH" "$REGDATA")
343     echo "Register MultiCloud with A&AI owner $CLOUD_OWNER verify response code: $RESPCODE"
344 }
345
346
347 register_dns_zone_proxied_designate()
348 {
349     local CLOUD_OWNER='pod25' 
350     local CLOUD_REGION
351     local CLOUD_VERSION='titanium_cloud'
352     local CLOUD_ENV
353     local DNSAAS_TENANT_NAME
354     local MCHOST
355     local MCURL
356     local MCMETHOD='-X POST'
357     local MCRESP='200'
358     local MCHEADERS='-H "Content-Type: application/json" -H "Accept: application/json"'
359     local MCAUTH=''
360     local MCDATA=''
361
362     ## export endpoint prefix
363     local MULTICLOUD_PLUGIN_ENDPOINT
364
365     CLOUD_REGION="$(cat /opt/config/openstack_region.txt)"
366     CLOUD_ENV="$(cat /opt/config/cloud_env.txt)"
367     if [ -z "$1" ]; then DCAE_ZONE="$(cat /opt/config/dcae_zone.txt)"; else DCAE_ZONE="$1"; fi
368     DNSAAS_TENANT_NAME="$(cat /opt/config/dnsaas_tenant_name.txt)"
369     MCHOST=$(cat /opt/config/openo_ip_addr.txt)
370     MCURL="http://$MCHOST:9005/api/multicloud-titanium_cloud/v0/swagger.json"
371
372     MCDATA='-d "{\"auth\":{\"tenantName\": \"'${DNSAAS_TENANT_NAME}'\"}}"'
373     MULTICLOUD_PLUGIN_ENDPOINT=http://${MCHOST}/api/multicloud-titanium_cloud/v0/${CLOUD_OWNER}_${CLOUD_REGION}
374
375      ### zone operations
376      # because all VM's use 10.0.100.1 as their first DNS server, the designate DNS server as seocnd, we need to use a
377      # domain outside of the first DNS server's domain
378     local DCAE_DOMAIN
379     local ZONENAME
380     DCAE_DOMAIN="$(cat /opt/config/dcae_domain.txt)"
381     ZONENAME="${DCAE_ZONE}.${DCAE_DOMAIN}."
382
383     echo "===> Register DNS zone $ZONENAME under $DNSAAS_TENANT_NAME"
384
385
386     ### Get Token
387     local TOKEN
388
389     MCURL="${MULTICLOUD_PLUGIN_ENDPOINT}/identity/v3/auth/tokens"
390     echo "=====> Getting token from $MCURL"
391     #TOKEN=$(call_api_for_response_header "$MCURL" "$MCMETHOD" "$MCRESP" "$MCHEADERS" "$MCAUTH" "$MCDATA" | grep 'X-Subject-Token' | sed "s/^.*: //")
392     TOKEN=$(curl -v -s -H "Content-Type: application/json" -X POST -d "{\"auth\":{\"tenantName\": \"${DNSAAS_TENANT_NAME}\"}}" "${MCURL}" 2>&1 | grep X-Subject-Token | sed "s/^.*: //")
393     echo "Received Keystone token $TOKEN from $MCURL"
394     if [ -z "$TOKEN" ]; then
395         echo "Faile to acquire token for creating DNS zone.  Exit"
396         exit 1
397     fi
398
399     local PROJECTID
400     PROJECTID=$(curl -v -s  -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones?name=${ZONENAME}" |grep 'project_id' |sed 's/^.*"project_id":"\([a-zA-Z0-9-]*\)",.*$/\1/')
401     if [ ! -z "$PROJECTID" ]; then 
402         ### query the zone with zone id
403         echo "!!!!!!> zone $ZONENAME already registered by project $PROJECTID"
404     else
405         ### create a zone
406         echo "=====> No zone of same name $ZONENAME found, creating new zone "
407         curl -sv -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X POST -d "{ \"name\": \"$ZONENAME\", \"email\": \"lji@research.att.com\"}" "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones"
408     fi
409
410     ### list zones
411     echo "=====> Zone listing"
412     curl -sv -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones" | python -m json.tool
413
414     ### query the zone with zone name
415     #echo "=====> Querying zone $ZONENAME"
416     #curl -s -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones?name=${ZONENAME}"
417
418     ### export ZONE id
419     local ZONEID
420     ZONEID=$(curl -v -sb  -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones?name=${ZONENAME}" |grep 'id' |sed 's/^.*"id":"\([a-zA-Z0-9-]*\)",.*$/\1/')
421     echo "=====> After creation, zone $ZONENAME ID is $ZONEID"
422
423     ### query the zone with zone id
424     #echo "=====> Querying zone $ZONENAME by ID $ZONEID"
425     #curl -sv -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones/${ZONEID}"
426 }
427
428
429 register_dns_zone_designate()
430 {
431     local HEADER_CONTENT_TYPE_JSON="Content-Type: application/json"
432     local HEADER_ACCEPT_JSON="Accept: application/json"
433     local HEADER_TOKEN
434     local DCAE_ZONE
435     local DCAE_DOMAIN
436     local ZONE_NAME
437     local ZONE_ID
438     local KEYSTONE_URL
439     local API_ENDPOINT
440     local API_DATA
441     local TENANT_NAME
442     local TENANT_ID
443     local ZONE_PROJECT_ID
444     
445     if [ -z "$1" ]; then DCAE_ZONE="$(cat /opt/config/dcae_zone.txt)"; else DCAE_ZONE="$1"; fi
446     DCAE_DOMAIN="$(cat /opt/config/dcae_domain.txt)"
447     ZONE_NAME="${DCAE_ZONE}.${DCAE_DOMAIN}."
448
449     TENANT_NAME="$(cat /opt/config/tenant_name.txt)"
450     TENANT_ID="$(cat /opt/config/tenant_id.txt)"
451
452     KEYSTONE_URL="$(cat /opt/config/openstack_keystone_url.txt)"
453     if [[ "$KEYSTONE_URL" == */v3 ]]; then
454         echo "$KEYSTONE_URL"
455     elif [[ "$KEYSTONE_URL" == */v2.0 ]]; then
456         echo "$KEYSTONE_URL"
457     else
458         KEYSTONE_URL="${KEYSTONE_URL}/v2.0"
459         echo "$KEYSTONE_URL"
460     fi
461
462     USERNAME="$(cat /opt/config/openstack_user.txt)"
463     PASSWORD="$(cat /opt/config/openstack_password.txt)"
464
465
466     API_ENDPOINT="${KEYSTONE_URL}/tokens"
467     API_DATA="{\"auth\": {\"project\": \"${TENANT_NAME}\", \"tenantId\": \"${TENANT_ID}\", \"passwordCredentials\": {\"username\": \"${USERNAME}\", \"password\": \"${PASSWORD}\"}}}"
468     
469     echo "===> Getting token from ${API_ENDPOINT}"
470     RESP=$(curl -s -v -H "${HEADER_CONTENT_TYPE_JSON}" -d "${API_DATA}" "${API_ENDPOINT}")
471
472     TOKEN="$(echo ${RESP} | jq -r .access.token.id)"
473     if [ -z "$TOKEN" ]; then
474         echo "Faile to acquire token for creating DNS zone.  Exit"
475         exit 1
476     fi
477     HEADER_TOKEN="X-Auth-Token: ${TOKEN}" 
478  
479
480     DESIGNATE_URL=$(echo ${RESP} | jq -r '.access.serviceCatalog[] | select(.name=="designate") | .endpoints[0].publicURL')
481     if [ -z ${DESIGNATE_URL} ]; then
482         echo "Fail to find Designate API endpoint.  Exit"
483         exit 1
484     fi
485
486
487     API_ENDPOINT="${DESIGNATE_URL}/v2/zones"
488     echo "===> Register DNS zone $ZONE_NAME at Designate API endpoint ${API_ENDPOINT}"
489    
490     RESP=$(curl -v -s -H $HEADER_TOKEN $API_ENDPOINT)
491     ZONE_ID=$(echo $RESP |jq -r --arg ZONE_NAME "$ZONE_NAME" '.zones[] |select(.name==$ZONE_NAME) |.id')
492     if [ -z "$ZONE_ID" ]; then
493         echo "======> Zone ${ZONE_NAME} does not exist.  Create"
494         API_DATA="{\"name\": \"${ZONE_NAME}\", \"email\": \"dcae@onap.org\", \"type\": \"PRIMARY\", \"ttl\": 7200, \"description\": \"DCAE DNS zoen created for ONAP deployment $DCAE_ZONE\"}"
495         RESP=$(curl -s -v -X POST -H "$HEADER_ACCEPT_JSON" -H "$HEADER_CONTENT_TYPE_JSON" -H "$HEADER_TOKEN" -d "$API_DATA" "$API_ENDPOINT")
496         ZONE_ID=$(echo $RESP | jq .id)
497
498         if [ -z "$ZONE_ID" ]; then
499             echo "Fail to create DNS zone ${ZONE_NAME}.  Exit"
500             exit 1
501         fi
502     else
503         echo "======> Zone ${ZONE_NAME} already exists."
504         API_ENDPOINT="${DESIGNATE_URL}/v2/zones/${ZONE_ID}"
505         RESP=$(curl -s -v -H "$HEADER_ACCEPT_JSON" -H "$HEADER_TOKEN" "$API_ENDPOINT")
506         ZONE_PROJECT_ID=$(echo $RESP | jq -r .project_id)
507         if [ "$ZONE_PROJECT_ID" != "noauth-project" ] && [ "$ZONE_PROJECT_ID" != "$TENANT_ID" ]; then
508             echo "======> Zone ${ZONE_NAME} owned by other projects, may have problem creating records"
509         else
510             echo "======> Zone ${ZONE_NAME} okay to create new records"
511         fi
512     fi
513 }
514
515 delete_dns_zone()
516 {
517     local CLOUD_OWNER='pod25'
518     local CLOUD_REGION
519     local CLOUD_VERSION='titanium_cloud'
520     local CLOUD_ENV
521     local DCAE_ZONE
522     local DNSAAS_TENANT_NAME
523     local MCHOST
524     local MCURL
525     local MCMETHOD='-X GET'
526     local MCRESP='200'
527     local MCHEADERS='-H "Real-Time: true" -H "Content-Type: application/json" -H "Accept: application/json"'
528     local MCAUTH=''
529     local MCDATA=''
530     local MULTICLOUD_PLUGIN_ENDPOINT
531
532     CLOUD_REGION="$(cat /opt/config/openstack_region.txt)"
533     CLOUD_ENV="$(cat /opt/config/cloud_env.txt)"
534     DCAE_ZONE="$(cat /opt/config/dcae_zone.txt)"
535     DNSAAS_TENANT_NAME="$(cat /opt/config/dnsaas_tenant_name.txt)"
536     MCHOST=$(cat /opt/config/openo_ip_addr.txt)
537     MCURL="http://$MCHOST:9005/api/multicloud-titanium_cloud/v0/swagger.json"
538
539     local DCAE_DOMAIN
540     local ZONENAME
541     DCAE_DOMAIN="$(cat /opt/config/dcae_domain.txt)"
542     ZONENAME="${DCAE_ZONE}.${DCAE_DOMAIN}."
543
544     MCDATA='"{\"auth\":{\"tenantName\": \"'${DNSAAS_TENANT_NAME}'\"}}"'
545     MULTICLOUD_PLUGIN_ENDPOINT=http://${MCHOST}/api/multicloud-titanium_cloud/v0/${CLOUD_OWNER}_${CLOUD_REGION}
546
547     ### Get Token
548     local TOKEN
549     TOKEN=$(curl -v -s -H "Content-Type: application/json" -X POST -d "{\"auth\":{\"tenantName\": \"${DNSAAS_TENANT_NAME}\"}}" "${MULTICLOUD_PLUGIN_ENDPOINT}/identity/v3/auth/tokens"  2>&1 | grep X-Subject-Token | sed "s/^.*: //")
550
551     local ZONEID
552     ZONEID=$(curl -v -s  -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones?name=${ZONENAME}" |sed 's/^.*"id":"\([a-zA-Z0-9-]*\)",.*$/\1/')
553
554     curl -s -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X DELETE "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones/${ZONEID}"
555 }
556
557 list_dns_zone() 
558 {
559     local CLOUD_OWNER='pod25'
560     local CLOUD_REGION
561     local CLOUD_VERSION='titanium_cloud'
562     local CLOUD_ENV
563     local DCAE_ZONE
564     local DNSAAS_TENANT_NAME
565     local MCHOST
566     local MCURL
567     local MCMETHOD='-X GET'
568     local MCRESP='200'
569     local MCHEADERS='-H "Real-Time: true" -H "Content-Type: application/json" -H "Accept: application/json"'
570     local MCAUTH=''
571     local MCDATA=''
572     local MULTICLOUD_PLUGIN_ENDPOINT
573
574     CLOUD_REGION="$(cat /opt/config/openstack_region.txt)"
575     CLOUD_ENV="$(cat /opt/config/cloud_env.txt)"
576     DCAE_ZONE="$(cat /opt/config/dcae_zone.txt)"
577     DNSAAS_TENANT_NAME="$(cat /opt/config/dnsaas_tenant_name.txt)"
578     MCHOST=$(cat /opt/config/openo_ip_addr.txt)
579     MCURL="http://$MCHOST:9005/api/multicloud-titanium_cloud/v0/swagger.json"
580
581     MCDATA='"{\"auth\":{\"tenantName\": \"'${DNSAAS_TENANT_NAME}'\"}}"'
582     MULTICLOUD_PLUGIN_ENDPOINT=http://${MCHOST}/api/multicloud-titanium_cloud/v0/${CLOUD_OWNER}_${CLOUD_REGION}
583
584     ### Get Token
585     local TOKEN
586     TOKEN=$(curl -v -s -H "Content-Type: application/json" -X POST -d "{\"auth\":{\"tenantName\": \"${DNSAAS_TENANT_NAME}\"}}" "${MULTICLOUD_PLUGIN_ENDPOINT}/identity/v3/auth/tokens"  2>&1 | grep X-Subject-Token | sed "s/^.*: //")
587
588     local DCAE_DOMAIN
589     local ZONENAME
590     DCAE_DOMAIN="$(cat /opt/config/dcae_domain.txt)"
591     ZONENAME="${DCAE_ZONE}.${DCAE_DOMAIN}."
592     local ZONEID
593     ZONEID=$(curl -v -s  -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones?name=${ZONENAME}" |sed 's/^.*"id":"\([a-zA-Z0-9-]*\)",.*$/\1/')
594
595     curl -v -s  -H "Content-Type: application/json" -H "X-Auth-Token: $TOKEN" -X GET "${MULTICLOUD_PLUGIN_ENDPOINT}/dns-delegate/v2/zones/${ZONEID}/recordsets"
596 }
597
598
599
600
601
602 NEXUS_USER=$(cat /opt/config/nexus_username.txt)
603 NEXUS_PASSWORD=$(cat /opt/config/nexus_password.txt)
604 NEXUS_DOCKER_REPO=$(cat /opt/config/nexus_docker_repo.txt)
605 DOCKER_VERSION=$(cat /opt/config/docker_version.txt)
606 # use rand_str as zone
607 ZONE=$(cat /opt/config/rand_str.txt)
608 MYFLOATIP=$(cat /opt/config/dcae_float_ip.txt)
609 MYLOCALIP=$(cat /opt/config/dcae_ip_addr.txt)
610
611 # start docker image pulling while we are waiting for A&AI to come online
612 docker login -u "$NEXUS_USER" -p "$NEXUS_PASSWORD" "$NEXUS_DOCKER_REPO"
613 docker pull "$NEXUS_DOCKER_REPO/onap/org.onap.dcaegen2.deployments.bootstrap:$DOCKER_VERSION" && docker pull nginx &
614
615 #########################################
616 # Wait for then register with A&AI
617 ########################################
618
619 DNSAAS_PROXYED=$(tr '[:upper:]' '[:lower:]' < /opt/config/dnsaas_config_enabled.txt)
620 if [ "$DNSAAS_PROXYED" == 'true' ]; then
621     echo "Using proxyed DNSaaS service, performing additional registration and configuration"
622     wait_for_aai_ready
623
624     register_multicloud_pod25_with_aai
625     register_multicloud_pod25dns_with_aai
626
627     verify_multicloud_registration
628
629     wait_for_multicloud_ready
630     register_dns_zone_proxied_designate "$ZONE" 
631     echo "Registration and configuration for proxying DNSaaS completed."
632 else
633     echo "Using Designate DNSaaS service, performing additional registration and configuration"
634     register_dns_zone_designate "$ZONE" 
635 fi
636
637
638
639
640
641 #########################################
642 # Start DCAE Bootstrap container
643 #########################################
644
645 chmod 777 /opt/app/config
646 rm -f /opt/config/runtime.ip.consul
647 rm -f /opt/config/runtime.ip.cm
648
649
650 #docker login -u "$NEXUS_USER" -p "$NEXUS_PASSWORD" "$NEXUS_DOCKER_REPO"
651 #docker pull "$NEXUS_DOCKER_REPO/onap/org.onap.dcaegen2.deployments.bootstrap:$DOCKER_VERSION"
652 docker run -d --name boot -v /opt/app/config:/opt/app/installer/config -e "LOCATION=$ZONE" "$NEXUS_DOCKER_REPO/onap/org.onap.dcaegen2.deployments.bootstrap:$DOCKER_VERSION"
653
654
655 # waiting for bootstrap to complete then starting nginx for proxying healthcheck calls
656 echo "Waiting for Consul to become accessible"
657 while [ ! -f /opt/app/config/runtime.ip.consul ]; do echo "."; sleep 30; done
658
659
660
661 # start proxy for consul's health check
662 CONSULIP=$(head -1 /opt/app/config/runtime.ip.consul | sed 's/[[:space:]]//g')
663 echo "Consul is available at $CONSULIP" 
664
665 cat >./nginx.conf <<EOL
666 server {
667     listen 80;
668     server_name dcae.simpledemo.onap.org;
669     location /healthcheck {
670         proxy_pass http://${CONSULIP}:8500/v1/health/state/passing;
671     }
672 }
673 EOL
674 docker run --name dcae-proxy -p 8080:80 -v "$(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf" -d nginx
675 echo "Healthcheck API available at http://${MYFLOATIP}:8080/healthcheck"
676 echo "                          or http://${MYLOCALIP}:8080/healthcheck"