3 # Copyright (C) 2014 Canonical Ltd.
5 # Authors: Nicolas Thomss <nicolas.thomas@canonical.com>
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.
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.
16 # You should have received a copy of the GNU General Public License
19 ## TODO use sudo apt-get install python-openstackclient instead
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
27 echo "This command is run to launch the first instance on a new Orange box Openstack deployment"
31 #Create a floating IP for the instance
32 if ( nova list | grep jumpserver2 >/dev/null )
34 FLOAT_IP="$(nova list | grep jumpserver2 | awk '{ print $13 }')"
36 FLOAT_IP="$(openstack ip floating create ext-net -c ip -f value)"
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" ];
47 INST_STATE=` nova show jumpserver2 | grep status | awk '{print $4}'`
48 echo "Jumpserver2 instance state is: "$INST_STATE" waiting to be active"
52 #Associate the floating IP with the new instance
53 nova floating-ip-associate jumpserver2 $FLOAT_IP || true
57 ### make it more Readable
58 export SSH="ssh -o StrictHostKeyChecking=no ubuntu@$FLOAT_IP"
62 $SSH sudo add-apt-repository -y ppa:juju/stable
64 #SSH into the instance and install Juju
65 $SSH sudo apt-get -y update
67 $SSH sudo apt-get -y install juju python-novaclient python-swiftclient
71 scp -o StrictHostKeyChecking=no ~/.ssh/id_rsa* ubuntu@$FLOAT_IP:~/.ssh/
74 #Output a juju cloud file that works on this cloud
79 auth-types: [access-key, userpass]
82 endpoint: $OS_AUTH_URL
84 $SSH juju add-cloud openstack os-cloud.yaml
86 #Output a juju cred file that works on this cloud
92 password: $OS_PASSWORD
93 tenant-name: $OS_TENANT_NAME
94 username: $OS_USERNAME
97 $SSH "juju add-credential openstack -f os-creds.yaml"
99 ## Creating images metadata
100 $SSH mkdir -p juju-meta
101 for s in precise trusty xenial win2012r2
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"
108 ## Generate tools (needed if windows is in).
109 $SSH "juju metadata generate-tools -d juju-meta/ "
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"
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
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)
127 ###juju bootstrap openstack openstack --config image-metadata-url=http://192.168.16.5/juju/images/ --config network=private --upload-tools --debug -v
130 my_ip=`ip route | grep src| grep -v virb|grep -v lxcb| head -1| cut -d " " -f 12 `
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"
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
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"
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"