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