Setup dhcp by maasng module
Change-Id: I6aaa48fb662ff357307adfeb38a01fbab89e2992
diff --git a/_states/maasng.py b/_states/maasng.py
index 3210e5d..ad30025 100644
--- a/_states/maasng.py
+++ b/_states/maasng.py
@@ -17,13 +17,15 @@
10: "raid-10",
}
+
def __virtual__():
'''
Load MaaSng module
'''
return 'maasng'
-def disk_layout_present(hostname, layout_type, root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None, disk={} , **kwargs):
+
+def disk_layout_present(hostname, layout_type, root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None, disk={}, **kwargs):
'''
Ensure that the disk layout does exist
@@ -36,7 +38,8 @@
machine = __salt__['maasng.get_machine'](hostname)
if "error" in machine:
- ret['comment'] = "State execution failed for machine {0}".format(hostname)
+ ret['comment'] = "State execution failed for machine {0}".format(
+ hostname)
ret['result'] = False
ret['changes'] = machine
return ret
@@ -47,16 +50,19 @@
if __opts__['test']:
ret['result'] = None
- ret['comment'] = 'Disk layout will be updated on {0}, this action will delete current layout.'.format(hostname)
+ ret['comment'] = 'Disk layout will be updated on {0}, this action will delete current layout.'.format(
+ hostname)
return ret
if layout_type == "flat":
- ret["changes"] = __salt__['maasng.update_disk_layout'](hostname, layout_type, root_size, root_device)
+ ret["changes"] = __salt__['maasng.update_disk_layout'](
+ hostname, layout_type, root_size, root_device)
elif layout_type == "lvm":
- ret["changes"] = __salt__['maasng.update_disk_layout'](hostname, layout_type, root_size, root_device, volume_group, volume_name, volume_size)
+ ret["changes"] = __salt__['maasng.update_disk_layout'](
+ hostname, layout_type, root_size, root_device, volume_group, volume_name, volume_size)
else:
ret["comment"] = "Not supported layout provided. Choose flat or lvm"
@@ -64,6 +70,7 @@
return ret
+
def raid_present(hostname, name, level, devices=[], partitions=[], partition_schema={}):
'''
Ensure that the raid does exist
@@ -78,7 +85,8 @@
machine = __salt__['maasng.get_machine'](hostname)
if "error" in machine:
- ret['comment'] = "State execution failed for machine {0}".format(hostname)
+ ret['comment'] = "State execution failed for machine {0}".format(
+ hostname)
ret['result'] = False
ret['changes'] = machine
return ret
@@ -89,18 +97,21 @@
if __opts__['test']:
ret['result'] = None
- ret['comment'] = 'Raid {0} will be updated on {1}'.format(name,hostname)
+ ret['comment'] = 'Raid {0} will be updated on {1}'.format(
+ name, hostname)
return ret
- #Validate that raid exists
- ##With correct devices/partition
- #OR
- ##Create raid
+ # Validate that raid exists
+ # With correct devices/partition
+ # OR
+ # Create raid
- ret["changes"] = __salt__['maasng.create_raid'](hostname=hostname, name=name, level=level, disks=devices, partitions=partitions)
+ ret["changes"] = __salt__['maasng.create_raid'](
+ hostname=hostname, name=name, level=level, disks=devices, partitions=partitions)
- #TODO partitions
- ret["changes"].update(disk_partition_present(hostname, name, partition_schema)["changes"])
+ # TODO partitions
+ ret["changes"].update(disk_partition_present(
+ hostname, name, partition_schema)["changes"])
if "error" in ret["changes"]:
ret["result"] = False
@@ -115,15 +126,15 @@
:param name: The name of the cloud that should not exist
'''
- #1. Validate that disk has correct values for size and mount
- #a. validate count of partitions
- #b. validate size of partitions
- #2. If not delete all partitions on disk and recreate schema
- #3. Validate type exists
- #if should not exits
- #delete mount and unformat
- #4. Validate mount exists
- #5. if not enforce umount or mount
+ # 1. Validate that disk has correct values for size and mount
+ # a. validate count of partitions
+ # b. validate size of partitions
+ # 2. If not delete all partitions on disk and recreate schema
+ # 3. Validate type exists
+ # if should not exits
+ # delete mount and unformat
+ # 4. Validate mount exists
+ # 5. if not enforce umount or mount
ret = {'name': hostname,
'changes': {},
@@ -132,7 +143,8 @@
machine = __salt__['maasng.get_machine'](hostname)
if "error" in machine:
- ret['comment'] = "State execution failed for machine {0}".format(hostname)
+ ret['comment'] = "State execution failed for machine {0}".format(
+ hostname)
ret['result'] = False
ret['changes'] = machine
return ret
@@ -148,7 +160,7 @@
partitions = __salt__['maasng.list_partitions'](hostname, disk)
- ##Calculate actual size in bytes from provided data
+ # Calculate actual size in bytes from provided data
for part_name, part in partition_schema.iteritems():
size, unit = part["size"][:-1], part["size"][-1]
part["calc_size"] = int(size) * SIZE[unit]
@@ -157,12 +169,13 @@
for part_name, part in partition_schema.iteritems():
LOG.info('validated {0}'.format(part["calc_size"]))
- LOG.info('validated {0}'.format(int(partitions[disk+"-"+part_name.split("-")[-1]]["size"])))
+ LOG.info('validated {0}'.format(
+ int(partitions[disk+"-"+part_name.split("-")[-1]]["size"])))
if part["calc_size"] == int(partitions[disk+"-"+part_name.split("-")[-1]]["size"]):
LOG.info('validated')
- #TODO validate size (size from maas is not same as calculate?)
- #TODO validate mount
- #TODO validate fs type
+ # TODO validate size (size from maas is not same as calculate?)
+ # TODO validate mount
+ # TODO validate fs type
else:
LOG.info('breaking')
break
@@ -172,8 +185,9 @@
LOG.info('delete')
for partition_name, partition in partitions.iteritems():
LOG.info(partition)
- ##TODO IF LVM create ERROR
- ret["changes"] = __salt__['maasng.delete_partition_by_id'](hostname, disk, partition["id"])
+ # TODO IF LVM create ERROR
+ ret["changes"] = __salt__['maasng.delete_partition_by_id'](
+ hostname, disk, partition["id"])
LOG.info('recreating')
for part_name, part in partition_schema.iteritems():
@@ -183,13 +197,15 @@
part["mount"] = None
if "type" not in part:
part["type"] = None
- ret["changes"] = __salt__['maasng.create_partition'](hostname, disk, part["size"], part["type"], part["mount"])
+ ret["changes"] = __salt__['maasng.create_partition'](
+ hostname, disk, part["size"], part["type"], part["mount"])
if "error" in ret["changes"]:
ret["result"] = False
return ret
+
def volume_group_present(hostname, name, devices=[], partitions=[]):
'''
Ensure that the disk layout does exist
@@ -197,13 +213,14 @@
:param name: The name of the cloud that should not exist
'''
ret = {'name': hostname,
- 'changes': {},
- 'result': True,
- 'comment': 'LVM group {0} presented on {1}'.format(name,hostname)}
+ 'changes': {},
+ 'result': True,
+ 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
machine = __salt__['maasng.get_machine'](hostname)
if "error" in machine:
- ret['comment'] = "State execution failed for machine {0}".format(hostname)
+ ret['comment'] = "State execution failed for machine {0}".format(
+ hostname)
ret['result'] = False
ret['changes'] = machine
return ret
@@ -212,25 +229,28 @@
ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname)
return ret
- #TODO validation if exists
+ # TODO validation if exists
vgs = __salt__['maasng.list_volume_groups'](hostname)
if name in vgs:
- #TODO validation for devices and partitions
+ # TODO validation for devices and partitions
return ret
if __opts__['test']:
ret['result'] = None
- ret['comment'] = 'LVM group {0} will be updated on {1}'.format(name,hostname)
+ ret['comment'] = 'LVM group {0} will be updated on {1}'.format(
+ name, hostname)
return ret
- ret["changes"] = __salt__['maasng.create_volume_group'](hostname, name, devices, partitions)
+ ret["changes"] = __salt__['maasng.create_volume_group'](
+ hostname, name, devices, partitions)
if "error" in ret["changes"]:
ret["result"] = False
return ret
+
def volume_present(hostname, name, volume_group_name, size, type=None, mount=None):
'''
Ensure that the disk layout does exist
@@ -241,11 +261,12 @@
ret = {'name': hostname,
'changes': {},
'result': True,
- 'comment': 'LVM group {0} presented on {1}'.format(name,hostname)}
+ 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
machine = __salt__['maasng.get_machine'](hostname)
if "error" in machine:
- ret['comment'] = "State execution failed for machine {0}".format(hostname)
+ ret['comment'] = "State execution failed for machine {0}".format(
+ hostname)
ret['result'] = False
ret['changes'] = machine
return ret
@@ -256,11 +277,13 @@
if __opts__['test']:
ret['result'] = None
- ret['comment'] = 'LVM volume {0} will be updated on {1}'.format(name,hostname)
+ ret['comment'] = 'LVM volume {0} will be updated on {1}'.format(
+ name, hostname)
- #TODO validation if exists
+ # TODO validation if exists
- ret["changes"] = __salt__['maasng.create_volume'](hostname, name, volume_group_name, size, type, mount)
+ ret["changes"] = __salt__['maasng.create_volume'](
+ hostname, name, volume_group_name, size, type, mount)
return ret
@@ -276,11 +299,12 @@
ret = {'name': hostname,
'changes': {},
'result': True,
- 'comment': 'LVM group {0} presented on {1}'.format(name,hostname)}
+ 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)}
machine = __salt__['maasng.get_machine'](hostname)
if "error" in machine:
- ret['comment'] = "State execution failed for machine {0}".format(hostname)
+ ret['comment'] = "State execution failed for machine {0}".format(
+ hostname)
ret['result'] = False
ret['changes'] = machine
return ret
@@ -291,10 +315,44 @@
if __opts__['test']:
ret['result'] = None
- ret['comment'] = 'LVM volume {0} will be updated on {1}'.format(name,hostname)
+ ret['comment'] = 'LVM volume {0} will be updated on {1}'.format(
+ name, hostname)
- #TODO disk validation if exists
+ # TODO disk validation if exists
ret["changes"] = __salt__['maasng.set_boot_disk'](hostname, name)
- return ret
\ No newline at end of file
+ return ret
+
+
+def update_vlan(name, fabric, vid, description, dhcp_on=False):
+ '''
+
+ :param name: Name of vlan
+ :param fabric: Name of fabric
+ :param vid: Vlan id
+ :param description: Description of vlan
+ :param dhcp_on: State of dhcp
+
+ '''
+
+ ret = {'name': fabric,
+ 'changes': {},
+ 'result': True,
+ 'comment': 'Module function maasng.update_vlan executed'}
+
+ ret["changes"] = __salt__['maasng.update_vlan'](
+ name=name, fabric=fabric, vid=vid, description=description, dhcp_on=dhcp_on)
+
+ if "error" in fabric:
+ ret['comment'] = "State execution failed for fabric {0}".format(fabric)
+ ret['result'] = False
+ ret['changes'] = fabric
+ return ret
+
+ if __opts__['test']:
+ ret['result'] = None
+ ret['comment'] = 'Vlan {0} will be updated for {1}'.format(vid, fabric)
+ return ret
+
+ return ret