forked from lorin/devstack-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot-cirros.py
executable file
·96 lines (74 loc) · 2.54 KB
/
boot-cirros.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python -u
'''
This script does the following
1. Connect the router to the public network
2. Add a public key
3. Boot a cirros instance
4. Attach a floating IP
'''
import socket
import os.path
import sys
import time
from novaclient.v1_1 import client as novaclient
from neutronclient.v2_0 import client as neutronclient
auth_url = "http://192.168.27.100:35357/v2.0"
username = "demo"
password = "password"
tenant_name = "demo"
neutron = neutronclient.Client(auth_url=auth_url,
username=username,
password=password,
tenant_name=tenant_name)
nova = novaclient.Client(auth_url=auth_url,
username=username,
api_key=password,
project_id=tenant_name)
print "Creating keypair: mykey...",
if not nova.keypairs.findall(name="mykey"):
with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as fpubkey:
nova.keypairs.create(name="mykey", public_key=fpubkey.read())
print "done"
print "Booting cirros instance...",
image = nova.images.find(name="cirros-0.3.1-x86_64-uec")
flavor = nova.flavors.find(name="m1.nano")
instance = nova.servers.create(name="cirros", image=image, flavor=flavor,
key_name="mykey")
# Poll at 5 second intervals, until the status is no longer 'BUILD'
status = instance.status
while status == 'BUILD':
time.sleep(5)
# Retrieve the instance again so the status field updates
instance = nova.servers.get(instance.id)
status = instance.status
print "done"
print "Creating floating ip...",
# Get external network
ext_net, = [x for x in neutron.list_networks()['networks']
if x['router:external']]
# Get the port corresponding to the instance
port, = [x for x in neutron.list_ports()['ports']
if x['device_id'] == instance.id]
# Create the floating ip
args = dict(floating_network_id=ext_net['id'],
port_id=port['id'])
ip_obj = neutron.create_floatingip(body={'floatingip': args})
print "done"
ip = ip_obj['floatingip']['floating_ip_address']
print "IP:", ip
print "Waiting for ssh to be ready on cirros instance...",
for i in range(60):
try:
seconds_to_wait = 1
ssh_port = 22
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(seconds_to_wait)
s.connect((ip, ssh_port))
except socket.timeout:
pass
else:
print "done"
break
else:
print "ssh server never came up!"
sys.exit(1)