5f9a3a4c960b9c00fd12a6e56af0602804983f48
[joid.git] / ci / openstack.sh
1 #!/bin/sh -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 unitAddress()
11 {
12         juju status | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"public-address\"]" 2> /dev/null
13 }
14
15 unitMachine()
16 {
17         juju status | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"services\"][\"$1\"][\"units\"][\"$1/$2\"][\"machine\"]" 2> /dev/null
18 }
19
20 # create external network and subnet in openstack
21 create_openrc() {
22   mkdir -m 0700 -p cloud
23   keystoneIp=$(unitAddress keystone 0)
24   adminPasswd=$(juju get keystone | grep admin-password -A 5 | grep value | awk '{print $2}')
25   configOpenrc admin $adminPasswd admin http://$keystoneIp:5000/v2.0 Canonical > cloud/admin-openrc
26   chmod 0600 cloud/admin-openrc
27 }
28
29 configOpenrc()
30 {
31         cat <<-EOF
32                 export OS_USERNAME=$1
33                 export OS_PASSWORD=$2
34                 export OS_TENANT_NAME=$3
35                 export OS_AUTH_URL=$4
36                 export OS_REGION_NAME=$5
37                 EOF
38 }
39
40 create_openrc
41
42 . ./cloud/admin-openrc
43
44 wget -P /tmp/images http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img
45 glance image-create --name "cirros-0.3.3-x86_64" --file /tmp/images/cirros-0.3.3-x86_64-disk.img --disk-format qcow2 --container-format bare --progress
46
47 #wget -P /tmp/images http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img
48 #glance image-create --name "ubuntu-trusty-daily" --file /tmp/images/trusty-server-cloudimg-amd64-disk1.img --disk-format qcow2 --container-format bare --progress
49 rm -rf /tmp/images
50
51 # adjust tiny image
52 nova flavor-delete m1.tiny
53 nova flavor-create m1.tiny 1 512 8 1
54
55 # configure security groups
56 #neutron security-group-rule-create --direction ingress --ethertype IPv4 --protocol icmp --remote-ip-prefix 0.0.0.0/0 default
57 #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
58
59 # import key pair
60 keystone tenant-create --name demo --description "Demo Tenant"
61 keystone user-create --name demo --tenant demo --pass demo --email demo@demo.demo
62
63 nova keypair-add --pub-key ~/.ssh/id_rsa.pub ubuntu-keypair
64
65 # configure external network
66 neutron net-create ext-net --router:external --provider:physical_network external --provider:network_type flat
67
68 ##
69 ## Parse Network config
70 ##
71
72 EXTERNAL_NETWORK=`grep floating-ip-range deployconfig.yaml | cut -d ' ' -f 4 `
73 # split EXTERNAL_NETWORK=first ip;last ip; gateway;network
74 IFS=',' read -r -a EXTNET <<< "$EXTERNAL_NETWORK"
75
76 EXTNET_FIP=${EXTNET[0]}
77 EXTNET_LIP=${EXTNET[1]}
78 EXTNET_GW=${EXTNET[2]}
79 EXTNET_NET=${EXTNET[3]}
80
81 neutron subnet-create ext-net --name ext-subnet \
82    --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
83       --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
84
85 # create vm network
86 neutron net-create demo-net
87 neutron subnet-create --name demo-subnet --gateway 10.20.5.1 demo-net 10.20.5.0/24
88
89 neutron router-create demo-router
90
91 neutron router-interface-add demo-router demo-subnet
92
93 neutron router-gateway-set demo-router ext-net
94
95 # create pool of floating ips
96 i=0
97 while [ $i -ne 10 ]; do
98         neutron floatingip-create ext-net
99         i=$((i + 1))
100 done
101
102 #http://docs.openstack.org/juno/install-guide/install/apt/content/launch-instance-neutron.html
103 # 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
104 # nova floating-ip-associate demo-instance1 10.5.8.35