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