This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
provisioning_serializers.py
475 lines (398 loc) · 17.5 KB
/
provisioning_serializers.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# -*- coding: utf-8 -*-
# Copyright 2013 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Provisioning serializers for orchestrator"""
from itertools import groupby
import netaddr
import six
from nailgun import consts
from nailgun import extensions
from nailgun.logger import logger
from nailgun import objects
from nailgun.orchestrator.base_serializers import MellanoxMixin
from nailgun.orchestrator.priority_serializers import PriorityStrategy
from nailgun.orchestrator import tasks_templates
from nailgun.settings import settings
from nailgun import utils
class ProvisioningSerializer(MellanoxMixin):
"""Provisioning serializer"""
@classmethod
def serialize(cls, cluster, nodes, ignore_customized=False):
"""Serialize cluster for provisioning."""
cluster_attrs = objects.Attributes.merged_attrs_values(
cluster.attributes
)
serialized_nodes = []
for customized, node_group in groupby(
nodes, lambda node: bool(node.replaced_provisioning_info)):
if customized and not ignore_customized:
serialized_nodes.extend(cls.serialize_customized(node_group))
else:
serialized_nodes.extend(
cls.serialize_nodes(cluster_attrs, node_group))
if cluster.replaced_provisioning_info:
serialized_info = cluster.replaced_provisioning_info
else:
serialized_info = cls.serialize_cluster_info(
cluster, cluster_attrs
)
serialized_info['fault_tolerance'] = cls.fault_tolerance(
cluster, nodes
)
serialized_info['nodes'] = serialized_nodes
return serialized_info
@classmethod
def serialize_node_info(cls, cluster_attrs, node):
data = cls.serialize_node(cluster_attrs, node)
extensions.fire_callback_on_node_serialization_for_provisioning(
node, data
)
return data
@classmethod
def serialize_cluster_info(cls, cluster, cluster_attrs):
data = cls.serialize_cluster(cluster, cluster_attrs)
extensions.fire_callback_on_cluster_serialization_for_provisioning(
cluster, data
)
return data
@classmethod
def serialize_cluster(cls, cluster, cluster_attrs):
return {
'engine': {
'url': settings.COBBLER_URL,
'username': settings.COBBLER_USER,
'password': settings.COBBLER_PASSWORD,
'master_ip': settings.MASTER_IP,
}
}
@classmethod
def serialize_customized(cls, nodes):
serialized = []
for node in nodes:
serialized.append(node.replaced_provisioning_info)
return serialized
@classmethod
def serialize_nodes(cls, cluster_attrs, nodes):
"""Serialize nodes."""
serialized_nodes = []
for node in nodes:
node_data = cls.serialize_node_info(cluster_attrs, node)
serialized_nodes.append(node_data)
return serialized_nodes
@classmethod
def serialize_cloud_init_templates(cls, release):
"""Serialize a dict of cloud-init templates.
It will serialize the names of cloud-init templates, thus allowing
nailgun to request particular version for every template to be
rendered during provisioning.
Eg.:
"cloud_init_templates": {
"boothook": "boothook_fuel_6.1_centos.jinja2",
"cloud_config": "cloud_config_fuel_6.1_centos.jinja2",
"meta_data": "meta_data_fuel_6.1_centos.jinja2",
}
"""
cloud_init_templates = {}
for k in (consts.CLOUD_INIT_TEMPLATES.boothook,
consts.CLOUD_INIT_TEMPLATES.cloud_config,
consts.CLOUD_INIT_TEMPLATES.meta_data):
cloud_init_templates[k] = '{0}_fuel_{1}_{2}.jinja2'.format(
k, release.environment_version,
release.operating_system.lower())
return cloud_init_templates
@classmethod
def serialize_node(cls, cluster_attrs, node):
"""Serialize a single node."""
serialized_node = {
'uid': node.uid,
'power_address': node.ip,
'name': objects.Node.get_slave_name(node),
# right now it duplicates to avoid possible issues
'slave_name': objects.Node.get_slave_name(node),
'hostname': objects.Node.get_node_fqdn(node),
'power_pass': cls.get_ssh_key_path(node),
'profile': cluster_attrs['cobbler']['profile'],
'power_type': 'ssh',
'power_user': 'root',
'name_servers': '\"%s\"' % settings.DNS_SERVERS,
'name_servers_search': '\"%s\"' % settings.DNS_SEARCH,
'netboot_enabled': '1',
# For provisioning phase
'kernel_options': {
'netcfg/choose_interface':
objects.Node.get_admin_physical_iface(node).mac,
'udevrules': cls.interfaces_mapping_for_udev(node)},
'ks_meta': {
'pm_data': {
'kernel_params': objects.Node.get_kernel_params(node)},
'fuel_version': node.cluster.fuel_version,
'cloud_init_templates':
cls.serialize_cloud_init_templates(node.cluster.release),
'puppet_auto_setup': 1,
'puppet_master': settings.PUPPET_MASTER_HOST,
'puppet_enable': 0,
'mco_auto_setup': 1,
'install_log_2_syslog': 1,
'mco_pskey': settings.MCO_PSKEY,
'mco_vhost': settings.MCO_VHOST,
'mco_host': settings.MCO_HOST,
'mco_user': settings.MCO_USER,
'mco_password': settings.MCO_PASSWORD,
'mco_connector': settings.MCO_CONNECTOR,
'mco_enable': 1,
# NOTE(aroma): identity parameter usually is added/updated
# by nailgun agent but due to particularities of its execution
# flow such action may lead to deployment failures [1].
# Hence we supply the information here so fuel-agent will
# create mcollective config initially with the data present,
# [1]: https://bugs.launchpad.net/fuel/+bug/1518306
'mco_identity': node.id,
'auth_key': "\"%s\"" % cluster_attrs.get('auth_key', ''),
'authorized_keys':
["\"%s\"" % key for key in settings.AUTHORIZED_KEYS],
'master_ip': settings.MASTER_IP,
'timezone': settings.TIMEZONE,
}}
provision_data = cluster_attrs.get('provision')
if provision_data:
if provision_data['method'] == consts.PROVISION_METHODS.image:
serialized_node['ks_meta']['image_data'] = \
provision_data['image_data']
serialized_node['ks_meta']['repo_setup'] = cluster_attrs['repo_setup']
vlan_splinters = cluster_attrs.get('vlan_splinters', {})
if vlan_splinters.get('vswitch') == 'kernel_lt':
serialized_node['ks_meta']['kernel_lt'] = 1
cls.inject_mellanox_settings_for_provisioning(
cluster_attrs, serialized_node)
net_manager = objects.Cluster.get_network_manager(node.cluster)
gw = net_manager.get_default_gateway(node.id)
admin_net = objects.NetworkGroup.get_admin_network_group(node)
serialized_node['ks_meta'].update({'gw': gw})
serialized_node['ks_meta'].update(
{'admin_net': admin_net.cidr}
)
serialized_node.update(cls.serialize_interfaces(node))
return serialized_node
@classmethod
def serialize_interfaces(cls, node):
interfaces = {}
interfaces_extra = {}
net_manager = objects.Cluster.get_network_manager(node.cluster)
admin_ip = net_manager.get_admin_ip_for_node(node)
admin_netmask = str(netaddr.IPNetwork(
objects.NetworkGroup.get_admin_network_group(node).cidr
).netmask)
for interface in node.nic_interfaces:
name = interface.name
interfaces[name] = {
'mac_address': interface.mac,
'static': '0'}
# interfaces_extra field in cobbler ks_meta
# means some extra data for network interfaces
# configuration. It is used by cobbler snippet.
# For example, cobbler interface model does not
# have 'peerdns' field, but we need this field
# to be configured. So we use interfaces_extra
# branch in order to set this unsupported field.
interfaces_extra[name] = {
'peerdns': 'no',
'onboot': 'no'}
# We want node to be able to PXE boot via any of its
# interfaces. That is why we add all discovered
# interfaces into cobbler system. But we want
# assignted fqdn to be resolved into one IP address
# because we don't completely support multiinterface
# configuration yet.
if interface.mac == objects.Node.\
get_admin_physical_iface(node).mac:
interfaces[name]['dns_name'] = \
objects.Node.get_node_fqdn(node)
interfaces[name]['netmask'] = admin_netmask
interfaces[name]['ip_address'] = admin_ip
interfaces_extra[name]['onboot'] = 'yes'
return {
'interfaces': interfaces,
'interfaces_extra': interfaces_extra}
@classmethod
def interfaces_mapping_for_udev(cls, node):
"""Serialize interfaces mapping for cobbler
:param node: node model
:returns: returns string, example:
00:02:03:04:04_eth0,00:02:03:04:05_eth1
"""
return ','.join((
'{0}_{1}'.format(i.mac, i.name) for i in node.nic_interfaces))
@classmethod
def get_ssh_key_path(cls, node):
"""Assign power pass depend on node state."""
if node.status == "discover":
logger.info(
u'Node %s seems booted with bootstrap image', node.full_name)
return settings.PATH_TO_BOOTSTRAP_SSH_KEY
logger.info(u'Node %s seems booted with real system', node.full_name)
return settings.PATH_TO_SSH_KEY
@classmethod
def fault_tolerance(cls, cluster, nodes):
may_fail = []
roles_metadata = objects.Cluster.get_roles(cluster)
for role in roles_metadata:
if 'fault_tolerance' in roles_metadata[role]:
tolerance = roles_metadata[role]['fault_tolerance']
# only percantage is supported for now
if not tolerance.endswith('%'):
continue
percentage = tolerance[:-1]
uids = []
for node in nodes:
if role in node.roles:
uids.append(node.uid)
may_fail.append({'uids': uids,
'percentage': int(percentage)})
return may_fail
class ProvisioningSerializer61(ProvisioningSerializer):
@classmethod
def serialize(cls, cluster, nodes, ignore_customized=False):
serialized_info = super(ProvisioningSerializer61, cls).serialize(
cluster, nodes, ignore_customized)
serialized_info['pre_provision'] = \
cls.serialize_pre_provision_tasks(cluster)
return serialized_info
@classmethod
def serialize_pre_provision_tasks(cls, cluster):
tasks = []
attrs = objects.Attributes.merged_attrs_values(cluster.attributes)
is_build_images = all([
cluster.release.operating_system == consts.RELEASE_OS.ubuntu,
attrs['provision']['method'] == consts.PROVISION_METHODS.image])
if is_build_images:
packages = cls._make_provisioning_package_list(attrs['provision'])
tasks.append(
tasks_templates.make_provisioning_images_task(
[consts.MASTER_NODE_UID],
attrs['repo_setup']['repos'],
attrs['provision'],
cluster.id,
packages))
PriorityStrategy().one_by_one(tasks)
return tasks
@classmethod
def serialize_node(cls, cluster_attrs, node):
serialized_node = super(ProvisioningSerializer61, cls).serialize_node(
cluster_attrs, node)
use_fedora = cluster_attrs.get('use_fedora_lt', {})
if use_fedora.get('kernel') == 'fedora_lt_kernel':
serialized_node['ks_meta']['kernel_lt'] = 1
return serialized_node
@classmethod
def _make_provisioning_package_list(cls, provision_data):
packages = provision_data.get('packages', '')
return list(six.moves.filter(
bool, (s.strip() for s in packages.split('\n'))))
def get_serializer_for_cluster(cluster):
"""Returns a serializer depends on a given `cluster`.
:param cluster: cluster to process
:returns: a serializer for a given cluster
"""
serializers_map = {
'5': ProvisioningSerializer,
'6.0': ProvisioningSerializer,
'6.1': ProvisioningSerializer61,
'7.0': ProvisioningSerializer70,
'8.0': ProvisioningSerializer80
}
for version, serializer in six.iteritems(serializers_map):
if cluster.release.environment_version.startswith(version):
return serializer
# by default, we should return latest serializer
return ProvisioningSerializer90
def serialize(cluster, nodes, ignore_customized=False):
"""Serialize cluster for provisioning."""
extensions.fire_callback_on_before_provisioning_serialization(
cluster, nodes, ignore_customized
)
serializer = get_serializer_for_cluster(cluster)
return serializer.serialize(
cluster, nodes, ignore_customized=ignore_customized
)
class ProvisioningSerializer70(ProvisioningSerializer61):
pass
class ProvisioningSerializer80(ProvisioningSerializer70):
@classmethod
def serialize_pre_provision_tasks(cls, cluster):
tasks = super(ProvisioningSerializer80,
cls).serialize_pre_provision_tasks(cluster)
attrs = objects.Attributes.merged_attrs_values(cluster.attributes)
if attrs['ironic']['enabled']:
tasks.append(
tasks_templates.generate_ironic_bootstrap_keys_task(
[consts.MASTER_NODE_UID],
cluster.id))
tasks.append(
tasks_templates.make_ironic_bootstrap_task(
[consts.MASTER_NODE_UID],
cluster.id))
PriorityStrategy().one_by_one(tasks)
return tasks
class ProvisioningSerializer90(ProvisioningSerializer80):
@classmethod
def serialize_node(cls, cluster_attrs, node):
serialized_node = super(ProvisioningSerializer90, cls).serialize_node(
cluster_attrs, node)
operator_user = cluster_attrs.get('operator_user')
service_user = cluster_attrs.get('service_user')
serialized_node['ks_meta']['user_accounts'] = []
# Make sure that there are no empty strings as this might mess up
# cloud init templates
if operator_user:
operator_user_sudo = utils.get_lines(operator_user['sudo'])
operator_user_authkeys = utils.get_lines(operator_user['authkeys'])
operator_user_dict = {
'name': operator_user['name'],
'password': operator_user['password'],
'homedir': operator_user['homedir'],
'sudo': operator_user_sudo,
'ssh_keys': operator_user_authkeys + settings.AUTHORIZED_KEYS,
}
serialized_node['ks_meta']['user_accounts'].append(
operator_user_dict)
if service_user:
service_user_sudo = utils.get_lines(service_user['sudo'])
root_password = service_user['root_password']
service_user_dict = {
'name': service_user['name'],
'homedir': service_user['homedir'],
'sudo': service_user_sudo,
'password': service_user['password'],
'ssh_keys': settings.AUTHORIZED_KEYS
}
root_user_dict = {
'name': 'root',
'homedir': '/root',
'password': root_password,
'ssh_keys': settings.AUTHORIZED_KEYS
}
serialized_node['ks_meta']['user_accounts'].append(
service_user_dict)
serialized_node['ks_meta']['user_accounts'].append(root_user_dict)
return serialized_node
class ProvisionLCMSerializer(ProvisioningSerializer90):
@classmethod
def serialize_cluster(cls, cluster, cluster_attrs):
cluster_info = super(ProvisionLCMSerializer, cls).serialize_cluster(
cluster, cluster_attrs
)
cluster_info['packages'] = cls._make_provisioning_package_list(
cluster_attrs['provision']
)
return cluster_info