Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1 | |
| 2 | import logging |
| 3 | from salt.exceptions import CommandExecutionError, SaltInvocationError |
| 4 | |
| 5 | LOG = logging.getLogger(__name__) |
| 6 | |
| 7 | SIZE = { |
| 8 | "M": 1000000, |
| 9 | "G": 1000000000, |
| 10 | "T": 1000000000000, |
| 11 | } |
| 12 | |
| 13 | RAID = { |
| 14 | 0: "raid-0", |
| 15 | 1: "raid-1", |
| 16 | 5: "raid-5", |
| 17 | 10: "raid-10", |
| 18 | } |
| 19 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 20 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 21 | def __virtual__(): |
| 22 | ''' |
| 23 | Load MaaSng module |
| 24 | ''' |
| 25 | return 'maasng' |
| 26 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 27 | |
| 28 | def disk_layout_present(hostname, layout_type, root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None, disk={}, **kwargs): |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 29 | ''' |
| 30 | Ensure that the disk layout does exist |
| 31 | |
| 32 | :param name: The name of the cloud that should not exist |
| 33 | ''' |
| 34 | ret = {'name': hostname, |
| 35 | 'changes': {}, |
| 36 | 'result': True, |
| 37 | 'comment': 'Disk layout "{0}" updated'.format(hostname)} |
| 38 | |
| 39 | machine = __salt__['maasng.get_machine'](hostname) |
| 40 | if "error" in machine: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 41 | ret['comment'] = "State execution failed for machine {0}".format( |
| 42 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 43 | ret['result'] = False |
| 44 | ret['changes'] = machine |
| 45 | return ret |
| 46 | |
| 47 | if machine["status_name"] != "Ready": |
| 48 | ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname) |
| 49 | return ret |
| 50 | |
| 51 | if __opts__['test']: |
| 52 | ret['result'] = None |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 53 | ret['comment'] = 'Disk layout will be updated on {0}, this action will delete current layout.'.format( |
| 54 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 55 | return ret |
| 56 | |
| 57 | if layout_type == "flat": |
| 58 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 59 | ret["changes"] = __salt__['maasng.update_disk_layout']( |
| 60 | hostname, layout_type, root_size, root_device) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 61 | |
| 62 | elif layout_type == "lvm": |
| 63 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 64 | ret["changes"] = __salt__['maasng.update_disk_layout']( |
| 65 | hostname, layout_type, root_size, root_device, volume_group, volume_name, volume_size) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 66 | |
| 67 | else: |
| 68 | ret["comment"] = "Not supported layout provided. Choose flat or lvm" |
| 69 | ret['result'] = False |
| 70 | |
| 71 | return ret |
| 72 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 73 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 74 | def raid_present(hostname, name, level, devices=[], partitions=[], partition_schema={}): |
| 75 | ''' |
| 76 | Ensure that the raid does exist |
| 77 | |
| 78 | :param name: The name of the cloud that should not exist |
| 79 | ''' |
| 80 | |
| 81 | ret = {'name': name, |
| 82 | 'changes': {}, |
| 83 | 'result': True, |
| 84 | 'comment': 'Raid {0} presented on {1}'.format(name, hostname)} |
| 85 | |
| 86 | machine = __salt__['maasng.get_machine'](hostname) |
| 87 | if "error" in machine: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 88 | ret['comment'] = "State execution failed for machine {0}".format( |
| 89 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 90 | ret['result'] = False |
| 91 | ret['changes'] = machine |
| 92 | return ret |
| 93 | |
| 94 | if machine["status_name"] != "Ready": |
| 95 | ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname) |
| 96 | return ret |
| 97 | |
| 98 | if __opts__['test']: |
| 99 | ret['result'] = None |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 100 | ret['comment'] = 'Raid {0} will be updated on {1}'.format( |
| 101 | name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 102 | return ret |
| 103 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 104 | # Validate that raid exists |
| 105 | # With correct devices/partition |
| 106 | # OR |
| 107 | # Create raid |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 108 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 109 | ret["changes"] = __salt__['maasng.create_raid']( |
| 110 | hostname=hostname, name=name, level=level, disks=devices, partitions=partitions) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 111 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 112 | # TODO partitions |
| 113 | ret["changes"].update(disk_partition_present( |
| 114 | hostname, name, partition_schema)["changes"]) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 115 | |
| 116 | if "error" in ret["changes"]: |
| 117 | ret["result"] = False |
| 118 | |
| 119 | return ret |
| 120 | |
| 121 | |
| 122 | def disk_partition_present(hostname, disk, partition_schema={}): |
| 123 | ''' |
| 124 | Ensure that the disk has correct partititioning schema |
| 125 | |
| 126 | :param name: The name of the cloud that should not exist |
| 127 | ''' |
| 128 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 129 | # 1. Validate that disk has correct values for size and mount |
| 130 | # a. validate count of partitions |
| 131 | # b. validate size of partitions |
| 132 | # 2. If not delete all partitions on disk and recreate schema |
| 133 | # 3. Validate type exists |
| 134 | # if should not exits |
| 135 | # delete mount and unformat |
| 136 | # 4. Validate mount exists |
| 137 | # 5. if not enforce umount or mount |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 138 | |
| 139 | ret = {'name': hostname, |
| 140 | 'changes': {}, |
| 141 | 'result': True, |
| 142 | 'comment': 'Disk layout {0} presented'.format(disk)} |
| 143 | |
| 144 | machine = __salt__['maasng.get_machine'](hostname) |
| 145 | if "error" in machine: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 146 | ret['comment'] = "State execution failed for machine {0}".format( |
| 147 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 148 | ret['result'] = False |
| 149 | ret['changes'] = machine |
| 150 | return ret |
| 151 | |
| 152 | if machine["status_name"] != "Ready": |
| 153 | ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname) |
| 154 | return ret |
| 155 | |
| 156 | if __opts__['test']: |
| 157 | ret['result'] = None |
| 158 | ret['comment'] = 'Partition schema will be changed on {0}'.format(disk) |
| 159 | return ret |
| 160 | |
| 161 | partitions = __salt__['maasng.list_partitions'](hostname, disk) |
| 162 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 163 | # Calculate actual size in bytes from provided data |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 164 | for part_name, part in partition_schema.iteritems(): |
| 165 | size, unit = part["size"][:-1], part["size"][-1] |
| 166 | part["calc_size"] = int(size) * SIZE[unit] |
| 167 | |
| 168 | if len(partitions) == len(partition_schema): |
| 169 | |
| 170 | for part_name, part in partition_schema.iteritems(): |
| 171 | LOG.info('validated {0}'.format(part["calc_size"])) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 172 | LOG.info('validated {0}'.format( |
| 173 | int(partitions[disk+"-"+part_name.split("-")[-1]]["size"]))) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 174 | if part["calc_size"] == int(partitions[disk+"-"+part_name.split("-")[-1]]["size"]): |
| 175 | LOG.info('validated') |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 176 | # TODO validate size (size from maas is not same as calculate?) |
| 177 | # TODO validate mount |
| 178 | # TODO validate fs type |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 179 | else: |
| 180 | LOG.info('breaking') |
| 181 | break |
| 182 | return ret |
| 183 | |
| 184 | #DELETE and RECREATE |
| 185 | LOG.info('delete') |
| 186 | for partition_name, partition in partitions.iteritems(): |
| 187 | LOG.info(partition) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 188 | # TODO IF LVM create ERROR |
| 189 | ret["changes"] = __salt__['maasng.delete_partition_by_id']( |
| 190 | hostname, disk, partition["id"]) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 191 | |
| 192 | LOG.info('recreating') |
| 193 | for part_name, part in partition_schema.iteritems(): |
| 194 | LOG.info("partitition for creation") |
| 195 | LOG.info(part) |
| 196 | if "mount" not in part: |
| 197 | part["mount"] = None |
| 198 | if "type" not in part: |
| 199 | part["type"] = None |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 200 | ret["changes"] = __salt__['maasng.create_partition']( |
| 201 | hostname, disk, part["size"], part["type"], part["mount"]) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 202 | |
| 203 | if "error" in ret["changes"]: |
| 204 | ret["result"] = False |
| 205 | |
| 206 | return ret |
| 207 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 208 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 209 | def volume_group_present(hostname, name, devices=[], partitions=[]): |
| 210 | ''' |
| 211 | Ensure that the disk layout does exist |
| 212 | |
| 213 | :param name: The name of the cloud that should not exist |
| 214 | ''' |
| 215 | ret = {'name': hostname, |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 216 | 'changes': {}, |
| 217 | 'result': True, |
| 218 | 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)} |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 219 | |
| 220 | machine = __salt__['maasng.get_machine'](hostname) |
| 221 | if "error" in machine: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 222 | ret['comment'] = "State execution failed for machine {0}".format( |
| 223 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 224 | ret['result'] = False |
| 225 | ret['changes'] = machine |
| 226 | return ret |
| 227 | |
| 228 | if machine["status_name"] != "Ready": |
| 229 | ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname) |
| 230 | return ret |
| 231 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 232 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 233 | vgs = __salt__['maasng.list_volume_groups'](hostname) |
| 234 | |
| 235 | if name in vgs: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 236 | # TODO validation for devices and partitions |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 237 | return ret |
| 238 | |
| 239 | if __opts__['test']: |
| 240 | ret['result'] = None |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 241 | ret['comment'] = 'LVM group {0} will be updated on {1}'.format( |
| 242 | name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 243 | return ret |
| 244 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 245 | ret["changes"] = __salt__['maasng.create_volume_group']( |
| 246 | hostname, name, devices, partitions) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 247 | |
| 248 | if "error" in ret["changes"]: |
| 249 | ret["result"] = False |
| 250 | |
| 251 | return ret |
| 252 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 253 | |
Ondrej Smola | 47b5675 | 2018-03-06 15:38:27 +0100 | [diff] [blame] | 254 | def volume_present(hostname, name, volume_group_name, size, type=None, mount=None): |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 255 | ''' |
| 256 | Ensure that the disk layout does exist |
| 257 | |
| 258 | :param name: The name of the cloud that should not exist |
| 259 | ''' |
| 260 | |
| 261 | ret = {'name': hostname, |
| 262 | 'changes': {}, |
| 263 | 'result': True, |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 264 | 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)} |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 265 | |
| 266 | machine = __salt__['maasng.get_machine'](hostname) |
| 267 | if "error" in machine: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 268 | ret['comment'] = "State execution failed for machine {0}".format( |
| 269 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 270 | ret['result'] = False |
| 271 | ret['changes'] = machine |
| 272 | return ret |
| 273 | |
| 274 | if machine["status_name"] != "Ready": |
| 275 | ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname) |
| 276 | return ret |
| 277 | |
| 278 | if __opts__['test']: |
| 279 | ret['result'] = None |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 280 | ret['comment'] = 'LVM volume {0} will be updated on {1}'.format( |
| 281 | name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 282 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 283 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 284 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 285 | ret["changes"] = __salt__['maasng.create_volume']( |
| 286 | hostname, name, volume_group_name, size, type, mount) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 287 | |
| 288 | return ret |
| 289 | |
| 290 | |
| 291 | def select_boot_disk(hostname, name): |
| 292 | ''' |
| 293 | Select disk that will be used to boot partition |
| 294 | |
| 295 | :param name: The name of disk on machine |
| 296 | :param hostname: The hostname of machine |
| 297 | ''' |
| 298 | |
| 299 | ret = {'name': hostname, |
| 300 | 'changes': {}, |
| 301 | 'result': True, |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 302 | 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)} |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 303 | |
| 304 | machine = __salt__['maasng.get_machine'](hostname) |
| 305 | if "error" in machine: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 306 | ret['comment'] = "State execution failed for machine {0}".format( |
| 307 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 308 | ret['result'] = False |
| 309 | ret['changes'] = machine |
| 310 | return ret |
| 311 | |
| 312 | if machine["status_name"] != "Ready": |
| 313 | ret['comment'] = 'Machine {0} is not in Ready state.'.format(hostname) |
| 314 | return ret |
| 315 | |
| 316 | if __opts__['test']: |
| 317 | ret['result'] = None |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 318 | ret['comment'] = 'LVM volume {0} will be updated on {1}'.format( |
| 319 | name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 320 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 321 | # TODO disk validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 322 | |
| 323 | ret["changes"] = __salt__['maasng.set_boot_disk'](hostname, name) |
| 324 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 325 | return ret |
| 326 | |
| 327 | |
Pavel Cizinsky | 864a329 | 2018-05-25 16:24:48 +0200 | [diff] [blame^] | 328 | def update_vlan(name, fabric, vid, description, primary_rack, dhcp_on=False): |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 329 | ''' |
| 330 | |
| 331 | :param name: Name of vlan |
| 332 | :param fabric: Name of fabric |
| 333 | :param vid: Vlan id |
| 334 | :param description: Description of vlan |
| 335 | :param dhcp_on: State of dhcp |
Pavel Cizinsky | 864a329 | 2018-05-25 16:24:48 +0200 | [diff] [blame^] | 336 | :param primary_rack: primary_rack |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 337 | |
| 338 | ''' |
| 339 | |
| 340 | ret = {'name': fabric, |
| 341 | 'changes': {}, |
| 342 | 'result': True, |
| 343 | 'comment': 'Module function maasng.update_vlan executed'} |
| 344 | |
| 345 | ret["changes"] = __salt__['maasng.update_vlan']( |
Pavel Cizinsky | 864a329 | 2018-05-25 16:24:48 +0200 | [diff] [blame^] | 346 | name=name, fabric=fabric, vid=vid, description=description, primary_rack=primary_rack, dhcp_on=dhcp_on) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 347 | |
| 348 | if "error" in fabric: |
| 349 | ret['comment'] = "State execution failed for fabric {0}".format(fabric) |
| 350 | ret['result'] = False |
| 351 | ret['changes'] = fabric |
| 352 | return ret |
| 353 | |
| 354 | if __opts__['test']: |
| 355 | ret['result'] = None |
| 356 | ret['comment'] = 'Vlan {0} will be updated for {1}'.format(vid, fabric) |
| 357 | return ret |
| 358 | |
| 359 | return ret |