Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Module for handling maas calls. |
| 4 | |
| 5 | :optdepends: pyapi-maas Python adapter |
| 6 | :configuration: This module is not usable until the following are specified |
| 7 | either in a pillar or in the minion's config file:: |
| 8 | |
| 9 | maas.url: 'https://maas.domain.com/' |
| 10 | maas.token: fdsfdsdsdsfa:fsdfae3fassd:fdsfdsfsafasdfsa |
| 11 | |
| 12 | ''' |
| 13 | |
| 14 | from __future__ import absolute_import |
| 15 | |
| 16 | import collections |
| 17 | import copy |
| 18 | import hashlib |
| 19 | import io |
| 20 | import json |
| 21 | import logging |
| 22 | import os.path |
| 23 | import time |
| 24 | import urllib2 |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 25 | # Salt utils |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 26 | from salt.exceptions import CommandExecutionError, SaltInvocationError |
| 27 | |
| 28 | LOG = logging.getLogger(__name__) |
| 29 | |
| 30 | SIZE = { |
| 31 | "M": 1000000, |
| 32 | "G": 1000000000, |
| 33 | "T": 1000000000000, |
| 34 | } |
| 35 | |
| 36 | RAID = { |
| 37 | 0: "raid-0", |
| 38 | 1: "raid-1", |
| 39 | 5: "raid-5", |
| 40 | 10: "raid-10", |
| 41 | } |
| 42 | |
| 43 | # Import third party libs |
| 44 | HAS_MASS = False |
| 45 | try: |
| 46 | from maas_client import MAASClient, MAASDispatcher, MAASOAuth |
| 47 | HAS_MASS = True |
| 48 | except ImportError: |
| 49 | LOG.debug('Missing MaaS client module is Missing. Skipping') |
| 50 | |
| 51 | |
| 52 | def __virtual__(): |
| 53 | ''' |
| 54 | Only load this module if maas-client |
| 55 | is installed on this minion. |
| 56 | ''' |
| 57 | if HAS_MASS: |
| 58 | return 'maasng' |
| 59 | return False |
| 60 | |
| 61 | |
| 62 | APIKEY_FILE = '/var/lib/maas/.maas_credentials' |
| 63 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 64 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 65 | def _format_data(data): |
| 66 | class Lazy: |
| 67 | def __str__(self): |
| 68 | return ' '.join(['{0}={1}'.format(k, v) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 69 | for k, v in data.iteritems()]) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 70 | return Lazy() |
| 71 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 72 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 73 | def _create_maas_client(): |
| 74 | global APIKEY_FILE |
| 75 | try: |
| 76 | api_token = file(APIKEY_FILE).read().splitlines()[-1].strip()\ |
| 77 | .split(':') |
| 78 | except: |
| 79 | LOG.exception('token') |
| 80 | auth = MAASOAuth(*api_token) |
| 81 | api_url = 'http://localhost:5240/MAAS' |
| 82 | dispatcher = MAASDispatcher() |
| 83 | return MAASClient(auth, dispatcher, api_url) |
| 84 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 85 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 86 | def _get_blockdevice_id_by_name(hostname, device): |
| 87 | |
| 88 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 89 | return list_blockdevices(hostname)[device]["id"] |
| 90 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 91 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 92 | def _get_volume_group_id_by_name(hostname, device): |
| 93 | |
| 94 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 95 | return list_volume_groups(hostname)[device]["id"] |
| 96 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 97 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 98 | def _get_partition_id_by_name(hostname, device, partition): |
| 99 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 100 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 101 | return list_partitions(hostname, device)[partition]["id"] |
| 102 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 103 | # MACHINE SECTION |
| 104 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 105 | |
| 106 | def get_machine(hostname): |
| 107 | ''' |
| 108 | Get information aboout specified machine |
| 109 | |
| 110 | CLI Example: |
| 111 | |
| 112 | .. code-block:: bash |
| 113 | |
| 114 | salt-call maasng.get_machine server_hostname |
| 115 | ''' |
| 116 | try: |
| 117 | return list_machines()[hostname] |
| 118 | except KeyError: |
| 119 | return {"error": "Machine not found on MaaS server"} |
| 120 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 121 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 122 | def list_machines(): |
| 123 | ''' |
| 124 | Get list of all machines from maas server |
| 125 | |
| 126 | CLI Example: |
| 127 | |
| 128 | .. code-block:: bash |
| 129 | |
| 130 | salt 'maas-node' maasng.list_machines |
| 131 | ''' |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 132 | machines = {} |
| 133 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 134 | json_res = json.loads(maas.get(u'api/2.0/machines/').read()) |
| 135 | for item in json_res: |
| 136 | machines[item["hostname"]] = item |
| 137 | return machines |
| 138 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 139 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 140 | def create_machine(): |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 141 | # TODO |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 142 | |
| 143 | return False |
| 144 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 145 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 146 | def update_machine(): |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 147 | # TODO |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 148 | |
| 149 | return False |
| 150 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 151 | # MACHINE OPERATIONS |
| 152 | # TODO |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 153 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 154 | # RAID SECTION |
| 155 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 156 | |
| 157 | def create_raid(hostname, name, level, disks=[], partitions=[], **kwargs): |
| 158 | ''' |
| 159 | Create new raid on machine. |
| 160 | |
| 161 | CLI Example: |
| 162 | |
| 163 | .. code-block:: bash |
| 164 | |
| 165 | salt-call maasng.create_raid hostname=kvm03 name=md0 level=1 disks=[vdb,vdc] partitions=[vdd-part1,vde-part1] |
| 166 | ''' |
| 167 | |
| 168 | result = {} |
| 169 | |
| 170 | if len(disks) == 0 and len(partitions) == 0: |
| 171 | result["error"] = "Disks or partitions need to be provided" |
| 172 | |
| 173 | disk_ids = [] |
| 174 | partition_ids = [] |
| 175 | |
| 176 | for disk in disks: |
| 177 | try: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 178 | disk_ids.append(str(_get_blockdevice_id_by_name(hostname, disk))) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 179 | except KeyError: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 180 | result["error"] = "Device {0} does not exists on machine {1}".format( |
| 181 | disk, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 182 | return result |
| 183 | |
| 184 | for partition in partitions: |
| 185 | try: |
| 186 | device = partition.split("-")[0] |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 187 | device_part = list_partitions(hostname, device) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 188 | partition_ids.append(str(device_part[partition]["id"])) |
| 189 | except KeyError: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 190 | result["error"] = "Partition {0} does not exists on machine {1}".format( |
| 191 | partition, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 192 | return result |
| 193 | |
| 194 | data = { |
| 195 | "name": name, |
| 196 | "level": RAID[int(level)], |
| 197 | "block_devices": disk_ids, |
| 198 | "partitions": partition_ids, |
| 199 | } |
| 200 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 201 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 202 | system_id = get_machine(hostname)["system_id"] |
| 203 | LOG.info(system_id) |
| 204 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 205 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 206 | LOG.info(data) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 207 | json_res = json.loads( |
| 208 | maas.post(u"api/2.0/nodes/{0}/raids/".format(system_id), None, **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 209 | LOG.info(json_res) |
| 210 | result["new"] = "Raid {0} created".format(name) |
| 211 | |
| 212 | return result |
| 213 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 214 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 215 | def list_raids(hostname): |
| 216 | ''' |
| 217 | Get list all raids on machine |
| 218 | |
| 219 | CLI Example: |
| 220 | |
| 221 | .. code-block:: bash |
| 222 | |
| 223 | salt-call maasng.list_raids server_hostname |
| 224 | ''' |
| 225 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 226 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 227 | system_id = get_machine(hostname)["system_id"] |
| 228 | LOG.info(system_id) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 229 | # TODO validation |
| 230 | json_res = json.loads( |
| 231 | maas.get(u"api/2.0/nodes/{0}/raids/".format(system_id)).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 232 | LOG.info(json_res) |
| 233 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 234 | # TODO return list of raid devices |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 235 | return True |
| 236 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 237 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 238 | def get_raid(hostname, name): |
| 239 | ''' |
| 240 | Get information about specific raid on machine |
| 241 | |
| 242 | CLI Example: |
| 243 | |
| 244 | .. code-block:: bash |
| 245 | |
| 246 | salt-call maasng.get_raids server_hostname md0 |
| 247 | ''' |
| 248 | |
| 249 | return list_raids(hostname)[name] |
| 250 | |
| 251 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 252 | # BLOCKDEVICES SECTION |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 253 | |
| 254 | def list_blockdevices(hostname): |
| 255 | ''' |
| 256 | Get list of all blockdevices (disks) on machine |
| 257 | |
| 258 | CLI Example: |
| 259 | |
| 260 | .. code-block:: bash |
| 261 | |
| 262 | salt 'maas-node' maasng.list_blockdevices server_hostname |
| 263 | salt-call maasng.list_blockdevices server_hostname |
| 264 | ''' |
| 265 | ret = {} |
| 266 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 267 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 268 | system_id = get_machine(hostname)["system_id"] |
| 269 | LOG.info(system_id) |
| 270 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 271 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 272 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 273 | json_res = json.loads( |
| 274 | maas.get(u"api/2.0/nodes/{0}/blockdevices/".format(system_id)).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 275 | LOG.info(json_res) |
| 276 | for item in json_res: |
| 277 | ret[item["name"]] = item |
| 278 | |
| 279 | return ret |
| 280 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 281 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 282 | def get_blockdevice(hostname, name): |
| 283 | ''' |
| 284 | Get information about blockdevice (disk) on machine |
| 285 | |
| 286 | CLI Example: |
| 287 | |
| 288 | .. code-block:: bash |
| 289 | |
| 290 | salt 'maas-node' maasng.get_blockdevice server_hostname sda |
| 291 | salt-call maasng.get_blockdevice server_hostname sda |
| 292 | ''' |
| 293 | |
| 294 | return list_blockdevices(hostname)[name] |
| 295 | |
| 296 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 297 | # PARTITIONS |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 298 | |
| 299 | def list_partitions(hostname, device): |
| 300 | ''' |
| 301 | Get list of all partitions on specific device located on specific machine |
| 302 | |
| 303 | CLI Example: |
| 304 | |
| 305 | .. code-block:: bash |
| 306 | |
| 307 | salt 'maas-node' maasng.list_partitions server_hostname sda |
| 308 | salt-call maasng.list_partitions server_hostname sda |
| 309 | ''' |
| 310 | ret = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 311 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 312 | system_id = get_machine(hostname)["system_id"] |
| 313 | LOG.info(system_id) |
| 314 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 315 | partitions = get_blockdevice(hostname, device)["partitions"] |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 316 | LOG.info(partitions) |
| 317 | |
| 318 | #json_res = json.loads(maas.get(u"api/2.0/nodes/{0}/blockdevices/{1}/partitions/".format(system_id, device_id)).read()) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 319 | # LOG.info(json_res) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 320 | |
| 321 | if len(device) > 0: |
| 322 | for item in partitions: |
| 323 | name = item["path"].split('/')[-1] |
| 324 | ret[name] = item |
| 325 | |
| 326 | return ret |
| 327 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 328 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 329 | def get_partition(hostname, device, partition): |
| 330 | ''' |
| 331 | Get information about specific parition on device located on machine |
| 332 | |
| 333 | CLI Example: |
| 334 | |
| 335 | .. code-block:: bash |
| 336 | |
| 337 | salt 'maas-node' maasng.get_partition server_hostname disk_name partition |
| 338 | salt-call maasng.get_partition server_hostname disk_name partition |
| 339 | |
| 340 | root_size = size in GB |
| 341 | ''' |
| 342 | |
| 343 | return list_partitions(partition)[name] |
| 344 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 345 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 346 | def create_partition(hostname, disk, size, fs_type=None, mount=None): |
| 347 | ''' |
| 348 | Create new partition on device. |
| 349 | |
| 350 | CLI Example: |
| 351 | |
| 352 | .. code-block:: bash |
| 353 | |
| 354 | salt 'maas-node' maasng.create_partition server_hostname disk_name 10 ext4 "/" |
| 355 | salt-call maasng.create_partition server_hostname disk_name 10 ext4 "/" |
| 356 | ''' |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 357 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 358 | result = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 359 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 360 | system_id = get_machine(hostname)["system_id"] |
| 361 | LOG.info(system_id) |
| 362 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 363 | device_id = _get_blockdevice_id_by_name(hostname, disk) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 364 | LOG.info(device_id) |
| 365 | |
| 366 | value, unit = size[:-1], size[-1] |
| 367 | calc_size = str(int(value) * SIZE[unit]) |
| 368 | LOG.info(calc_size) |
| 369 | |
| 370 | data = { |
| 371 | "size": calc_size |
| 372 | } |
| 373 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 374 | # TODO validation |
| 375 | partition = json.loads(maas.post( |
| 376 | u"api/2.0/nodes/{0}/blockdevices/{1}/partitions/".format(system_id, device_id), None, **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 377 | LOG.info(partition) |
| 378 | result["partition"] = "Partition created on {0}".format(disk) |
| 379 | |
| 380 | if fs_type != None: |
| 381 | data_fs_type = { |
| 382 | "fstype": fs_type |
| 383 | } |
| 384 | partition_id = str(partition["id"]) |
| 385 | LOG.info("Partition id: " + partition_id) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 386 | # TODO validation |
| 387 | json_res = json.loads(maas.post(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 388 | system_id, device_id, partition_id), "format", **data_fs_type).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 389 | LOG.info(json_res) |
| 390 | result["filesystem"] = "Filesystem {0} created".format(fs_type) |
| 391 | |
| 392 | if mount != None: |
| 393 | data = { |
| 394 | "mount_point": mount |
| 395 | } |
| 396 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 397 | # TODO validation |
| 398 | json_res = json.loads(maas.post(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 399 | system_id, device_id, str(partition['id'])), "mount", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 400 | LOG.info(json_res) |
| 401 | result["mount"] = "Mount point {0} created".format(mount) |
| 402 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 403 | return result |
| 404 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 405 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 406 | def delete_partition(hostname, disk, partition_name): |
| 407 | ''' |
| 408 | Delete partition on device. |
| 409 | |
| 410 | CLI Example: |
| 411 | |
| 412 | .. code-block:: bash |
| 413 | |
| 414 | salt 'maas-node' maasng.delete_partition server_hostname disk_name partition_name |
| 415 | salt-call maasng.delete_partition server_hostname disk_name partition_name |
| 416 | |
| 417 | root_size = size in GB |
| 418 | ''' |
| 419 | result = {} |
| 420 | data = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 421 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 422 | system_id = get_machine(hostname)["system_id"] |
| 423 | LOG.info(system_id) |
| 424 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 425 | device_id = _get_blockdevice_id_by_name(hostname, disk) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 426 | LOG.info(device_id) |
| 427 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 428 | partition_id = _get_partition_id_by_name(hostname, disk, partition_name) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 429 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 430 | maas.delete(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 431 | system_id, device_id, partition_id)).read() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 432 | result["new"] = "Partition {0} deleted".format(partition_name) |
| 433 | return result |
| 434 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 435 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 436 | def delete_partition_by_id(hostname, disk, partition_id): |
| 437 | ''' |
| 438 | Delete partition on device. Partition spefified by id of parition |
| 439 | |
| 440 | CLI Example: |
| 441 | |
| 442 | .. code-block:: bash |
| 443 | |
| 444 | salt 'maas-node' maasng.delete_partition_by_id server_hostname disk_name partition_id |
| 445 | salt-call maasng.delete_partition_by_id server_hostname disk_name partition_id |
| 446 | |
| 447 | root_size = size in GB |
| 448 | ''' |
| 449 | result = {} |
| 450 | data = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 451 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 452 | system_id = get_machine(hostname)["system_id"] |
| 453 | LOG.info(system_id) |
| 454 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 455 | device_id = _get_blockdevice_id_by_name(hostname, disk) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 456 | LOG.info(device_id) |
| 457 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 458 | maas.delete(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 459 | system_id, device_id, partition_id)).read() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 460 | result["new"] = "Partition {0} deleted".format(partition_id) |
| 461 | return result |
| 462 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 463 | # CREATE DISK LAYOUT |
| 464 | # TODO |
| 465 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 466 | |
| 467 | def update_disk_layout(hostname, layout, root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None): |
| 468 | ''' |
| 469 | Update disk layout. Flat or LVM layout supported. |
| 470 | |
| 471 | CLI Example: |
| 472 | |
| 473 | .. code-block:: bash |
| 474 | |
| 475 | salt 'maas-node' maasng.update_disk_layout server_hostname lvm root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None |
| 476 | salt-call maasng.update_disk_layout server_hostname lvm root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None |
| 477 | |
| 478 | root_size = size in GB |
| 479 | ''' |
| 480 | result = {} |
| 481 | data = { |
| 482 | "storage_layout": layout, |
| 483 | } |
| 484 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 485 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 486 | system_id = get_machine(hostname)["system_id"] |
| 487 | LOG.info(system_id) |
| 488 | |
| 489 | if root_size != None: |
| 490 | bit_size = str(root_size * 1073741824) |
| 491 | LOG.info(bit_size) |
| 492 | data["root_size"] = bit_size |
| 493 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 494 | if root_device != None: |
| 495 | LOG.info(root_device) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 496 | data["root_device"] = str( |
| 497 | _get_blockdevice_id_by_name(hostname, root_device)) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 498 | |
| 499 | if layout == 'lvm': |
| 500 | if volume_group != None: |
| 501 | LOG.info(volume_group) |
| 502 | data["vg_name"] = volume_group |
| 503 | if volume_name != None: |
| 504 | LOG.info(volume_name) |
| 505 | data["lv_name"] = volume_name |
| 506 | if volume_size != None: |
| 507 | vol_size = str(volume_size * 1073741824) |
| 508 | LOG.info(vol_size) |
| 509 | data["lv_size"] = vol_size |
| 510 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 511 | # TODO validation |
| 512 | json_res = json.loads(maas.post( |
| 513 | u"api/2.0/machines/{0}/".format(system_id), "set_storage_layout", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 514 | LOG.info(json_res) |
| 515 | result["new"] = { |
| 516 | "storage_layout": layout, |
| 517 | } |
| 518 | |
| 519 | return result |
| 520 | |
| 521 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 522 | # LVM |
| 523 | # TODO |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 524 | |
| 525 | def list_volume_groups(hostname): |
| 526 | ''' |
| 527 | Get list of all volume group on machine. |
| 528 | |
| 529 | CLI Example: |
| 530 | |
| 531 | .. code-block:: bash |
| 532 | |
| 533 | salt 'maas-node' maasng.list_volume_groups server_hostname |
| 534 | salt-call maasng.list_volume_groups server_hostname |
| 535 | ''' |
| 536 | volume_groups = {} |
| 537 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 538 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 539 | system_id = get_machine(hostname)["system_id"] |
| 540 | LOG.info(system_id) |
| 541 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 542 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 543 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 544 | json_res = json.loads( |
| 545 | maas.get(u"api/2.0/nodes/{0}/volume-groups/".format(system_id)).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 546 | LOG.info(json_res) |
| 547 | for item in json_res: |
| 548 | volume_groups[item["name"]] = item |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 549 | # return |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 550 | return volume_groups |
| 551 | |
| 552 | |
| 553 | def get_volume_group(hostname, name): |
| 554 | ''' |
| 555 | Get information about specific volume group on machine. |
| 556 | |
| 557 | CLI Example: |
| 558 | |
| 559 | .. code-block:: bash |
| 560 | |
| 561 | salt 'maas-node' maasng.list_blockdevices server_hostname |
| 562 | salt-call maasng.list_blockdevices server_hostname |
| 563 | ''' |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 564 | # TODO validation that exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 565 | return list_volume_groups(hostname)[name] |
| 566 | |
| 567 | |
| 568 | def create_volume_group(hostname, volume_group_name, disks=[], partitions=[]): |
| 569 | ''' |
| 570 | Create new volume group on machine. Disks or partitions needs to be provided. |
| 571 | |
| 572 | CLI Example: |
| 573 | |
| 574 | .. code-block:: bash |
| 575 | |
| 576 | salt 'maas-node' maasng.create_volume_group volume_group_name, disks=[sda,sdb], partitions=[] |
| 577 | salt-call maasng.create_volume_group server_hostname |
| 578 | ''' |
| 579 | result = {} |
| 580 | |
| 581 | data = { |
| 582 | "name": volume_group_name, |
| 583 | } |
| 584 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 585 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 586 | system_id = get_machine(hostname)["system_id"] |
| 587 | LOG.info(system_id) |
| 588 | |
| 589 | disk_ids = [] |
| 590 | partition_ids = [] |
| 591 | |
| 592 | for disk in disks: |
| 593 | p_disk = get_blockdevice(hostname, disk) |
| 594 | if p_disk["partition_table_type"] == None: |
| 595 | disk_ids.append(str(p_disk["id"])) |
| 596 | else: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 597 | result["error"] = "Device {0} on machine {1} cointains partition table".format( |
| 598 | disk, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 599 | return result |
| 600 | |
| 601 | for partition in partitions: |
| 602 | try: |
| 603 | device = partition.split("-")[0] |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 604 | device_part = list_partitions(hostname, device) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 605 | partition_ids.append(str(device_part[partition]["id"])) |
| 606 | except KeyError: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 607 | result["error"] = "Partition {0} does not exists on machine {1}".format( |
| 608 | partition, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 609 | return result |
| 610 | |
| 611 | data["block_devices"] = disk_ids |
| 612 | data["partitions"] = partition_ids |
| 613 | LOG.info(partition_ids) |
| 614 | LOG.info(partitions) |
| 615 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 616 | # TODO validation |
| 617 | json_res = json.loads(maas.post( |
| 618 | u"api/2.0/nodes/{0}/volume-groups/".format(system_id), None, **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 619 | LOG.info(json_res) |
| 620 | result["new"] = "Volume group {0} created".format(json_res["name"]) |
| 621 | |
| 622 | return result |
| 623 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 624 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 625 | def delete_volume_group(hostname, name): |
| 626 | ''' |
| 627 | Delete volume group on machine. |
| 628 | |
| 629 | CLI Example: |
| 630 | |
| 631 | .. code-block:: bash |
| 632 | |
| 633 | salt 'maas-node' maasng.delete_volume_group server_hostname vg0 |
| 634 | salt-call maasng.delete_volume_group server_hostname vg0 |
| 635 | ''' |
| 636 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 637 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 638 | system_id = get_machine(hostname)["system_id"] |
| 639 | LOG.info(system_id) |
| 640 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 641 | # TODO partitions |
| 642 | # for partition in partitions: |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 643 | # temp = disk.split("-") |
| 644 | # disk_ids.append(str(_get_blockdevice_id_by_name(hostname, temp[] partition))) |
| 645 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 646 | # TODO partitions |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 647 | vg_id = name |
| 648 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 649 | # TODO validation |
| 650 | json_res = json.loads(maas.delete( |
| 651 | u"api/2.0/nodes/{0}/volume-group/{1}/".format(system_id, vg_id)).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 652 | LOG.info(json_res) |
| 653 | |
| 654 | return True |
| 655 | |
| 656 | |
| 657 | def create_volume(hostname, volume_name, volume_group, size, fs_type=None, mount=None): |
| 658 | ''' |
| 659 | Create volume on volume group. |
| 660 | |
| 661 | CLI Example: |
| 662 | |
| 663 | .. code-block:: bash |
| 664 | |
| 665 | salt 'maas-node' maasng.create_volume server_hostname volume_name, volume_group, size, fs_type=None, mount=None |
| 666 | salt-call maasng.create_volume server_hostname volume_name, volume_group, size, fs_type=None, mount=None |
| 667 | ''' |
| 668 | |
| 669 | data = { |
| 670 | "name": volume_name, |
| 671 | } |
| 672 | |
| 673 | value, unit = size[:-1], size[-1] |
| 674 | bit_size = str(int(value) * SIZE[unit]) |
| 675 | LOG.info(bit_size) |
| 676 | |
| 677 | data["size"] = bit_size |
| 678 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 679 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 680 | system_id = get_machine(hostname)["system_id"] |
| 681 | LOG.info(system_id) |
| 682 | |
| 683 | volume_group_id = str(_get_volume_group_id_by_name(hostname, volume_group)) |
| 684 | |
| 685 | LOG.info(volume_group_id) |
| 686 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 687 | # TODO validation |
| 688 | json_res = json.loads(maas.post(u"api/2.0/nodes/{0}/volume-group/{1}/".format( |
| 689 | system_id, volume_group_id), "create_logical_volume", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 690 | LOG.info(json_res) |
| 691 | |
| 692 | if fs_type != None or mount != None: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 693 | ret = create_volume_filesystem( |
| 694 | hostname, volume_group + "-" + volume_name, fs_type, mount) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 695 | |
| 696 | return True |
| 697 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 698 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 699 | def create_volume_filesystem(hostname, device, fs_type=None, mount=None): |
| 700 | |
| 701 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 702 | system_id = get_machine(hostname)["system_id"] |
| 703 | |
| 704 | blockdevices_id = _get_blockdevice_id_by_name(hostname, device) |
| 705 | data = {} |
| 706 | if fs_type != None: |
| 707 | data["fstype"] = fs_type |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 708 | # TODO validation |
| 709 | json_res = json.loads(maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 710 | system_id, blockdevices_id), "format", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 711 | LOG.info(json_res) |
| 712 | |
| 713 | if mount != None: |
| 714 | data["mount_point"] = mount |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 715 | # TODO validation |
| 716 | json_res = json.loads(maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 717 | system_id, blockdevices_id), "mount", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 718 | LOG.info(json_res) |
| 719 | |
| 720 | return True |
| 721 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 722 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 723 | def create_partition_filesystem(hostname, partition, fs_type=None, mount=None): |
| 724 | |
| 725 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 726 | system_id = get_machine(hostname)["system_id"] |
| 727 | |
| 728 | blockdevices_id = _get_blockdevice_id_by_name(hostname, device) |
| 729 | data = {} |
| 730 | if fs_type != None: |
| 731 | data["fstype"] = fs_type |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 732 | # TODO validation |
| 733 | json_res = json.loads(maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 734 | system_id, blockdevices_id), "format", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 735 | LOG.info(json_res) |
| 736 | |
| 737 | if mount != None: |
| 738 | data["mount_point"] = mount |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 739 | # TODO validation |
| 740 | json_res = json.loads(maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 741 | system_id, blockdevices_id), "mount", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 742 | LOG.info(json_res) |
| 743 | |
| 744 | return True |
| 745 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 746 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 747 | def set_boot_disk(hostname, name): |
| 748 | ''' |
| 749 | Create volume on volume group. |
| 750 | |
| 751 | CLI Example: |
| 752 | |
| 753 | .. code-block:: bash |
| 754 | |
| 755 | salt 'maas-node' maasng.set_boot_disk server_hostname disk_name |
| 756 | salt-call maasng.set_boot_disk server_hostname disk_name |
| 757 | ''' |
| 758 | data = {} |
| 759 | result = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 760 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 761 | system_id = get_machine(hostname)["system_id"] |
| 762 | blockdevices_id = _get_blockdevice_id_by_name(hostname, name) |
| 763 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 764 | maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 765 | system_id, blockdevices_id), "set_boot_disk", **data).read() |
| 766 | # TODO validation for error response (disk does not exists and node does not exists) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 767 | result["new"] = "Disk {0} was set as bootable".format(name) |
| 768 | |
| 769 | return result |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 770 | |
| 771 | |
| 772 | def list_fabric(): |
| 773 | ''' |
| 774 | Get list of all fabric |
| 775 | |
| 776 | CLI Example: |
| 777 | |
| 778 | .. code-block:: bash |
| 779 | |
| 780 | salt 'maas-node' maasng.list_fabric |
| 781 | ''' |
| 782 | fabrics = {} |
| 783 | maas = _create_maas_client() |
| 784 | json_res = json.loads(maas.get(u'api/2.0/fabrics/').read()) |
| 785 | LOG.info(json_res) |
| 786 | for item in json_res: |
| 787 | fabrics[item["name"]] = item |
| 788 | return fabrics |
| 789 | |
| 790 | |
| 791 | def create_fabric(name): |
| 792 | ''' |
| 793 | Create new fabric. |
| 794 | |
| 795 | CLI Example: |
| 796 | |
| 797 | .. code-block:: bash |
| 798 | |
| 799 | salt 'maas-node' maasng.create_fabric |
| 800 | ''' |
| 801 | result = {} |
| 802 | data = { |
| 803 | "name": name, |
| 804 | "description": '', |
| 805 | "class_type": '', |
| 806 | |
| 807 | } |
| 808 | |
| 809 | maas = _create_maas_client() |
| 810 | json_res = json.loads(maas.post(u"api/2.0/fabrics/", None, **data).read()) |
| 811 | LOG.info(json_res) |
| 812 | result["new"] = "Fabrics {0} created".format(json_res["name"]) |
| 813 | return result |
| 814 | |
| 815 | |
| 816 | def list_subnet(): |
| 817 | ''' |
| 818 | Get list of all subnets |
| 819 | |
| 820 | CLI Example: |
| 821 | |
| 822 | .. code-block:: bash |
| 823 | |
| 824 | salt 'maas-node' maasng.list_subnet |
| 825 | ''' |
| 826 | subnets = {} |
| 827 | maas = _create_maas_client() |
| 828 | json_res = json.loads(maas.get(u'api/2.0/subnets/').read()) |
| 829 | LOG.info(json_res) |
| 830 | for item in json_res: |
| 831 | subnets[item["name"]] = item |
| 832 | return subnets |
| 833 | |
| 834 | |
| 835 | def list_vlans(fabric): |
| 836 | ''' |
| 837 | Get list of all vlans for specific fabric |
| 838 | |
| 839 | CLI Example: |
| 840 | |
| 841 | .. code-block:: bash |
| 842 | |
| 843 | salt 'maas-node' maasng.list_vlans |
| 844 | ''' |
| 845 | vlans = {} |
| 846 | maas = _create_maas_client() |
| 847 | fabric_id = get_fabric(fabric) |
| 848 | |
| 849 | json_res = json.loads( |
| 850 | maas.get(u'api/2.0/fabrics/{0}/vlans/'.format(fabric_id)).read()) |
| 851 | LOG.info(json_res) |
| 852 | for item in json_res: |
| 853 | vlans[item["name"]] = item |
| 854 | return vlans |
| 855 | |
| 856 | |
| 857 | def get_fabric(fabric): |
| 858 | ''' |
| 859 | Get id for specific fabric |
| 860 | |
| 861 | CLI Example: |
| 862 | |
| 863 | .. code-block:: bash |
| 864 | |
| 865 | salt-call maasng.get_fabric fabric_name |
| 866 | ''' |
| 867 | try: |
| 868 | return list_fabric()[fabric]['id'] |
| 869 | except KeyError: |
| 870 | return {"error": "Frabic not found on MaaS server"} |
| 871 | |
| 872 | |
Pavel Cizinsky | 864a329 | 2018-05-25 16:24:48 +0200 | [diff] [blame^] | 873 | def update_vlan(name, fabric, vid, description, primary_rack, dhcp_on=False): |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 874 | ''' |
| 875 | Update vlan |
| 876 | |
| 877 | CLI Example: |
| 878 | |
| 879 | .. code-block:: bash |
| 880 | |
| 881 | salt 'maas-node' maasng.update_vlan name, fabric, vid, description, dhcp_on |
| 882 | ''' |
| 883 | result = {} |
| 884 | |
| 885 | data = { |
| 886 | "name": name, |
| 887 | "dhcp_on": str(dhcp_on), |
| 888 | "description": description, |
Pavel Cizinsky | 864a329 | 2018-05-25 16:24:48 +0200 | [diff] [blame^] | 889 | "primary_rack": primary_rack, |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 890 | } |
| 891 | maas = _create_maas_client() |
| 892 | fabric_id = get_fabric(fabric) |
| 893 | |
| 894 | json_res = json.loads(maas.put( |
| 895 | u'api/2.0/fabrics/{0}/vlans/{1}/'.format(fabric_id, vid), **data).read()) |
| 896 | print(json_res) |
| 897 | result["new"] = "Vlan {0} was updated".format(json_res["name"]) |
| 898 | |
| 899 | return result |