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