modified to upload the cloud imaged for lxd containers for Xenial.
[joid.git] / juju / joid-configure-openstack
1 #!/bin/bash
2 #
3 #    Copyright (C) 2014 Canonical Ltd.
4 #
5 #    Authors: Nicolas Thomss  <nicolas.thomas@canonical.com>
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, version 3 of the License.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 set -ex
20
21 echo "This command is run to configure an Orange-Box Openstack deployment"
22
23 NEUTRON_FIXED_NET_CIDR="192.168.16.0/22"
24
25 #Check if VIP is set on keystone
26 #keystone_VIP=`juju get-config keystone| python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"settings\"][\"vip\"][\"value\"]"||true`
27 keystone_VIP=`juju get keystone| python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"settings\"][\"vip\"][\"value\"]"||true`
28
29 keystoneIp() {
30     KEYSTONE=$(juju status keystone |grep public-address|sed -- 's/.*\: //')
31     if [ $(echo $KEYSTONE|wc -w) == 1 ];then
32         echo $KEYSTONE
33     else
34         juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)['settings']['vip']['value']"
35     fi
36 }
37
38 if [ -n "$keystone_VIP" ]
39 then
40     keystone=$keystone_VIP
41 else
42     #if os-public-hostname is set and not VIP assume we need to use those at OpenStack endpoints.
43     keystone=$(keystoneIp)
44     keystone_PUBENDPOINT=`juju get keystone| python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"settings\"][\"os-public-hostname\"][\"value\"]"||true`
45 fi
46
47
48 echo "export SERVICE_ENDPOINT=http://$keystone:35357/v2.0/
49 unset SERVICE_TOKEN
50 unset SERVICE_ENDPOINT 
51 export OS_AUTH_URL=http://$keystone:35357/v2.0/
52 export OS_USERNAME=$(juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"settings\"][\"admin-user\"][\"value\"]")
53 export OS_PASSWORD=$(juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"settings\"][\"admin-password\"][\"value\"]")
54 export OS_TENANT_NAME=admin
55 export OS_REGION_NAME=$(juju get keystone | python -c "import yaml; import sys; print yaml.load(sys.stdin)[\"settings\"][\"region\"][\"value\"]")
56 " > ~/nova.rc
57
58 source ~/nova.rc
59
60 # Determine the tenant id for the configured tenant name.
61 export TENANT_ID="$(keystone tenant-list | grep $OS_TENANT_NAME | awk '{ print $2 }')"
62
63 if [ "$TENANT_ID" = "" ]; then
64         echo "Unable to find tenant ID, keystone auth problem"
65         exit
66 fi
67
68 echo "Configuring Openstack Neutron Networking"
69
70 #create ext network with neutron for floating IPs
71 EXTERNAL_NETWORK_ID=$(neutron net-show ext-net | grep " id" | awk '{print $4}')
72
73 #Create private network for neutron for tenant VMs
74 neutron net-show private > /dev/null 2>&1 || neutron net-create private
75 neutron subnet-show private_subnet > /dev/null 2>&1 || neutron subnet-create private $NEUTRON_FIXED_NET_CIDR -- --name private_subnet --dns_nameservers list=true 8.8.8.8
76 SUBNET_ID=$(neutron subnet-show private_subnet | grep " id" | awk '{print $4}')
77
78 #Create router for external network and private network
79 neutron router-show provider-router > /dev/null 2>&1 || neutron router-create --tenant-id $TENANT_ID provider-router
80 ROUTER_ID=$(neutron router-show provider-router | grep " id" | awk '{print $4}')
81
82 neutron router-gateway-clear provider-router || true
83 neutron router-gateway-set $ROUTER_ID $EXTERNAL_NETWORK_ID
84 ## make it always ok to have it indempodent.
85 neutron router-interface-add $ROUTER_ID $SUBNET_ID || true
86
87
88 echo "Configuring security groups for access to ICMP, SSH and RDP by default"
89
90 #Configure the default security group to allow ICMP and SSH
91 neutron security-group-rule-list | grep "icmp" > /dev/null 2>&1 || neutron security-group-rule-create --direction ingress --ethertype IPv4 --protocol icmp --remote-ip-prefix 0.0.0.0/0 default
92 neutron security-group-rule-list | grep "22/tcp" > /dev/null 2>&1 || 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
93 neutron security-group-rule-list | grep "3389/tcp" > /dev/null 2>&1 || neutron security-group-rule-create --direction ingress --ethertype IPv4 --protocol tcp --port-range-min 3389 --port-range-max 3389 --remote-ip-prefix 0.0.0.0/0 default
94
95 echo "Uploading default SSH key"
96
97 #Upload a default SSH key
98 nova keypair-list | grep default  > /dev/null 2>&1 || nova  keypair-add --pub-key ~/.ssh/id_rsa.pub default > /dev/null 2>&1
99
100 echo "Modifying the flavors to be better sized for the Orange Box"
101
102 #Modify the flavours to fit better on the OB
103 #nova flavor-create FLAVOR_NAME FLAVOR_ID RAM_IN_MB ROOT_DISK_IN_GB NUMBER_OF_VCPUS
104 nova flavor-delete m1.tiny > /dev/null 2>&1
105 nova flavor-delete m1.small > /dev/null 2>&1
106 nova flavor-delete m1.medium > /dev/null 2>&1
107 nova flavor-delete m1.large > /dev/null 2>&1
108 nova flavor-delete m1.xlarge > /dev/null 2>&1
109 nova flavor-create --is-public true m1.tiny auto 512 5 1 > /dev/null 2>&1
110 nova flavor-create --is-public true m1.small auto 1024 10 1 > /dev/null 2>&1
111 nova flavor-create --is-public true m1.medium auto 2048 10 2 > /dev/null 2>&1
112 nova flavor-create --is-public true m1.large auto 3072 10 2 > /dev/null 2>&1
113 ## need extra for windows image (15g)
114 nova flavor-create --is-public true m1.xlarge auto 8096 30 4  > /dev/null 2>&1
115
116 echo "modifying default quotas for admin user"
117
118 #Modify quotas for the tenant to allow large deployments
119 nova quota-update --instances 400 $TENANT_ID
120 nova quota-update --cores 800 $TENANT_ID
121 nova quota-update --ram 404800 $TENANT_ID
122 nova quota-update --security-groups 4000 $TENANT_ID
123 nova quota-update --floating-ips -1 $TENANT_ID
124 nova quota-update --security-group-rules -1 $TENANT_ID
125
126 ### need to find how to change quota for the project not the tenant
127
128 ### modify default quota the same way..
129 nova quota-class-update --instances 400 $TENANT_ID
130 nova quota-class-update --cores 800 $TENANT_ID
131 nova quota-class-update --ram 404800 $TENANT_ID
132 nova quota-class-update --security-groups 4000 $TENANT_ID
133 nova quota-class-update --floating_ips -1 $TENANT_ID
134 nova quota-class-update --security-group-rules -1 $TENANT_ID
135
136 echo "Uploading images to glance"
137
138 #Upload images to glance
139 #glance image-list | grep "Precise x86_64" > /dev/null 2>&1 || glance image-create --name="Precise x86_64" --visibility=public --container-format=ovf --disk-format=qcow2 <  /srv/data/precise-server-cloudimg-amd64-disk1.img
140 glance image-list | grep "Trusty x86_64" > /dev/null 2>&1 || glance image-create --name="Trusty x86_64" --visibility=public --container-format=ovf --disk-format=qcow2 <  /srv/data/trusty-server-cloudimg-amd64-disk1.img
141 glance image-list | grep "Xenial x86_64" > /dev/null 2>&1 || glance image-create --name="Xenial x86_64" --visibility=public --container-format=ovf --disk-format=qcow2 <  /srv/data/xenial-server-cloudimg-amd64-disk1.img
142 glance image-list | grep "CentOS 6.4" > /dev/null 2>&1 || glance image-create --name="CentOS 6.4" --visibility=public --container-format=bare --disk-format=qcow2 < /srv/data/centos6.4-x86_64-gold-master.img
143 glance image-list | grep "Cirros 0.3" > /dev/null 2>&1 || glance image-create --name="Cirros 0.3" --visibility=public --container-format=bare --disk-format=qcow2 < /srv/data/cirros-0.3.4-x86_64-disk.img
144 glance image-list | grep "Xenial LXC x86_64" > /dev/null 2>&1 || glance image-create --name="Xenial LXC x86_64" --visibility=public --container-format=bare --disk-format=root-tar --property architecture="x86_64"  < /srv/data/xenial-server-cloudimg-amd64-root.tar.gz
145 glance image-list | grep "Cirros LXC 0.3" > /dev/null 2>&1 || glance image-create --name="Cirros LXC 0.3" --visibility=public --container-format=bare --disk-format=root-tar --property architecture="x86_64"  < /srv/data/cirros-0.3.4-x86_64-lxc.tar.gz
146 ## image name is used by script to generate metadata .. don't screw the series
147 #glance image-list | grep "win2012r2" > /dev/null 2>&1 || gzip -cd /srv/data/windows_server_2012_r2_standard_eval_kvm_20151021.qcow2.gz |glance image-create --name="Windows Server 2012" --is-public=true --container-format=bare --disk-format=qcow2 --progress