[JOID] workaround for congress client with publicapi enabled
[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 if [ -f ./deployconfig.yaml ];then
11     EXTERNAL_NETWORK=`grep floating-ip-range deployconfig.yaml | cut -d ' ' -f 4 `
12
13     # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
14
15     EXTNET=(${EXTERNAL_NETWORK//,/ })
16
17     EXTNET_FIP=${EXTNET[0]}
18     EXTNET_LIP=${EXTNET[1]}
19     EXTNET_GW=${EXTNET[2]}
20     EXTNET_NET=${EXTNET[3]}
21     EXTNET_PORT=`grep "ext-port" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
22     ADMNET_GW=`grep "admNetgway" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
23     API_FQDN=`grep "os-domain-name" deployconfig.yaml | cut -d ' ' -f 4 | sed -e 's/ //' | tr ',' ' '`
24 fi
25
26 # launch eth on computer nodes and remove default gw route
27 launch_eth() {
28     computer_list=$(juju status --format short | grep -Eo 'nova-compute/[0-9]')
29     for node in $computer_list; do
30         echo "node name is ${node}"
31         juju ssh $node "sudo ifconfig $EXTNET_PORT up"
32         #juju ssh $node "sudo route del default gw $ADMNET_GW"
33     done
34 }
35
36 # Update gateway mac to onos for l3 function
37 update_gw_mac() {
38     ## get gateway mac
39     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})'")
40     ## set external gateway mac in onos
41     juju set onos-controller gateway-mac=$EXTNET_GW_MAC
42 }
43
44 unitAddress() {
45         juju status | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"public-address\"]" 2> /dev/null
46 }
47
48 unitMachine() {
49         juju status | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"machine\"]" 2> /dev/null
50 }
51
52 keystoneIp() {
53     KEYSTONE=$(juju status keystone |grep public-address|sed -- 's/.*\: //')
54     if [ $(echo $KEYSTONE|wc -w) == 1 ];then
55         echo $KEYSTONE
56     else
57         juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']"
58     fi
59 }
60
61 # create external network and subnet in openstack
62 create_openrc() {
63     mkdir -m 0700 -p cloud
64     keystoneIp=$(keystoneIp)
65     adminPasswd=$(juju get keystone | grep admin-password -A 5 | grep value | awk '{print $2}' 2> /dev/null)
66     configOpenrc admin $adminPasswd admin http://$keystoneIp:5000/v2.0 Canonical > cloud/admin-openrc
67     chmod 0600 cloud/admin-openrc
68 }
69
70 configOpenrc() {
71 if [ "$API_FQDN" != "''" ]; then
72     cat <<-EOF
73         export OS_USERNAME=$1
74         export OS_PASSWORD=$2
75         export OS_TENANT_NAME=$3
76         export OS_AUTH_URL=$4
77         export OS_REGION_NAME=$5
78         export OS_ENDPOINT_TYPE='internalURL'
79         export CINDER_ENDPOINT_TYPE='internalURL'
80         export GLANCE_ENDPOINT_TYPE='internalURL'
81         export KEYSTONE_ENDPOINT_TYPE='internalURL'
82         export NEUTRON_ENDPOINT_TYPE='internalURL'
83         export NOVA_ENDPOINT_TYPE='internalURL'
84 EOF
85 else
86     cat <<-EOF
87         export OS_USERNAME=$1
88         export OS_PASSWORD=$2
89         export OS_TENANT_NAME=$3
90         export OS_AUTH_URL=$4
91         export OS_REGION_NAME=$5
92 EOF
93
94 fi
95 }
96
97 if [ "$API_FQDN" != "''" ]; then
98     # Push api fqdn local ip to all /etc/hosts
99     API_FQDN=$(juju get keystone | python -c "import yaml; import sys;\
100         print yaml.load(sys.stdin)['settings']['os-public-hostname']['value']")
101
102     KEYSTONEIP=$(keystoneIp)
103     juju run --all "if grep $API_FQDN /etc/hosts > /dev/null; then \
104                         echo 'API FQDN already present'; \
105                     else \
106                         sudo sh -c 'echo $KEYSTONEIP $API_FQDN >> /etc/hosts'; \
107                         echo 'API FQDN injected'; \
108                     fi"
109
110     #change in jumphost as well as below commands will run on jumphost
111
112     if grep $API_FQDN /etc/hosts; then
113         echo 'API FQDN already present'
114     else
115         sudo sh -c "echo $KEYSTONEIP $API_FQDN >> /etc/hosts"
116         echo 'API FQDN injected'
117     fi
118 fi
119
120 # Create an load openrc
121 create_openrc
122
123 . ./cloud/admin-openrc
124
125 #wget -P /tmp/images http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img
126 #openstack image create --file /tmp/images/cirros-0.3.3-x86_64-disk.img --disk-format qcow2 --container-format bare "cirros-0.3.3-x86_64"
127
128 #wget -P /tmp/images http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img
129 #openstack image create --file /tmp/images/trusty-server-cloudimg-amd64-disk1.img --disk-format qcow2 --container-format bare "ubuntu-trusty-daily"
130 #wget -P /tmp/images http://cloud-images.ubuntu.com/trusty/current/xenial-server-cloudimg-amd64.tar.gz
131 #openstack image create --file /tmp/images/xenial-server-cloudimg-amd64.tar.gz --container-format bare --disk-format raw "xenial-server-cloudimg-amd64"
132
133 #rm -rf /tmp/images
134
135 # adjust tiny image
136 #nova flavor-delete m1.tiny
137 #nova flavor-create m1.tiny 1 512 8 1
138
139
140 # import key pair
141 #openstack project create --description "Demo Tenant" demo
142 #openstack user create --project demo --password demo --email demo@demo.demo demo
143
144 #openstack keypair create --public-key ~/.ssh/id_rsa.pub ubuntu-keypair
145
146 #Modify the flavours to fit better
147 #nova flavor-create FLAVOR_NAME FLAVOR_ID RAM_IN_MB ROOT_DISK_IN_GB NUMBER_OF_VCPUS
148 #nova flavor-delete m1.tiny > /dev/null 2>&1
149 #nova flavor-delete m1.small > /dev/null 2>&1
150 #nova flavor-delete m1.medium > /dev/null 2>&1
151 #nova flavor-delete m1.large > /dev/null 2>&1
152 #nova flavor-delete m1.xlarge > /dev/null 2>&1
153 #nova flavor-create --is-public true m1.tiny auto 512 5 1 > /dev/null 2>&1
154 #nova flavor-create --is-public true m1.small auto 1024 10 1 > /dev/null 2>&1
155 #nova flavor-create --is-public true m1.medium auto 2048 10 2 > /dev/null 2>&1
156 #nova flavor-create --is-public true m1.large auto 3072 10 2 > /dev/null 2>&1
157 ## need extra for windows image (15g)
158 #nova flavor-create --is-public true m1.xlarge auto 8096 30 4  > /dev/null 2>&1
159
160 echo "modifying default quotas for admin user"
161
162 TENANT_ID=admin
163
164 #Modify quotas for the tenant to allow large deployments
165 #nova quota-update --instances 400 $TENANT_ID
166 #nova quota-update --cores 800 $TENANT_ID
167 #nova quota-update --ram 404800 $TENANT_ID
168 #nova quota-update --security-groups 4000 $TENANT_ID
169 #nova quota-update --floating_ips -1 $TENANT_ID
170 #nova quota-update --security-group-rules -1 $TENANT_ID
171
172 ### need to find how to change quota for the project not the tenant
173
174 ### modify default quota the same way..
175 #nova quota-class-update --instances 400 $TENANT_ID
176 #nova quota-class-update --cores 800 $TENANT_ID
177 #nova quota-class-update --ram 404800 $TENANT_ID
178 #nova quota-class-update --security-groups 4000 $TENANT_ID
179 #nova quota-class-update --floating-ips -1 $TENANT_ID
180 #nova quota-class-update --security-group-rules -1 $TENANT_ID
181
182 # configure external network
183
184 ##
185 ## Create external subnet Network
186 ##
187
188 #neutron net-create ext_net --shared --router:external=True
189 neutron net-create ext_net --router:external=True
190
191 if [ "onos" == "$1" ]; then
192     launch_eth
193     neutron subnet-create ext_net --name ext-subnet \
194        --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
195        --disable-dhcp --gateway $EXTNET_GW --dns-nameserver 8.8.8.8 $EXTNET_NET
196     #neutron subnet-create ext_net --name ext-subnet $EXTNET_NET
197     #update_gw_mac
198 elif [ "nosdn" == "$1" ]; then
199     neutron subnet-create ext_net --name ext-subnet \
200        --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
201        --disable-dhcp --gateway $EXTNET_GW --dns-nameserver 8.8.8.8 $EXTNET_NET
202     # configure security groups
203     #neutron security-group-rule-create --direction ingress --ethertype IPv4 --protocol icmp --remote-ip-prefix 0.0.0.0/0 default
204     #neutron security-group-rule-create --direction ingress --ethertype IPv4 --protocol tcp --port-range-min 22 --port-range-max 22 --remote-ip-prefix 0.0.0.0/0 default
205 else
206     neutron subnet-create ext_net --name ext-subnet \
207        --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
208        --disable-dhcp --gateway $EXTNET_GW --dns-nameserver 8.8.8.8 $EXTNET_NET
209 fi
210
211
212 # create vm network
213 neutron net-create demo-net
214 neutron subnet-create --name demo-subnet --gateway 10.20.5.1 demo-net 10.20.5.0/24
215
216 neutron router-create demo-router
217
218 neutron router-interface-add demo-router demo-subnet
219
220 neutron router-gateway-set demo-router ext_net
221
222 # create pool of floating ips
223 i=0
224 while [ $i -ne 3 ]; do
225     neutron floatingip-create ext_net
226     i=$((i + 1))
227 done
228
229 #http://docs.openstack.org/juno/install-guide/install/apt/content/launch-instance-neutron.html
230 # nova boot --flavor m1.small --image cirros-0.3.3-x86_64 --nic net-id=b65479a4-3638-4595-9245-6e41ccd8bfd8 --security-group default --key-name ubuntu-keypair demo-instance1
231 # nova floating-ip-associate demo-instance1 10.5.8.35
232
233 # Create Congress datasources
234 sudo apt-get install -y python-congressclient
235
236 # Remove public endpoint and recreate it from internal
237 # Waiting congress client can be use with internal endpoints
238 if [ "$API_FQDN" != "''" ]; then
239     CONGRESS_PUB_ENDPOINT=$(openstack endpoint list --service policy --interface public -c ID -f value)
240     openstack endpoint delete $CONGRESS_PUB_ENDPOINT
241     CONGRESS_NEW_PUB_ENDPOINT=$(openstack endpoint list --service policy --interface internal -c URL -f value)
242     openstack endpoint create --region Canonical policy public $CONGRESS_NEW_PUB_ENDPOINT
243 fi
244
245 openstack congress datasource create nova "nova" \
246   --config username=$OS_USERNAME \
247   --config tenant_name=$OS_TENANT_NAME \
248   --config password=$OS_PASSWORD \
249   --config auth_url=http://$keystoneIp:5000/v2.0
250 openstack congress datasource create neutronv2 "neutronv2" \
251   --config username=$OS_USERNAME \
252   --config tenant_name=$OS_TENANT_NAME \
253   --config password=$OS_PASSWORD \
254   --config auth_url=http://$keystoneIp:5000/v2.0
255 openstack congress datasource create ceilometer "ceilometer" \
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 cinder "cinder" \
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 glancev2 "glancev2" \
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 keystone "keystone" \
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