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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
##############################################################################
# Copyright (c) 2015 Ericsson AB and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
""" Logical model
"""
from __future__ import absolute_import
from six.moves import range
class Object(object):
"""Base class for classes in the logical model
Contains common attributes and methods
"""
def __init__(self, name, context):
# model identities and reference
self.name = name
self._context = context
# stack identities
self.stack_name = None
self.stack_id = None
@property
def dn(self):
"""returns distinguished name for object"""
return self.name + "." + self._context.name
class PlacementGroup(Object):
"""Class that represents a placement group in the logical model
Concept comes from the OVF specification. Policy should be one of
"availability" or "affinity (there are more but they are not supported)"
"""
map = {}
def __init__(self, name, context, policy):
if policy not in ["affinity", "availability"]:
raise ValueError("placement group '%s', policy '%s' is not valid" %
(name, policy))
self.name = name
self.members = set()
self.stack_name = context.name + "-" + name
self.policy = policy
PlacementGroup.map[name] = self
def add_member(self, name):
self.members.add(name)
@staticmethod
def get(name):
return PlacementGroup.map.get(name)
class ServerGroup(Object): # pragma: no cover
"""Class that represents a server group in the logical model
Policy should be one of "anti-affinity" or "affinity"
"""
map = {}
def __init__(self, name, context, policy):
super(ServerGroup, self).__init__(name, context)
if policy not in {"affinity", "anti-affinity"}:
raise ValueError("server group '%s', policy '%s' is not valid" %
(name, policy))
self.name = name
self.members = set()
self.stack_name = context.name + "-" + name
self.policy = policy
ServerGroup.map[name] = self
def add_member(self, name):
self.members.add(name)
@staticmethod
def get(name):
return ServerGroup.map.get(name)
class Router(Object):
"""Class that represents a router in the logical model"""
def __init__(self, name, network_name, context, external_gateway_info):
super(Router, self).__init__(name, context)
self.stack_name = context.name + "-" + network_name + "-" + self.name
self.stack_if_name = self.stack_name + "-if0"
self.external_gateway_info = external_gateway_info
class Network(Object):
"""Class that represents a network in the logical model"""
list = []
def __init__(self, name, context, attrs):
super(Network, self).__init__(name, context)
self.stack_name = context.name + "-" + self.name
self.subnet_stack_name = self.stack_name + "-subnet"
self.subnet_cidr = attrs.get('cidr', '10.0.1.0/24')
self.enable_dhcp = attrs.get('enable_dhcp', 'true')
self.router = None
self.physical_network = attrs.get('physical_network', 'physnet1')
self.provider = attrs.get('provider')
self.segmentation_id = attrs.get('segmentation_id')
self.network_type = attrs.get('network_type')
self.port_security_enabled = attrs.get('port_security_enabled')
self.allowed_address_pairs = attrs.get('allowed_address_pairs', [])
try:
# we require 'null' or '' to disable setting gateway_ip
self.gateway_ip = attrs['gateway_ip']
except KeyError:
# default to explicit None
self.gateway_ip = None
else:
# null is None in YAML, so we have to convert back to string
if self.gateway_ip is None:
self.gateway_ip = "null"
if "external_network" in attrs:
self.router = Router("router", self.name,
context, attrs["external_network"])
self.vld_id = attrs.get("vld_id")
Network.list.append(self)
def has_route_to(self, network_name):
"""determines if this network has a route to the named network"""
if self.router and self.router.external_gateway_info == network_name:
return True
return False
@staticmethod
def find_by_route_to(external_network):
"""finds a network that has a route to the specified network"""
for network in Network.list:
if network.has_route_to(external_network):
return network
@staticmethod
def find_external_network():
"""return the name of an external network some network in this
context has a route to
"""
for network in Network.list:
if network.router:
return network.router.external_gateway_info
return None
class Server(Object): # pragma: no cover
"""Class that represents a server in the logical model"""
list = []
def __init__(self, name, context, attrs):
super(Server, self).__init__(name, context)
self.stack_name = self.name + "." + context.name
self.keypair_name = context.keypair_name
self.secgroup_name = context.secgroup_name
self.user = context.user
self.context = context
self.public_ip = None
self.private_ip = None
self.user_data = ''
self.interfaces = {}
if attrs is None:
attrs = {}
self.placement_groups = []
placement = attrs.get("placement", [])
placement = placement if isinstance(placement, list) else [placement]
for p in placement:
pg = PlacementGroup.get(p)
if not pg:
raise ValueError("server '%s', placement '%s' is invalid" %
(name, p))
self.placement_groups.append(pg)
pg.add_member(self.stack_name)
self.volume = None
if "volume" in attrs:
self.volume = attrs.get("volume")
self.volume_mountpoint = None
if "volume_mountpoint" in attrs:
self.volume_mountpoint = attrs.get("volume_mountpoint")
# support servergroup attr
self.server_group = None
sg = attrs.get("server_group")
if sg:
server_group = ServerGroup.get(sg)
if not server_group:
raise ValueError("server '%s', server_group '%s' is invalid" %
(name, sg))
self.server_group = server_group
server_group.add_member(self.stack_name)
self.instances = 1
if "instances" in attrs:
self.instances = attrs["instances"]
# dict with key network name, each item is a dict with port name and ip
self.ports = {}
self.floating_ip = None
self.floating_ip_assoc = None
if "floating_ip" in attrs:
self.floating_ip = {}
self.floating_ip_assoc = {}
if self.floating_ip is not None:
ext_net = Network.find_external_network()
assert ext_net is not None
self.floating_ip["external_network"] = ext_net
self._image = None
if "image" in attrs:
self._image = attrs["image"]
self._flavor = None
if "flavor" in attrs:
self._flavor = attrs["flavor"]
self.user_data = attrs.get('user_data', '')
Server.list.append(self)
@property
def image(self):
"""returns a server's image name"""
if self._image:
return self._image
else:
return self._context.image
@property
def flavor(self):
"""returns a server's flavor name"""
if self._flavor:
return self._flavor
else:
return self._context.flavor
def _add_instance(self, template, server_name, networks, scheduler_hints):
"""adds to the template one server and corresponding resources"""
port_name_list = []
for network in networks:
port_name = server_name + "-" + network.name + "-port"
self.ports[network.name] = {"stack_name": port_name}
# we can't use secgroups if port_security_enabled is False
if network.port_security_enabled:
sec_group_id = self.secgroup_name
else:
sec_group_id = None
# don't refactor to pass in network object, that causes JSON
# circular ref encode errors
template.add_port(port_name, network.stack_name, network.subnet_stack_name,
sec_group_id=sec_group_id, provider=network.provider,
allowed_address_pairs=network.allowed_address_pairs)
port_name_list.append(port_name)
if self.floating_ip:
external_network = self.floating_ip["external_network"]
if network.has_route_to(external_network):
self.floating_ip["stack_name"] = server_name + "-fip"
template.add_floating_ip(self.floating_ip["stack_name"],
external_network,
port_name,
network.router.stack_if_name,
sec_group_id)
self.floating_ip_assoc["stack_name"] = \
server_name + "-fip-assoc"
template.add_floating_ip_association(
self.floating_ip_assoc["stack_name"],
self.floating_ip["stack_name"],
port_name)
if self.flavor:
if isinstance(self.flavor, dict):
self.flavor["name"] = \
self.flavor.setdefault("name", self.stack_name + "-flavor")
template.add_flavor(**self.flavor)
self.flavor_name = self.flavor["name"]
else:
self.flavor_name = self.flavor
if self.volume:
if isinstance(self.volume, dict):
self.volume["name"] = \
self.volume.setdefault("name", server_name + "-volume")
template.add_volume(**self.volume)
template.add_volume_attachment(server_name, self.volume["name"],
mountpoint=self.volume_mountpoint)
else:
template.add_volume_attachment(server_name, self.volume,
mountpoint=self.volume_mountpoint)
template.add_server(server_name, self.image, flavor=self.flavor_name,
flavors=self.context.flavors,
ports=port_name_list,
user=self.user,
key_name=self.keypair_name,
user_data=self.user_data,
scheduler_hints=scheduler_hints)
def add_to_template(self, template, networks, scheduler_hints=None):
"""adds to the template one or more servers (instances)"""
if self.instances == 1:
server_name = self.stack_name
self._add_instance(template, server_name, networks,
scheduler_hints=scheduler_hints)
else:
# TODO(hafe) fix or remove, no test/sample for this
for i in range(self.instances):
server_name = "%s-%d" % (self.stack_name, i)
self._add_instance(template, server_name, networks,
scheduler_hints=scheduler_hints)
def update_scheduler_hints(scheduler_hints, added_servers, placement_group):
"""update scheduler hints from server's placement configuration
TODO: this code is openstack specific and should move somewhere else
"""
if placement_group.policy == "affinity":
if "same_host" in scheduler_hints:
host_list = scheduler_hints["same_host"]
else:
host_list = scheduler_hints["same_host"] = []
else:
if "different_host" in scheduler_hints:
host_list = scheduler_hints["different_host"]
else:
host_list = scheduler_hints["different_host"] = []
for name in added_servers:
if name in placement_group.members:
host_list.append({'get_resource': name})
|