blob: 5c686520fe445b1502c1d0ac073adbce46256dcf [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
Ivan Berezovskiy499b2502019-10-07 16:31:32 +0400375def vlan_present_in_fabric(name, fabric, vlan, primary_rack, description='', dhcp_on=False, mtu=1500, relay_vlan=None):
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,
Ivan Berezovskiy499b2502019-10-07 16:31:32 +0400408 dhcp_on=dhcp_on,
409 relay_vlan=relay_vlan)
Michael Polenchukd25da792018-07-19 18:27:11 +0400410 ret['comment'] = 'Vlan {0} has ' \
azvyagintsevf3515c82018-06-26 18:59:05 +0300411 'been created for {1}'.format(name, fabric)
412 elif _rez == 'update':
413 _id = __salt__['maasng.list_vlans'](fabric)[vlan]['id']
414 changes = __salt__['maasng.create_vlan_in_fabric'](name=name,
415 fabric=fabric,
416 vlan=vlan,
Petr Ruzicka80471852018-07-13 14:08:27 +0200417 mtu=mtu,
azvyagintsevf3515c82018-06-26 18:59:05 +0300418 description=description,
419 primary_rack=primary_rack,
420 dhcp_on=dhcp_on,
421 update=True,
Ivan Berezovskiy499b2502019-10-07 16:31:32 +0400422 vlan_id=_id,
423 relay_vlan=relay_vlan)
Michael Polenchukd25da792018-07-19 18:27:11 +0400424 ret['comment'] = 'Vlan {0} has been ' \
azvyagintsevf3515c82018-06-26 18:59:05 +0300425 'updated for {1}'.format(name, fabric)
426 ret['changes'] = changes
427
428 if "error" in changes:
429 ret['comment'] = "State execution failed for fabric {0}".format(fabric)
430 ret['result'] = False
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200431 return ret
432
433 return ret
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300434
435
azvyagintseve2e37a12018-11-01 14:45:49 +0200436def boot_source_present(url, keyring_file='', keyring_data='',
437 delete_undefined_sources=False,
438 delete_undefined_sources_except_urls=[]):
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300439 """
440 Process maas boot-sources: link to maas-ephemeral repo
441
442
443 :param url: The URL of the BootSource.
444 :param keyring_file: The path to the keyring file for this BootSource.
445 :param keyring_data: The GPG keyring for this BootSource, base64-encoded data.
azvyagintseve2e37a12018-11-01 14:45:49 +0200446 :param delete_undefined_sources: Delete all boot-sources, except defined in reclass
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300447 """
448 ret = {'name': url,
449 'changes': {},
450 'result': True,
451 'comment': 'boot-source {0} presented'.format(url)}
452
453 if __opts__['test']:
454 ret['result'] = None
455 ret['comment'] = 'boot-source {0} will be updated'.format(url)
azvyagintseve2e37a12018-11-01 14:45:49 +0200456 maas_boot_sources = maasng('get_boot_source')
457 # TODO implement check and update for keyrings!
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300458 if url in maas_boot_sources.keys():
459 ret["result"] = True
460 ret["comment"] = 'boot-source {0} alredy exist'.format(url)
azvyagintseve2e37a12018-11-01 14:45:49 +0200461 else:
462 ret["changes"] = maasng('create_boot_source', url,
463 keyring_filename=keyring_file,
464 keyring_data=keyring_data)
465 if delete_undefined_sources:
466 ret["changes"] = merge2dicts(ret.get('changes', {}),
467 maasng('boot_sources_delete_all_others',
468 except_urls=delete_undefined_sources_except_urls))
469 # Re-import data
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300470 return ret
471
472
473def boot_sources_selections_present(bs_url, os, release, arches="*",
474 subarches="*", labels="*", wait=True):
475 """
azvyagintsevcb54d142018-06-19 16:18:32 +0300476 Process maas boot-sources selection: set of resource configurathions,
477 to be downloaded from boot-source bs_url.
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300478
479 :param bs_url: Boot-source url
azvyagintsevcb54d142018-06-19 16:18:32 +0300480 :param os: The OS (e.g. ubuntu, centos) for which to import
481 resources.Required.
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300482 :param release: The release for which to import resources. Required.
483 :param arches: The architecture list for which to import resources.
484 :param subarches: The subarchitecture list for which to import resources.
485 :param labels: The label lists for which to import resources.
486 :param wait: Initiate import and wait for done.
487
488 """
489 ret = {'name': bs_url,
490 'changes': {},
491 'result': True,
492 'comment': 'boot-source {0} selection present'.format(bs_url)}
493
494 if __opts__['test']:
495 ret['result'] = None
azvyagintsevf3515c82018-06-26 18:59:05 +0300496 ret['comment'] = 'boot-source {0}' \
497 'selection will be updated'.format(bs_url)
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300498
azvyagintseve2e37a12018-11-01 14:45:49 +0200499 maas_boot_sources = maasng('get_boot_source')
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300500 if bs_url not in maas_boot_sources.keys():
501 ret["result"] = False
azvyagintsevf3515c82018-06-26 18:59:05 +0300502 ret["comment"] = 'Requested boot-source' \
503 '{0} not exist! Unable' \
504 'to proceed selection for it'.format(bs_url)
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300505 return ret
506
azvyagintseve2e37a12018-11-01 14:45:49 +0200507 ret = maasng('create_boot_source_selections', bs_url, os, release,
508 arches=arches,
509 subarches=subarches,
510 labels=labels,
511 wait=wait)
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300512 return ret
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200513
514
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300515def iprange_present(name, type_range, start_ip, end_ip, subnet=None,
516 comment=None):
517 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200518
519 :param name: Name of iprange
520 :param type_range: Type of iprange
521 :param start_ip: Start ip of iprange
522 :param end_ip: End ip of iprange
523 :param comment: Comment for specific iprange
524
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300525 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200526
527 ret = {'name': name,
528 'changes': {},
529 'result': True,
530 'comment': 'Module function maasng.iprange_present executed'}
531
azvyagintsevf3515c82018-06-26 18:59:05 +0300532 # Check, that range already defined
533 _rez = __salt__['maasng.get_startip'](start_ip)
534 if 'start_ip' in _rez.keys():
535 if _rez["start_ip"] == start_ip:
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200536 ret['comment'] = 'Iprange {0} already exist.'.format(name)
537 return ret
538
539 if __opts__['test']:
540 ret['result'] = None
azvyagintsevf3515c82018-06-26 18:59:05 +0300541 ret['comment'] = 'Ip range {0} will be ' \
542 'created with start ip: {1} ' \
543 'and end ip: {2} and ' \
544 'type {3}'.format(name, start_ip, end_ip, type_range)
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200545 return ret
546
azvyagintsevf3515c82018-06-26 18:59:05 +0300547 changes = __salt__['maasng.create_iprange'](type_range=type_range,
548 start_ip=start_ip,
Pavel Cizinsky8f9ba8e2018-09-10 14:31:49 +0200549 end_ip=end_ip, subnet=subnet, comment=comment)
azvyagintsevf3515c82018-06-26 18:59:05 +0300550 ret["changes"] = changes
551 if "error" in changes:
552 ret['comment'] = "State execution failed for iprange {0}".format(name)
553 ret['result'] = False
554 return ret
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200555 return ret
556
557
azvyagintsevf3515c82018-06-26 18:59:05 +0300558def subnet_present(cidr, name, fabric, gateway_ip, vlan):
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300559 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200560
561 :param cidr: Cidr for subnet
562 :param name: Name of subnet
563 :param fabric: Name of fabric for subnet
564 :param gateway_ip: gateway_ip
565
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300566 """
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200567
568 ret = {'name': name,
569 'changes': {},
570 'result': True,
571 'comment': 'Module function maasng.subnet_present executed'}
572
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200573 if __opts__['test']:
574 ret['result'] = None
575 ret['comment'] = 'Subnet {0} will be created for {1}'.format(
576 name, fabric)
577 return ret
azvyagintsevf3515c82018-06-26 18:59:05 +0300578 # Check, that subnet already defined
579 _rez = __salt__['maasng.check_subnet'](cidr, name, fabric, gateway_ip)
580 if _rez == 'not_exist':
581 changes = __salt__['maasng.create_subnet'](cidr=cidr, name=name,
582 fabric=fabric,
583 gateway_ip=gateway_ip,
584 vlan=vlan)
585 ret['comment'] = 'Subnet {0} ' \
586 'has been created for {1}'.format(name, fabric)
587 elif _rez == 'update':
588 _id = __salt__['maasng.list_subnets'](sort_by='cidr')[cidr]['id']
589 changes = __salt__['maasng.create_subnet'](cidr=cidr, name=name,
590 fabric=fabric,
591 gateway_ip=gateway_ip,
592 vlan=vlan, update=True,
593 subnet_id=_id)
594 ret['comment'] = 'Subnet {0} ' \
595 'has been updated for {1}'.format(name, fabric)
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200596
azvyagintsevf3515c82018-06-26 18:59:05 +0300597 if "error" in changes:
598 ret['comment'] = "State execution failed for subnet {0}".format(name)
599 ret['result'] = False
600 ret['changes'] = changes
601 return ret
602
603 return ret
604
605
azvyagintsevf0904ac2018-07-05 18:53:26 +0300606def fabric_present(name, description=None):
azvyagintsevf3515c82018-06-26 18:59:05 +0300607 """
608
609 :param name: Name of fabric
610 :param description: Name of description
611
612 """
613
614 ret = {'name': name,
615 'changes': {},
616 'result': True,
617 'comment': 'Module function maasng.fabric_present executed'}
618
619 if __opts__['test']:
620 ret['result'] = None
azvyagintseva80fdfb2018-07-16 22:34:45 +0300621 ret['comment'] = 'fabric {0} will be updated'.format(name)
azvyagintsevf3515c82018-06-26 18:59:05 +0300622 return ret
623 # All requested subnets
624 _r_subnets = __salt__['config.get']('maas').get('region', {}).get('subnets',
625 {})
626 # Assumed subnet CIDrs, expected to be in requested fabric
azvyagintsevefb6f5d2018-07-10 14:16:19 +0300627 _a_subnets = [_r_subnets[f]['cidr'] for f in _r_subnets.keys() if
azvyagintsevf3515c82018-06-26 18:59:05 +0300628 _r_subnets[f]['fabric'] == name]
629 _rez = __salt__['maasng.check_fabric_guess_with_cidr'](name=name,
630 cidrs=_a_subnets)
631
632 if 'not_exist' in _rez:
633 changes = __salt__['maasng.create_fabric'](name=name,
634 description=description)
635 ret['new'] = 'Fabric {0} has been created'.format(name)
636 elif 'update' in _rez:
637 f_id = _rez['update']
638 changes = __salt__['maasng.create_fabric'](name=name,
639 description=description,
640 update=True, fabric_id=f_id)
641 ret['new'] = 'Fabric {0} has been updated'.format(name)
642 ret['changes'] = changes
643
644 if "error" in changes:
645 ret['comment'] = "State execution failed for fabric {0}".format(fabric)
646 ret['result'] = False
647 return ret
Pavel Cizinsky8dd85b52018-06-18 21:40:13 +0200648
649 return ret
Pavel Cizinsky8f9ba8e2018-09-10 14:31:49 +0200650
651
652def sshkey_present(name, sshkey):
653 """
654
655 :param name: Name of user
656 :param sshkey: SSH key for MAAS user
657
658 """
659
660 ret = {'name': name,
661 'changes': {},
662 'result': True,
663 'comment': 'Module function maasng.ssshkey_present executed'}
664
665 # Check, that subnet already defined
666 _rez = __salt__['maasng.get_sshkey'](sshkey)
667 if 'key' in _rez.keys():
668 if _rez["key"] == sshkey:
669 ret['comment'] = 'SSH key {0} already exist for user {1}.'.format(
670 sshkey, name)
671 return ret
672
673 if __opts__['test']:
674 ret['result'] = None
675 ret['comment'] = 'SSH key {0} will be add it to MAAS for user {1}'.format(
676 sshkey, name)
677
678 return ret
679
680 changes = __salt__['maasng.add_sshkey'](sshkey=sshkey)
681 ret['comment'] = 'SSH-key {0} ' \
682 'has been added for user {1}'.format(sshkey, name)
683
684 ret['changes'] = changes
685
686 if "error" in changes:
687 ret['comment'] = "State execution failed for sshkey: {0}".format(
688 sshkey)
689 ret['result'] = False
690 ret['changes'] = changes
691 return ret
692
693 return ret