modfied to cleanup copying of yaml files from .juju/
[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 opnfvlab=$2
11 opnfvsdn=$1
12
13 if [ -f ./deployconfig.yaml ];then
14     EXTERNAL_NETWORK=`grep floating-ip-range deployconfig.yaml | cut -d ' ' -f 4 `
15
16     # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
17
18     EXTNET=(${EXTERNAL_NETWORK//,/ })
19
20     EXTNET_FIP=${EXTNET[0]}
21     EXTNET_LIP=${EXTNET[1]}
22     EXTNET_GW=${EXTNET[2]}
23     EXTNET_NET=${EXTNET[3]}
24     EXTNET_PORT=`grep "ext-port" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
25     ADMNET_GW=`grep "admNetgway" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
26     API_FQDN=`grep "os-domain-name" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
27 fi
28
29 # launch eth on computer nodes and remove default gw route
30 launch_eth() {
31     computer_list=$(juju status --format short | grep -Eo 'nova-compute/[0-9]')
32     for node in $computer_list; do
33         echo "node name is ${node}"
34         juju ssh $node "sudo ifconfig $EXTNET_PORT up"
35         #juju ssh $node "sudo route del default gw $ADMNET_GW"
36     done
37 }
38
39 # Update gateway mac to onos for l3 function
40 update_gw_mac() {
41     ## get gateway mac
42     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})'")
43     ## set external gateway mac in onos
44     juju set onos-controller gateway-mac=$EXTNET_GW_MAC
45 }
46
47 unitAddress() {
48         juju status | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"public-address\"]" 2> /dev/null
49 }
50
51 unitMachine() {
52         juju status | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"machine\"]" 2> /dev/null
53 }
54
55 keystoneIp() {
56     KEYSTONE=$(juju status keystone |grep public-address|sed -- 's/.*\: //')
57     if [ $(echo $KEYSTONE|wc -w) == 1 ];then
58         echo $KEYSTONE
59     else
60         juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']"
61     fi
62 }
63
64 # create external network and subnet in openstack
65 create_openrc() {
66     mkdir -m 0700 -p cloud
67     keystoneIp=$(keystoneIp)
68     adminPasswd=$(juju get keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
69     configOpenrc admin $adminPasswd admin http://$keystoneIp:5000/v2.0 RegionOne > cloud/admin-openrc
70     chmod 0600 cloud/admin-openrc
71 }
72
73 configOpenrc() {
74 if [ "$API_FQDN" != "None" ]; then
75     cat <<-EOF
76         export OS_USERNAME=$1
77         export OS_PASSWORD=$2
78         export OS_TENANT_NAME=$3
79         export OS_AUTH_URL=$4
80         export OS_REGION_NAME=$5
81         export OS_ENDPOINT_TYPE='internalURL'
82         export CINDER_ENDPOINT_TYPE='internalURL'
83         export GLANCE_ENDPOINT_TYPE='internalURL'
84         export KEYSTONE_ENDPOINT_TYPE='internalURL'
85         export NEUTRON_ENDPOINT_TYPE='internalURL'
86         export NOVA_ENDPOINT_TYPE='internalURL'
87 EOF
88 else
89     cat <<-EOF
90         export OS_USERNAME=$1
91         export OS_PASSWORD=$2
92         export OS_TENANT_NAME=$3
93         export OS_AUTH_URL=$4
94         export OS_REGION_NAME=$5
95 EOF
96
97 fi
98 }
99
100 if [ "$API_FQDN" != "None" ]; then
101     # Push api fqdn local ip to all /etc/hosts
102     API_FQDN=$(juju get keystone | python -c "import yaml; import sys;\
103         print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
104
105     KEYSTONEIP=$(keystoneIp)
106     juju run --all "if grep $API_FQDN /etc/hosts > /dev/null; then \
107                         echo 'API FQDN already present'; \
108                     else \
109                         sudo sh -c 'echo $KEYSTONEIP $API_FQDN >> /etc/hosts'; \
110                         echo 'API FQDN injected'; \
111                     fi"
112
113     #change in jumphost as well as below commands will run on jumphost
114
115     if grep $API_FQDN /etc/hosts; then
116         echo 'API FQDN already present'
117     else
118         sudo sh -c "echo $KEYSTONEIP $API_FQDN >> /etc/hosts"
119         echo 'API FQDN injected'
120     fi
121 fi
122
123 # Create an load openrc
124 create_openrc
125
126 . ./cloud/admin-openrc
127
128 echo "...... deploy public api proxy ......"
129
130 if [ "$opnfvlab" == "orangepod1" ] && [ "$opnfvsdn" == "nosdn" ]; then # only for first test phase
131     if [ -e ./labconfig.yaml ]; then
132         PUB_API_NET=$(grep floating-ip-range ./labconfig.yaml |cut -d/ -f2)
133         PUB_API_IP=$(grep public-api-ip ./labconfig.yaml |cut -d: -f2)
134         juju run --unit nodes/0 "sudo ip a a ${PUB_API_IP}/${PUB_API_NET} dev br-ex" || true
135         juju run --unit nodes/0 "sudo ip l set dev br-ex up" || true
136         python genPublicAPIProxyBundle.py -l labconfig.yaml >> bundles.yaml
137         juju-deployer -vW -d -t 7200 -r 5 -c bundles.yaml $opnfvdistro-"$openstack" || true
138     fi
139 fi
140
141 echo "...... deploy end public api proxy ......"
142
143 ##
144 ## removing the swift API endpoint which is created by radosgw.
145 ## one option is not to used radosgw and other one is remove endpoint.
146 ##
147
148 #echo "Removing swift endpoint and service"
149 #swift_service_id=$(openstack service list | grep swift | cut -d ' ' -f 2)
150 #swift_endpoint_id=$(openstack endpoint list | grep swift | cut -d ' ' -f 2)
151 #openstack endpoint delete $swift_endpoint_id
152 #openstack service delete $swift_service_id
153
154 ##
155 ## Create external subnet Network
156 ##
157
158 if [ "onos" == "$opnfvsdn" ]; then
159     launch_eth
160     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net --router:external=True
161     neutron subnet-show ext-subnet > /dev/null 2>&1 || neutron subnet-create ext-net \
162        --name ext-subnet --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
163        --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
164     #neutron subnet-create ext-net --name ext-subnet $EXTNET_NET
165     #update_gw_mac
166 elif [ "nosdn" == "$opnfvsdn" ]; then
167     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
168                                              --router:external=True \
169                                              --provider:network_type flat \
170                                               --provider:physical_network external
171
172     neutron subnet-show ext-subnet > /dev/null 2>&1 || neutron subnet-create ext-net \
173        --name ext-subnet --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
174        --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
175 else
176     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net --router:external=True
177     neutron subnet-show ext-subnet > /dev/null 2>&1 || neutron subnet-create ext-net \
178        --name ext-subnet --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
179        --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
180 fi
181
182
183 # Create Congress datasources
184 sudo apt-get install -y python-congressclient
185
186 openstack congress datasource create nova "nova" \
187   --config username=$OS_USERNAME \
188   --config tenant_name=$OS_TENANT_NAME \
189   --config password=$OS_PASSWORD \
190   --config auth_url=http://$keystoneIp:5000/v2.0
191 openstack congress datasource create neutronv2 "neutronv2" \
192   --config username=$OS_USERNAME \
193   --config tenant_name=$OS_TENANT_NAME \
194   --config password=$OS_PASSWORD \
195   --config auth_url=http://$keystoneIp:5000/v2.0
196 openstack congress datasource create ceilometer "ceilometer" \
197   --config username=$OS_USERNAME \
198   --config tenant_name=$OS_TENANT_NAME \
199   --config password=$OS_PASSWORD \
200   --config auth_url=http://$keystoneIp:5000/v2.0
201 openstack congress datasource create cinder "cinder" \
202   --config username=$OS_USERNAME \
203   --config tenant_name=$OS_TENANT_NAME \
204   --config password=$OS_PASSWORD \
205   --config auth_url=http://$keystoneIp:5000/v2.0
206 openstack congress datasource create glancev2 "glancev2" \
207   --config username=$OS_USERNAME \
208   --config tenant_name=$OS_TENANT_NAME \
209   --config password=$OS_PASSWORD \
210   --config auth_url=http://$keystoneIp:5000/v2.0
211 openstack congress datasource create keystone "keystone" \
212   --config username=$OS_USERNAME \
213   --config tenant_name=$OS_TENANT_NAME \
214   --config password=$OS_PASSWORD \
215   --config auth_url=http://$keystoneIp:5000/v2.0
216