blob: 96fe5292c4dee45d578c8c53fde61402c0cfdab9 [file] [log] [blame]
Ondrej Smolab57a23b2018-01-24 11:18:24 +01001import logging
2from salt.exceptions import CommandExecutionError, SaltInvocationError
3
4LOG = logging.getLogger(__name__)
5
6SIZE = {
7 "M": 1000000,
8 "G": 1000000000,
9 "T": 1000000000000,
10}
11
12RAID = {
13 0: "raid-0",
14 1: "raid-1",
15 5: "raid-5",
16 10: "raid-10",
17}
18
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020019
Ondrej Smolab57a23b2018-01-24 11:18:24 +010020def __virtual__():
azvyagintsevf3515c82018-06-26 18:59:05 +030021 """
Ondrej Smolab57a23b2018-01-24 11:18:24 +010022 Load MaaSng module
azvyagintsevf3515c82018-06-26 18:59:05 +030023 """
Ondrej Smolab57a23b2018-01-24 11:18:24 +010024 return 'maasng'
25
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020026
azvyagintseve2e37a12018-11-01 14:45:49 +020027def maasng(funcname, *args, **kwargs):
28 """
29 Simple wrapper, for __salt__ maasng
30 :param funcname:
31 :param args:
32 :param kwargs:
33 :return:
34 """
35 return __salt__['maasng.{}'.format(funcname)](*args, **kwargs)
36
37
38def merge2dicts(d1, d2):
39 z = d1.copy()
40 z.update(d2)
41 return z
42
43
azvyagintsev3ff2ef12018-06-01 21:30:45 +030044def disk_layout_present(hostname, layout_type, root_size=None, root_device=None,
45 volume_group=None, volume_name=None, volume_size=None,
46 disk={}, **kwargs):
Ondrej Smolab57a23b2018-01-24 11:18:24 +010047 '''
48 Ensure that the disk layout does exist
49
50 :param name: The name of the cloud that should not exist
51 '''
52 ret = {'name': hostname,
53 'changes': {},
54 'result': True,
55 'comment': 'Disk layout "{0}" updated'.format(hostname)}
56
57 machine = __salt__['maasng.get_machine'](hostname)
58 if "error" in machine:
Alexei Lugovoie5b64122018-11-06 12:30:01 +010059 if 0 in machine["error"]:
60 ret['comment'] = "No such machine {0}".format(hostname)
61 ret['changes'] = machine
62 else:
63 ret['comment'] = "State execution failed for machine {0}".format(hostname)
64 ret['result'] = False
65 ret['changes'] = machine
Ondrej Smolab57a23b2018-01-24 11:18:24 +010066 return ret
67
68 if machine["status_name"] != "Ready":
69 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
70 return ret
71
72 if __opts__['test']:
73 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020074 ret['comment'] = 'Disk layout will be updated on {0}, this action will delete current layout.'.format(
75 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010076 return ret
77
78 if layout_type == "flat":
79
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020080 ret["changes"] = __salt__['maasng.update_disk_layout'](
81 hostname, layout_type, root_size, root_device)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010082
83 elif layout_type == "lvm":
84
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020085 ret["changes"] = __salt__['maasng.update_disk_layout'](
86 hostname, layout_type, root_size, root_device, volume_group, volume_name, volume_size)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010087
azvyagintsevbca1f462018-05-25 19:06:46 +030088 elif layout_type == "custom":
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +020089 ret["changes"] = __salt__[
90 'maasng.update_disk_layout'](hostname, layout_type)
azvyagintsevbca1f462018-05-25 19:06:46 +030091
Ondrej Smolab57a23b2018-01-24 11:18:24 +010092 else:
93 ret["comment"] = "Not supported layout provided. Choose flat or lvm"
94 ret['result'] = False
95
96 return ret
97
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020098
azvyagintsev3ff2ef12018-06-01 21:30:45 +030099def raid_present(hostname, name, level, devices=[], partitions=[],
100 partition_schema={}):
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100101 '''
102 Ensure that the raid does exist
103
104 :param name: The name of the cloud that should not exist
105 '''
106
107 ret = {'name': name,
108 'changes': {},
109 'result': True,
110 'comment': 'Raid {0} presented on {1}'.format(name, hostname)}
111
112 machine = __salt__['maasng.get_machine'](hostname)
113 if "error" in machine:
Alexei Lugovoie5b64122018-11-06 12:30:01 +0100114 if 0 in machine["error"]:
115 ret['comment'] = "No such machine {0}".format(hostname)
116 ret['changes'] = machine
117 else:
118 ret['comment'] = "State execution failed for machine {0}".format(
119 hostname)
120 ret['result'] = False
121 ret['changes'] = machine
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100122 return ret
123
124 if machine["status_name"] != "Ready":
125 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
126 return ret
127
128 if __opts__['test']:
129 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200130 ret['comment'] = 'Raid {0} will be updated on {1}'.format(
131 name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100132 return ret
133
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200134 # Validate that raid exists
135 # With correct devices/partition
136 # OR
137 # Create raid
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100138
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200139 ret["changes"] = __salt__['maasng.create_raid'](
140 hostname=hostname, name=name, level=level, disks=devices, partitions=partitions)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100141
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200142 # TODO partitions
143 ret["changes"].update(disk_partition_present(
144 hostname, name, partition_schema)["changes"])
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100145
146 if "error" in ret["changes"]:
147 ret["result"] = False
148
149 return ret
150
151
Denis Egorenkodecf41b2018-11-07 13:04:18 +0400152def disk_partition_present(hostname, name, partition_schema={}):
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100153 '''
154 Ensure that the disk has correct partititioning schema
155
156 :param name: The name of the cloud that should not exist
157 '''
158
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200159 # 1. Validate that disk has correct values for size and mount
160 # a. validate count of partitions
161 # b. validate size of partitions
162 # 2. If not delete all partitions on disk and recreate schema
163 # 3. Validate type exists
164 # if should not exits
165 # delete mount and unformat
166 # 4. Validate mount exists
167 # 5. if not enforce umount or mount
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100168
169 ret = {'name': hostname,
170 'changes': {},
171 'result': True,
Denis Egorenkodecf41b2018-11-07 13:04:18 +0400172 'comment': 'Disk layout {0} presented'.format(name)}
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100173
174 machine = __salt__['maasng.get_machine'](hostname)
175 if "error" in machine:
Alexei Lugovoie5b64122018-11-06 12:30:01 +0100176 if 0 in machine["error"]:
177 ret['comment'] = "No such machine {0}".format(hostname)
178 ret['changes'] = machine
179 else:
180 ret['comment'] = "State execution failed for machine {0}".format(
181 hostname)
182 ret['result'] = False
183 ret['changes'] = machine
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100184 return ret
185
186 if machine["status_name"] != "Ready":
187 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
188 return ret
189
190 if __opts__['test']:
191 ret['result'] = None
Denis Egorenkodecf41b2018-11-07 13:04:18 +0400192 ret['comment'] = 'Partition schema will be changed on {0}'.format(name)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100193 return ret
194
Denis Egorenkodecf41b2018-11-07 13:04:18 +0400195 partitions = __salt__['maasng.list_partitions'](hostname, name)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100196
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200197 # Calculate actual size in bytes from provided data
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100198 for part_name, part in partition_schema.iteritems():
199 size, unit = part["size"][:-1], part["size"][-1]
200 part["calc_size"] = int(size) * SIZE[unit]
201
202 if len(partitions) == len(partition_schema):
203
204 for part_name, part in partition_schema.iteritems():
205 LOG.info('validated {0}'.format(part["calc_size"]))
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200206 LOG.info('validated {0}'.format(
Denis Egorenkodecf41b2018-11-07 13:04:18 +0400207 int(partitions[name+"-"+part_name.split("-")[-1]]["size"])))
208 if part["calc_size"] == int(partitions[name+"-"+part_name.split("-")[-1]]["size"]):
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100209 LOG.info('validated')
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200210 # TODO validate size (size from maas is not same as calculate?)
211 # TODO validate mount
212 # TODO validate fs type
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100213 else:
214 LOG.info('breaking')
215 break
216 return ret
217
218 #DELETE and RECREATE
219 LOG.info('delete')
220 for partition_name, partition in partitions.iteritems():
221 LOG.info(partition)
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200222 # TODO IF LVM create ERROR
223 ret["changes"] = __salt__['maasng.delete_partition_by_id'](
Denis Egorenkodecf41b2018-11-07 13:04:18 +0400224 hostname, name, partition["id"])
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100225
226 LOG.info('recreating')
227 for part_name, part in partition_schema.iteritems():
228 LOG.info("partitition for creation")
229 LOG.info(part)
230 if "mount" not in part:
231 part["mount"] = None
232 if "type" not in part:
233 part["type"] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200234 ret["changes"] = __salt__['maasng.create_partition'](
Denis Egorenkodecf41b2018-11-07 13:04:18 +0400235 hostname, name, part["size"], part["type"], part["mount"])
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100236
237 if "error" in ret["changes"]:
238 ret["result"] = False
239
240 return ret
241
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200242
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100243def volume_group_present(hostname, name, devices=[], partitions=[]):
244 '''
245 Ensure that the disk layout does exist
246
247 :param name: The name of the cloud that should not exist
248 '''
249 ret = {'name': hostname,
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200250 'changes': {},
251 'result': True,
252 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100253
254 machine = __salt__['maasng.get_machine'](hostname)
255 if "error" in machine:
Alexei Lugovoie5b64122018-11-06 12:30:01 +0100256 if 0 in machine["error"]:
257 ret['comment'] = "No such machine {0}".format(hostname)
258 ret['changes'] = machine
259 else:
260 ret['comment'] = "State execution" \
261 "failed for machine {0}".format(hostname)
262 ret['result'] = False
263 ret['changes'] = machine
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100264 return ret
265
266 if machine["status_name"] != "Ready":
267 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
268 return ret
269
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200270 # TODO validation if exists
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100271 vgs = __salt__['maasng.list_volume_groups'](hostname)
272
273 if name in vgs:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200274 # TODO validation for devices and partitions
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100275 return ret
276
277 if __opts__['test']:
278 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200279 ret['comment'] = 'LVM group {0} will be updated on {1}'.format(
280 name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100281 return ret
282
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200283 ret["changes"] = __salt__['maasng.create_volume_group'](
284 hostname, name, devices, partitions)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100285
286 if "error" in ret["changes"]:
287 ret["result"] = False
288
289 return ret
290
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200291
azvyagintsevf3515c82018-06-26 18:59:05 +0300292def volume_present(hostname, name, volume_group_name, size, type=None,
293 mount=None):
294 """
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100295 Ensure that the disk layout does exist
296
297 :param name: The name of the cloud that should not exist
azvyagintsevf3515c82018-06-26 18:59:05 +0300298 """
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100299
300 ret = {'name': hostname,
301 'changes': {},
302 'result': True,
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200303 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100304
305 machine = __salt__['maasng.get_machine'](hostname)
306 if "error" in machine:
Alexei Lugovoie5b64122018-11-06 12:30:01 +0100307 if 0 in machine["error"]:
308 ret['comment'] = "No such machine {0}".format(hostname)
309 ret['changes'] = machine
310 else:
311 ret['comment'] = "State execution failed for machine {0}".format(
312 hostname)
313 ret['result'] = False
314 ret['changes'] = machine
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100315 return ret
316
317 if machine["status_name"] != "Ready":
318 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
319 return ret
320
321 if __opts__['test']:
322 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200323 ret['comment'] = 'LVM volume {0} will be updated on {1}'.format(
324 name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100325
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200326 # TODO validation if exists
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100327
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200328 ret["changes"] = __salt__['maasng.create_volume'](
329 hostname, name, volume_group_name, size, type, mount)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100330
331 return ret
332
333
334def select_boot_disk(hostname, name):
335 '''
336 Select disk that will be used to boot partition
337
338 :param name: The name of disk on machine
339 :param hostname: The hostname of machine
340 '''
341
342 ret = {'name': hostname,
343 'changes': {},
344 'result': True,
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200345 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100346
347 machine = __salt__['maasng.get_machine'](hostname)
348 if "error" in machine:
Alexei Lugovoie5b64122018-11-06 12:30:01 +0100349 if 0 in machine["error"]:
350 ret['comment'] = "No such machine {0}".format(hostname)
351 ret['changes'] = machine
352 else:
353 ret['comment'] = "State execution" \
354 "failed for machine {0}".format(hostname)
355 ret['result'] = False
356 ret['changes'] = machine
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100357 return ret
358
359 if machine["status_name"] != "Ready":
360 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
361 return ret
362
363 if __opts__['test']:
364 ret['result'] = None
azvyagintsevf3515c82018-06-26 18:59:05 +0300365 ret['comment'] = 'LVM volume {0}' \
366 'will be updated on {1}'.format(name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100367
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200368 # TODO disk validation if exists
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100369
370 ret["changes"] = __salt__['maasng.set_boot_disk'](hostname, name)
371
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200372 return ret
373
374
Petr Ruzicka80471852018-07-13 14:08:27 +0200375def vlan_present_in_fabric(name, fabric, vlan, primary_rack, description='', dhcp_on=False, mtu=1500):
azvyagintsevf3515c82018-06-26 18:59:05 +0300376 """
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200377
378 :param name: Name of vlan
379 :param fabric: Name of fabric
azvyagintsevf3515c82018-06-26 18:59:05 +0300380 :param vlan: Vlan id
Petr Ruzicka80471852018-07-13 14:08:27 +0200381 :param mtu: MTU
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200382 :param description: Description of vlan
383 :param dhcp_on: State of dhcp
Pavel Cizinsky864a3292018-05-25 16:24:48 +0200384 :param primary_rack: primary_rack
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200385
azvyagintsevf3515c82018-06-26 18:59:05 +0300386 """
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200387
388 ret = {'name': fabric,
389 'changes': {},
390 'result': True,
391 'comment': 'Module function maasng.update_vlan executed'}
392
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200393 if __opts__['test']:
394 ret['result'] = None
Pavel Cizinsky8f9ba8e2018-09-10 14:31:49 +0200395 ret['comment'] = 'Vlan {0} will be updated for {1}'.format(
396 vlan, fabric)
azvyagintsevf3515c82018-06-26 18:59:05 +0300397 return ret
398 # Check, that vlan already defined
399 _rez = __salt__['maasng.check_vlan_in_fabric'](fabric=fabric,
400 vlan=vlan)
401 if _rez == 'not_exist':
402 changes = __salt__['maasng.create_vlan_in_fabric'](name=name,
403 fabric=fabric,
404 vlan=vlan,
Petr Ruzicka80471852018-07-13 14:08:27 +0200405 mtu=mtu,
azvyagintsevf3515c82018-06-26 18:59:05 +0300406 description=description,
407 primary_rack=primary_rack,
408 dhcp_on=dhcp_on)
Michael Polenchukd25da792018-07-19 18:27:11 +0400409 ret['comment'] = 'Vlan {0} has ' \
azvyagintsevf3515c82018-06-26 18:59:05 +0300410 'been created for {1}'.format(name, fabric)
411 elif _rez == 'update':
412 _id = __salt__['maasng.list_vlans'](fabric)[vlan]['id']
413 changes = __salt__['maasng.create_vlan_in_fabric'](name=name,
414 fabric=fabric,
415 vlan=vlan,
Petr Ruzicka80471852018-07-13 14:08:27 +0200416 mtu=mtu,
azvyagintsevf3515c82018-06-26 18:59:05 +0300417 description=description,
418 primary_rack=primary_rack,
419 dhcp_on=dhcp_on,
420 update=True,
421 vlan_id=_id)
Michael Polenchukd25da792018-07-19 18:27:11 +0400422 ret['comment'] = 'Vlan {0} has been ' \
azvyagintsevf3515c82018-06-26 18:59:05 +0300423 'updated for {1}'.format(name, fabric)
424 ret['changes'] = changes
425
426 if "error" in changes:
427 ret['comment'] = "State execution failed for fabric {0}".format(fabric)
428 ret['result'] = False
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200429 return ret
430
431 return ret
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300432
433
azvyagintseve2e37a12018-11-01 14:45:49 +0200434def boot_source_present(url, keyring_file='', keyring_data='',
435 delete_undefined_sources=False,
436 delete_undefined_sources_except_urls=[]):
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300437 """
438 Process maas boot-sources: link to maas-ephemeral repo
439
440
441 :param url: The URL of the BootSource.
442 :param keyring_file: The path to the keyring file for this BootSource.
443 :param keyring_data: The GPG keyring for this BootSource, base64-encoded data.
azvyagintseve2e37a12018-11-01 14:45:49 +0200444 :param delete_undefined_sources: Delete all boot-sources, except defined in reclass
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300445 """
446 ret = {'name': url,
447 'changes': {},
448 'result': True,
449 'comment': 'boot-source {0} presented'.format(url)}
450
451 if __opts__['test']:
452 ret['result'] = None
453 ret['comment'] = 'boot-source {0} will be updated'.format(url)
azvyagintseve2e37a12018-11-01 14:45:49 +0200454 maas_boot_sources = maasng('get_boot_source')
455 # TODO implement check and update for keyrings!
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300456 if url in maas_boot_sources.keys():
457 ret["result"] = True
458 ret["comment"] = 'boot-source {0} alredy exist'.format(url)
azvyagintseve2e37a12018-11-01 14:45:49 +0200459 else:
460 ret["changes"] = maasng('create_boot_source', url,
461 keyring_filename=keyring_file,
462 keyring_data=keyring_data)
463 if delete_undefined_sources:
464 ret["changes"] = merge2dicts(ret.get('changes', {}),
465 maasng('boot_sources_delete_all_others',
466 except_urls=delete_undefined_sources_except_urls))
467 # Re-import data
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300468 return ret
469
470
471def boot_sources_selections_present(bs_url, os, release, arches="*",
472 subarches="*", labels="*", wait=True):
473 """
azvyagintsevcb54d142018-06-19 16:18:32 +0300474 Process maas boot-sources selection: set of resource configurathions,
475 to be downloaded from boot-source bs_url.
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300476
477 :param bs_url: Boot-source url
azvyagintsevcb54d142018-06-19 16:18:32 +0300478 :param os: The OS (e.g. ubuntu, centos) for which to import
479 resources.Required.
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300480 :param release: The release for which to import resources. Required.
481 :param arches: The architecture list for which to import resources.
482 :param subarches: The subarchitecture list for which to import resources.
483 :param labels: The label lists for which to import resources.
484 :param wait: Initiate import and wait for done.
485
486 """
487 ret = {'name': bs_url,
488 'changes': {},
489 'result': True,
490 'comment': 'boot-source {0} selection present'.format(bs_url)}
491
492 if __opts__['test']:
493 ret['result'] = None
azvyagintsevf3515c82018-06-26 18:59:05 +0300494 ret['comment'] = 'boot-source {0}' \
495 'selection will be updated'.format(bs_url)
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300496
azvyagintseve2e37a12018-11-01 14:45:49 +0200497 maas_boot_sources = maasng('get_boot_source')
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300498 if bs_url not in maas_boot_sources.keys():
499 ret["result"] = False
azvyagintsevf3515c82018-06-26 18:59:05 +0300500 ret["comment"] = 'Requested boot-source' \
501 '{0} not exist! Unable' \
502 'to proceed selection for it'.format(bs_url)
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300503 return ret
504
azvyagintseve2e37a12018-11-01 14:45:49 +0200505 ret = maasng('create_boot_source_selections', bs_url, os, release,
506 arches=arches,
507 subarches=subarches,
508 labels=labels,
509 wait=wait)
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300510 return ret
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200511
512
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300513def iprange_present(name, type_range, start_ip, end_ip, subnet=None,
514 comment=None):
515 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200516
517 :param name: Name of iprange
518 :param type_range: Type of iprange
519 :param start_ip: Start ip of iprange
520 :param end_ip: End ip of iprange
521 :param comment: Comment for specific iprange
522
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300523 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200524
525 ret = {'name': name,
526 'changes': {},
527 'result': True,
528 'comment': 'Module function maasng.iprange_present executed'}
529
azvyagintsevf3515c82018-06-26 18:59:05 +0300530 # Check, that range already defined
531 _rez = __salt__['maasng.get_startip'](start_ip)
532 if 'start_ip' in _rez.keys():
533 if _rez["start_ip"] == start_ip:
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200534 ret['comment'] = 'Iprange {0} already exist.'.format(name)
535 return ret
536
537 if __opts__['test']:
538 ret['result'] = None
azvyagintsevf3515c82018-06-26 18:59:05 +0300539 ret['comment'] = 'Ip range {0} will be ' \
540 'created with start ip: {1} ' \
541 'and end ip: {2} and ' \
542 'type {3}'.format(name, start_ip, end_ip, type_range)
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200543 return ret
544
azvyagintsevf3515c82018-06-26 18:59:05 +0300545 changes = __salt__['maasng.create_iprange'](type_range=type_range,
546 start_ip=start_ip,
Pavel Cizinsky8f9ba8e2018-09-10 14:31:49 +0200547 end_ip=end_ip, subnet=subnet, comment=comment)
azvyagintsevf3515c82018-06-26 18:59:05 +0300548 ret["changes"] = changes
549 if "error" in changes:
550 ret['comment'] = "State execution failed for iprange {0}".format(name)
551 ret['result'] = False
552 return ret
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200553 return ret
554
555
azvyagintsevf3515c82018-06-26 18:59:05 +0300556def subnet_present(cidr, name, fabric, gateway_ip, vlan):
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300557 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200558
559 :param cidr: Cidr for subnet
560 :param name: Name of subnet
561 :param fabric: Name of fabric for subnet
562 :param gateway_ip: gateway_ip
563
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300564 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200565
566 ret = {'name': name,
567 'changes': {},
568 'result': True,
569 'comment': 'Module function maasng.subnet_present executed'}
570
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200571 if __opts__['test']:
572 ret['result'] = None
573 ret['comment'] = 'Subnet {0} will be created for {1}'.format(
574 name, fabric)
575 return ret
azvyagintsevf3515c82018-06-26 18:59:05 +0300576 # Check, that subnet already defined
577 _rez = __salt__['maasng.check_subnet'](cidr, name, fabric, gateway_ip)
578 if _rez == 'not_exist':
579 changes = __salt__['maasng.create_subnet'](cidr=cidr, name=name,
580 fabric=fabric,
581 gateway_ip=gateway_ip,
582 vlan=vlan)
583 ret['comment'] = 'Subnet {0} ' \
584 'has been created for {1}'.format(name, fabric)
585 elif _rez == 'update':
586 _id = __salt__['maasng.list_subnets'](sort_by='cidr')[cidr]['id']
587 changes = __salt__['maasng.create_subnet'](cidr=cidr, name=name,
588 fabric=fabric,
589 gateway_ip=gateway_ip,
590 vlan=vlan, update=True,
591 subnet_id=_id)
592 ret['comment'] = 'Subnet {0} ' \
593 'has been updated for {1}'.format(name, fabric)
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200594
azvyagintsevf3515c82018-06-26 18:59:05 +0300595 if "error" in changes:
596 ret['comment'] = "State execution failed for subnet {0}".format(name)
597 ret['result'] = False
598 ret['changes'] = changes
599 return ret
600
601 return ret
602
603
azvyagintsevf0904ac2018-07-05 18:53:26 +0300604def fabric_present(name, description=None):
azvyagintsevf3515c82018-06-26 18:59:05 +0300605 """
606
607 :param name: Name of fabric
608 :param description: Name of description
609
610 """
611
612 ret = {'name': name,
613 'changes': {},
614 'result': True,
615 'comment': 'Module function maasng.fabric_present executed'}
616
617 if __opts__['test']:
618 ret['result'] = None
azvyagintseva80fdfb2018-07-16 22:34:45 +0300619 ret['comment'] = 'fabric {0} will be updated'.format(name)
azvyagintsevf3515c82018-06-26 18:59:05 +0300620 return ret
621 # All requested subnets
622 _r_subnets = __salt__['config.get']('maas').get('region', {}).get('subnets',
623 {})
624 # Assumed subnet CIDrs, expected to be in requested fabric
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300625 _a_subnets = [_r_subnets[f]['cidr'] for f in _r_subnets.keys() if
azvyagintsevf3515c82018-06-26 18:59:05 +0300626 _r_subnets[f]['fabric'] == name]
627 _rez = __salt__['maasng.check_fabric_guess_with_cidr'](name=name,
628 cidrs=_a_subnets)
629
630 if 'not_exist' in _rez:
631 changes = __salt__['maasng.create_fabric'](name=name,
632 description=description)
633 ret['new'] = 'Fabric {0} has been created'.format(name)
634 elif 'update' in _rez:
635 f_id = _rez['update']
636 changes = __salt__['maasng.create_fabric'](name=name,
637 description=description,
638 update=True, fabric_id=f_id)
639 ret['new'] = 'Fabric {0} has been updated'.format(name)
640 ret['changes'] = changes
641
642 if "error" in changes:
643 ret['comment'] = "State execution failed for fabric {0}".format(fabric)
644 ret['result'] = False
645 return ret
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200646
647 return ret
Pavel Cizinsky8f9ba8e2018-09-10 14:31:49 +0200648
649
650def sshkey_present(name, sshkey):
651 """
652
653 :param name: Name of user
654 :param sshkey: SSH key for MAAS user
655
656 """
657
658 ret = {'name': name,
659 'changes': {},
660 'result': True,
661 'comment': 'Module function maasng.ssshkey_present executed'}
662
663 # Check, that subnet already defined
664 _rez = __salt__['maasng.get_sshkey'](sshkey)
665 if 'key' in _rez.keys():
666 if _rez["key"] == sshkey:
667 ret['comment'] = 'SSH key {0} already exist for user {1}.'.format(
668 sshkey, name)
669 return ret
670
671 if __opts__['test']:
672 ret['result'] = None
673 ret['comment'] = 'SSH key {0} will be add it to MAAS for user {1}'.format(
674 sshkey, name)
675
676 return ret
677
678 changes = __salt__['maasng.add_sshkey'](sshkey=sshkey)
679 ret['comment'] = 'SSH-key {0} ' \
680 'has been added for user {1}'.format(sshkey, name)
681
682 ret['changes'] = changes
683
684 if "error" in changes:
685 ret['comment'] = "State execution failed for sshkey: {0}".format(
686 sshkey)
687 ret['result'] = False
688 ret['changes'] = changes
689 return ret
690
691 return ret