blob: 1cc69f76f851c16e5bd35e96bcc74b339db338d2 [file] [log] [blame]
Ondrej Smolab57a23b2018-01-24 11:18:24 +01001
2import logging
3from salt.exceptions import CommandExecutionError, SaltInvocationError
4
5LOG = logging.getLogger(__name__)
6
7SIZE = {
8 "M": 1000000,
9 "G": 1000000000,
10 "T": 1000000000000,
11}
12
13RAID = {
14 0: "raid-0",
15 1: "raid-1",
16 5: "raid-5",
17 10: "raid-10",
18}
19
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020020
Ondrej Smolab57a23b2018-01-24 11:18:24 +010021def __virtual__():
22 '''
23 Load MaaSng module
24 '''
25 return 'maasng'
26
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020027
azvyagintsev3ff2ef12018-06-01 21:30:45 +030028def disk_layout_present(hostname, layout_type, root_size=None, root_device=None,
29 volume_group=None, volume_name=None, volume_size=None,
30 disk={}, **kwargs):
Ondrej Smolab57a23b2018-01-24 11:18:24 +010031 '''
32 Ensure that the disk layout does exist
33
34 :param name: The name of the cloud that should not exist
35 '''
36 ret = {'name': hostname,
37 'changes': {},
38 'result': True,
39 'comment': 'Disk layout "{0}" updated'.format(hostname)}
40
41 machine = __salt__['maasng.get_machine'](hostname)
42 if "error" in machine:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020043 ret['comment'] = "State execution failed for machine {0}".format(
44 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010045 ret['result'] = False
46 ret['changes'] = machine
47 return ret
48
49 if machine["status_name"] != "Ready":
50 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
51 return ret
52
53 if __opts__['test']:
54 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020055 ret['comment'] = 'Disk layout will be updated on {0}, this action will delete current layout.'.format(
56 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010057 return ret
58
59 if layout_type == "flat":
60
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020061 ret["changes"] = __salt__['maasng.update_disk_layout'](
62 hostname, layout_type, root_size, root_device)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010063
64 elif layout_type == "lvm":
65
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020066 ret["changes"] = __salt__['maasng.update_disk_layout'](
67 hostname, layout_type, root_size, root_device, volume_group, volume_name, volume_size)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010068
azvyagintsevbca1f462018-05-25 19:06:46 +030069 elif layout_type == "custom":
70 ret["changes"] = __salt__['maasng.update_disk_layout'](hostname, layout_type)
71
Ondrej Smolab57a23b2018-01-24 11:18:24 +010072 else:
73 ret["comment"] = "Not supported layout provided. Choose flat or lvm"
74 ret['result'] = False
75
76 return ret
77
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020078
azvyagintsev3ff2ef12018-06-01 21:30:45 +030079def raid_present(hostname, name, level, devices=[], partitions=[],
80 partition_schema={}):
Ondrej Smolab57a23b2018-01-24 11:18:24 +010081 '''
82 Ensure that the raid does exist
83
84 :param name: The name of the cloud that should not exist
85 '''
86
87 ret = {'name': name,
88 'changes': {},
89 'result': True,
90 'comment': 'Raid {0} presented on {1}'.format(name, hostname)}
91
92 machine = __salt__['maasng.get_machine'](hostname)
93 if "error" in machine:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +020094 ret['comment'] = "State execution failed for machine {0}".format(
95 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +010096 ret['result'] = False
97 ret['changes'] = machine
98 return ret
99
100 if machine["status_name"] != "Ready":
101 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
102 return ret
103
104 if __opts__['test']:
105 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200106 ret['comment'] = 'Raid {0} will be updated on {1}'.format(
107 name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100108 return ret
109
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200110 # Validate that raid exists
111 # With correct devices/partition
112 # OR
113 # Create raid
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100114
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200115 ret["changes"] = __salt__['maasng.create_raid'](
116 hostname=hostname, name=name, level=level, disks=devices, partitions=partitions)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100117
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200118 # TODO partitions
119 ret["changes"].update(disk_partition_present(
120 hostname, name, partition_schema)["changes"])
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100121
122 if "error" in ret["changes"]:
123 ret["result"] = False
124
125 return ret
126
127
128def disk_partition_present(hostname, disk, partition_schema={}):
129 '''
130 Ensure that the disk has correct partititioning schema
131
132 :param name: The name of the cloud that should not exist
133 '''
134
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200135 # 1. Validate that disk has correct values for size and mount
136 # a. validate count of partitions
137 # b. validate size of partitions
138 # 2. If not delete all partitions on disk and recreate schema
139 # 3. Validate type exists
140 # if should not exits
141 # delete mount and unformat
142 # 4. Validate mount exists
143 # 5. if not enforce umount or mount
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100144
145 ret = {'name': hostname,
146 'changes': {},
147 'result': True,
148 'comment': 'Disk layout {0} presented'.format(disk)}
149
150 machine = __salt__['maasng.get_machine'](hostname)
151 if "error" in machine:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200152 ret['comment'] = "State execution failed for machine {0}".format(
153 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100154 ret['result'] = False
155 ret['changes'] = machine
156 return ret
157
158 if machine["status_name"] != "Ready":
159 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
160 return ret
161
162 if __opts__['test']:
163 ret['result'] = None
164 ret['comment'] = 'Partition schema will be changed on {0}'.format(disk)
165 return ret
166
167 partitions = __salt__['maasng.list_partitions'](hostname, disk)
168
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200169 # Calculate actual size in bytes from provided data
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100170 for part_name, part in partition_schema.iteritems():
171 size, unit = part["size"][:-1], part["size"][-1]
172 part["calc_size"] = int(size) * SIZE[unit]
173
174 if len(partitions) == len(partition_schema):
175
176 for part_name, part in partition_schema.iteritems():
177 LOG.info('validated {0}'.format(part["calc_size"]))
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200178 LOG.info('validated {0}'.format(
179 int(partitions[disk+"-"+part_name.split("-")[-1]]["size"])))
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100180 if part["calc_size"] == int(partitions[disk+"-"+part_name.split("-")[-1]]["size"]):
181 LOG.info('validated')
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200182 # TODO validate size (size from maas is not same as calculate?)
183 # TODO validate mount
184 # TODO validate fs type
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100185 else:
186 LOG.info('breaking')
187 break
188 return ret
189
190 #DELETE and RECREATE
191 LOG.info('delete')
192 for partition_name, partition in partitions.iteritems():
193 LOG.info(partition)
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200194 # TODO IF LVM create ERROR
195 ret["changes"] = __salt__['maasng.delete_partition_by_id'](
196 hostname, disk, partition["id"])
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100197
198 LOG.info('recreating')
199 for part_name, part in partition_schema.iteritems():
200 LOG.info("partitition for creation")
201 LOG.info(part)
202 if "mount" not in part:
203 part["mount"] = None
204 if "type" not in part:
205 part["type"] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200206 ret["changes"] = __salt__['maasng.create_partition'](
207 hostname, disk, part["size"], part["type"], part["mount"])
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100208
209 if "error" in ret["changes"]:
210 ret["result"] = False
211
212 return ret
213
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200214
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100215def volume_group_present(hostname, name, devices=[], partitions=[]):
216 '''
217 Ensure that the disk layout does exist
218
219 :param name: The name of the cloud that should not exist
220 '''
221 ret = {'name': hostname,
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200222 'changes': {},
223 'result': True,
224 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100225
226 machine = __salt__['maasng.get_machine'](hostname)
227 if "error" in machine:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200228 ret['comment'] = "State execution failed for machine {0}".format(
229 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100230 ret['result'] = False
231 ret['changes'] = machine
232 return ret
233
234 if machine["status_name"] != "Ready":
235 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
236 return ret
237
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200238 # TODO validation if exists
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100239 vgs = __salt__['maasng.list_volume_groups'](hostname)
240
241 if name in vgs:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200242 # TODO validation for devices and partitions
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100243 return ret
244
245 if __opts__['test']:
246 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200247 ret['comment'] = 'LVM group {0} will be updated on {1}'.format(
248 name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100249 return ret
250
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200251 ret["changes"] = __salt__['maasng.create_volume_group'](
252 hostname, name, devices, partitions)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100253
254 if "error" in ret["changes"]:
255 ret["result"] = False
256
257 return ret
258
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200259
Ondrej Smola47b56752018-03-06 15:38:27 +0100260def volume_present(hostname, name, volume_group_name, size, type=None, mount=None):
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100261 '''
262 Ensure that the disk layout does exist
263
264 :param name: The name of the cloud that should not exist
265 '''
266
267 ret = {'name': hostname,
268 'changes': {},
269 'result': True,
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200270 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100271
272 machine = __salt__['maasng.get_machine'](hostname)
273 if "error" in machine:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200274 ret['comment'] = "State execution failed for machine {0}".format(
275 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100276 ret['result'] = False
277 ret['changes'] = machine
278 return ret
279
280 if machine["status_name"] != "Ready":
281 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
282 return ret
283
284 if __opts__['test']:
285 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200286 ret['comment'] = 'LVM volume {0} will be updated on {1}'.format(
287 name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100288
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200289 # TODO validation if exists
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100290
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200291 ret["changes"] = __salt__['maasng.create_volume'](
292 hostname, name, volume_group_name, size, type, mount)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100293
294 return ret
295
296
297def select_boot_disk(hostname, name):
298 '''
299 Select disk that will be used to boot partition
300
301 :param name: The name of disk on machine
302 :param hostname: The hostname of machine
303 '''
304
305 ret = {'name': hostname,
306 'changes': {},
307 'result': True,
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200308 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100309
310 machine = __salt__['maasng.get_machine'](hostname)
311 if "error" in machine:
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200312 ret['comment'] = "State execution failed for machine {0}".format(
313 hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100314 ret['result'] = False
315 ret['changes'] = machine
316 return ret
317
318 if machine["status_name"] != "Ready":
319 ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
320 return ret
321
322 if __opts__['test']:
323 ret['result'] = None
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200324 ret['comment'] = 'LVM volume {0} will be updated on {1}'.format(
325 name, hostname)
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100326
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200327 # TODO disk validation if exists
Ondrej Smolab57a23b2018-01-24 11:18:24 +0100328
329 ret["changes"] = __salt__['maasng.set_boot_disk'](hostname, name)
330
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200331 return ret
332
333
Pavel Cizinsky864a3292018-05-25 16:24:48 +0200334def update_vlan(name, fabric, vid, description, primary_rack, dhcp_on=False):
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200335 '''
336
337 :param name: Name of vlan
338 :param fabric: Name of fabric
339 :param vid: Vlan id
340 :param description: Description of vlan
341 :param dhcp_on: State of dhcp
Pavel Cizinsky864a3292018-05-25 16:24:48 +0200342 :param primary_rack: primary_rack
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200343
344 '''
345
346 ret = {'name': fabric,
347 'changes': {},
348 'result': True,
349 'comment': 'Module function maasng.update_vlan executed'}
350
351 ret["changes"] = __salt__['maasng.update_vlan'](
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300352 name=name, fabric=fabric, vid=vid, description=description,
353 primary_rack=primary_rack, dhcp_on=dhcp_on)
Pavel Cizinsky0995e8f2018-05-04 17:10:37 +0200354
355 if "error" in fabric:
356 ret['comment'] = "State execution failed for fabric {0}".format(fabric)
357 ret['result'] = False
358 ret['changes'] = fabric
359 return ret
360
361 if __opts__['test']:
362 ret['result'] = None
363 ret['comment'] = 'Vlan {0} will be updated for {1}'.format(vid, fabric)
364 return ret
365
366 return ret
azvyagintsev3ff2ef12018-06-01 21:30:45 +0300367
368
369def boot_source_present(url, keyring_file='', keyring_data=''):
370 """
371 Process maas boot-sources: link to maas-ephemeral repo
372
373
374 :param url: The URL of the BootSource.
375 :param keyring_file: The path to the keyring file for this BootSource.
376 :param keyring_data: The GPG keyring for this BootSource, base64-encoded data.
377 """
378 ret = {'name': url,
379 'changes': {},
380 'result': True,
381 'comment': 'boot-source {0} presented'.format(url)}
382
383 if __opts__['test']:
384 ret['result'] = None
385 ret['comment'] = 'boot-source {0} will be updated'.format(url)
386
387 maas_boot_sources = __salt__['maasng.get_boot_source']()
388 # TODO imlpement check and update for keyrings!
389 if url in maas_boot_sources.keys():
390 ret["result"] = True
391 ret["comment"] = 'boot-source {0} alredy exist'.format(url)
392 return ret
393 ret["changes"] = __salt__['maasng.create_boot_source'](url,
394 keyring_filename=keyring_file,
395 keyring_data=keyring_data)
396 return ret
397
398
399def boot_sources_selections_present(bs_url, os, release, arches="*",
400 subarches="*", labels="*", wait=True):
401 """
402 Process maas boot-sources selection: set of resource configurathions, to be downloaded from boot-source bs_url.
403
404 :param bs_url: Boot-source url
405 :param os: The OS (e.g. ubuntu, centos) for which to import resources.Required.
406 :param release: The release for which to import resources. Required.
407 :param arches: The architecture list for which to import resources.
408 :param subarches: The subarchitecture list for which to import resources.
409 :param labels: The label lists for which to import resources.
410 :param wait: Initiate import and wait for done.
411
412 """
413 ret = {'name': bs_url,
414 'changes': {},
415 'result': True,
416 'comment': 'boot-source {0} selection present'.format(bs_url)}
417
418 if __opts__['test']:
419 ret['result'] = None
420 ret['comment'] = 'boot-source {0} selection will be updated'.format(
421 bs_url)
422
423 maas_boot_sources = __salt__['maasng.get_boot_source']()
424 if bs_url not in maas_boot_sources.keys():
425 ret["result"] = False
426 ret[
427 "comment"] = 'Requested boot-source {0} not exist! Unable to proceed selection for it'.format(
428 bs_url)
429 return ret
430
431 ret["changes"] = __salt__['maasng.create_boot_source_selections'](bs_url,
432 os,
433 release,
434 arches=arches,
435 subarches=subarches,
436 labels=labels,
437 wait=wait)
438 return ret