-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrackspace.py
More file actions
174 lines (147 loc) · 5.65 KB
/
rackspace.py
File metadata and controls
174 lines (147 loc) · 5.65 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Deis cloud provider implementation for Rackspace Open Cloud.
"""
from __future__ import unicode_literals
import json
import logging
import novaclient
import pyrax
# TODO: this seems like a bad import, but can't update the layer
# correctly otherwise.
from api.models import Layer
logger = logging.getLogger(__name__)
## TODO: add syd and hkg when they gain performance flavors in early 2014
RACKSPACE_REGIONS = {
'dfw': 'Dallas',
'iad': 'Northern Virginia',
'ord': 'Chicago',
'lon': 'London',
#'syd': 'Sydney',
#'hkg': 'Hong Kong',
}
RACKSPACE_DEFAULT_REGION = 'dfw'
def seed_flavors():
"""Seed the database with default flavors for each Rackspace region.
:rtype: list of dicts containing flavor data
"""
flavors = []
for r in RACKSPACE_REGIONS.keys():
flavors.append({
'id': 'rackspace-{}'.format(r),
'provider': 'rackspace',
'params': json.dumps({
'region': r,
# 'image': ?
'flavor': 'performance1-2', # 2GB Performance Instance, 2 VCPUs, 2048 / 80GB
})
})
return flavors
def build_layer(layer):
"""
Build a layer.
:param layer: a dict containing formation, id, params, and creds info
"""
region = layer['params'].setdefault('region', RACKSPACE_DEFAULT_REGION)
creds = layer['creds']
conn = _create_rackspace_connection(creds, region)
name = "{formation}-{id}".format(**layer)
# import a new keypair using the layer key material
conn.keypairs.create(name, layer['ssh_public_key'])
# Rackspace images only have the root user created by default
layer_ = Layer.objects.get(id=layer['id'], formation__id=layer['formation'])
layer_.ssh_username = 'root'
layer_.save()
def destroy_layer(layer):
"""
Destroy a layer.
:param layer: a dict containing formation, id, params, and creds info
"""
region = layer['params'].setdefault('region', RACKSPACE_DEFAULT_REGION)
creds = layer['creds']
conn = _create_rackspace_connection(creds, region)
name = "{formation}-{id}".format(**layer)
# delete the keypair we created in build_layer
try:
conn.keypairs.delete(name)
except (novaclient.exceptions.NotFound, pyrax.exceptions.NotFound) as err:
logger.warning("rackspace.destroy_layer: {}".format(err))
def build_node(node):
"""
Build a node.
:param node: a dict containing formation, layer, params, and creds info.
:rtype: a tuple of (provider_id, fully_qualified_domain_name, metadata)
"""
params, creds = node['params'], node['creds']
region = params.setdefault('region', RACKSPACE_DEFAULT_REGION)
conn = _create_rackspace_connection(creds, region)
name = "{formation}-{layer}".format(**node)
params['key_name'] = name
tags = {'Name': name}
# look up the saved image / snapshot by name 'deis-base-image', until we
# can create a public image at Rackspace--at which point we call list_images().
try:
image = next(i for i in conn.list_snapshots() if i.name == 'deis-base-image')
except StopIteration:
msg = """\
Can't find saved image "deis-base-image" in region {}. Please follow the
instructions in prepare-rackspace-image.sh before scaling Deis nodes.
""".format(region)
raise EnvironmentError(msg)
srv = conn.servers.create(
name, image.id, params['flavor'], meta=tags, key_name=params['key_name'])
server = pyrax.utils.wait_for_build(srv)
provider_id = server.id
# TODO: is this the right way to fetch the fqdn?
public_addrs = server.addresses.get('public', [])
ipv4_addrs = [entry['addr'] for entry in public_addrs if entry['version'] == 4]
fqdn = ipv4_addrs[0] if ipv4_addrs else None
metadata = _format_metadata(server)
return provider_id, fqdn, metadata
def destroy_node(node):
"""
Destroy a node.
:param node: a dict containing a node's provider_id, params, and creds
"""
params, creds = node['params'], node['creds']
region = params.setdefault('region', 'dfw')
conn = _create_rackspace_connection(creds, region)
provider_id = node['provider_id']
try:
server = conn.servers.get(provider_id)
server.delete()
pyrax.utils.wait_until(server, 'status', ['DELETED', 'ERROR'])
except (novaclient.exceptions.NotFound, pyrax.exceptions.NotFound) as err:
logger.warning("rackspace.destroy_node: {}".format(err))
def _create_rackspace_connection(creds, region):
"""
Connect to a Rackspace region with the given credentials.
:param creds: a dict containing a Rackspace username and api_key
:region: the name of a Rackspace region, such as "ord"
:rtype: a connected :class:`~novaclient.v1_1.client.Client`
:raises EnvironmentError: if no credentials are provided
"""
if not creds:
raise EnvironmentError('No credentials provided')
pyrax.set_setting('identity_type', creds['identity_type'])
pyrax.set_credentials(creds['username'], creds['api_key'])
return pyrax.connect_to_cloudservers(region.upper())
def _format_metadata(server):
return {
'addresses': server.addresses,
'created': server.created,
'flavor': server.flavor,
'hostId': server.hostId,
'human_id': server.human_id,
'id': server.id,
'image': server.image,
'key_name': server.key_name,
'is_loaded': server.is_loaded(),
'metadata': server.metadata,
'name': server.name,
'networks': server.networks,
'progress': server.progress,
'status': server.status,
'tenant_id': server.tenant_id,
'updated': server.updated,
'user_id': server.user_id,
}