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