by default moving to public End point.
[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     if [ $(juju status keystone --format=short | grep " keystone"|wc -l) == 1 ];then
75         unitAddress keystone 0
76     else
77         if [[ "$jujuver" < "2" ]]; then
78             juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']" | cut -d " " -f 1
79         else
80             juju config keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']" | cut -d " " -f 1
81         fi
82     fi
83 }
84
85 # create external network and subnet in openstack
86 create_openrc() {
87     mkdir -m 0700 -p cloud
88     keystoneIp=$(keystoneIp)
89     if [[ "$jujuver" < "2" ]]; then
90         adminPasswd=$(juju get keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
91     else
92         adminPasswd=$(juju config keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
93     fi
94
95     v3api=`juju config keystone  preferred-api-version`
96
97     if [[ "$v3api" == "3" ]]; then
98         configOpenrc admin $adminPasswd admin http://$keystoneIp:5000/v3 RegionOne publicURL > ~/joid_config/admin-openrc
99         chmod 0600 ~/joid_config/admin-openrc
100         source ~/joid_config/admin-openrc
101         projectid=`openstack project show admin -c id -f value`
102         configOpenrc admin $adminPasswd admin http://$keystoneIp:5000/v3 RegionOne publicURL $projectid > ~/joid_config/admin-openrcpublic
103     else
104         configOpenrc2 admin $adminPasswd admin http://$keystoneIp:5000/v2.0 RegionOne > ~/joid_config/admin-openrc
105         chmod 0600 ~/joid_config/admin-openrc
106     fi
107 }
108
109 configOpenrc2() {
110 cat <<-EOF
111 export SERVICE_ENDPOINT=$4
112 unset SERVICE_TOKEN
113 unset SERVICE_ENDPOINT
114 export OS_USERNAME=$1
115 export OS_PASSWORD=$2
116 export OS_TENANT_NAME=$3
117 export OS_AUTH_URL=$4
118 export OS_REGION_NAME=$5
119 EOF
120 }
121
122 configOpenrc() {
123 cat <<-EOF
124 export OS_NO_CACHE='true'
125 export OS_TENANT_NAME=$3
126 #export OS_TENANT_ID=$7
127 export OS_PROJECT_NAME=$3
128 export OS_USERNAME=$1
129 export OS_PASSWORD=$2
130 export OS_IDENTITY_API_VERSION=3
131 export OS_DEFAULT_DOMAIN=admin_domain
132 export OS_USER_DOMAIN_NAME=admin_domain
133 export OS_PROJECT_DOMAIN_NAME=admin_domain
134 export OS_AUTH_STRATEGY='keystone'
135 export OS_REGION_NAME=$5
136 export CINDER_ENDPOINT_TYPE=$6
137 export GLANCE_ENDPOINT_TYPE=$6
138 export KEYSTONE_ENDPOINT_TYPE=$6
139 export NOVA_ENDPOINT_TYPE=$6
140 export NEUTRON_ENDPOINT_TYPE=$6
141 export OS_ENDPOINT_TYPE=$6
142 export OS_INTERFACE=public
143 export OS_AUTH_URL=$4
144 EOF
145 }
146
147 if [ "$API_FQDN" != "None" ]; then
148     # Push api fqdn local ip to all /etc/hosts
149     if [[ "$jujuver" < "2" ]]; then
150         API_FQDN=$(juju get keystone | python -c "import yaml; import sys;\
151             print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
152     else
153         API_FQDN=$(juju config keystone | python -c "import yaml; import sys;\
154             print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
155     fi
156
157
158     KEYSTONEIP=$(keystoneIp)
159     juju run --all "if grep $API_FQDN /etc/hosts > /dev/null; then \
160                         echo 'API FQDN already present'; \
161                     else \
162                         sudo sh -c 'echo $KEYSTONEIP $API_FQDN >> /etc/hosts'; \
163                         echo 'API FQDN injected'; \
164                     fi"
165
166     # remove this enhancement for heat that does not manage endpoints
167     juju run --service=heat "cp /etc/hosts /tmp/hosts ; \
168                              grep -v $API_FQDN /tmp/hosts > /etc/hosts"
169
170     #change in jumphost as well as below commands will run on jumphost
171     if grep $API_FQDN /etc/hosts; then
172         echo 'API FQDN already present'
173     else
174         sudo sh -c "echo $KEYSTONEIP $API_FQDN >> /etc/hosts"
175         echo 'API FQDN injected'
176     fi
177 fi
178
179 # Create an load openrc
180 create_openrc
181
182 . ~/joid_config/admin-openrc
183
184 echo "...... deploy public api proxy ......"
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         PUB_API_MASK=$(grep floating-ip-range ./labconfig.yaml |cut -d/ -f2)
192         PUB_API_NET=$(grep floating-ip-range ./labconfig.yaml |cut -d, -f4)
193         PUB_API_IP=$(grep public-api-ip ./labconfig.yaml |cut -d: -f2)
194         if grep "- type: public" ./labconfig.yaml; then
195             # The public network exists on MAAS, so we push the dns record to it
196
197             # Recover maas ips and login to it
198             maas_ip=$(grep " ip_address" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //')
199             maas_pubip=$(grep floating-ip-range ./labconfig.yaml |cut -d, -f4 |perl -pe 's!^(.*)\.\d+/\d+$!$1.5!')
200             apikey=$(grep maas-oauth: environments.yaml | cut -d "'" -f 2)
201             maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
202
203             # Configure maas to add the record
204             CLUSTER_UUID=$(maas ubuntu node-groups list | grep uuid | cut -d\" -f4)
205             PUBLIC_MAAS_IF=$(maas ubuntu node-group-interfaces list $cluster_uuid |\
206                              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'])")
207             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}
208             maas maas ipaddresses reserve network=${PUB_API_NET} requested_address=${PUB_API_IP} hostname=${API_FQDN}
209             dig ${PUB_API_IP} @${maas_ip} # just for log
210         fi
211         juju run --unit nodes/0 "sudo ip a a ${PUB_API_IP}/${PUB_API_MASK} dev br-ex" || true
212         juju run --unit nodes/0 "sudo ip l set dev br-ex up" || true
213         python genPublicAPIProxyBundle.py -l labconfig.yaml >> bundles.yaml
214         juju-deployer -vW -d -t 7200 -r 5 -c bundles.yaml $opnfvdistro-"$opnfvos" || true
215     fi
216 fi
217
218 echo "...... deploy end public api proxy ......"
219
220 ##
221 ## removing the swift API endpoint which is created by radosgw.
222 ## one option is not to used radosgw and other one is remove endpoint.
223 ##
224
225 #echo "Removing swift endpoint and service"
226 #swift_service_id=$(openstack service list | grep swift | cut -d ' ' -f 2)
227 #swift_endpoint_id=$(openstack endpoint list | grep swift | cut -d ' ' -f 2)
228 #openstack endpoint delete $swift_endpoint_id
229 #openstack service delete $swift_service_id
230
231 ##
232 ## Create external subnet Network
233 ##
234
235 if [ "onos" == "$opnfvsdn" ]; then
236     launch_eth
237     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
238                                                    --router:external=True
239 else
240     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
241                                                    --router:external=True \
242                                                    --provider:network_type flat \
243                                                    --provider:physical_network physnet1
244 fi
245
246 neutron subnet-show ext-subnet > /dev/null 2>&1 || neutron subnet-create ext-net \
247    --name ext-subnet --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
248    --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
249
250 #congress team is not updating and supporting charm anymore so defer it.
251
252 # Create Congress datasources
253 #sudo apt-get install -y python-congressclient
254
255 #openstack congress datasource create nova "nova" \
256 #  --config username=$OS_USERNAME \
257 #  --config tenant_name=$OS_TENANT_NAME \
258 #  --config password=$OS_PASSWORD \
259 #  --config auth_url=http://$keystoneIp:5000/v2.0
260 #openstack congress datasource create neutronv2 "neutronv2" \
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 ceilometer "ceilometer" \
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 cinder "cinder" \
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 glancev2 "glancev2" \
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 keystone "keystone" \
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