-
Notifications
You must be signed in to change notification settings - Fork 0
/
provision.py
82 lines (68 loc) · 2.23 KB
/
provision.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
#!/usr/bin/env python
import requests #http://docs.python-requests.org/en/latest
import json
KEYSTONE_URL = 'http://controller:5000/v2.0/'
TENANT = 'demo'
USERNAME = 'demo'
PASSWORD = 'secret'
r = requests.post("%s/tokens"% (KEYSTONE_URL,), json={
"auth": {
"tenantName": TENANT,
"passwordCredentials": {
"username": USERNAME,
"password": PASSWORD,
}
}
})
if r.ok:
token = r.json()['access']['token']['id']
else:
raise Exception(r.text)
# Place the token in the X-Auth-Token header
headers = {'X-Auth-Token': token}
# Find the link to the Nova API in the service catalog:
for service in r.json()['access']['serviceCatalog']:
if service['type'] == 'compute':
# There may be more than one endpoint, but we'll take the first
nova_endpoint = service['endpoints'][0]['publicURL']
if service['type'] == 'network':
neutron_endpoint = service['endpoints'][0]['publicURL']
keypair_ref = None
# Get the list of available keypairs:
keypairs = requests.get("%s/os-keypairs"% nova_endpoint, headers=headers)
# take the first one
keypair_ref = keypairs.json()['keypairs'][0]['keypair']['name']
image_ref = None
# Get the list of available images:
images = requests.get("%s/images"% nova_endpoint, headers=headers)
# Select the "cirros" image:
for image in images.json()['images']:
if image['name'] == 'cirros':
image_ref = image['id']
flavor_ref = None
# Get the list of available flavors:
flavors = requests.get("%s/flavors"% nova_endpoint, headers=headers)
# Select the "m1.small" flavor
for flavor in flavors.json()['flavors']:
if flavor['name'] == 'm1.small':
flavor_ref = flavor['id']
network_ref = None
# Get the list of available networks:
networks = requests.get("%s/v2.0/networks"% neutron_endpoint, headers=headers)
# Select the "private" network
for network in networks.json()['networks']:
if network['name'] == 'private':
network_ref = network['id']
# Launch the instance:
server = requests.post("%s/servers"% (nova_endpoint,), json={
"server": {
"name": "new_server",
"imageRef": image_ref,
"flavorRef": flavor_ref,
"key_name": keypair_ref,
"networks": [
{ "uuid": network_ref }
]
}
}, headers=headers)
server_ref = server.json()['server']['id']