3 # Copyright (C) 2014 Canonical Ltd.
5 # Authors: Nicolas Thomss <nicolas.thomas@canonical.com>
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 ## TODO use sudo apt-get install python-openstackclient instead
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
28 echo "This command is run to launch the first instance on a new Orange box Openstack deployment"
30 source ~/joid_config/admin-openrc
33 #Create a floating IP for the instance
34 if ( openstack server list | grep jumpserver2 >/dev/null )
37 FLOAT_IP="$(openstack server list | grep jumpserver2 | awk '{ print $13 }')"
39 FLOAT_IP="$(openstack floating ip create ext-net -c floating_ip_address -f value)"
43 NET_UUID="$(openstack network list | grep private | awk '{ print $2 }')"
44 IMAGE_UUID="$(openstack image list | grep "Xenial x86_64" | awk '{ print $2 }')"
45 IMAGE_LXD_UUID="$(openstack image list | grep "Xenial LXC x86_64" | awk '{ print $2 }')"
46 openstack server list | grep jumpserver2 ||openstack server create --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=` openstack server show jumpserver2 | grep status | awk '{print $4}'`
49 while [ $INST_STATE != "ACTIVE" ];
51 INST_STATE=` openstack server show jumpserver2 | grep status | awk '{print $4}'`
52 echo "Jumpserver2 instance state is: "$INST_STATE" waiting to be active"
57 #Associate the floating IP with the new instance
58 if [ "$associated" -eq "0" ]; then
59 openstack ip floating add $FLOAT_IP jumpserver2 || true
65 ### make it more Readable
66 export SSH="ssh -o StrictHostKeyChecking=no ubuntu@$FLOAT_IP"
70 $SSH sudo add-apt-repository -y ppa:juju/stable
72 #SSH into the instance and install Juju
73 $SSH sudo apt-get -y update
75 $SSH sudo apt-get -y install juju python-novaclient python-swiftclient
79 scp -o StrictHostKeyChecking=no ~/.ssh/id_rsa* ubuntu@$FLOAT_IP:~/.ssh/
82 #Output a juju cloud file that works on this cloud
87 auth-types: [access-key, userpass]
90 endpoint: $OS_AUTH_URL
92 $SSH juju add-cloud openstack os-cloud.yaml --replace
94 #Output a juju cred file that works on this cloud
100 password: $OS_PASSWORD
101 tenant-name: $OS_TENANT_NAME
102 username: $OS_USERNAME
105 $SSH "juju add-credential openstack -f os-creds.yaml" --replace
107 ## Creating images metadata
108 $SSH mkdir -p juju-meta
109 for s in precise trusty xenial win2012r2
111 #Create juju metadata
112 IMAGE_UUID="$(openstack image list | grep -i "$s x86_64" | awk '{ print $2 }')"
113 IMAGE_LXC_UUID="$(openstack 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"
117 ## Generate tools (needed if windows is in).
118 $SSH "juju metadata generate-tools -d juju-meta/ "
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
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"
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"
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)
137 ###juju bootstrap openstack openstack --config image-metadata-url=http://192.168.16.5/juju/images/ --config network=private --upload-tools --debug -v
140 my_ip=`ip route | grep src| grep -v virb|grep -v lxcb| head -1| cut -d " " -f 12 `
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"
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
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"
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"