Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 2 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 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 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 12 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 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 |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 22 | import time |
| 23 | import urllib2 |
Dzmitry Stremkouski | d95bd2e | 2018-12-03 17:35:46 +0100 | [diff] [blame^] | 24 | import netaddr |
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__(): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 53 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 54 | Only load this module if maas-client |
| 55 | is installed on this minion. |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 56 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 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 | |
azvyagintsev | 5894707 | 2018-06-29 12:09:48 +0300 | [diff] [blame] | 73 | def _create_maas_client(api_url=None): |
| 74 | if not api_url: |
| 75 | api_url = 'http://localhost:5240/MAAS' |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 76 | global APIKEY_FILE |
| 77 | try: |
| 78 | api_token = file(APIKEY_FILE).read().splitlines()[-1].strip()\ |
| 79 | .split(':') |
| 80 | except: |
| 81 | LOG.exception('token') |
| 82 | auth = MAASOAuth(*api_token) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 83 | dispatcher = MAASDispatcher() |
| 84 | return MAASClient(auth, dispatcher, api_url) |
| 85 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 86 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 87 | def _get_blockdevice_id_by_name(hostname, device): |
| 88 | |
| 89 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 90 | return list_blockdevices(hostname)[device]["id"] |
| 91 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 92 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 93 | def _get_volume_group_id_by_name(hostname, device): |
| 94 | |
| 95 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 96 | return list_volume_groups(hostname)[device]["id"] |
| 97 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 98 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 99 | def _get_volume_id_by_name(hostname, volume_name, volume_group, maas_volname=True): |
| 100 | |
| 101 | if not maas_volname: |
| 102 | # MAAS-like name |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 103 | volume_name = str("%s-%s" % (volume_group, volume_name)) |
| 104 | # TODO validation |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 105 | return get_volumes(hostname, volume_group)[volume_name]["id"] |
| 106 | |
| 107 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 108 | def _get_partition_id_by_name(hostname, device, partition): |
| 109 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 110 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 111 | return list_partitions(hostname, device)[partition]["id"] |
| 112 | |
Dzmitry Stremkouski | d95bd2e | 2018-12-03 17:35:46 +0100 | [diff] [blame^] | 113 | def is_valid_ipv4(address): |
| 114 | """Verify that address represents a valid IPv4 address. |
| 115 | :param address: Value to verify |
| 116 | :type address: string |
| 117 | :returns: bool |
| 118 | .. versionadded:: 1.1 |
| 119 | """ |
| 120 | try: |
| 121 | return netaddr.valid_ipv4(address) |
| 122 | except netaddr.AddrFormatError: |
| 123 | return False |
| 124 | |
| 125 | def is_valid_ipv6(address): |
| 126 | """Verify that address represents a valid IPv6 address. |
| 127 | :param address: Value to verify |
| 128 | :type address: string |
| 129 | :returns: bool |
| 130 | .. versionadded:: 1.1 |
| 131 | """ |
| 132 | if not address: |
| 133 | return False |
| 134 | |
| 135 | parts = address.rsplit("%", 1) |
| 136 | address = parts[0] |
| 137 | scope = parts[1] if len(parts) > 1 else None |
| 138 | if scope is not None and (len(scope) < 1 or len(scope) > 15): |
| 139 | return False |
| 140 | |
| 141 | try: |
| 142 | return netaddr.valid_ipv6(address, netaddr.core.INET_PTON) |
| 143 | except netaddr.AddrFormatError: |
| 144 | return False |
| 145 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 146 | # MACHINE SECTION |
| 147 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 148 | |
| 149 | def get_machine(hostname): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 150 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 151 | Get information aboout specified machine |
| 152 | |
| 153 | CLI Example: |
| 154 | |
| 155 | .. code-block:: bash |
| 156 | |
| 157 | salt-call maasng.get_machine server_hostname |
Alexei Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 158 | |
| 159 | Error codes: |
| 160 | 0 : Machine not found |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 161 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 162 | try: |
| 163 | return list_machines()[hostname] |
| 164 | except KeyError: |
Alexei Lugovoi | e5b6412 | 2018-11-06 12:30:01 +0100 | [diff] [blame] | 165 | return {"error": |
| 166 | { 0: "Machine not found" } |
| 167 | } |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 168 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 169 | |
Alexandru Avadanii | 48c51f4 | 2018-09-22 20:14:10 +0200 | [diff] [blame] | 170 | def list_machines(status_filter=None): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 171 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 172 | Get list of all machines from maas server |
| 173 | |
| 174 | CLI Example: |
| 175 | |
| 176 | .. code-block:: bash |
| 177 | |
| 178 | salt 'maas-node' maasng.list_machines |
Alexandru Avadanii | 48c51f4 | 2018-09-22 20:14:10 +0200 | [diff] [blame] | 179 | salt 'maas-node' maasng.list_machines status_filter=[Deployed,Ready] |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 180 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 181 | machines = {} |
| 182 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 183 | json_res = json.loads(maas.get(u'api/2.0/machines/').read()) |
| 184 | for item in json_res: |
Alexandru Avadanii | 48c51f4 | 2018-09-22 20:14:10 +0200 | [diff] [blame] | 185 | if not status_filter or item['status_name'] in status_filter: |
| 186 | machines[item["hostname"]] = item |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 187 | return machines |
| 188 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 189 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 190 | def create_machine(): |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 191 | # TODO |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 192 | |
| 193 | return False |
| 194 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 195 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 196 | def update_machine(): |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 197 | # TODO |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 198 | |
| 199 | return False |
| 200 | |
Alexandru Avadanii | 48c51f4 | 2018-09-22 20:14:10 +0200 | [diff] [blame] | 201 | def delete_machine(hostname): |
| 202 | """ |
| 203 | Delete specified machine |
| 204 | |
| 205 | CLI Example: |
| 206 | |
| 207 | .. code-block:: bash |
| 208 | |
| 209 | salt 'maas-node' maasng.delete_machine server_hostname |
| 210 | salt-call maasng.delete_machine server_hostname |
| 211 | """ |
| 212 | result = {} |
| 213 | maas = _create_maas_client() |
| 214 | system_id = get_machine(hostname)["system_id"] |
| 215 | LOG.debug('delete_machine: {}'.format(system_id)) |
| 216 | maas.delete( |
| 217 | u"api/2.0/machines/{0}/".format(system_id)).read() |
| 218 | |
| 219 | result["new"] = "Machine {0} deleted".format(hostname) |
| 220 | return result |
| 221 | |
Dzmitry Stremkouski | d95bd2e | 2018-12-03 17:35:46 +0100 | [diff] [blame^] | 222 | def machine_power_state(hostname): |
| 223 | """ |
| 224 | Query the power state of a node. |
| 225 | |
| 226 | :param hostname: Node hostname |
| 227 | |
| 228 | CLI Example: |
| 229 | |
| 230 | .. code-block:: bash |
| 231 | |
| 232 | salt 'maas-node' maasng.machine_power_state kvm06 |
| 233 | |
| 234 | """ |
| 235 | result = {} |
| 236 | maas = _create_maas_client() |
| 237 | system_id = get_machine(hostname)["system_id"] |
| 238 | LOG.debug('action_machine: {}'.format(system_id)) |
| 239 | |
| 240 | # TODO validation |
| 241 | json_res = json.loads(maas.get( |
| 242 | u"api/2.0/machines/{0}/".format(system_id), "query_power_state").read()) |
| 243 | LOG.info(json_res) |
| 244 | |
| 245 | return json_res |
| 246 | |
Alexandru Avadanii | 48c51f4 | 2018-09-22 20:14:10 +0200 | [diff] [blame] | 247 | def action_machine(hostname, action, comment=None): |
| 248 | """ |
| 249 | Send simple action (e.g. mark_broken, mark_fixed) to machine. |
| 250 | |
| 251 | :param action: Action to send for machine (one of MaaS' op codes) |
| 252 | :param comment: Optional comment for the event log. |
| 253 | |
| 254 | CLI Example: |
| 255 | |
| 256 | .. code-block:: bash |
| 257 | |
| 258 | salt 'maas-node' maasng.action_machine server_hostname mark_broken comment='dead' |
| 259 | """ |
| 260 | result = {} |
| 261 | data = {} |
| 262 | maas = _create_maas_client() |
| 263 | system_id = get_machine(hostname)["system_id"] |
| 264 | LOG.debug('action_machine: {}'.format(system_id)) |
| 265 | |
| 266 | # TODO validation |
| 267 | if comment: |
| 268 | data["comment"] = comment |
| 269 | json_res = json.loads(maas.post( |
| 270 | u"api/2.0/machines/{0}/".format(system_id), action, **data).read()) |
| 271 | LOG.info(json_res) |
| 272 | result["new"] = "Machine {0} action {1} executed".format(hostname, action) |
| 273 | |
| 274 | return result |
| 275 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 276 | # END MACHINE SECTION |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 277 | # RAID SECTION |
| 278 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 279 | |
| 280 | def create_raid(hostname, name, level, disks=[], partitions=[], **kwargs): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 281 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 282 | Create new raid on machine. |
| 283 | |
| 284 | CLI Example: |
| 285 | |
| 286 | .. code-block:: bash |
| 287 | |
| 288 | salt-call maasng.create_raid hostname=kvm03 name=md0 level=1 disks=[vdb,vdc] partitions=[vdd-part1,vde-part1] |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 289 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 290 | |
| 291 | result = {} |
| 292 | |
| 293 | if len(disks) == 0 and len(partitions) == 0: |
| 294 | result["error"] = "Disks or partitions need to be provided" |
| 295 | |
| 296 | disk_ids = [] |
| 297 | partition_ids = [] |
| 298 | |
| 299 | for disk in disks: |
| 300 | try: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 301 | disk_ids.append(str(_get_blockdevice_id_by_name(hostname, disk))) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 302 | except KeyError: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 303 | result["error"] = "Device {0} does not exists on machine {1}".format( |
| 304 | disk, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 305 | return result |
| 306 | |
| 307 | for partition in partitions: |
| 308 | try: |
| 309 | device = partition.split("-")[0] |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 310 | device_part = list_partitions(hostname, device) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 311 | partition_ids.append(str(device_part[partition]["id"])) |
| 312 | except KeyError: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 313 | result["error"] = "Partition {0} does not exists on machine {1}".format( |
| 314 | partition, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 315 | return result |
| 316 | |
| 317 | data = { |
| 318 | "name": name, |
| 319 | "level": RAID[int(level)], |
| 320 | "block_devices": disk_ids, |
| 321 | "partitions": partition_ids, |
| 322 | } |
| 323 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 324 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 325 | system_id = get_machine(hostname)["system_id"] |
| 326 | LOG.info(system_id) |
| 327 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 328 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 329 | LOG.info(data) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 330 | json_res = json.loads( |
| 331 | 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] | 332 | LOG.info(json_res) |
| 333 | result["new"] = "Raid {0} created".format(name) |
| 334 | |
| 335 | return result |
| 336 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 337 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 338 | def list_raids(hostname): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 339 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 340 | Get list all raids on machine |
| 341 | |
| 342 | CLI Example: |
| 343 | |
| 344 | .. code-block:: bash |
| 345 | |
| 346 | salt-call maasng.list_raids server_hostname |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 347 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 348 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 349 | raids = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 350 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 351 | system_id = get_machine(hostname)["system_id"] |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 352 | # TODO validation |
| 353 | json_res = json.loads( |
| 354 | maas.get(u"api/2.0/nodes/{0}/raids/".format(system_id)).read()) |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 355 | LOG.debug('list_raids:{} {}'.format(system_id, json_res)) |
| 356 | for item in json_res: |
| 357 | raids[item["name"]] = item |
| 358 | return raids |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 359 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 360 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 361 | def get_raid(hostname, name): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 362 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 363 | Get information about specific raid on machine |
| 364 | |
| 365 | CLI Example: |
| 366 | |
| 367 | .. code-block:: bash |
| 368 | |
| 369 | salt-call maasng.get_raids server_hostname md0 |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 370 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 371 | |
| 372 | return list_raids(hostname)[name] |
| 373 | |
| 374 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 375 | def _get_raid_id_by_name(hostname, raid_name): |
| 376 | return get_raid(hostname, raid_name)['id'] |
| 377 | |
| 378 | |
| 379 | def delete_raid(hostname, raid_name): |
| 380 | """ |
| 381 | Delete RAID on a machine. |
| 382 | |
| 383 | CLI Example: |
| 384 | |
| 385 | .. code-block:: bash |
| 386 | |
| 387 | salt 'maas-node' maasng.delete_raid server_hostname raid_name |
| 388 | salt-call maasng.delete_raid server_hostname raid_name |
| 389 | """ |
| 390 | result = {} |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 391 | maas = _create_maas_client() |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 392 | system_id = get_machine(hostname)["system_id"] |
| 393 | raid_id = _get_raid_id_by_name(hostname, raid_name) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 394 | LOG.debug('delete_raid: {} {}'.format(system_id, raid_id)) |
| 395 | maas.delete( |
| 396 | u"api/2.0/nodes/{0}/raid/{1}/".format(system_id, raid_id)).read() |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 397 | |
| 398 | result["new"] = "Raid {0} deleted".format(raid_name) |
| 399 | return result |
| 400 | |
| 401 | # END RAID SECTION |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 402 | # BLOCKDEVICES SECTION |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 403 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 404 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 405 | def list_blockdevices(hostname): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 406 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 407 | Get list of all blockdevices (disks) on machine |
| 408 | |
| 409 | CLI Example: |
| 410 | |
| 411 | .. code-block:: bash |
| 412 | |
| 413 | salt 'maas-node' maasng.list_blockdevices server_hostname |
| 414 | salt-call maasng.list_blockdevices server_hostname |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 415 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 416 | ret = {} |
| 417 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 418 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 419 | system_id = get_machine(hostname)["system_id"] |
| 420 | LOG.info(system_id) |
| 421 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 422 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 423 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 424 | json_res = json.loads( |
| 425 | 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] | 426 | LOG.info(json_res) |
| 427 | for item in json_res: |
| 428 | ret[item["name"]] = item |
| 429 | |
| 430 | return ret |
| 431 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 432 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 433 | def get_blockdevice(hostname, name): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 434 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 435 | Get information about blockdevice (disk) on machine |
| 436 | |
| 437 | CLI Example: |
| 438 | |
| 439 | .. code-block:: bash |
| 440 | |
| 441 | salt 'maas-node' maasng.get_blockdevice server_hostname sda |
| 442 | salt-call maasng.get_blockdevice server_hostname sda |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 443 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 444 | |
| 445 | return list_blockdevices(hostname)[name] |
| 446 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 447 | # END BLOCKDEVICES SECTION |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 448 | # PARTITIONS |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 449 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 450 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 451 | def list_partitions(hostname, device): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 452 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 453 | Get list of all partitions on specific device located on specific machine |
| 454 | |
| 455 | CLI Example: |
| 456 | |
| 457 | .. code-block:: bash |
| 458 | |
| 459 | salt 'maas-node' maasng.list_partitions server_hostname sda |
| 460 | salt-call maasng.list_partitions server_hostname sda |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 461 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 462 | ret = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 463 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 464 | system_id = get_machine(hostname)["system_id"] |
| 465 | LOG.info(system_id) |
| 466 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 467 | partitions = get_blockdevice(hostname, device)["partitions"] |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 468 | LOG.info(partitions) |
| 469 | |
| 470 | #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] | 471 | # LOG.info(json_res) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 472 | |
| 473 | if len(device) > 0: |
| 474 | for item in partitions: |
| 475 | name = item["path"].split('/')[-1] |
| 476 | ret[name] = item |
| 477 | |
| 478 | return ret |
| 479 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 480 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 481 | def get_partition(hostname, device, partition): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 482 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 483 | Get information about specific parition on device located on machine |
| 484 | |
| 485 | CLI Example: |
| 486 | |
| 487 | .. code-block:: bash |
| 488 | |
| 489 | salt 'maas-node' maasng.get_partition server_hostname disk_name partition |
| 490 | salt-call maasng.get_partition server_hostname disk_name partition |
| 491 | |
| 492 | root_size = size in GB |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 493 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 494 | |
| 495 | return list_partitions(partition)[name] |
| 496 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 497 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 498 | def create_partition(hostname, disk, size, fs_type=None, mount=None): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 499 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 500 | Create new partition on device. |
| 501 | |
| 502 | CLI Example: |
| 503 | |
| 504 | .. code-block:: bash |
| 505 | |
| 506 | salt 'maas-node' maasng.create_partition server_hostname disk_name 10 ext4 "/" |
| 507 | salt-call maasng.create_partition server_hostname disk_name 10 ext4 "/" |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 508 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 509 | # TODO validation |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 510 | result = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 511 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 512 | system_id = get_machine(hostname)["system_id"] |
| 513 | LOG.info(system_id) |
| 514 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 515 | device_id = _get_blockdevice_id_by_name(hostname, disk) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 516 | LOG.info(device_id) |
| 517 | |
| 518 | value, unit = size[:-1], size[-1] |
| 519 | calc_size = str(int(value) * SIZE[unit]) |
| 520 | LOG.info(calc_size) |
| 521 | |
| 522 | data = { |
| 523 | "size": calc_size |
| 524 | } |
| 525 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 526 | # TODO validation |
| 527 | partition = json.loads(maas.post( |
| 528 | 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] | 529 | LOG.info(partition) |
| 530 | result["partition"] = "Partition created on {0}".format(disk) |
| 531 | |
| 532 | if fs_type != None: |
| 533 | data_fs_type = { |
| 534 | "fstype": fs_type |
| 535 | } |
| 536 | partition_id = str(partition["id"]) |
| 537 | LOG.info("Partition id: " + partition_id) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 538 | # TODO validation |
| 539 | json_res = json.loads(maas.post(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 540 | system_id, device_id, partition_id), "format", **data_fs_type).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 541 | LOG.info(json_res) |
| 542 | result["filesystem"] = "Filesystem {0} created".format(fs_type) |
| 543 | |
| 544 | if mount != None: |
| 545 | data = { |
| 546 | "mount_point": mount |
| 547 | } |
| 548 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 549 | # TODO validation |
| 550 | json_res = json.loads(maas.post(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 551 | system_id, device_id, str(partition['id'])), "mount", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 552 | LOG.info(json_res) |
| 553 | result["mount"] = "Mount point {0} created".format(mount) |
| 554 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 555 | return result |
| 556 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 557 | |
Dzmitry Stremkouski | d95bd2e | 2018-12-03 17:35:46 +0100 | [diff] [blame^] | 558 | def list_dnsresources(): |
| 559 | """ |
| 560 | List DNS resources known to MAAS. |
| 561 | |
| 562 | CLI Example: |
| 563 | |
| 564 | .. code-block:: bash |
| 565 | |
| 566 | salt-call maasng.list_dnsresources |
| 567 | |
| 568 | """ |
| 569 | result = {} |
| 570 | res_json = [] |
| 571 | maas = _create_maas_client() |
| 572 | |
| 573 | # TODO validation |
| 574 | result = json.loads(maas.get(u"api/2.0/dnsresources/").read()) |
| 575 | for elem in result: |
| 576 | ip_addresses = [] |
| 577 | for ip in elem["ip_addresses"]: |
| 578 | ip_addresses.append(ip["ip"]) |
| 579 | res_json.append( |
| 580 | { |
| 581 | "ip_addresses": ip_addresses, |
| 582 | "id": elem["id"], |
| 583 | "fqdn": elem["fqdn"], |
| 584 | "hostname": elem["fqdn"].split(".")[0] |
| 585 | } |
| 586 | ) |
| 587 | |
| 588 | LOG.debug(res_json) |
| 589 | |
| 590 | return res_json |
| 591 | |
| 592 | |
| 593 | def list_ipaddresses(): |
| 594 | """ |
| 595 | List IP addresses known to MAAS. |
| 596 | |
| 597 | CLI Example: |
| 598 | |
| 599 | .. code-block:: bash |
| 600 | |
| 601 | salt-call maasng.list_ipaddresses |
| 602 | |
| 603 | """ |
| 604 | result = {} |
| 605 | res_json = [] |
| 606 | maas = _create_maas_client() |
| 607 | |
| 608 | # TODO validation |
| 609 | result = json.loads(maas.get(u"api/2.0/ipaddresses/?all").read()) |
| 610 | for elem in result: |
| 611 | res_json.append( |
| 612 | { |
| 613 | "ip": elem["ip"], |
| 614 | "owner": { "username": elem["owner"]["username"] }, |
| 615 | "created": elem["created"], |
| 616 | "alloc_type_name": elem["alloc_type_name"], |
| 617 | "alloc_type": elem["alloc_type"], |
| 618 | "subnet": { |
| 619 | "id": elem["subnet"]["id"], |
| 620 | "cidr": elem["subnet"]["cidr"], |
| 621 | "name": elem["subnet"]["name"] |
| 622 | } |
| 623 | } |
| 624 | ) |
| 625 | |
| 626 | LOG.debug(res_json) |
| 627 | |
| 628 | return res_json |
| 629 | |
| 630 | |
| 631 | def reserve_ipaddress(hostname,subnet,ip=""): |
| 632 | """ |
| 633 | Reserve IP address for specified hostname in specified subnet |
| 634 | |
| 635 | CLI Example: |
| 636 | |
| 637 | .. code-block:: bash |
| 638 | |
| 639 | salt-call maasng.reserve_ipaddress hostname 192.168.0.0/24 192.168.0.254 |
| 640 | salt-call maasng.reserve_ipaddress hostname 192.168.0.0/24 |
| 641 | |
| 642 | """ |
| 643 | result = {} |
| 644 | data = {} |
| 645 | maas = _create_maas_client() |
| 646 | |
| 647 | data = { |
| 648 | "subnet": subnet, |
| 649 | "hostname": hostname |
| 650 | } |
| 651 | |
| 652 | if ip: |
| 653 | data["ip"] = ip |
| 654 | |
| 655 | # TODO validation |
| 656 | result = json.loads(maas.post(u"api/2.0/ipaddresses/", "reserve", **data).read()) |
| 657 | res_json = { |
| 658 | "created": result["created"], |
| 659 | "type": "DNS", |
| 660 | "hostname": hostname, |
| 661 | "ip": result["ip"] |
| 662 | } |
| 663 | |
| 664 | LOG.info(res_json) |
| 665 | |
| 666 | return res_json |
| 667 | |
| 668 | |
| 669 | def release_ipaddress(ipaddress): |
| 670 | """ |
| 671 | Release an IP address that was previously reserved by the user. |
| 672 | |
| 673 | CLI Example: |
| 674 | |
| 675 | .. code-block:: bash |
| 676 | |
| 677 | salt-call maasng.release_ipaddress 192.168.2.10 |
| 678 | |
| 679 | """ |
| 680 | result = {} |
| 681 | data = {} |
| 682 | maas = _create_maas_client() |
| 683 | |
| 684 | data = { |
| 685 | "ip": ipaddress |
| 686 | } |
| 687 | |
| 688 | # TODO validation |
| 689 | return maas.post(u"api/2.0/ipaddresses/", "release", **data).read() |
| 690 | |
| 691 | |
| 692 | def sync_address_pool(): |
| 693 | """ |
| 694 | Manage address pool for ext_pillar. |
| 695 | |
| 696 | CLI Example: |
| 697 | |
| 698 | .. code-block:: bash |
| 699 | |
| 700 | salt-call maasng.sync_address_pool |
| 701 | |
| 702 | """ |
| 703 | |
| 704 | address_pool = __pillar__["address_pool"] |
| 705 | LOG.debug("Address pool:") |
| 706 | LOG.debug(address_pool) |
| 707 | |
| 708 | cluster_networks = __pillar__["cluster_networks"] |
| 709 | LOG.debug("Cluster networks:") |
| 710 | LOG.debug(cluster_networks) |
| 711 | |
| 712 | dnsresources = list_dnsresources() |
| 713 | LOG.debug("DNS resources:") |
| 714 | LOG.debug(dnsresources) |
| 715 | |
| 716 | machines = list_machines() |
| 717 | LOG.debug("Machines:") |
| 718 | LOG.debug(machines) |
| 719 | |
| 720 | for net in address_pool: |
| 721 | if net == "external": |
| 722 | continue |
| 723 | for addr in address_pool[net]['pool']: |
| 724 | ipaddr = address_pool[net]['pool'][addr] |
| 725 | if ipaddr == "": |
| 726 | LOG.debug('Releasing IP address for: ' + addr) |
| 727 | release_required = False |
| 728 | for elem in dnsresources: |
| 729 | if elem["hostname"] == addr: |
| 730 | release_required = True |
| 731 | ip_addresses = elem["ip_addresses"] |
| 732 | if release_required: |
| 733 | for ip in ip_addresses: |
| 734 | res = release_ipaddress(ip) |
| 735 | LOG.debug(res) |
| 736 | else: |
| 737 | LOG.debug('IP for ' + addr + ' already released') |
| 738 | elif is_valid_ipv6(ipaddr) or is_valid_ipv4(ipaddr): |
| 739 | LOG.debug('Ensure static IP address "' + ipaddr + '" for ' + addr) |
| 740 | reserve_required = True |
| 741 | for elem in dnsresources: |
| 742 | if elem["hostname"] == addr: |
| 743 | reserve_required = False |
| 744 | for elem, elemval in machines.iteritems(): |
| 745 | for iface in elemval["interface_set"]: |
| 746 | for link in iface["links"]: |
| 747 | if "ip_address" in link: |
| 748 | if link["ip_address"] == ipaddr: |
| 749 | reserve_required = False |
| 750 | if reserve_required: |
| 751 | res = reserve_ipaddress(addr, cluster_networks[net]['cidr'], ipaddr) |
| 752 | reserve_required = False |
| 753 | LOG.debug(res) |
| 754 | else: |
| 755 | LOG.debug('Static IP address "' + ipaddr + '" for ' + addr + ' ensured') |
| 756 | else: |
| 757 | LOG.debug('Requesting IP address for' + addr) |
| 758 | reserve_required = True |
| 759 | for elem in dnsresources: |
| 760 | if elem["hostname"] == addr: |
| 761 | reserve_required = False |
| 762 | ip = elem["ip_addresses"][0] |
| 763 | if reserve_required: |
| 764 | res = reserve_ipaddress(addr, cluster_networks[net]['cidr']) |
| 765 | LOG.debug(res) |
| 766 | else: |
| 767 | LOG.debug(addr + " already has IP " + ip) |
| 768 | |
| 769 | return True |
| 770 | |
| 771 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 772 | def delete_partition(hostname, disk, partition_name): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 773 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 774 | Delete partition on device. |
| 775 | |
| 776 | CLI Example: |
| 777 | |
| 778 | .. code-block:: bash |
| 779 | |
| 780 | salt 'maas-node' maasng.delete_partition server_hostname disk_name partition_name |
| 781 | salt-call maasng.delete_partition server_hostname disk_name partition_name |
| 782 | |
| 783 | root_size = size in GB |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 784 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 785 | result = {} |
| 786 | data = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 787 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 788 | system_id = get_machine(hostname)["system_id"] |
| 789 | LOG.info(system_id) |
| 790 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 791 | device_id = _get_blockdevice_id_by_name(hostname, disk) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 792 | LOG.info(device_id) |
| 793 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 794 | partition_id = _get_partition_id_by_name(hostname, disk, partition_name) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 795 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 796 | maas.delete(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 797 | system_id, device_id, partition_id)).read() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 798 | result["new"] = "Partition {0} deleted".format(partition_name) |
| 799 | return result |
| 800 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 801 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 802 | def delete_partition_by_id(hostname, disk, partition_id): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 803 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 804 | Delete partition on device. Partition spefified by id of parition |
| 805 | |
| 806 | CLI Example: |
| 807 | |
| 808 | .. code-block:: bash |
| 809 | |
| 810 | salt 'maas-node' maasng.delete_partition_by_id server_hostname disk_name partition_id |
| 811 | salt-call maasng.delete_partition_by_id server_hostname disk_name partition_id |
| 812 | |
| 813 | root_size = size in GB |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 814 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 815 | result = {} |
| 816 | data = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 817 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 818 | system_id = get_machine(hostname)["system_id"] |
| 819 | LOG.info(system_id) |
| 820 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 821 | device_id = _get_blockdevice_id_by_name(hostname, disk) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 822 | LOG.info(device_id) |
| 823 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 824 | maas.delete(u"api/2.0/nodes/{0}/blockdevices/{1}/partition/{2}".format( |
| 825 | system_id, device_id, partition_id)).read() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 826 | result["new"] = "Partition {0} deleted".format(partition_id) |
| 827 | return result |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 828 | # END PARTITIONS |
| 829 | # DISK LAYOUT |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 830 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 831 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 832 | def drop_storage_schema(hostname, disk=None): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 833 | """ |
| 834 | #1. Drop lv |
| 835 | #2. Drop vg |
| 836 | #3. Drop md # need to zero-block? |
| 837 | #3. Drop part |
| 838 | """ |
| 839 | |
| 840 | if __opts__['test']: |
| 841 | ret['result'] = None |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 842 | ret['comment'] = 'Storage schema on {0} will be removed'.format( |
| 843 | hostname) |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 844 | return ret |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 845 | # TODO validation if exists |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 846 | vgs = list_volume_groups(hostname) |
| 847 | for vg in vgs: |
| 848 | delete_volume_group(hostname, vg) |
| 849 | |
| 850 | raids = list_raids(hostname) |
| 851 | for raid in raids: |
| 852 | delete_raid(hostname, raid) |
| 853 | |
| 854 | blocks = list_blockdevices(hostname) |
| 855 | for block_d in blocks: |
| 856 | partitions = __salt__['maasng.list_partitions'](hostname, block_d) |
| 857 | for partition_name, partition in partitions.iteritems(): |
| 858 | LOG.info('delete partition:\n{}'.format(partition)) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 859 | __salt__['maasng.delete_partition_by_id']( |
| 860 | hostname, block_d, partition["id"]) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 861 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 862 | |
| 863 | def update_disk_layout(hostname, layout, root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 864 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 865 | Update disk layout. Flat or LVM layout supported. |
| 866 | |
| 867 | CLI Example: |
| 868 | |
| 869 | .. code-block:: bash |
| 870 | |
| 871 | 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 |
| 872 | salt-call maasng.update_disk_layout server_hostname lvm root_size=None, root_device=None, volume_group=None, volume_name=None, volume_size=None |
| 873 | |
| 874 | root_size = size in GB |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 875 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 876 | result = {} |
| 877 | data = { |
| 878 | "storage_layout": layout, |
| 879 | } |
| 880 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 881 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 882 | system_id = get_machine(hostname)["system_id"] |
| 883 | LOG.info(system_id) |
| 884 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 885 | if layout == 'custom': |
| 886 | drop_storage_schema(hostname) |
| 887 | result["new"] = { |
| 888 | "storage_layout": layout, |
| 889 | } |
| 890 | |
| 891 | return result |
| 892 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 893 | if root_size != None: |
| 894 | bit_size = str(root_size * 1073741824) |
| 895 | LOG.info(bit_size) |
| 896 | data["root_size"] = bit_size |
| 897 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 898 | if root_device != None: |
| 899 | LOG.info(root_device) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 900 | data["root_device"] = str( |
| 901 | _get_blockdevice_id_by_name(hostname, root_device)) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 902 | |
| 903 | if layout == 'lvm': |
| 904 | if volume_group != None: |
| 905 | LOG.info(volume_group) |
| 906 | data["vg_name"] = volume_group |
| 907 | if volume_name != None: |
| 908 | LOG.info(volume_name) |
| 909 | data["lv_name"] = volume_name |
| 910 | if volume_size != None: |
| 911 | vol_size = str(volume_size * 1073741824) |
| 912 | LOG.info(vol_size) |
| 913 | data["lv_size"] = vol_size |
| 914 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 915 | # TODO validation |
| 916 | json_res = json.loads(maas.post( |
| 917 | 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] | 918 | LOG.info(json_res) |
| 919 | result["new"] = { |
| 920 | "storage_layout": layout, |
| 921 | } |
| 922 | |
| 923 | return result |
| 924 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 925 | # END DISK LAYOUT |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 926 | # LVM |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 927 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 928 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 929 | def list_volume_groups(hostname): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 930 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 931 | Get list of all volume group on machine. |
| 932 | |
| 933 | CLI Example: |
| 934 | |
| 935 | .. code-block:: bash |
| 936 | |
| 937 | salt 'maas-node' maasng.list_volume_groups server_hostname |
| 938 | salt-call maasng.list_volume_groups server_hostname |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 939 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 940 | volume_groups = {} |
| 941 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 942 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 943 | system_id = get_machine(hostname)["system_id"] |
| 944 | LOG.info(system_id) |
| 945 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 946 | # TODO validation if exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 947 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 948 | json_res = json.loads( |
| 949 | 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] | 950 | LOG.info(json_res) |
| 951 | for item in json_res: |
| 952 | volume_groups[item["name"]] = item |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 953 | # return |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 954 | return volume_groups |
| 955 | |
| 956 | |
| 957 | def get_volume_group(hostname, name): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 958 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 959 | Get information about specific volume group on machine. |
| 960 | |
| 961 | CLI Example: |
| 962 | |
| 963 | .. code-block:: bash |
| 964 | |
| 965 | salt 'maas-node' maasng.list_blockdevices server_hostname |
| 966 | salt-call maasng.list_blockdevices server_hostname |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 967 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 968 | # TODO validation that exists |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 969 | return list_volume_groups(hostname)[name] |
| 970 | |
| 971 | |
| 972 | def create_volume_group(hostname, volume_group_name, disks=[], partitions=[]): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 973 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 974 | Create new volume group on machine. Disks or partitions needs to be provided. |
| 975 | |
| 976 | CLI Example: |
| 977 | |
| 978 | .. code-block:: bash |
| 979 | |
| 980 | salt 'maas-node' maasng.create_volume_group volume_group_name, disks=[sda,sdb], partitions=[] |
| 981 | salt-call maasng.create_volume_group server_hostname |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 982 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 983 | result = {} |
| 984 | |
| 985 | data = { |
| 986 | "name": volume_group_name, |
| 987 | } |
| 988 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 989 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 990 | system_id = get_machine(hostname)["system_id"] |
| 991 | LOG.info(system_id) |
| 992 | |
| 993 | disk_ids = [] |
| 994 | partition_ids = [] |
| 995 | |
| 996 | for disk in disks: |
| 997 | p_disk = get_blockdevice(hostname, disk) |
| 998 | if p_disk["partition_table_type"] == None: |
| 999 | disk_ids.append(str(p_disk["id"])) |
| 1000 | else: |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1001 | result["error"] = "Device {0} on" \ |
| 1002 | "machine {1} cointains partition" \ |
| 1003 | "table".format(disk, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1004 | return result |
| 1005 | |
| 1006 | for partition in partitions: |
| 1007 | try: |
| 1008 | device = partition.split("-")[0] |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1009 | device_part = list_partitions(hostname, device) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1010 | partition_ids.append(str(device_part[partition]["id"])) |
| 1011 | except KeyError: |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1012 | result["error"] = "Partition {0} does" \ |
| 1013 | "not exists on " \ |
| 1014 | "machine {1}".format(partition, hostname) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1015 | return result |
| 1016 | |
| 1017 | data["block_devices"] = disk_ids |
| 1018 | data["partitions"] = partition_ids |
| 1019 | LOG.info(partition_ids) |
| 1020 | LOG.info(partitions) |
| 1021 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1022 | # TODO validation |
| 1023 | json_res = json.loads(maas.post( |
| 1024 | 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] | 1025 | LOG.info(json_res) |
| 1026 | result["new"] = "Volume group {0} created".format(json_res["name"]) |
| 1027 | |
| 1028 | return result |
| 1029 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1030 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1031 | def delete_volume_group(hostname, name): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1032 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1033 | Delete volume group on machine. |
| 1034 | |
| 1035 | CLI Example: |
| 1036 | |
| 1037 | .. code-block:: bash |
| 1038 | |
| 1039 | salt 'maas-node' maasng.delete_volume_group server_hostname vg0 |
| 1040 | salt-call maasng.delete_volume_group server_hostname vg0 |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1041 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1042 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1043 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1044 | system_id = get_machine(hostname)["system_id"] |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1045 | LOG.debug('delete_volume_group:{}'.format(system_id)) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1046 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1047 | vg_id = str(_get_volume_group_id_by_name(hostname, name)) |
| 1048 | for vol in get_volumes(hostname, name): |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1049 | delete_volume(hostname, vol, name) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1050 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1051 | # TODO validation |
| 1052 | json_res = json.loads(maas.delete( |
| 1053 | u"api/2.0/nodes/{0}/volume-group/{1}/".format(system_id, vg_id)).read() or 'null') |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1054 | LOG.info(json_res) |
| 1055 | |
| 1056 | return True |
| 1057 | |
| 1058 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1059 | def create_volume(hostname, volume_name, volume_group, size, fs_type=None, |
| 1060 | mount=None): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1061 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1062 | Create volume on volume group. |
| 1063 | |
| 1064 | CLI Example: |
| 1065 | |
| 1066 | .. code-block:: bash |
| 1067 | |
| 1068 | salt 'maas-node' maasng.create_volume server_hostname volume_name, volume_group, size, fs_type=None, mount=None |
| 1069 | salt-call maasng.create_volume server_hostname volume_name, volume_group, size, fs_type=None, mount=None |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1070 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1071 | |
| 1072 | data = { |
| 1073 | "name": volume_name, |
| 1074 | } |
| 1075 | |
| 1076 | value, unit = size[:-1], size[-1] |
| 1077 | bit_size = str(int(value) * SIZE[unit]) |
| 1078 | LOG.info(bit_size) |
| 1079 | |
| 1080 | data["size"] = bit_size |
| 1081 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1082 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1083 | system_id = get_machine(hostname)["system_id"] |
| 1084 | LOG.info(system_id) |
| 1085 | |
| 1086 | volume_group_id = str(_get_volume_group_id_by_name(hostname, volume_group)) |
| 1087 | |
| 1088 | LOG.info(volume_group_id) |
| 1089 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1090 | # TODO validation |
| 1091 | json_res = json.loads(maas.post(u"api/2.0/nodes/{0}/volume-group/{1}/".format( |
| 1092 | system_id, volume_group_id), "create_logical_volume", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1093 | LOG.info(json_res) |
| 1094 | |
| 1095 | if fs_type != None or mount != None: |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1096 | ret = create_volume_filesystem( |
| 1097 | hostname, volume_group + "-" + volume_name, fs_type, mount) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1098 | |
| 1099 | return True |
| 1100 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1101 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1102 | def delete_volume(hostname, volume_name, volume_group): |
| 1103 | """ |
| 1104 | Delete volume from volume group. |
| 1105 | Tips: maas always use 'volume_group-volume_name' name schema.Example: 'vg0-glusterfs' |
| 1106 | This function expexts same format. |
| 1107 | |
| 1108 | CLI Example: |
| 1109 | |
| 1110 | .. code-block:: bash |
| 1111 | |
| 1112 | salt 'maas-node' maasng.delete_volume server_hostname volume_name volume_group |
| 1113 | salt 'maas-node' maasng.delete_volume server_hostname vg0-vol0 vg0 |
| 1114 | salt-call maasng.delete_volume server_hostname volume_name volume_group |
| 1115 | """ |
| 1116 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1117 | maas = _create_maas_client() |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1118 | system_id = get_machine(hostname)["system_id"] |
| 1119 | LOG.debug('delete_volume:{}'.format(system_id)) |
| 1120 | |
| 1121 | volume_group_id = str(_get_volume_group_id_by_name(hostname, volume_group)) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1122 | volume_id = str(_get_volume_id_by_name( |
| 1123 | hostname, volume_name, volume_group)) |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1124 | |
| 1125 | if None in [volume_group_id, volume_id]: |
| 1126 | return False |
| 1127 | |
| 1128 | data = { |
| 1129 | "id": volume_id, |
| 1130 | } |
| 1131 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1132 | # TODO validation |
| 1133 | json_res = json.loads(maas.post(u"api/2.0/nodes/{0}/volume-group/{1}/".format( |
| 1134 | system_id, volume_group_id), "delete_logical_volume", **data).read() or 'null') |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1135 | return True |
| 1136 | |
| 1137 | |
| 1138 | def get_volumes(hostname, vg_name): |
| 1139 | """ |
| 1140 | Get list of volumes in volume group. |
| 1141 | """ |
| 1142 | volumes = {} |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1143 | _volumes = list_volume_groups( |
| 1144 | hostname)[vg_name].get('logical_volumes', False) |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1145 | if _volumes: |
| 1146 | for item in _volumes: |
| 1147 | volumes[item["name"]] = item |
| 1148 | return volumes |
| 1149 | |
| 1150 | # END LVM |
| 1151 | |
| 1152 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1153 | def create_volume_filesystem(hostname, device, fs_type=None, mount=None): |
| 1154 | |
| 1155 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1156 | system_id = get_machine(hostname)["system_id"] |
| 1157 | |
| 1158 | blockdevices_id = _get_blockdevice_id_by_name(hostname, device) |
| 1159 | data = {} |
| 1160 | if fs_type != None: |
| 1161 | data["fstype"] = fs_type |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1162 | # TODO validation |
| 1163 | json_res = json.loads(maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 1164 | system_id, blockdevices_id), "format", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1165 | LOG.info(json_res) |
| 1166 | |
| 1167 | if mount != None: |
| 1168 | data["mount_point"] = mount |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1169 | # TODO validation |
| 1170 | json_res = json.loads(maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 1171 | system_id, blockdevices_id), "mount", **data).read()) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1172 | LOG.info(json_res) |
| 1173 | |
| 1174 | return True |
| 1175 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1176 | |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1177 | def set_boot_disk(hostname, name): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1178 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1179 | Create volume on volume group. |
| 1180 | |
| 1181 | CLI Example: |
| 1182 | |
| 1183 | .. code-block:: bash |
| 1184 | |
| 1185 | salt 'maas-node' maasng.set_boot_disk server_hostname disk_name |
| 1186 | salt-call maasng.set_boot_disk server_hostname disk_name |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1187 | """ |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1188 | data = {} |
| 1189 | result = {} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1190 | maas = _create_maas_client() |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1191 | system_id = get_machine(hostname)["system_id"] |
| 1192 | blockdevices_id = _get_blockdevice_id_by_name(hostname, name) |
| 1193 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1194 | maas.post(u"/api/2.0/nodes/{0}/blockdevices/{1}/".format( |
| 1195 | system_id, blockdevices_id), "set_boot_disk", **data).read() |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1196 | # TODO validation for error response |
| 1197 | # (disk does not exists and node does not exists) |
Ondrej Smola | b57a23b | 2018-01-24 11:18:24 +0100 | [diff] [blame] | 1198 | result["new"] = "Disk {0} was set as bootable".format(name) |
| 1199 | |
| 1200 | return result |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1201 | |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1202 | # NETWORKING |
| 1203 | |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1204 | |
| 1205 | def list_fabric(): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1206 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1207 | Get list of all fabric |
| 1208 | |
| 1209 | CLI Example: |
| 1210 | |
| 1211 | .. code-block:: bash |
| 1212 | |
| 1213 | salt 'maas-node' maasng.list_fabric |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1214 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1215 | fabrics = {} |
| 1216 | maas = _create_maas_client() |
| 1217 | json_res = json.loads(maas.get(u'api/2.0/fabrics/').read()) |
| 1218 | LOG.info(json_res) |
| 1219 | for item in json_res: |
| 1220 | fabrics[item["name"]] = item |
| 1221 | return fabrics |
| 1222 | |
| 1223 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1224 | def check_fabric(name): |
| 1225 | """ |
| 1226 | Simple check that fabric already defined |
| 1227 | Return format: |
| 1228 | update - require update |
| 1229 | correct - fully coincides # not implemented |
| 1230 | not_exist - need's to be created |
| 1231 | """ |
| 1232 | |
| 1233 | ret = 'not_exist' |
| 1234 | fabrics = list_fabric() |
| 1235 | if name in fabrics.keys(): |
| 1236 | LOG.debug("Requested fabrics with name:{} already exist".format(name)) |
| 1237 | ret = 'update' |
| 1238 | return ret |
| 1239 | |
| 1240 | |
| 1241 | def check_fabric_guess_with_cidr(name, cidrs): |
| 1242 | """ |
| 1243 | Check, that fabric already defined OR it was autodiscovered |
| 1244 | WA to fix issue with hardcoded 'fabric-0' |
| 1245 | - Find all auto-discovered subnets by cidr |
| 1246 | - find all subnets, that SHOULD be configured to THIS subent |
| 1247 | Warning: most probably, will fail if some subnet defined |
| 1248 | to another fabric :( |
| 1249 | |
| 1250 | { 'update' : ID } - require update |
| 1251 | { 'correct' : ID } - fully coincides # not implemented |
| 1252 | { 'not_exist' : None } - need's to be created |
| 1253 | |
| 1254 | CLI Example: |
| 1255 | |
| 1256 | .. code-block:: bash |
| 1257 | |
| 1258 | salt 'maas-node' maasng.check_fabric_guess_with_cidr name='' cidrs=[] |
| 1259 | """ |
| 1260 | |
| 1261 | ret = {'not_exist': None} |
| 1262 | fabrics = list_fabric() |
| 1263 | # Simple check |
| 1264 | if name in fabrics: |
| 1265 | LOG.debug("Requested fabrics with name:{} already exist".format(name)) |
| 1266 | f_id = fabrics[name]['id'] |
| 1267 | ret = {'update': f_id} |
| 1268 | # Cidr check |
| 1269 | # All discovered subnets by cidr |
| 1270 | d_subnets = list_subnets(sort_by='cidr') |
| 1271 | # Check, that requested cidr already in discovered. |
| 1272 | # If it is - it would mean that fabric already |
| 1273 | # exist(fabric-0,most probably) but should be renamed. |
| 1274 | # Works only for first shot ;( |
| 1275 | # due curren-single-maas logic for 'vlan-subnet' mapping. |
| 1276 | # Probably, it will fail with future MAAS releases. |
| 1277 | for cidr in cidrs: |
| 1278 | if cidr in d_subnets: |
| 1279 | f_id = d_subnets[cidr]['vlan']['fabric_id'] |
| 1280 | f_name = d_subnets[cidr]['vlan']['fabric'] |
| 1281 | LOG.warning("Detected cidr:{} in fabric:{}".format(cidr, f_name)) |
| 1282 | LOG.warning("Guessing, that fabric " |
| 1283 | "with current name:{}\n should be " |
| 1284 | "renamed to:{}".format(f_name, name)) |
| 1285 | ret = {'update': f_id} |
| 1286 | return ret |
| 1287 | return ret |
| 1288 | |
| 1289 | |
azvyagintsev | f0904ac | 2018-07-05 18:53:26 +0300 | [diff] [blame] | 1290 | def create_fabric(name, description=None, fabric_id=None, update=False): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1291 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1292 | Create new fabric. |
| 1293 | |
| 1294 | CLI Example: |
| 1295 | |
| 1296 | .. code-block:: bash |
| 1297 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1298 | salt 'maas-node' maasng.create_fabric name='123' |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1299 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1300 | result = {} |
| 1301 | data = { |
| 1302 | "name": name, |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1303 | "class_type": '', |
| 1304 | |
| 1305 | } |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1306 | if description: |
| 1307 | data['description'] = description |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1308 | |
| 1309 | maas = _create_maas_client() |
azvyagintsev | f0904ac | 2018-07-05 18:53:26 +0300 | [diff] [blame] | 1310 | json_res = None |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1311 | try: |
| 1312 | if update: |
| 1313 | json_res = json.loads( |
| 1314 | maas.put(u"api/2.0/fabrics/{0}/".format(fabric_id), |
| 1315 | **data).read()) |
| 1316 | result["new"] = "Fabric {0} created".format(json_res["name"]) |
| 1317 | else: |
| 1318 | json_res = json.loads( |
| 1319 | maas.post(u"api/2.0/fabrics/", None, **data).read()) |
| 1320 | result["changes"] = "Fabric {0} updated".format(json_res["name"]) |
| 1321 | except Exception as inst: |
| 1322 | LOG.debug("create_fabric data:{}".format(data)) |
| 1323 | try: |
| 1324 | m = inst.readlines() |
| 1325 | except: |
| 1326 | m = inst.message |
| 1327 | LOG.error("Message:{0}".format(m)) |
| 1328 | result['result'] = False |
| 1329 | result['comment'] = 'Error creating fabric: {0}'.format(name) |
| 1330 | result['error'] = m |
| 1331 | return result |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1332 | LOG.debug("crete_fabric:{}".format(json_res)) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1333 | result['result'] = True |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1334 | return result |
| 1335 | |
| 1336 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1337 | def list_subnets(sort_by='name'): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1338 | """ |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1339 | Get list of subnets from maas server |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1340 | |
| 1341 | CLI Example: |
| 1342 | |
| 1343 | .. code-block:: bash |
| 1344 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1345 | salt 'maas-node' maasng.list_subnets |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1346 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1347 | subnets = {} |
| 1348 | maas = _create_maas_client() |
| 1349 | json_res = json.loads(maas.get(u'api/2.0/subnets/').read()) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1350 | for item in json_res: |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1351 | subnets[item[sort_by]] = item |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1352 | return subnets |
| 1353 | |
| 1354 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1355 | def list_vlans(fabric, sort_by='vid'): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1356 | """ |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1357 | Get list of vlans in fabric |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1358 | |
| 1359 | CLI Example: |
| 1360 | |
| 1361 | .. code-block:: bash |
| 1362 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1363 | salt 'maas-node' maasng.list_vlans fabric_name |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1364 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1365 | vlans = {} |
| 1366 | maas = _create_maas_client() |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1367 | fabric_id = get_fabricid(fabric) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1368 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1369 | try: |
| 1370 | json_res = json.loads( |
| 1371 | maas.get(u'api/2.0/fabrics/{0}/vlans/'.format(fabric_id)).read()) |
| 1372 | except Exception as inst: |
| 1373 | m = inst.readlines() |
| 1374 | LOG.error("Message:{0}".format(m)) |
| 1375 | LOG.debug(json_res) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1376 | for item in json_res: |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1377 | vlans[item[sort_by]] = item |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1378 | return vlans |
| 1379 | |
| 1380 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1381 | def get_fabricid(fabric): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1382 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1383 | Get id for specific fabric |
| 1384 | |
| 1385 | CLI Example: |
| 1386 | |
| 1387 | .. code-block:: bash |
| 1388 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1389 | salt 'maas-node' maasng.get_fabricid fabric_name |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1390 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1391 | try: |
| 1392 | return list_fabric()[fabric]['id'] |
| 1393 | except KeyError: |
azvyagintsev | efb6f5d | 2018-07-10 14:16:19 +0300 | [diff] [blame] | 1394 | return {"error": "Fabric not found on MaaS server"} |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1395 | |
| 1396 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1397 | def check_vlan_in_fabric(fabric, vlan): |
| 1398 | """ |
| 1399 | Check that VLAN exactly defined |
| 1400 | Return format: |
| 1401 | update - require update |
| 1402 | correct - fully coincides # not implemented |
| 1403 | not_exist - need's to be created |
| 1404 | """ |
| 1405 | |
| 1406 | ret = 'not_exist' |
| 1407 | vlans = list_vlans(fabric) |
| 1408 | if vlan in vlans.keys(): |
| 1409 | LOG.debug("Requested VLAN:{} already exist" |
| 1410 | "in FABRIC:{}".format(vlan, fabric)) |
| 1411 | ret = 'update' |
| 1412 | return ret |
| 1413 | |
| 1414 | |
Petr Ruzicka | 8047185 | 2018-07-13 14:08:27 +0200 | [diff] [blame] | 1415 | def create_vlan_in_fabric(name, fabric, vlan, description, primary_rack, mtu=1500, |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1416 | dhcp_on=False, update=False, vlan_id=""): |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1417 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1418 | Update vlan |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1419 | CLI Example: |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1420 | .. code-block:: bash |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1421 | salt 'maas-node' maasng.update_vlan name, fabric, vid, description, dhcp_on |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1422 | """ |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1423 | result = {} |
| 1424 | |
| 1425 | data = { |
| 1426 | "name": name, |
| 1427 | "dhcp_on": str(dhcp_on), |
| 1428 | "description": description, |
azvyagintsev | 6913e5e | 2018-07-05 11:42:53 +0300 | [diff] [blame] | 1429 | "primary_rack": list_racks()[primary_rack]['system_id'], |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1430 | } |
Michael Polenchuk | d25da79 | 2018-07-19 18:27:11 +0400 | [diff] [blame] | 1431 | if mtu: |
| 1432 | data['mtu'] = str(mtu) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1433 | vlan = str(vlan) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1434 | maas = _create_maas_client() |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1435 | fabric_id = get_fabricid(fabric) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1436 | try: |
| 1437 | if update: |
| 1438 | # MAAS have buggy logic here. Fallowing api reference, here should |
| 1439 | # be passed VID - which mean, API ID for vlan. |
| 1440 | # Otherwise, at least for maas 2.3.3-6498-ge4db91d exactly VLAN |
| 1441 | # should be passed. so, make temp.backward-convertation. |
| 1442 | # json_res = json.loads(maas.put(u'api/2.0/fabrics/{0}/vlans/{1}/'.format(fabric_id,vlan_id), **data).read()) |
| 1443 | json_res = json.loads(maas.put( |
| 1444 | u'api/2.0/fabrics/{0}/vlans/{1}/'.format(fabric_id, vlan), |
| 1445 | **data).read()) |
| 1446 | else: |
| 1447 | data['vid'] = str(vlan) |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1448 | json_res = json.loads(maas.post( |
| 1449 | u'api/2.0/fabrics/{0}/vlans/'.format(fabric_id), None, **data).read()) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1450 | except Exception as inst: |
| 1451 | LOG.debug("create_vlan_in_fabric data:{}".format(data)) |
| 1452 | try: |
| 1453 | m = inst.readlines() |
| 1454 | except: |
| 1455 | m = inst.message |
| 1456 | LOG.error("Message:{0}".format(m)) |
| 1457 | result['result'] = False |
| 1458 | result['comment'] = 'Error updating vlan: {0}'.format(name) |
| 1459 | result['error'] = m |
| 1460 | return result |
| 1461 | LOG.debug("create_vlan_in_fabric:{}".format(json_res)) |
Pavel Cizinsky | 0995e8f | 2018-05-04 17:10:37 +0200 | [diff] [blame] | 1462 | result["new"] = "Vlan {0} was updated".format(json_res["name"]) |
| 1463 | |
| 1464 | return result |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1465 | |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1466 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1467 | def check_subnet(cidr, name, fabric, gateway_ip): |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1468 | """ |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1469 | Check that subnet exactly defined |
| 1470 | Return format: |
| 1471 | update - require update |
| 1472 | correct - fully coincides # not implemented |
| 1473 | not_exist - need's to be created |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1474 | """ |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1475 | |
| 1476 | ret = 'not_exist' |
| 1477 | subnets = list_subnets(sort_by='cidr') |
| 1478 | if cidr in subnets.keys(): |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1479 | LOG.debug("Requested subnet cidr:{} already exist".format(cidr)) |
| 1480 | ret = 'update' |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1481 | return ret |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1482 | |
| 1483 | |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1484 | def create_subnet(cidr='', name='', fabric='', gateway_ip='', vlan='', |
| 1485 | update=False, subnet_id=''): |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1486 | """ |
| 1487 | Create subnet |
| 1488 | |
| 1489 | CLI Example: |
| 1490 | |
| 1491 | .. code-block:: bash |
| 1492 | |
| 1493 | salt 'maas-node' maasng.create_subnet cidr, name, fabric, gateway_ip |
| 1494 | """ |
| 1495 | |
| 1496 | fabric_id = get_fabricid(fabric) |
| 1497 | result = {} |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1498 | vlan = str(vlan) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1499 | data = { |
| 1500 | "cidr": cidr, |
| 1501 | "name": name, |
| 1502 | "fabric": str(fabric_id), |
| 1503 | "gateway_ip": gateway_ip, |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1504 | 'vlan': vlan, |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1505 | } |
| 1506 | maas = _create_maas_client() |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1507 | # FIXME: vlan definition not work in 2.3.3-6498-ge4db91d. |
| 1508 | LOG.warning("Ignoring parameter vlan:{}".format(vlan)) |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1509 | data.pop('vlan', '') |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1510 | try: |
| 1511 | if update: |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1512 | json_res = json.loads( |
| 1513 | maas.put(u"api/2.0/subnets/{0}/".format(subnet_id), **data).read()) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1514 | else: |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1515 | json_res = json.loads( |
| 1516 | maas.post(u"api/2.0/subnets/", None, **data).read()) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1517 | except Exception as inst: |
| 1518 | LOG.debug("create_subnet data:{}".format(data)) |
| 1519 | try: |
| 1520 | m = inst.readlines() |
| 1521 | except: |
| 1522 | m = inst.message |
| 1523 | LOG.error("Message:{0}".format(m)) |
| 1524 | result['result'] = False |
| 1525 | result['comment'] = 'Error creating subnet: {0}'.format(name) |
| 1526 | result['error'] = m |
| 1527 | return result |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1528 | LOG.debug("create_subnet:{}".format(json_res)) |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1529 | result["new"] = "Subnet {0} with CIDR {1}" \ |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1530 | "and gateway {2} was created".format( |
| 1531 | name, cidr, gateway_ip) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1532 | |
| 1533 | return result |
| 1534 | |
| 1535 | |
| 1536 | def get_subnet(subnet): |
| 1537 | """ |
| 1538 | Get details for specific subnet |
| 1539 | |
| 1540 | CLI Example: |
| 1541 | |
| 1542 | .. code-block:: bash |
| 1543 | |
| 1544 | salt 'maas-node' maasng.get_subnet subnet_name |
| 1545 | """ |
| 1546 | try: |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1547 | return list_subnets()[subnet] |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1548 | except KeyError: |
| 1549 | return {"error": "Subnet not found on MaaS server"} |
| 1550 | |
| 1551 | |
| 1552 | def get_subnetid(subnet): |
| 1553 | """ |
| 1554 | Get id for specific subnet |
| 1555 | |
| 1556 | CLI Example: |
| 1557 | |
| 1558 | .. code-block:: bash |
| 1559 | |
| 1560 | salt 'maas-node' maasng.get_subnetid subnet_name |
| 1561 | """ |
| 1562 | try: |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1563 | return list_subnets()[subnet]['id'] |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1564 | except KeyError: |
| 1565 | return {"error": "Subnet not found on MaaS server"} |
| 1566 | |
| 1567 | |
| 1568 | def list_ipranges(): |
| 1569 | """ |
| 1570 | Get list of all ipranges from maas server |
| 1571 | |
| 1572 | CLI Example: |
| 1573 | |
| 1574 | .. code-block:: bash |
| 1575 | |
| 1576 | salt 'maas-node' maasng.list_ipranges |
| 1577 | """ |
| 1578 | ipranges = {} |
| 1579 | maas = _create_maas_client() |
| 1580 | json_res = json.loads(maas.get(u'api/2.0/ipranges/').read()) |
| 1581 | for item in json_res: |
| 1582 | ipranges[item["start_ip"]] = item |
| 1583 | return ipranges |
| 1584 | |
| 1585 | |
azvyagintsev | f0904ac | 2018-07-05 18:53:26 +0300 | [diff] [blame] | 1586 | def create_iprange(type_range, start_ip, end_ip, subnet=None, comment=None): |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1587 | """ |
| 1588 | Create ip range |
| 1589 | |
| 1590 | CLI Example: |
| 1591 | |
| 1592 | .. code-block:: bash |
| 1593 | |
| 1594 | salt 'maas-node' maasng.create_iprange type, start ip, end ip, comment |
| 1595 | """ |
| 1596 | result = {} |
| 1597 | |
| 1598 | data = { |
| 1599 | "type": type_range, |
| 1600 | "start_ip": start_ip, |
| 1601 | "end_ip": end_ip, |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1602 | } |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1603 | if comment: |
| 1604 | data['comment'] = comment |
azvyagintsev | f0904ac | 2018-07-05 18:53:26 +0300 | [diff] [blame] | 1605 | if subnet: |
| 1606 | subnet_id = list_subnets()[subnet]['id'] |
| 1607 | data['subnet'] = str(subnet_id) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1608 | maas = _create_maas_client() |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1609 | _name = "Type:{}: {}-{}".format(type_range, start_ip, end_ip) |
| 1610 | try: |
| 1611 | json_res = json.loads( |
| 1612 | maas.post(u"api/2.0/ipranges/", None, **data).read()) |
| 1613 | except Exception as inst: |
| 1614 | try: |
| 1615 | m = inst.readlines() |
| 1616 | except: |
| 1617 | m = inst.message |
| 1618 | LOG.error("Message:{0}".format(m)) |
| 1619 | result['result'] = False |
| 1620 | result['comment'] = 'Error creating iprange:{0}'.format(_name) |
| 1621 | result['error'] = m |
| 1622 | return result |
| 1623 | result["new"] = "Iprange: {0} has been created".format(_name) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1624 | LOG.debug("create_iprange:{}".format(json_res)) |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1625 | |
| 1626 | return result |
| 1627 | |
| 1628 | |
| 1629 | def get_iprangeid(start_ip): |
| 1630 | """ |
| 1631 | Get id for ip range from maas server |
| 1632 | |
| 1633 | CLI Example: |
| 1634 | |
| 1635 | .. code-block:: bash |
| 1636 | |
| 1637 | salt 'maas-node' maasng.get_iprangeid start_ip |
| 1638 | """ |
| 1639 | try: |
| 1640 | return list_ipranges()[start_ip]['id'] |
| 1641 | except KeyError: |
| 1642 | return {"error": "Ip range not found on MaaS server"} |
| 1643 | |
| 1644 | |
| 1645 | def get_startip(start_ip): |
| 1646 | """ |
| 1647 | Get start ip for ip range |
| 1648 | |
| 1649 | CLI Example: |
| 1650 | |
| 1651 | .. code-block:: bash |
| 1652 | |
| 1653 | salt 'maas-node' maasng.get_startip start ip |
| 1654 | """ |
| 1655 | try: |
| 1656 | return list_ipranges()[start_ip] |
| 1657 | except KeyError: |
| 1658 | return {"error": "Ip range not found on MaaS server"} |
azvyagintsev | bca1f46 | 2018-05-25 19:06:46 +0300 | [diff] [blame] | 1659 | # END NETWORKING |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1660 | |
| 1661 | # MAAS CONFIG SECTION |
| 1662 | |
| 1663 | |
azvyagintsev | 5894707 | 2018-06-29 12:09:48 +0300 | [diff] [blame] | 1664 | def _getHTTPCode(url): |
azvyagintsev | fc1fcff | 2018-06-29 16:44:54 +0300 | [diff] [blame] | 1665 | code = '003' |
| 1666 | m = '' |
azvyagintsev | 5894707 | 2018-06-29 12:09:48 +0300 | [diff] [blame] | 1667 | try: |
| 1668 | connection = urllib2.urlopen(url) |
| 1669 | code = connection.getcode() |
| 1670 | connection.close() |
azvyagintsev | fc1fcff | 2018-06-29 16:44:54 +0300 | [diff] [blame] | 1671 | except (urllib2.HTTPError, urllib2.URLError) as e: |
| 1672 | try: |
| 1673 | code = e.getcode() |
| 1674 | except: |
| 1675 | m = e.reason |
| 1676 | pass |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1677 | LOG.debug("Unexpected http code:{} from " |
| 1678 | "url:{}\nwith message:{}".format(code, url, m)) |
azvyagintsev | 5894707 | 2018-06-29 12:09:48 +0300 | [diff] [blame] | 1679 | pass |
| 1680 | return code |
| 1681 | |
| 1682 | |
| 1683 | def wait_for_http_code(url=None, expected=[200]): |
| 1684 | """ |
| 1685 | Simple function, which just wait for avaible api, aka wait for 200. |
| 1686 | |
| 1687 | CLI Example: |
| 1688 | |
| 1689 | .. code-block:: bash |
| 1690 | |
| 1691 | salt 'maas-node' maasng.wait_for_http_code url expected=[200] |
| 1692 | |
| 1693 | """ |
| 1694 | ret = {} |
| 1695 | started_at = time.time() |
| 1696 | poll_time = 5 |
| 1697 | timeout = 60 * 2 |
| 1698 | while _getHTTPCode(url) not in expected: |
| 1699 | c_timeout = timeout - (time.time() - started_at) |
| 1700 | if c_timeout <= 0: |
| 1701 | ret['result'] = False |
azvyagintsev | fc1fcff | 2018-06-29 16:44:54 +0300 | [diff] [blame] | 1702 | ret["comment"] = "api:{} not answered in time".format(url) |
azvyagintsev | 5894707 | 2018-06-29 12:09:48 +0300 | [diff] [blame] | 1703 | return ret |
| 1704 | LOG.info( |
| 1705 | "Waiting for api:{0}\n" |
| 1706 | "sleep for:{1}s " |
| 1707 | "Left:{2}/{3}s".format(url, poll_time, round(c_timeout), |
| 1708 | timeout)) |
| 1709 | time.sleep(poll_time) |
| 1710 | ret['result'] = True |
| 1711 | ret["comment"] = "MAAS API:{} up.".format(url) |
| 1712 | return ret |
| 1713 | |
| 1714 | |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1715 | def _get_boot_source_id_by_url(url): |
| 1716 | # FIXME: fix ret\validation |
| 1717 | try: |
| 1718 | bs_id = get_boot_source(url=url)["id"] |
| 1719 | except KeyError: |
| 1720 | return {"error": "boot-source:{0} not exist!".format(url)} |
| 1721 | return bs_id |
| 1722 | |
| 1723 | |
| 1724 | def get_boot_source(url=None): |
| 1725 | """ |
| 1726 | Read a boot source by url. If url not specified - return all. |
| 1727 | |
| 1728 | CLI Example: |
| 1729 | |
| 1730 | .. code-block:: bash |
| 1731 | |
| 1732 | salt 'maas-node' maasng.get_boot_source url |
| 1733 | |
| 1734 | """ |
| 1735 | boot_sources = {} |
| 1736 | maas = _create_maas_client() |
| 1737 | json_res = json.loads(maas.get(u'api/2.0/boot-sources/').read() or 'null') |
| 1738 | for item in json_res: |
| 1739 | boot_sources[str(item["url"])] = item |
| 1740 | if url: |
| 1741 | return boot_sources.get(url, {}) |
| 1742 | return boot_sources |
| 1743 | |
| 1744 | |
| 1745 | def delete_boot_source(url, bs_id=None): |
| 1746 | """ |
| 1747 | Delete a boot source by url. |
| 1748 | |
| 1749 | CLI Example: |
| 1750 | |
| 1751 | .. code-block:: bash |
| 1752 | |
| 1753 | sal 'maas-node' maasng.delete url |
| 1754 | |
| 1755 | """ |
| 1756 | result = {} |
| 1757 | if not bs_id: |
| 1758 | bs_id = _get_boot_source_id_by_url(url) |
| 1759 | maas = _create_maas_client() |
| 1760 | json_res = json.loads(maas.delete( |
| 1761 | u'/api/2.0/boot-sources/{0}/'.format(bs_id)).read() or 'null') |
| 1762 | LOG.debug("delete_boot_source:{}".format(json_res)) |
| 1763 | result["new"] = "Boot-resource {0} deleted".format(url) |
| 1764 | return result |
| 1765 | |
| 1766 | |
| 1767 | def boot_sources_delete_all_others(except_urls=[]): |
| 1768 | """ |
| 1769 | Delete all boot-sources, except defined in 'except_urls' list. |
| 1770 | """ |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 1771 | result = {'changes': {}} |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1772 | maas_boot_sources = get_boot_source() |
| 1773 | if 0 in [len(except_urls), len(maas_boot_sources)]: |
| 1774 | result['result'] = None |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 1775 | result["comment"] = "'except_urls' or 'maas boot-sources' for " \ |
| 1776 | "delete empty. No changes goinng to be." |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1777 | return result |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 1778 | # FIXME: fix 'changes' accumulator |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1779 | for url in maas_boot_sources.keys(): |
| 1780 | if url not in except_urls: |
| 1781 | LOG.info("Removing boot-source:{}".format(url)) |
| 1782 | boot_resources_import(action='stop_import', wait=True) |
| 1783 | result["changes"] = delete_boot_source(url) |
| 1784 | return result |
| 1785 | |
| 1786 | |
| 1787 | def create_boot_source(url, keyring_filename='', keyring_data='', wait=False): |
| 1788 | """ |
| 1789 | Create and import maas boot-source: link to maas-ephemeral repo |
| 1790 | Be aware, those step will import resource to rack ctrl, but you also need to import |
| 1791 | them into the region! |
| 1792 | |
| 1793 | |
| 1794 | :param url: The URL of the BootSource. |
| 1795 | :param keyring_filename: The path to the keyring file for this BootSource. |
| 1796 | :param keyring_data: The GPG keyring for this BootSource, base64-encoded data. |
| 1797 | |
| 1798 | """ |
| 1799 | |
| 1800 | # TODO: not work with 'update' currently => keyring update may fail. |
| 1801 | result = {} |
| 1802 | |
| 1803 | data = { |
| 1804 | "url": url, |
| 1805 | "keyring_filename": keyring_filename, |
| 1806 | "keyring_data": str(keyring_data), |
| 1807 | } |
| 1808 | |
| 1809 | maas = _create_maas_client() |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1810 | if url in get_boot_source(): |
| 1811 | result['result'] = None |
| 1812 | result["comment"] = "boot resource already exist" |
| 1813 | return result |
| 1814 | |
| 1815 | # NOTE: maas.post will return 400, if url already defined. |
| 1816 | json_res = json.loads( |
| 1817 | maas.post(u'api/2.0/boot-sources/', None, **data).read()) |
| 1818 | if wait: |
| 1819 | LOG.debug( |
| 1820 | "Sleep for 5s,to get MaaS some time to process previous request") |
| 1821 | time.sleep(5) |
| 1822 | ret = boot_resources_is_importing(wait=True) |
| 1823 | if ret is dict: |
| 1824 | return ret |
| 1825 | LOG.debug("create_boot_source:{}".format(json_res)) |
| 1826 | result["new"] = "boot resource {0} was created".format(json_res["url"]) |
| 1827 | |
| 1828 | return result |
| 1829 | |
| 1830 | |
| 1831 | def boot_resources_import(action='import', wait=False): |
| 1832 | """ |
| 1833 | import/stop_import the boot resources. |
| 1834 | |
| 1835 | :param action: import\stop_import |
| 1836 | :param wait: True\False. Wait till process finished. |
| 1837 | |
| 1838 | CLI Example: |
| 1839 | |
| 1840 | .. code-block:: bash |
| 1841 | |
| 1842 | salt 'maas-node' maasng.boot_resources_import action='import' |
| 1843 | |
| 1844 | """ |
| 1845 | maas = _create_maas_client() |
| 1846 | # Have no idea why, but usual jsonloads not work here.. |
| 1847 | imp = maas.post(u'api/2.0/boot-resources/', action) |
| 1848 | if imp.code == 200: |
| 1849 | LOG.debug('boot_resources_import:{}'.format(imp.readline())) |
| 1850 | if wait: |
| 1851 | boot_resources_is_importing(wait=True) |
| 1852 | return True |
| 1853 | else: |
| 1854 | return False |
| 1855 | |
| 1856 | |
| 1857 | def boot_resources_is_importing(wait=False): |
| 1858 | maas = _create_maas_client() |
| 1859 | result = {} |
| 1860 | if wait: |
| 1861 | started_at = time.time() |
| 1862 | poll_time = 5 |
| 1863 | timeout = 60 * 15 |
| 1864 | while boot_resources_is_importing(wait=False): |
| 1865 | c_timeout = timeout - (time.time() - started_at) |
| 1866 | if c_timeout <= 0: |
| 1867 | result['result'] = False |
| 1868 | result["comment"] = "Boot-resources import not finished in time" |
| 1869 | return result |
| 1870 | LOG.info( |
| 1871 | "Waiting boot-resources import done\n" |
| 1872 | "sleep for:{}s " |
| 1873 | "Left:{}/{}s".format(poll_time, round(c_timeout), timeout)) |
| 1874 | time.sleep(poll_time) |
| 1875 | return json.loads( |
| 1876 | maas.get(u'api/2.0/boot-resources/', 'is_importing').read()) |
| 1877 | else: |
| 1878 | return json.loads( |
| 1879 | maas.get(u'api/2.0/boot-resources/', 'is_importing').read()) |
| 1880 | |
| 1881 | ##### |
Pavel Cizinsky | 8dd85b5 | 2018-06-18 21:40:13 +0200 | [diff] [blame] | 1882 | # def boot_sources_selections_delete_all_others(except_urls=[]): |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1883 | # """ |
| 1884 | # """ |
| 1885 | # result = {} |
| 1886 | # return result |
| 1887 | |
| 1888 | |
| 1889 | def is_boot_source_selections_in(dict1, list1): |
| 1890 | """ |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1891 | Check that requested boot-selection already in maas bs selections, |
| 1892 | if True- return bss id. |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1893 | # FIXME: those hack check doesn't look good. |
| 1894 | """ |
| 1895 | for bs in list1: |
| 1896 | same = set(dict1.keys()) & set(bs.keys()) |
| 1897 | if all(elem in same for elem in |
| 1898 | ['os', 'release', 'arches', 'subarches', 'labels']): |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1899 | LOG.debug("boot-selection in maas:{0}\n" |
| 1900 | "looks same to requested:{1}".format(bs, dict1)) |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1901 | return bs['id'] |
| 1902 | return False |
| 1903 | |
| 1904 | |
| 1905 | def get_boot_source_selections(bs_url): |
| 1906 | """ |
| 1907 | Get boot-source selections. |
| 1908 | """ |
| 1909 | # check for key_error! |
| 1910 | bs_id = _get_boot_source_id_by_url(bs_url) |
| 1911 | maas = _create_maas_client() |
| 1912 | json_res = json.loads( |
| 1913 | maas.get(u'/api/2.0/boot-sources/{0}/selections/'.format(bs_id)).read()) |
| 1914 | LOG.debug( |
| 1915 | "get_boot_source_selections for url:{} \n{}".format(bs_url, json_res)) |
| 1916 | return json_res |
| 1917 | |
| 1918 | |
| 1919 | def create_boot_source_selections(bs_url, os, release, arches="*", |
| 1920 | subarches="*", labels="*", wait=True): |
| 1921 | """ |
| 1922 | Create a new boot source selection for bs_url. |
| 1923 | :param os: The OS (e.g. ubuntu, centos) for which to import resources.Required. |
| 1924 | :param release: The release for which to import resources. Required. |
| 1925 | :param arches: The architecture list for which to import resources. |
| 1926 | :param subarches: The subarchitecture list for which to import resources. |
| 1927 | :param labels: The label lists for which to import resources. |
| 1928 | """ |
| 1929 | |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1930 | result = {"result": True, 'name': bs_url, 'changes': None} |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1931 | |
| 1932 | data = { |
| 1933 | "os": os, |
| 1934 | "release": release, |
| 1935 | "arches": arches, |
| 1936 | "subarches": subarches, |
| 1937 | "labels": labels, |
| 1938 | } |
| 1939 | |
| 1940 | maas = _create_maas_client() |
| 1941 | bs_id = _get_boot_source_id_by_url(bs_url) |
| 1942 | # TODO add pre-create verify |
| 1943 | maas_bs_s = get_boot_source_selections(bs_url) |
| 1944 | if is_boot_source_selections_in(data, maas_bs_s): |
| 1945 | result["result"] = True |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1946 | result["comment"] = 'Requested boot-source selection ' \ |
| 1947 | 'for {0} already exist.'.format( |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1948 | bs_url) |
| 1949 | return result |
| 1950 | |
| 1951 | # NOTE: maas.post will return 400, if url already defined. |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 1952 | # Also, maas need's some time to import info about stream. |
| 1953 | # unfortunatly, maas don't have any call to check stream-import-info - so, we need to implement |
| 1954 | # at least simple retry ;( |
| 1955 | json_res = False |
| 1956 | poll_time = 5 |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 1957 | for i in range(0, 10): |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 1958 | try: |
| 1959 | json_res = json.loads( |
| 1960 | maas.post(u'api/2.0/boot-sources/{0}/selections/'.format(bs_id), None, |
| 1961 | **data).read()) |
| 1962 | except Exception as inst: |
| 1963 | m = inst.readlines() |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1964 | LOG.warning("boot_source_selections " |
| 1965 | "catch error during processing. Most-probably, " |
azvyagintsev | e2e37a1 | 2018-11-01 14:45:49 +0200 | [diff] [blame] | 1966 | "streams data not imported yet.\nSleep:{}s " |
| 1967 | "Retry:{}/10".format(poll_time, i)) |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 1968 | LOG.warning("Message:{0}".format(m)) |
| 1969 | time.sleep(poll_time) |
| 1970 | continue |
| 1971 | break |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1972 | LOG.debug("create_boot_source_selections:{}".format(json_res)) |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 1973 | if not json_res: |
| 1974 | result["result"] = False |
azvyagintsev | f3515c8 | 2018-06-26 18:59:05 +0300 | [diff] [blame] | 1975 | result["comment"] = 'Failed to create requested boot-source selection' \ |
| 1976 | ' for {0}.'.format(bs_url) |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 1977 | return result |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1978 | if wait: |
| 1979 | LOG.debug( |
| 1980 | "Sleep for 5s,to get MaaS some time to process previous request") |
| 1981 | time.sleep(5) |
| 1982 | ret = boot_resources_import(action='import', wait=True) |
| 1983 | if ret is dict: |
| 1984 | return ret |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 1985 | result["comment"] = "boot-source selection for {0} was created".format( |
| 1986 | bs_url) |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 1987 | result["new"] = data |
azvyagintsev | 3ff2ef1 | 2018-06-01 21:30:45 +0300 | [diff] [blame] | 1988 | |
| 1989 | return result |
| 1990 | |
| 1991 | # END MAAS CONFIG SECTION |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 1992 | |
| 1993 | # RACK CONTROLLERS SECTION |
| 1994 | |
| 1995 | |
| 1996 | def get_rack(hostname): |
| 1997 | """ |
| 1998 | Get information about specified rackd |
| 1999 | |
| 2000 | CLI Example: |
| 2001 | |
| 2002 | .. code-block:: bash |
| 2003 | |
| 2004 | salt-call maasng.get_rack rack_hostname |
| 2005 | """ |
| 2006 | try: |
| 2007 | return list_racks()[hostname] |
| 2008 | except KeyError: |
| 2009 | return {"error": "rack:{} not found on MaaS server".format(hostname)} |
| 2010 | |
| 2011 | |
azvyagintsev | 6913e5e | 2018-07-05 11:42:53 +0300 | [diff] [blame] | 2012 | def list_racks(sort_by='hostname'): |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 2013 | """ |
| 2014 | Get list of all rack controllers from maas server |
| 2015 | |
| 2016 | CLI Example: |
| 2017 | |
| 2018 | .. code-block:: bash |
| 2019 | |
| 2020 | salt-call maasng.list_racks |
| 2021 | """ |
| 2022 | racks = {} |
| 2023 | maas = _create_maas_client() |
| 2024 | json_res = json.loads( |
| 2025 | maas.get(u"/api/2.0/rackcontrollers/").read() or 'null') |
| 2026 | for item in json_res: |
azvyagintsev | 6913e5e | 2018-07-05 11:42:53 +0300 | [diff] [blame] | 2027 | racks[item[sort_by]] = item |
azvyagintsev | cb54d14 | 2018-06-19 16:18:32 +0300 | [diff] [blame] | 2028 | return racks |
| 2029 | |
| 2030 | |
| 2031 | def sync_bs_to_rack(hostname=None): |
| 2032 | """ |
| 2033 | Sync RACK boot-sources with REGION. If no hostname probided - sync to all. |
| 2034 | |
| 2035 | CLI Example: |
| 2036 | |
| 2037 | .. code-block:: bash |
| 2038 | |
| 2039 | salt-call maasng.sync_bs_to_rack rack_hostname |
| 2040 | """ |
| 2041 | ret = {} |
| 2042 | maas = _create_maas_client() |
| 2043 | if not hostname: |
| 2044 | LOG.info("boot-sources sync initiated for ALL Rack's") |
| 2045 | # Convert to json-like format |
| 2046 | json_res = json.loads('["{0}"]'.format( |
| 2047 | maas.post(u"/api/2.0/rackcontrollers/", |
| 2048 | 'import_boot_images').read())) |
| 2049 | LOG.debug("sync_bs_to_rack:{}".format(json_res)) |
| 2050 | ret['result'] = True |
| 2051 | ret['comment'] = "boot-sources sync initiated for ALL Rack's" |
| 2052 | return ret |
| 2053 | LOG.info("boot-sources sync initiated for RACK:{0}".format(hostname)) |
| 2054 | # Convert to json-like format |
| 2055 | json_res = json.loads('["{0}"]'.format(maas.post( |
| 2056 | u"/api/2.0/rackcontrollers/{0}/".format( |
| 2057 | get_rack(hostname)['system_id']), |
| 2058 | 'import_boot_images').read())) |
| 2059 | LOG.debug("sync_bs_to_rack:{}".format(json_res)) |
| 2060 | ret['result'] = True |
| 2061 | ret['comment'] = "boot-sources sync initiated for {0} Rack's".format( |
| 2062 | hostname) |
| 2063 | return |
| 2064 | |
| 2065 | |
| 2066 | def rack_list_boot_imgs(hostname): |
| 2067 | ret = {} |
| 2068 | maas = _create_maas_client() |
| 2069 | LOG.debug("rack_list_boot_imgs:{}".format(hostname)) |
| 2070 | ret = json.loads(maas.get(u"/api/2.0/rackcontrollers/{0}/".format( |
| 2071 | get_rack(hostname)['system_id']), 'list_boot_images').read() or 'null') |
| 2072 | return ret |
| 2073 | |
| 2074 | |
| 2075 | def is_rack_synced(hostname): |
| 2076 | rez = rack_list_boot_imgs(hostname)['status'] |
| 2077 | if rez == 'synced': |
| 2078 | return True |
| 2079 | return False |
| 2080 | |
| 2081 | # TODO do we actually need _exact_ check per-pack? |
| 2082 | # def wait_for_images_on_rack(hostname): |
| 2083 | # |
| 2084 | # """ |
| 2085 | # WA function, to be able check that RACK actually done SYNC images |
| 2086 | # for REQUIRED images at least. |
| 2087 | # Required image to be fetched from |
| 2088 | # reclass:maas:region:boot_sources_selections:[keys]:os/release formation |
| 2089 | # |
| 2090 | # CLI Example: |
| 2091 | # |
| 2092 | # .. code-block:: bash |
| 2093 | # |
| 2094 | # salt-call maasng.wait_for_sync_bs_to_rack rack_hostname |
| 2095 | # """ |
| 2096 | # try: |
| 2097 | # bss = __salt__['config.get']('maas')['region']['boot_sources_selections'] |
| 2098 | # except KeyError: |
| 2099 | # ret['result'] = None |
| 2100 | # ret['comment'] = "boot_sources_selections definition for sync not found." |
| 2101 | # return ret |
| 2102 | # s_names = [] |
| 2103 | # # Format u'name': u'ubuntu/xenial' |
| 2104 | # for v in bss.values():s_names.append("{0}/{1}".format(v['os'],v['release'])) |
| 2105 | # # Each names, should be in rack and whole rack should be in sync-ed state |
| 2106 | |
| 2107 | |
| 2108 | def sync_and_wait_bs_to_all_racks(): |
| 2109 | """ |
| 2110 | Sync ALL rack's with regions source images. |
| 2111 | |
| 2112 | CLI Example: |
| 2113 | |
| 2114 | .. code-block:: bash |
| 2115 | |
| 2116 | salt-call maasng.sync_and_wait_bs_to_all_racks |
| 2117 | """ |
| 2118 | sync_bs_to_rack() |
| 2119 | for rack in list_racks().keys(): |
| 2120 | wait_for_sync_bs_to_rack(hostname=rack) |
| 2121 | return True |
| 2122 | |
| 2123 | |
| 2124 | def wait_for_sync_bs_to_rack(hostname=None): |
| 2125 | """ |
| 2126 | Wait for boot images sync finished, on exact rack |
| 2127 | |
| 2128 | CLI Example: |
| 2129 | |
| 2130 | .. code-block:: bash |
| 2131 | |
| 2132 | salt-call maasng.wait_for_sync_bs_to_rack rack_hostname |
| 2133 | """ |
| 2134 | ret = {} |
| 2135 | started_at = time.time() |
| 2136 | poll_time = 5 |
| 2137 | timeout = 60 * 15 |
| 2138 | while not is_rack_synced(hostname): |
| 2139 | c_timeout = timeout - (time.time() - started_at) |
| 2140 | if c_timeout <= 0: |
| 2141 | ret['result'] = False |
| 2142 | ret[ |
| 2143 | "comment"] = "Boot-resources sync on rackd:{0}" \ |
| 2144 | "not finished in time".format( |
| 2145 | hostname) |
| 2146 | return ret |
| 2147 | LOG.info( |
| 2148 | "Waiting boot-resources sync done to rack:{0}\n" |
| 2149 | "sleep for:{1}s " |
| 2150 | "Left:{2}/{3}s".format(hostname, poll_time, round(c_timeout), |
| 2151 | timeout)) |
| 2152 | time.sleep(poll_time) |
| 2153 | ret['result'] = is_rack_synced(hostname) |
| 2154 | ret["comment"] = "Boot-resources sync on rackd:{0} finished".format( |
| 2155 | hostname) |
| 2156 | return ret |
| 2157 | |
| 2158 | # END RACK CONTROLLERS SECTION |
Pavel Cizinsky | 8f9ba8e | 2018-09-10 14:31:49 +0200 | [diff] [blame] | 2159 | # SSHKEYS |
| 2160 | |
| 2161 | |
| 2162 | def list_sshkeys(): |
| 2163 | """ |
| 2164 | Get list of all sshkeys |
| 2165 | |
| 2166 | CLI Example: |
| 2167 | |
| 2168 | .. code-block:: bash |
| 2169 | |
| 2170 | salt 'maas-node' maasng.list_sshkeys |
| 2171 | salt-call maasng.list_sshkeys |
| 2172 | """ |
| 2173 | ssh = {} |
| 2174 | maas = _create_maas_client() |
| 2175 | json_res = json.loads(maas.get(u'api/2.0/account/prefs/sshkeys/').read()) |
| 2176 | LOG.info(json_res) |
| 2177 | for item in json_res: |
| 2178 | ssh[item["key"]] = item |
| 2179 | return ssh |
| 2180 | |
| 2181 | |
| 2182 | def add_sshkey(sshkey): |
| 2183 | """ |
| 2184 | Add SSH key for user to MAAS. |
| 2185 | |
| 2186 | CLI Example: |
| 2187 | |
| 2188 | .. code-block:: bash |
| 2189 | |
| 2190 | salt 'maas-node' maasng.add_sshkey sshkey |
| 2191 | salt-call maasng.add_sshkey sshkey |
| 2192 | """ |
| 2193 | data = { |
| 2194 | "key": sshkey, |
| 2195 | } |
| 2196 | result = {} |
| 2197 | maas = _create_maas_client() |
| 2198 | |
| 2199 | maas.post(u"/api/2.0/account/prefs/sshkeys/", None, **data).read() |
| 2200 | result["new"] = "SSH Key {0} was added.".format(sshkey) |
| 2201 | |
| 2202 | return result |
| 2203 | |
| 2204 | |
| 2205 | def get_sshkey(sshkey): |
| 2206 | """ |
| 2207 | Get start ip for ip range |
| 2208 | |
| 2209 | CLI Example: |
| 2210 | |
| 2211 | .. code-block:: bash |
| 2212 | |
| 2213 | salt 'maas-node' maasng.get_sshkey sshkey |
| 2214 | salt-call maasng.get_sshkey sshkey |
| 2215 | """ |
| 2216 | try: |
| 2217 | return list_sshkeys()[sshkey] |
| 2218 | except KeyError: |
| 2219 | return {"error": "SSH key not found on MaaS server"} |
| 2220 | # END SSHKEYS |