Merge "External network for ocl"
[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 fi
36
37 # launch eth on computer nodes and remove default gw route
38 launch_eth() {
39     computer_list=$(juju status --format short | grep -Eo 'nova-compute/[0-9]')
40     for node in $computer_list; do
41         echo "node name is ${node}"
42         juju ssh $node "sudo ifconfig $EXTNET_PORT up"
43         #juju ssh $node "sudo route del default gw $ADMNET_GW"
44     done
45 }
46
47 # Update gateway mac to onos for l3 function
48 update_gw_mac() {
49     ## get gateway mac
50     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})'")
51     ## set external gateway mac in onos
52     if [[ "$jujuver" < "2" ]]; then
53         juju set onos-controller gateway-mac=$EXTNET_GW_MAC
54     else
55         juju config onos-controller gateway-mac=$EXTNET_GW_MAC
56     fi
57 }
58
59 unitAddress() {
60     if [[ "$jujuver" < "2" ]]; then
61         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
62     else
63         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
64     fi
65 }
66
67 unitMachine() {
68     if [[ "$jujuver" < "2" ]]; then
69         juju status --format yaml | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"machine\"]" 2> /dev/null
70     else
71         juju status --format yaml | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"applications\"][\"$1\"][\"units\"][\"$1/$2\"][\"machine\"]" 2> /dev/null
72     fi
73 }
74
75 keystoneIp() {
76     if [ $(juju status keystone --format=short | grep " keystone"|wc -l) == 1 ];then
77         unitAddress keystone 0
78     else
79         if [[ "$jujuver" < "2" ]]; then
80             juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']" | cut -d " " -f 1
81         else
82             juju config keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']" | cut -d " " -f 1
83         fi
84     fi
85 }
86
87 # create external network and subnet in openstack
88 create_openrc() {
89     echo_info "Creating the openrc (OpenStack client environment scripts)"
90
91     mkdir -m 0700 -p cloud
92     keystoneIp=$(keystoneIp)
93     if [[ "$jujuver" < "2" ]]; then
94         adminPasswd=$(juju get keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
95     else
96         adminPasswd=$(juju config keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
97     fi
98
99     v3api=`juju config keystone  preferred-api-version`
100
101     if [[ "$v3api" == "3" ]]; then
102         configOpenrc admin $adminPasswd admin http://$keystoneIp:5000/v3 RegionOne publicURL > ~/joid_config/admin-openrc
103         chmod 0600 ~/joid_config/admin-openrc
104         source ~/joid_config/admin-openrc
105         projectid=`openstack project show admin -c id -f value`
106         projectdomainid=`openstack domain show admin_domain -c id -f value`
107         userdomainid=`openstack user show admin -c domain_id -f value`
108         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`
109         configOpenrc admin $adminPasswd admin $urlapi RegionOne > ~/joid_config/admin-openrc
110     else
111         configOpenrc2 admin $adminPasswd admin http://$keystoneIp:5000/v2.0 RegionOne > ~/joid_config/admin-openrc
112         chmod 0600 ~/joid_config/admin-openrc
113     fi
114 }
115
116 configOpenrc2() {
117 cat <<-EOF
118 export SERVICE_ENDPOINT=$4
119 unset SERVICE_TOKEN
120 unset SERVICE_ENDPOINT
121 export OS_USERNAME=$1
122 export OS_PASSWORD=$2
123 export OS_TENANT_NAME=$3
124 export OS_AUTH_URL=$4
125 export OS_REGION_NAME=$5
126 EOF
127 }
128
129 configOpenrc() {
130 cat <<-EOF
131 #export OS_NO_CACHE='true'
132 export OS_AUTH_URL=$4
133 export OS_USER_DOMAIN_NAME=admin_domain
134 export OS_PROJECT_DOMAIN_NAME=admin_domain
135 export OS_USERNAME=$1
136 export OS_TENANT_NAME=$3
137 export OS_PROJECT_NAME=$3
138 export OS_PASSWORD=$2
139 export OS_IDENTITY_API_VERSION=3
140 export OS_REGION_NAME=$5
141 #export OS_INSECURE=true
142 #export OS_CASSL=~/joid_config/ca.pem
143 EOF
144 }
145
146 # Create an load openrc
147 create_openrc
148
149 . ~/joid_config/admin-openrc
150
151 ##
152 ## removing the swift API endpoint which is created by radosgw.
153 ## one option is not to used radosgw and other one is remove endpoint.
154 ##
155
156 #echo "Removing swift endpoint and service"
157 #swift_service_id=$(openstack service list | grep swift | cut -d ' ' -f 2)
158 #swift_endpoint_id=$(openstack endpoint list | grep swift | cut -d ' ' -f 2)
159 #openstack endpoint delete $swift_endpoint_id
160 #openstack service delete $swift_service_id
161
162 ##
163 ## Create external subnet Network
164 ##
165
166 echo_info "Creating external network with neutron"
167
168 if [ "onos" == "$opnfvsdn" ]; then
169     launch_eth
170     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
171                                                    --router:external=True
172 if [ "ocl" == "$opnfvsdn" ]; then
173     if [ ! neutron net-show ext-net > /dev/null 2>&1 ]
174     then 
175       neutron net-create ext-net 
176       LAST_UNIT=`juju status | grep nova-compute/ | tail -1 | cut -c14`
177       for ii in {0..$LAST_UNIT}
178       do
179         ssh $II sudo docker exec contrail-controller \
180           python /opt/contrail/utils/provision_vgw_interface.py\
181            --oper create --interface vgw1 --subnets $SUBNET --routes 0.0.0.0/24 --vrf default-domain:admin:$FIP-VN:$FIP-VN  
182       done
183     fi
184 else
185     neutron net-show ext-net > /dev/null 2>&1 || neutron net-create ext-net \
186                                                    --router:external=True \
187                                                    --provider:network_type flat \
188                                                    --provider:physical_network physnet1
189 fi
190
191 neutron subnet-show ext-subnet > /dev/null 2>&1 || neutron subnet-create ext-net \
192    --name ext-subnet --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
193    --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
194
195 domain_id=$(openstack domain show admin_domain -f value -c id)
196 project_id=$(openstack project show admin --domain $domain_id -f value -c id)
197
198 openstack role add --project $project_id --user admin admin || true
199
200 #congress team is not updating and supporting charm anymore so defer it.
201
202 # Create Congress datasources
203 #sudo apt-get install -y python-congressclient
204
205 #openstack congress datasource create nova "nova" \
206 #  --config username=$OS_USERNAME \
207 #  --config tenant_name=$OS_TENANT_NAME \
208 #  --config password=$OS_PASSWORD \
209 #  --config auth_url=http://$keystoneIp:5000/v2.0
210 #openstack congress datasource create neutronv2 "neutronv2" \
211 #  --config username=$OS_USERNAME \
212 #  --config tenant_name=$OS_TENANT_NAME \
213 #  --config password=$OS_PASSWORD \
214 #  --config auth_url=http://$keystoneIp:5000/v2.0
215 #openstack congress datasource create ceilometer "ceilometer" \
216 #  --config username=$OS_USERNAME \
217 #  --config tenant_name=$OS_TENANT_NAME \
218 #  --config password=$OS_PASSWORD \
219 #  --config auth_url=http://$keystoneIp:5000/v2.0
220 #openstack congress datasource create cinder "cinder" \
221 #  --config username=$OS_USERNAME \
222 #  --config tenant_name=$OS_TENANT_NAME \
223 #  --config password=$OS_PASSWORD \
224 #  --config auth_url=http://$keystoneIp:5000/v2.0
225 #openstack congress datasource create glancev2 "glancev2" \
226 #  --config username=$OS_USERNAME \
227 #  --config tenant_name=$OS_TENANT_NAME \
228 #  --config password=$OS_PASSWORD \
229 #  --config auth_url=http://$keystoneIp:5000/v2.0
230 #openstack congress datasource create keystone "keystone" \
231 #  --config username=$OS_USERNAME \
232 #  --config tenant_name=$OS_TENANT_NAME \
233 #  --config password=$OS_PASSWORD \
234 #  --config auth_url=http://$keystoneIp:5000/v2.0