Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1 | import logging |
| 2 | from salt.exceptions import CommandExecutionError, SaltInvocationError |
| 3 | |
| 4 | LOG = logging.getLogger(__name__) |
| 5 | |
| 6 | SIZE = { |
| 7 | "M": 1000000, |
| 8 | "G": 1000000000, |
| 9 | "T": 1000000000000, |
| 10 | } |
| 11 | |
| 12 | RAID = { |
| 13 | 0: "raid-0", |
| 14 | 1: "raid-1", |
| 15 | 5: "raid-5", |
| 16 | 10: "raid-10", |
| 17 | } |
| 18 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 19 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 20 | def __virtual__(): |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 21 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 22 | Load MaaSng module |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 23 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 24 | return 'maasng' |
| 25 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 26 | |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 27 | def 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 | |
| 38 | def merge2dicts(d1, d2): |
| 39 | z = d1.copy() |
| 40 | z.update(d2) |
| 41 | return z |
| 42 | |
| 43 | |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 44 | def 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 47 | ''' |
| 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 Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 59 | 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 66 | 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 74 | ret['comment'] = 'Disk layout will be updated on {0}, this action will delete current layout.'.format( |
| 75 | hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 76 | return ret |
| 77 | |
| 78 | if layout_type == "flat": |
| 79 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 80 | ret["changes"] = __salt__['maasng.update_disk_layout']( |
| 81 | hostname, layout_type, root_size, root_device) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 82 | |
| 83 | elif layout_type == "lvm": |
| 84 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 85 | ret["changes"] = __salt__['maasng.update_disk_layout']( |
| 86 | 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] | 87 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 88 | elif layout_type == "custom": |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 89 | ret["changes"] = __salt__[ |
| 90 | 'maasng.update_disk_layout'](hostname, layout_type) |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 91 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 92 | else: |
| 93 | ret["comment"] = "Not supported layout provided. Choose flat or lvm" |
| 94 | ret['result'] = False |
| 95 | |
| 96 | return ret |
| 97 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 98 | |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 99 | def raid_present(hostname, name, level, devices=[], partitions=[], |
| 100 | partition_schema={}): |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 101 | ''' |
| 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 Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 114 | 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 122 | 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 130 | ret['comment'] = 'Raid {0} will be updated on {1}'.format( |
| 131 | name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 132 | return ret |
| 133 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 134 | # Validate that raid exists |
| 135 | # With correct devices/partition |
| 136 | # OR |
| 137 | # Create raid |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 138 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 139 | ret["changes"] = __salt__['maasng.create_raid']( |
| 140 | hostname=hostname, name=name, level=level, disks=devices, partitions=partitions) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 141 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 142 | # TODO partitions |
| 143 | ret["changes"].update(disk_partition_present( |
| 144 | hostname, name, partition_schema)["changes"]) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 145 | |
| 146 | if "error" in ret["changes"]: |
| 147 | ret["result"] = False |
| 148 | |
| 149 | return ret |
| 150 | |
| 151 | |
Denis Egorenko | decf41b | 2018-11-07 13:04:18 +0400 | [diff] [blame^] | 152 | def disk_partition_present(hostname, name, partition_schema={}): |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 153 | ''' |
| 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 159 | # 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 168 | |
| 169 | ret = {'name': hostname, |
| 170 | 'changes': {}, |
| 171 | 'result': True, |
Denis Egorenko | decf41b | 2018-11-07 13:04:18 +0400 | [diff] [blame^] | 172 | 'comment': 'Disk layout {0} presented'.format(name)} |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 173 | |
| 174 | machine = __salt__['maasng.get_machine'](hostname) |
| 175 | if "error" in machine: |
Alexei Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 176 | 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 184 | 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 Egorenko | decf41b | 2018-11-07 13:04:18 +0400 | [diff] [blame^] | 192 | ret['comment'] = 'Partition schema will be changed on {0}'.format(name) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 193 | return ret |
| 194 | |
Denis Egorenko | decf41b | 2018-11-07 13:04:18 +0400 | [diff] [blame^] | 195 | partitions = __salt__['maasng.list_partitions'](hostname, name) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 196 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 197 | # Calculate actual size in bytes from provided data |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 198 | 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 206 | LOG.info('validated {0}'.format( |
Denis Egorenko | decf41b | 2018-11-07 13:04:18 +0400 | [diff] [blame^] | 207 | int(partitions[name+"-"+part_name.split("-")[-1]]["size"]))) |
| 208 | if part["calc_size"] == int(partitions[name+"-"+part_name.split("-")[-1]]["size"]): |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 209 | LOG.info('validated') |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 210 | # TODO validate size (size from maas is not same as calculate?) |
| 211 | # TODO validate mount |
| 212 | # TODO validate fs type |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 213 | 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 222 | # TODO IF LVM create ERROR |
| 223 | ret["changes"] = __salt__['maasng.delete_partition_by_id']( |
Denis Egorenko | decf41b | 2018-11-07 13:04:18 +0400 | [diff] [blame^] | 224 | hostname, name, partition["id"]) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 225 | |
| 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 234 | ret["changes"] = __salt__['maasng.create_partition']( |
Denis Egorenko | decf41b | 2018-11-07 13:04:18 +0400 | [diff] [blame^] | 235 | hostname, name, part["size"], part["type"], part["mount"]) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 236 | |
| 237 | if "error" in ret["changes"]: |
| 238 | ret["result"] = False |
| 239 | |
| 240 | return ret |
| 241 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 242 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 243 | def 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 250 | 'changes': {}, |
| 251 | 'result': True, |
| 252 | 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)} |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 253 | |
| 254 | machine = __salt__['maasng.get_machine'](hostname) |
| 255 | if "error" in machine: |
Alexei Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 256 | 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 264 | 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 270 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 271 | vgs = __salt__['maasng.list_volume_groups'](hostname) |
| 272 | |
| 273 | if name in vgs: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 274 | # TODO validation for devices and partitions |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 275 | return ret |
| 276 | |
| 277 | if __opts__['test']: |
| 278 | ret['result'] = None |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 279 | ret['comment'] = 'LVM group {0} will be updated on {1}'.format( |
| 280 | name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 281 | return ret |
| 282 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 283 | ret["changes"] = __salt__['maasng.create_volume_group']( |
| 284 | hostname, name, devices, partitions) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 285 | |
| 286 | if "error" in ret["changes"]: |
| 287 | ret["result"] = False |
| 288 | |
| 289 | return ret |
| 290 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 291 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 292 | def volume_present(hostname, name, volume_group_name, size, type=None, |
| 293 | mount=None): |
| 294 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 295 | Ensure that the disk layout does exist |
| 296 | |
| 297 | :param name: The name of the cloud that should not exist |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 298 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 299 | |
| 300 | ret = {'name': hostname, |
| 301 | 'changes': {}, |
| 302 | 'result': True, |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 303 | 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)} |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 304 | |
| 305 | machine = __salt__['maasng.get_machine'](hostname) |
| 306 | if "error" in machine: |
Alexei Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 307 | 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 315 | 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 323 | ret['comment'] = 'LVM volume {0} will be updated on {1}'.format( |
| 324 | name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 325 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 326 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 327 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 328 | ret["changes"] = __salt__['maasng.create_volume']( |
| 329 | hostname, name, volume_group_name, size, type, mount) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 330 | |
| 331 | return ret |
| 332 | |
| 333 | |
| 334 | def 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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 345 | 'comment': 'LVM group {0} presented on {1}'.format(name, hostname)} |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 346 | |
| 347 | machine = __salt__['maasng.get_machine'](hostname) |
| 348 | if "error" in machine: |
Alexei Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 349 | 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 Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 357 | 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 |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 365 | ret['comment'] = 'LVM volume {0}' \ |
| 366 | 'will be updated on {1}'.format(name, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 367 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 368 | # TODO disk validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 369 | |
| 370 | ret["changes"] = __salt__['maasng.set_boot_disk'](hostname, name) |
| 371 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 372 | return ret |
| 373 | |
| 374 | |
Petr Ruzicka | 8047185 | 2018-07-13 14:08:27 +0200 | [diff] [blame] | 375 | def vlan_present_in_fabric(name, fabric, vlan, primary_rack, description='', dhcp_on=False, mtu=1500): |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 376 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 377 | |
| 378 | :param name: Name of vlan |
| 379 | :param fabric: Name of fabric |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 380 | :param vlan: Vlan id |
Petr Ruzicka | 8047185 | 2018-07-13 14:08:27 +0200 | [diff] [blame] | 381 | :param mtu: MTU |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 382 | :param description: Description of vlan |
| 383 | :param dhcp_on: State of dhcp |
Pavel Cizinsky | 864a329 | 2018-05-25 16:24:48 +0200 | [diff] [blame] | 384 | :param primary_rack: primary_rack |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 385 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 386 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 387 | |
| 388 | ret = {'name': fabric, |
| 389 | 'changes': {}, |
| 390 | 'result': True, |
| 391 | 'comment': 'Module function maasng.update_vlan executed'} |
| 392 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 393 | if __opts__['test']: |
| 394 | ret['result'] = None |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 395 | ret['comment'] = 'Vlan {0} will be updated for {1}'.format( |
| 396 | vlan, fabric) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 397 | 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 Ruzicka | 8047185 | 2018-07-13 14:08:27 +0200 | [diff] [blame] | 405 | mtu=mtu, |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 406 | description=description, |
| 407 | primary_rack=primary_rack, |
| 408 | dhcp_on=dhcp_on) |
Michael Polenchuk | d25da79 | 2018-07-19 18:27:11 +0400 | [diff] [blame] | 409 | ret['comment'] = 'Vlan {0} has ' \ |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 410 | '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 Ruzicka | 8047185 | 2018-07-13 14:08:27 +0200 | [diff] [blame] | 416 | mtu=mtu, |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 417 | description=description, |
| 418 | primary_rack=primary_rack, |
| 419 | dhcp_on=dhcp_on, |
| 420 | update=True, |
| 421 | vlan_id=_id) |
Michael Polenchuk | d25da79 | 2018-07-19 18:27:11 +0400 | [diff] [blame] | 422 | ret['comment'] = 'Vlan {0} has been ' \ |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 423 | '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 Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 429 | return ret |
| 430 | |
| 431 | return ret |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 432 | |
| 433 | |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 434 | def boot_source_present(url, keyring_file='', keyring_data='', |
| 435 | delete_undefined_sources=False, |
| 436 | delete_undefined_sources_except_urls=[]): |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 437 | """ |
| 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. |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 444 | :param delete_undefined_sources: Delete all boot-sources, except defined in reclass |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 445 | """ |
| 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) |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 454 | maas_boot_sources = maasng('get_boot_source') |
| 455 | # TODO implement check and update for keyrings! |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 456 | if url in maas_boot_sources.keys(): |
| 457 | ret["result"] = True |
| 458 | ret["comment"] = 'boot-source {0} alredy exist'.format(url) |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 459 | 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 |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 468 | return ret |
| 469 | |
| 470 | |
| 471 | def boot_sources_selections_present(bs_url, os, release, arches="*", |
| 472 | subarches="*", labels="*", wait=True): |
| 473 | """ |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 474 | Process maas boot-sources selection: set of resource configurathions, |
| 475 | to be downloaded from boot-source bs_url. |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 476 | |
| 477 | :param bs_url: Boot-source url |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 478 | :param os: The OS (e.g. ubuntu, centos) for which to import |
| 479 | resources.Required. |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 480 | :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 |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 494 | ret['comment'] = 'boot-source {0}' \ |
| 495 | 'selection will be updated'.format(bs_url) |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 496 | |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 497 | maas_boot_sources = maasng('get_boot_source') |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 498 | if bs_url not in maas_boot_sources.keys(): |
| 499 | ret["result"] = False |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 500 | ret["comment"] = 'Requested boot-source' \ |
| 501 | '{0} not exist! Unable' \ |
| 502 | 'to proceed selection for it'.format(bs_url) |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 503 | return ret |
| 504 | |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 505 | ret = maasng('create_boot_source_selections', bs_url, os, release, |
| 506 | arches=arches, |
| 507 | subarches=subarches, |
| 508 | labels=labels, |
| 509 | wait=wait) |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 510 | return ret |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 511 | |
| 512 | |
azvyagintsev | efb6f5d | 2018-07-10 14:16:19 +0300 | [diff] [blame] | 513 | def iprange_present(name, type_range, start_ip, end_ip, subnet=None, |
| 514 | comment=None): |
| 515 | """ |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 516 | |
| 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 | |
azvyagintsev | efb6f5d | 2018-07-10 14:16:19 +0300 | [diff] [blame] | 523 | """ |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 524 | |
| 525 | ret = {'name': name, |
| 526 | 'changes': {}, |
| 527 | 'result': True, |
| 528 | 'comment': 'Module function maasng.iprange_present executed'} |
| 529 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 530 | # 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 Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 534 | ret['comment'] = 'Iprange {0} already exist.'.format(name) |
| 535 | return ret |
| 536 | |
| 537 | if __opts__['test']: |
| 538 | ret['result'] = None |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 539 | 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 Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 543 | return ret |
| 544 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 545 | changes = __salt__['maasng.create_iprange'](type_range=type_range, |
| 546 | start_ip=start_ip, |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 547 | end_ip=end_ip, subnet=subnet, comment=comment) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 548 | 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 Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 553 | return ret |
| 554 | |
| 555 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 556 | def subnet_present(cidr, name, fabric, gateway_ip, vlan): |
azvyagintsev | efb6f5d | 2018-07-10 14:16:19 +0300 | [diff] [blame] | 557 | """ |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 558 | |
| 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 | |
azvyagintsev | efb6f5d | 2018-07-10 14:16:19 +0300 | [diff] [blame] | 564 | """ |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 565 | |
| 566 | ret = {'name': name, |
| 567 | 'changes': {}, |
| 568 | 'result': True, |
| 569 | 'comment': 'Module function maasng.subnet_present executed'} |
| 570 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 571 | if __opts__['test']: |
| 572 | ret['result'] = None |
| 573 | ret['comment'] = 'Subnet {0} will be created for {1}'.format( |
| 574 | name, fabric) |
| 575 | return ret |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 576 | # 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 Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 594 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 595 | 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 | |
azvyagintsev | f0904ac | 2018-07-05 18:53:26 +0300 | [diff] [blame] | 604 | def fabric_present(name, description=None): |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 605 | """ |
| 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 |
azvyagintsev | a80fdfb | 2018-07-16 22:34:45 +0300 | [diff] [blame] | 619 | ret['comment'] = 'fabric {0} will be updated'.format(name) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 620 | 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 |
azvyagintsev | efb6f5d | 2018-07-10 14:16:19 +0300 | [diff] [blame] | 625 | _a_subnets = [_r_subnets[f]['cidr'] for f in _r_subnets.keys() if |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 626 | _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 Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 646 | |
| 647 | return ret |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 648 | |
| 649 | |
| 650 | def 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 |