151e60e745ce0d0834b6faae7984596914de41b2
[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 common/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 > ~/joid_config/admin-openrcinternal
111         configOpenrc admin $adminPasswd admin $urlapi > ~/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_AUTH_URL=$4
135 export OS_USER_DOMAIN_NAME=admin_domain
136 export OS_PROJECT_DOMAIN_NAME=admin_domain
137 export OS_USERNAME=$1
138 export OS_TENANT_NAME=$3
139 export OS_PROJECT_NAME=$3
140 export OS_PASSWORD=$2
141 export OS_VOLUME_API_VERSION=2
142 export OS_IDENTITY_API_VERSION=3
143 export OS_IMAGE_API_VERSION=2
144 EOF
145 }
146
147 if ([ $API_FQDN ] && [ $API_FQDN != "null" ] && [ $API_FQDN != "None" ]); then
148     echo_info "OS domain name was specified - injecting API FQDN to nodes"
149
150     # Push api fqdn local ip to all /etc/hosts
151     if [[ "$jujuver" < "2" ]]; then
152         API_FQDN=$(juju get keystone | python -c "import yaml; import sys;\
153             print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
154     else
155         API_FQDN=$(juju config keystone | python -c "import yaml; import sys;\
156             print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
157     fi
158
159
160     KEYSTONEIP=$(keystoneIp)
161     juju run --all "if grep $API_FQDN /etc/hosts > /dev/null; then \
162                         echo 'API FQDN already present'; \
163                     else \
164                         sudo sh -c 'echo $KEYSTONEIP $API_FQDN >> /etc/hosts'; \
165                         echo 'API FQDN injected'; \
166                     fi"
167
168     # remove this enhancement for heat that does not manage endpoints
169     juju run --application=heat "cp /etc/hosts /tmp/hosts ; \
170                              grep -v $API_FQDN /tmp/hosts > /etc/hosts"
171
172     #change in jumphost as well as below commands will run on jumphost
173     if grep $API_FQDN /etc/hosts; then
174         echo 'API FQDN already present'
175     else
176         sudo sh -c "echo $KEYSTONEIP $API_FQDN >> /etc/hosts"
177         echo 'API FQDN injected'
178     fi
179 fi
180
181 # Create an load openrc
182 create_openrc
183
184 . ~/joid_config/admin-openrc
185
186 if ([ "$opnfvlab" == "orangepod1" ] \
187     || [ "$opnfvlab" == "intelpod6" ]) \
188     && [ "$opnfvsdn" == "nosdn" ] \
189     && [ "$API_FQDN" != "None" ]; then # only for first test phase
190     if [ -e ./labconfig.yaml ]; then
191         echo_info "Deploying public API proxy"
192
193         PUB_API_MASK=$(grep floating-ip-range ./labconfig.yaml |cut -d/ -f2)
194         PUB_API_NET=$(grep floating-ip-range ./labconfig.yaml |cut -d, -f4)
195         PUB_API_IP=$(grep public-api-ip ./labconfig.yaml |cut -d: -f2)
196         if grep "- type: public" ./labconfig.yaml; then
197             # The public network exists on MAAS, so we push the dns record to it
198
199             # Recover maas ips and login to it
200             maas_ip=$(grep " ip_address" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //')
201             maas_pubip=$(grep floating-ip-range ./labconfig.yaml |cut -d, -f4 |perl -pe 's!^(.*)\.\d+/\d+$!$1.5!')
202             apikey=$(grep maas-oauth: environments.yaml | cut -d "'" -f 2)
203             maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
204
205             # Configure maas to add the record
206             CLUSTER_UUID=$(maas ubuntu node-groups list | grep uuid | cut -d\" -f4)
207             PUBLIC_MAAS_IF=$(maas ubuntu node-group-interfaces list $cluster_uuid |\
208                              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'])")
209             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}
210             maas maas ipaddresses reserve network=${PUB_API_NET} requested_address=${PUB_API_IP} hostname=${API_FQDN}
211             dig ${PUB_API_IP} @${maas_ip} # just for log
212         fi
213         juju run --unit nodes/0 "sudo ip a a ${PUB_API_IP}/${PUB_API_MASK} dev br-ex" || true
214         juju run --unit nodes/0 "sudo ip l set dev br-ex up" || true
215         python genPublicAPIProxyBundle.py -l labconfig.yaml >> bundles.yaml
216         juju-deployer -vW -d -t 7200 -r 5 -c bundles.yaml $opnfvdistro-"$opnfvos" || true
217
218         echo_info "Public API proxy deployed!"
219     fi
220 fi
221
222
223 ##
224 ## removing the swift API endpoint which is created by radosgw.
225 ## one option is not to used radosgw and other one is remove endpoint.
226 ##
227
228 #echo "Removing swift endpoint and service"
229 #swift_service_id=$(openstack service list | grep swift | cut -d ' ' -f 2)
230 #swift_endpoint_id=$(openstack endpoint list | grep swift | cut -d ' ' -f 2)
231 #openstack endpoint delete $swift_endpoint_id
232 #openstack service delete $swift_service_id
233
234 ##
235 ## Create external subnet Network
236 ##
237
238 echo_info "Creating external network with neutron"
239
240 if [ "onos" == "$opnfvsdn" ]; then
241     launch_eth
242     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
243                                                    --router:external=True
244 else
245     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
246                                                    --router:external=True \
247                                                    --provider:network_type flat \
248                                                    --provider:physical_network physnet1
249 fi
250
251 neutron subnet-show ext-subnet > /dev/null 2>&1 || neutron subnet-create ext-net \
252    --name ext-subnet --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
253    --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
254
255 #congress team is not updating and supporting charm anymore so defer it.
256
257 # Create Congress datasources
258 #sudo apt-get install -y python-congressclient
259
260 #openstack congress datasource create nova "nova" \
261 #  --config username=$OS_USERNAME \
262 #  --config tenant_name=$OS_TENANT_NAME \
263 #  --config password=$OS_PASSWORD \
264 #  --config auth_url=http://$keystoneIp:5000/v2.0
265 #openstack congress datasource create neutronv2 "neutronv2" \
266 #  --config username=$OS_USERNAME \
267 #  --config tenant_name=$OS_TENANT_NAME \
268 #  --config password=$OS_PASSWORD \
269 #  --config auth_url=http://$keystoneIp:5000/v2.0
270 #openstack congress datasource create ceilometer "ceilometer" \
271 #  --config username=$OS_USERNAME \
272 #  --config tenant_name=$OS_TENANT_NAME \
273 #  --config password=$OS_PASSWORD \
274 #  --config auth_url=http://$keystoneIp:5000/v2.0
275 #openstack congress datasource create cinder "cinder" \
276 #  --config username=$OS_USERNAME \
277 #  --config tenant_name=$OS_TENANT_NAME \
278 #  --config password=$OS_PASSWORD \
279 #  --config auth_url=http://$keystoneIp:5000/v2.0
280 #openstack congress datasource create glancev2 "glancev2" \
281 #  --config username=$OS_USERNAME \
282 #  --config tenant_name=$OS_TENANT_NAME \
283 #  --config password=$OS_PASSWORD \
284 #  --config auth_url=http://$keystoneIp:5000/v2.0
285 #openstack congress datasource create keystone "keystone" \
286 #  --config username=$OS_USERNAME \
287 #  --config tenant_name=$OS_TENANT_NAME \
288 #  --config password=$OS_PASSWORD \
289 #  --config auth_url=http://$keystoneIp:5000/v2.0