1429b5441eb874104933794eea097bab34163c60
[joid.git] / juju / configure-juju-on-openstack
1 #!/bin/bash
2 #
3 #    Copyright (C) 2014 Canonical Ltd.
4 #
5 #    Authors: Nicolas Thomss  <nicolas.thomas@canonical.com>
6 #
7 #   Licensed under the Apache License, Version 2.0 (the "License");
8 #   you may not use this file except in compliance with the License.
9 #   You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #   Unless required by applicable law or agreed to in writing, software
14 #   distributed under the License is distributed on an "AS IS" BASIS,
15 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #   See the License for the specific language governing permissions and
17 #   limitations under the License.
18
19 set -ex
20 ## TODO use sudo apt-get install python-openstackclient    instead
21 ## examples:
22 ## openstack  ip floating list  --format=csv
23 ##  openstack  ip floating create  --format=shell ext_net
24 ## to avoid table parsing..
25 ## openstack server show -c status --format value my-instance-name
26
27
28 echo "This command is run to launch the first instance on a new Orange box Openstack deployment"
29
30 source ~/nova.rc
31 associated=0
32
33 #Create a floating IP for the instance
34 if ( nova list | grep jumpserver2 >/dev/null )
35 then
36     associated=1
37     FLOAT_IP="$(nova list | grep jumpserver2 | awk '{ print $13 }')"
38 else
39     FLOAT_IP="$(openstack ip floating create ext-net -c ip -f value)"
40 fi
41
42 #Launch an instance
43 NET_UUID="$(nova net-list | grep private | awk '{ print $2 }')"
44 IMAGE_UUID="$(nova image-list | grep "Xenial x86_64" | awk '{ print $2 }')"
45 IMAGE_LXD_UUID="$(nova image-list | grep "Xenial LXC x86_64" | awk '{ print $2 }')"
46 nova list | grep jumpserver2 ||nova boot --flavor m1.small --key-name default --image $IMAGE_UUID --nic net-id=$NET_UUID --security-group default jumpserver2
47 # checking jumpserver2 state until active ..
48 INST_STATE=` nova show jumpserver2 | grep status | awk '{print $4}'`
49 while [ $INST_STATE != "ACTIVE" ];
50 do
51     INST_STATE=` nova show jumpserver2 | grep status | awk '{print $4}'`
52     echo "Jumpserver2 instance state is: "$INST_STATE" waiting to be active"
53     sleep 1
54 done
55 sleep 6
56
57 #Associate the floating IP with the new instance
58 if [ "$associated" -eq "0" ]; then
59    nova floating-ip-associate jumpserver2 $FLOAT_IP || true
60    sleep 90
61 fi
62
63 sleep 90
64
65 ### make it more Readable
66 export SSH="ssh -o StrictHostKeyChecking=no ubuntu@$FLOAT_IP"
67
68
69 #Add juju stable repo
70 $SSH sudo add-apt-repository -y ppa:juju/stable
71
72 #SSH into the instance and install Juju
73 $SSH sudo apt-get -y update
74
75 $SSH sudo apt-get -y install juju python-novaclient python-swiftclient
76
77
78 #copy over SSH keys
79 scp -o StrictHostKeyChecking=no ~/.ssh/id_rsa* ubuntu@$FLOAT_IP:~/.ssh/
80
81
82 #Output a juju cloud file that works on this cloud
83 $SSH \
84 "echo 'clouds:
85     openstack:
86       type: openstack
87       auth-types: [access-key, userpass]
88       regions:
89         $OS_REGION_NAME:
90           endpoint: $OS_AUTH_URL
91 ' > os-cloud.yaml"
92 $SSH juju add-cloud openstack os-cloud.yaml --replace
93
94 #Output a juju cred file that works on this cloud
95 $SSH \
96 "echo 'credentials:
97   openstack:
98     openstack:
99       auth-type: userpass
100       password: $OS_PASSWORD
101       tenant-name: $OS_TENANT_NAME
102       username: $OS_USERNAME
103 ' > os-creds.yaml"
104
105 $SSH "juju add-credential openstack -f os-creds.yaml" --replace
106
107 ## Creating images metadata
108 $SSH mkdir -p juju-meta
109 for s in precise trusty xenial win2012r2
110 do
111 #Create juju metadata
112     IMAGE_UUID="$(nova image-list | grep -i "$s x86_64" | awk '{ print $2 }')"
113     IMAGE_LXC_UUID="$(nova image-list | grep -i "$s LXC x86_64" | awk '{ print $2 }')"
114     [ -n "$IMAGE_UUID" ] &&  $SSH "juju metadata generate-image -a amd64 -u $OS_AUTH_URL -i $IMAGE_UUID -r $OS_REGION_NAME -d juju-meta/ -s $s"
115 done
116
117 ## Generate tools (needed if windows is in).
118 $SSH "juju metadata generate-tools -d juju-meta/ "
119
120 ## upload images and tools streams to swift and make public
121 $SSH swift --os-auth-url $OS_AUTH_URL --os-username $OS_USERNAME  --os-password $OS_PASSWORD \
122  --os-tenant-name $OS_TENANT_NAME  post -r '.r:*' juju-meta
123
124 $SSH "cd ~/juju-meta/ && swift --os-auth-url $OS_AUTH_URL --os-username $OS_USERNAME \
125  --os-password $OS_PASSWORD --os-tenant-name $OS_TENANT_NAME \
126 upload juju-meta images"
127
128 $SSH "cd ~/juju-meta/ && swift --os-auth-url $OS_AUTH_URL --os-username $OS_USERNAME \
129  --os-password $OS_PASSWORD --os-tenant-name $OS_TENANT_NAME \
130 upload juju-meta tools"
131
132 ## collect the URL for getting the images
133 $($SSH swift --os-auth-url $OS_AUTH_URL --os-username $OS_USERNAME  --os-password $OS_PASSWORD \
134  --os-tenant-name $OS_TENANT_NAME auth)
135
136 ###
137 ###juju bootstrap openstack openstack --config image-metadata-url=http://192.168.16.5/juju/images/ --config network=private --upload-tools --debug -v
138 ###
139
140 my_ip=`ip route | grep src| grep -v virb|grep -v lxcb| head -1| cut -d " " -f 12 `
141 #Bootstrap Juju
142 ##$SSH "juju bootstrap openstack openstack --metadata-source=/var/www/html/juju-meta/ --upload-tools"
143 $SSH "juju bootstrap openstack openstack --config image-metadata-url=$OS_STORAGE_URL/juju-meta/images/ --config tools-metadata-url=$OS_STORAGE_URL/juju-meta/tools/ --config network=private"
144
145
146
147 ## useless if juju floating ip option on.
148 #########################################
149 #Create a new floating IP and associate with juju bootstrap instance
150 #INSTANCE_ID="$(nova list | grep juju-openstack-machine-0 | awk '{ print $2}')"
151 #FLOAT_IP2="$(nova floating-ip-create | grep ext_net | awk '{ print $2}')"
152 #nova floating-ip-associate $INSTANCE_ID $FLOAT_IP2
153
154 #Print the address of Juju-gui for deployments on Openstack
155 echo "Now connect to the Juju-GUI at: http://$FLOAT_IP2/ to continue deployments on Openstack."
156 echo "Pass: $OS_PASSWORD"
157
158 echo "OR Log in to openstack Jumpserver2 to deploy from command line"
159 echo "ssh ubuntu@$FLOAT_IP"
160 echo " You must set the following if creating a new model:"
161 echo " juju set-model-config image-metadata-url=$OS_STORAGE_URL/juju-meta/images/ tools-metadata-url=$OS_STORAGE_URL/juju-meta/tools/ network=private"
162 exit
163