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