Improve and highlight logging to console (2)
[joid.git] / ci / openstack.sh
1 #!/bin/bash -ex
2 ##############################################################################
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7 ##############################################################################
8
9 set -ex
10
11 source tools.sh
12
13 #./openstack.sh "$opnfvsdn" "$opnfvlab" "$opnfvdistro" "$openstack" || true
14
15 opnfvsdn=$1
16 opnfvlab=$2
17 opnfvdistro=$3
18 opnfvos=$4
19
20 jujuver=`juju --version`
21
22 if [ -f ./deployconfig.yaml ];then
23     EXTERNAL_NETWORK=`grep floating-ip-range deployconfig.yaml | cut -d ' ' -f 4 `
24
25     # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
26
27     EXTNET=(${EXTERNAL_NETWORK//,/ })
28
29     EXTNET_FIP=${EXTNET[0]}
30     EXTNET_LIP=${EXTNET[1]}
31     EXTNET_GW=${EXTNET[2]}
32     EXTNET_NET=${EXTNET[3]}
33     EXTNET_PORT=`grep "ext-port" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
34     ADMNET_GW=`grep "admNetgway" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
35     API_FQDN=`grep "os-domain-name" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
36 fi
37
38 # launch eth on computer nodes and remove default gw route
39 launch_eth() {
40     computer_list=$(juju status --format short | grep -Eo 'nova-compute/[0-9]')
41     for node in $computer_list; do
42         echo "node name is ${node}"
43         juju ssh $node "sudo ifconfig $EXTNET_PORT up"
44         #juju ssh $node "sudo route del default gw $ADMNET_GW"
45     done
46 }
47
48 # Update gateway mac to onos for l3 function
49 update_gw_mac() {
50     ## get gateway mac
51     EXTNET_GW_MAC=$(juju ssh nova-compute/0 "arp -a ${EXTNET_GW} | grep -Eo '([0-9a-fA-F]{2})(([/\s:-][0-9a-fA-F]{2}){5})'")
52     ## set external gateway mac in onos
53     if [[ "$jujuver" < "2" ]]; then
54         juju set onos-controller gateway-mac=$EXTNET_GW_MAC
55     else
56         juju config onos-controller gateway-mac=$EXTNET_GW_MAC
57     fi
58 }
59
60 unitAddress() {
61     if [[ "$jujuver" < "2" ]]; then
62         juju status --format yaml | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"public-address\"]" 2> /dev/null
63     else
64         juju status --format yaml | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"applications\"][\"$1\"][\"units\"][\"$1/$2\"][\"public-address\"]" 2> /dev/null
65     fi
66 }
67
68 unitMachine() {
69     if [[ "$jujuver" < "2" ]]; then
70         juju status --format yaml | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"machine\"]" 2> /dev/null
71     else
72         juju status --format yaml | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"applications\"][\"$1\"][\"units\"][\"$1/$2\"][\"machine\"]" 2> /dev/null
73     fi
74 }
75
76 keystoneIp() {
77     if [ $(juju status keystone --format=short | grep " keystone"|wc -l) == 1 ];then
78         unitAddress keystone 0
79     else
80         if [[ "$jujuver" < "2" ]]; then
81             juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']" | cut -d " " -f 1
82         else
83             juju config keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']" | cut -d " " -f 1
84         fi
85     fi
86 }
87
88 # create external network and subnet in openstack
89 create_openrc() {
90     echo_info "Creating the openrc (OpenStack client environment scripts)"
91
92     mkdir -m 0700 -p cloud
93     keystoneIp=$(keystoneIp)
94     if [[ "$jujuver" < "2" ]]; then
95         adminPasswd=$(juju get keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
96     else
97         adminPasswd=$(juju config keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
98     fi
99
100     v3api=`juju config keystone  preferred-api-version`
101
102     if [[ "$v3api" == "3" ]]; then
103         configOpenrc admin $adminPasswd admin http://$keystoneIp:5000/v3 RegionOne publicURL > ~/joid_config/admin-openrc
104         chmod 0600 ~/joid_config/admin-openrc
105         source ~/joid_config/admin-openrc
106         projectid=`openstack project show admin -c id -f value`
107         projectdomainid=`openstack domain show admin_domain -c id -f value`
108         userdomainid=`openstack user show admin -c domain_id -f value`
109         urlapi=`openstack catalog show keystone --format yaml | python -c "import yaml; import sys; print yaml.load(sys.stdin)['endpoints']" | grep public | cut -d " " -f 4`
110         configOpenrc admin $adminPasswd admin $urlapi RegionOne publicURL $projectid $projectdomainid $userdomainid > ~/joid_config/admin-openrcinternal
111         configOpenrc admin $adminPasswd admin $urlapi RegionOne publicURL $projectid $projectdomainid $userdomainid > ~/joid_config/admin-openrc
112     else
113         configOpenrc2 admin $adminPasswd admin http://$keystoneIp:5000/v2.0 RegionOne > ~/joid_config/admin-openrc
114         chmod 0600 ~/joid_config/admin-openrc
115     fi
116 }
117
118 configOpenrc2() {
119 cat <<-EOF
120 export SERVICE_ENDPOINT=$4
121 unset SERVICE_TOKEN
122 unset SERVICE_ENDPOINT
123 export OS_USERNAME=$1
124 export OS_PASSWORD=$2
125 export OS_TENANT_NAME=$3
126 export OS_AUTH_URL=$4
127 export OS_REGION_NAME=$5
128 EOF
129 }
130
131 configOpenrc() {
132 cat <<-EOF
133 export OS_NO_CACHE='true'
134 export OS_TENANT_NAME=$3
135 #export OS_TENANT_ID=$7
136 export OS_PROJECT_NAME=$3
137 export OS_USERNAME=$1
138 export OS_PASSWORD=$2
139 export OS_IDENTITY_API_VERSION=3
140 export OS_DEFAULT_DOMAIN=admin_domain
141 export OS_USER_DOMAIN_NAME=admin_domain
142 export OS_USER_DOMAIN_ID=$9
143 export OS_PROJECT_DOMAIN_NAME=admin_domain
144 export OS_PROJECT_DOMAIN_ID=$8
145 export OS_AUTH_STRATEGY='keystone'
146 export OS_REGION_NAME=$5
147 export CINDER_ENDPOINT_TYPE=$6
148 export GLANCE_ENDPOINT_TYPE=$6
149 export KEYSTONE_ENDPOINT_TYPE=$6
150 export NOVA_ENDPOINT_TYPE=$6
151 export NEUTRON_ENDPOINT_TYPE=$6
152 export OS_ENDPOINT_TYPE=$6
153 export OS_INTERFACE=public
154 export OS_AUTH_URL=$4
155 EOF
156 }
157
158 if ([ $API_FQDN ] && [ $API_FQDN != "null" ] && [ $API_FQDN != "None" ]); then
159     echo_info "OS domain name was specified - injecting API FQDN to nodes"
160
161     # Push api fqdn local ip to all /etc/hosts
162     if [[ "$jujuver" < "2" ]]; then
163         API_FQDN=$(juju get keystone | python -c "import yaml; import sys;\
164             print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
165     else
166         API_FQDN=$(juju config keystone | python -c "import yaml; import sys;\
167             print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
168     fi
169
170
171     KEYSTONEIP=$(keystoneIp)
172     juju run --all "if grep $API_FQDN /etc/hosts > /dev/null; then \
173                         echo 'API FQDN already present'; \
174                     else \
175                         sudo sh -c 'echo $KEYSTONEIP $API_FQDN >> /etc/hosts'; \
176                         echo 'API FQDN injected'; \
177                     fi"
178
179     # remove this enhancement for heat that does not manage endpoints
180     juju run --application=heat "cp /etc/hosts /tmp/hosts ; \
181                              grep -v $API_FQDN /tmp/hosts > /etc/hosts"
182
183     #change in jumphost as well as below commands will run on jumphost
184     if grep $API_FQDN /etc/hosts; then
185         echo 'API FQDN already present'
186     else
187         sudo sh -c "echo $KEYSTONEIP $API_FQDN >> /etc/hosts"
188         echo 'API FQDN injected'
189     fi
190 fi
191
192 # Create an load openrc
193 create_openrc
194
195 . ~/joid_config/admin-openrc
196
197 if ([ "$opnfvlab" == "orangepod1" ] \
198     || [ "$opnfvlab" == "intelpod6" ]) \
199     && [ "$opnfvsdn" == "nosdn" ] \
200     && [ "$API_FQDN" != "None" ]; then # only for first test phase
201     if [ -e ./labconfig.yaml ]; then
202         echo_info "Deploying public API proxy"
203
204         PUB_API_MASK=$(grep floating-ip-range ./labconfig.yaml |cut -d/ -f2)
205         PUB_API_NET=$(grep floating-ip-range ./labconfig.yaml |cut -d, -f4)
206         PUB_API_IP=$(grep public-api-ip ./labconfig.yaml |cut -d: -f2)
207         if grep "- type: public" ./labconfig.yaml; then
208             # The public network exists on MAAS, so we push the dns record to it
209
210             # Recover maas ips and login to it
211             maas_ip=$(grep " ip_address" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //')
212             maas_pubip=$(grep floating-ip-range ./labconfig.yaml |cut -d, -f4 |perl -pe 's!^(.*)\.\d+/\d+$!$1.5!')
213             apikey=$(grep maas-oauth: environments.yaml | cut -d "'" -f 2)
214             maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
215
216             # Configure maas to add the record
217             CLUSTER_UUID=$(maas ubuntu node-groups list | grep uuid | cut -d\" -f4)
218             PUBLIC_MAAS_IF=$(maas ubuntu node-group-interfaces list $cluster_uuid |\
219                              python -c "import yaml; import sys; cfg=yaml.load(sys.stdin); net_h={net['ip']:net['name'] for net in cfg}; print(net_h['$maas_pubip'])")
220             maas maas node-group-interface update ${CLUSTER_UUID} ${PUBLIC_MAAS_IF} static_ip_range_high=${PUB_API_IP} static_ip_range_low=${PUB_API_IP}
221             maas maas ipaddresses reserve network=${PUB_API_NET} requested_address=${PUB_API_IP} hostname=${API_FQDN}
222             dig ${PUB_API_IP} @${maas_ip} # just for log
223         fi
224         juju run --unit nodes/0 "sudo ip a a ${PUB_API_IP}/${PUB_API_MASK} dev br-ex" || true
225         juju run --unit nodes/0 "sudo ip l set dev br-ex up" || true
226         python genPublicAPIProxyBundle.py -l labconfig.yaml >> bundles.yaml
227         juju-deployer -vW -d -t 7200 -r 5 -c bundles.yaml $opnfvdistro-"$opnfvos" || true
228
229         echo_info "Public API proxy deployed!"
230     fi
231 fi
232
233
234 ##
235 ## removing the swift API endpoint which is created by radosgw.
236 ## one option is not to used radosgw and other one is remove endpoint.
237 ##
238
239 #echo "Removing swift endpoint and service"
240 #swift_service_id=$(openstack service list | grep swift | cut -d ' ' -f 2)
241 #swift_endpoint_id=$(openstack endpoint list | grep swift | cut -d ' ' -f 2)
242 #openstack endpoint delete $swift_endpoint_id
243 #openstack service delete $swift_service_id
244
245 ##
246 ## Create external subnet Network
247 ##
248
249 echo_info "Creating external network with neutron"
250
251 if [ "onos" == "$opnfvsdn" ]; then
252     launch_eth
253     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
254                                                    --router:external=True
255 else
256     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
257                                                    --router:external=True \
258                                                    --provider:network_type flat \
259                                                    --provider:physical_network physnet1
260 fi
261
262 neutron subnet-show ext-subnet > /dev/null 2>&1 || neutron subnet-create ext-net \
263    --name ext-subnet --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
264    --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
265
266 #congress team is not updating and supporting charm anymore so defer it.
267
268 # Create Congress datasources
269 #sudo apt-get install -y python-congressclient
270
271 #openstack congress datasource create nova "nova" \
272 #  --config username=$OS_USERNAME \
273 #  --config tenant_name=$OS_TENANT_NAME \
274 #  --config password=$OS_PASSWORD \
275 #  --config auth_url=http://$keystoneIp:5000/v2.0
276 #openstack congress datasource create neutronv2 "neutronv2" \
277 #  --config username=$OS_USERNAME \
278 #  --config tenant_name=$OS_TENANT_NAME \
279 #  --config password=$OS_PASSWORD \
280 #  --config auth_url=http://$keystoneIp:5000/v2.0
281 #openstack congress datasource create ceilometer "ceilometer" \
282 #  --config username=$OS_USERNAME \
283 #  --config tenant_name=$OS_TENANT_NAME \
284 #  --config password=$OS_PASSWORD \
285 #  --config auth_url=http://$keystoneIp:5000/v2.0
286 #openstack congress datasource create cinder "cinder" \
287 #  --config username=$OS_USERNAME \
288 #  --config tenant_name=$OS_TENANT_NAME \
289 #  --config password=$OS_PASSWORD \
290 #  --config auth_url=http://$keystoneIp:5000/v2.0
291 #openstack congress datasource create glancev2 "glancev2" \
292 #  --config username=$OS_USERNAME \
293 #  --config tenant_name=$OS_TENANT_NAME \
294 #  --config password=$OS_PASSWORD \
295 #  --config auth_url=http://$keystoneIp:5000/v2.0
296 #openstack congress datasource create keystone "keystone" \
297 #  --config username=$OS_USERNAME \
298 #  --config tenant_name=$OS_TENANT_NAME \
299 #  --config password=$OS_PASSWORD \
300 #  --config auth_url=http://$keystoneIp:5000/v2.0