Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Module for handling maas calls. |
| 4 | |
| 5 | :optdepends: pyapi-maas Python adapter |
| 6 | :configuration: This module is not usable until the following are specified |
| 7 | either in a pillar or in the minion's config file:: |
| 8 | |
| 9 | maas.url: 'https://maas.domain.com/' |
| 10 | maas.token: fdsfdsdsdsfa:fsdfae3fassd:fdsfdsfsafasdfsa |
| 11 | |
| 12 | ''' |
| 13 | |
| 14 | from __future__ import absolute_import |
| 15 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 16 | import collections |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 17 | import copy |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 18 | import hashlib |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 19 | import io |
smolaon | 27359ae | 2016-03-11 17:15:34 +0100 | [diff] [blame] | 20 | import json |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 21 | import logging |
| 22 | import os.path |
| 23 | import time |
| 24 | import urllib2 |
smolaon | 27359ae | 2016-03-11 17:15:34 +0100 | [diff] [blame] | 25 | |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 26 | LOG = logging.getLogger(__name__) |
| 27 | |
| 28 | # Import third party libs |
| 29 | HAS_MASS = False |
| 30 | try: |
Damian Szeluga | d0ac0ac | 2017-03-29 15:15:33 +0200 | [diff] [blame] | 31 | from maas_client import MAASClient, MAASDispatcher, MAASOAuth |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 32 | HAS_MASS = True |
| 33 | except ImportError: |
Damian Szeluga | a400421 | 2017-05-17 15:43:14 +0200 | [diff] [blame] | 34 | LOG.debug('Missing python-oauth module. Skipping') |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 35 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 36 | |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 37 | def __virtual__(): |
| 38 | ''' |
| 39 | Only load this module if maas-client |
| 40 | is installed on this minion. |
| 41 | ''' |
| 42 | if HAS_MASS: |
| 43 | return 'maas' |
| 44 | return False |
| 45 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 46 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 47 | APIKEY_FILE = '/var/lib/maas/.maas_credentials' |
| 48 | |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 49 | STATUS_NAME_DICT = dict([ |
| 50 | (0, 'New'), (1, 'Commissioning'), (2, 'Failed commissioning'), |
| 51 | (3, 'Missing'), (4, 'Ready'), (5, 'Reserved'), (10, 'Allocated'), |
| 52 | (9, 'Deploying'), (6, 'Deployed'), (7, 'Retired'), (8, 'Broken'), |
| 53 | (11, 'Failed deployment'), (12, 'Releasing'), |
| 54 | (13, 'Releasing failed'), (14, 'Disk erasing'), |
| 55 | (15, 'Failed disk erasing')]) |
| 56 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 57 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 58 | def _format_data(data): |
| 59 | class Lazy: |
| 60 | def __str__(self): |
| 61 | return ' '.join(['{0}={1}'.format(k, v) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 62 | for k, v in data.iteritems()]) |
Krzysztof Szukiełojć | 52cc6b0 | 2017-04-06 09:53:43 +0200 | [diff] [blame] | 63 | return Lazy() |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 64 | |
| 65 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 66 | def _create_maas_client(): |
| 67 | global APIKEY_FILE |
| 68 | try: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 69 | api_token = file(APIKEY_FILE).read().splitlines()[-1].strip()\ |
| 70 | .split(':') |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 71 | except: |
| 72 | LOG.exception('token') |
| 73 | auth = MAASOAuth(*api_token) |
| 74 | api_url = 'http://localhost:5240/MAAS' |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 75 | dispatcher = MAASDispatcher() |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 76 | return MAASClient(auth, dispatcher, api_url) |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 77 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 78 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 79 | class MaasObject(object): |
| 80 | def __init__(self): |
| 81 | self._maas = _create_maas_client() |
| 82 | self._extra_data_urls = {} |
| 83 | self._extra_data = {} |
| 84 | self._update = False |
| 85 | self._element_key = 'name' |
| 86 | self._update_key = 'id' |
| 87 | |
| 88 | def send(self, data): |
| 89 | LOG.info('%s %s', self.__class__.__name__.lower(), _format_data(data)) |
| 90 | if self._update: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 91 | return self._maas.put( |
| 92 | self._update_url.format(data[self._update_key]), **data).read() |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 93 | if isinstance(self._create_url, tuple): |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 94 | return self._maas.post(self._create_url[0].format(**data), |
| 95 | *self._create_url[1:], **data).read() |
Krzysztof Szukiełojć | 76d9a5c | 2017-04-14 12:00:42 +0200 | [diff] [blame] | 96 | return self._maas.post(self._create_url.format(**data), |
| 97 | None, **data).read() |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 98 | |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 99 | def process(self, objects_name=None): |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 100 | ret = { |
| 101 | 'success': [], |
| 102 | 'errors': {}, |
| 103 | 'updated': [], |
| 104 | } |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 105 | try: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 106 | config = __salt__['config.get']('maas') |
| 107 | for part in self._config_path.split('.'): |
| 108 | config = config.get(part, {}) |
| 109 | extra = {} |
| 110 | for name, url_call in self._extra_data_urls.iteritems(): |
| 111 | key = 'id' |
| 112 | key_name = 'name' |
| 113 | if isinstance(url_call, tuple): |
| 114 | if len(url_call) == 2: |
| 115 | url_call, key = url_call[:] |
| 116 | else: |
| 117 | url_call, key, key_name = url_call[:] |
| 118 | json_res = json.loads(self._maas.get(url_call).read()) |
| 119 | if key: |
| 120 | extra[name] = {v[key_name]: v[key] for v in json_res} |
| 121 | else: |
Krzysztof Szukiełojć | 52cc6b0 | 2017-04-06 09:53:43 +0200 | [diff] [blame] | 122 | extra[name] = {v[key_name]: v for v in json_res} |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 123 | if self._all_elements_url: |
| 124 | all_elements = {} |
| 125 | elements = self._maas.get(self._all_elements_url).read() |
| 126 | res_json = json.loads(elements) |
| 127 | for element in res_json: |
| 128 | if isinstance(element, (str, unicode)): |
| 129 | all_elements[element] = {} |
| 130 | else: |
| 131 | all_elements[element[self._element_key]] = element |
| 132 | else: |
| 133 | all_elements = {} |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 134 | |
| 135 | def process_single(name, config_data): |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 136 | self._update = False |
| 137 | try: |
| 138 | data = self.fill_data(name, config_data, **extra) |
| 139 | if data is None: |
| 140 | ret['updated'].append(name) |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 141 | return |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 142 | if name in all_elements: |
| 143 | self._update = True |
| 144 | data = self.update(data, all_elements[name]) |
| 145 | self.send(data) |
| 146 | ret['updated'].append(name) |
| 147 | else: |
| 148 | self.send(data) |
| 149 | ret['success'].append(name) |
| 150 | except urllib2.HTTPError as e: |
| 151 | etxt = e.read() |
Krzysztof Szukiełojć | 199d5af | 2017-04-12 13:23:10 +0200 | [diff] [blame] | 152 | LOG.error('Failed for object %s reason %s', name, etxt) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 153 | ret['errors'][name] = str(etxt) |
| 154 | except Exception as e: |
Krzysztof Szukiełojć | 199d5af | 2017-04-12 13:23:10 +0200 | [diff] [blame] | 155 | LOG.error('Failed for object %s reason %s', name, e) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 156 | ret['errors'][name] = str(e) |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 157 | if objects_name is not None: |
| 158 | if ',' in objects_name: |
| 159 | objects_name = objects_name.split(',') |
| 160 | else: |
| 161 | objects_name = [objects_name] |
| 162 | for object_name in objects_name: |
| 163 | process_single(object_name, config[object_name]) |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 164 | else: |
| 165 | for name, config_data in config.iteritems(): |
| 166 | process_single(name, config_data) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 167 | except Exception as e: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 168 | LOG.exception('Error Global') |
| 169 | raise |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 170 | if ret['errors']: |
| 171 | raise Exception(ret) |
| 172 | return ret |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 173 | |
| 174 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 175 | class Fabric(MaasObject): |
| 176 | def __init__(self): |
| 177 | super(Fabric, self).__init__() |
| 178 | self._all_elements_url = u'api/2.0/fabrics/' |
| 179 | self._create_url = u'api/2.0/fabrics/' |
| 180 | self._update_url = u'api/2.0/fabrics/{0}/' |
| 181 | self._config_path = 'region.fabrics' |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 182 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 183 | def fill_data(self, name, fabric): |
| 184 | data = { |
| 185 | 'name': name, |
| 186 | 'description': fabric.get('description', ''), |
| 187 | } |
| 188 | if 'class_type' in fabric: |
| 189 | data['class_type'] = fabric.get('class_type'), |
| 190 | return data |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 191 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 192 | def update(self, new, old): |
| 193 | new['id'] = str(old['id']) |
| 194 | return new |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 195 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 196 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 197 | class Subnet(MaasObject): |
| 198 | def __init__(self): |
| 199 | super(Subnet, self).__init__() |
| 200 | self._all_elements_url = u'api/2.0/subnets/' |
| 201 | self._create_url = u'api/2.0/subnets/' |
| 202 | self._update_url = u'api/2.0/subnets/{0}/' |
| 203 | self._config_path = 'region.subnets' |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 204 | self._extra_data_urls = {'fabrics': u'api/2.0/fabrics/'} |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 205 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 206 | def fill_data(self, name, subnet, fabrics): |
| 207 | data = { |
| 208 | 'name': name, |
| 209 | 'fabric': str(fabrics[subnet.get('fabric', '')]), |
| 210 | 'cidr': subnet.get('cidr'), |
| 211 | 'gateway_ip': subnet['gateway_ip'], |
| 212 | } |
| 213 | self._iprange = subnet['iprange'] |
| 214 | return data |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 215 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 216 | def update(self, new, old): |
| 217 | new['id'] = str(old['id']) |
| 218 | return new |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 219 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 220 | def send(self, data): |
| 221 | response = super(Subnet, self).send(data) |
| 222 | res_json = json.loads(response) |
| 223 | self._process_iprange(res_json['id']) |
| 224 | return response |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 225 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 226 | def _process_iprange(self, subnet_id): |
| 227 | ipranges = json.loads(self._maas.get(u'api/2.0/ipranges/').read()) |
| 228 | LOG.warn('all %s ipranges %s', subnet_id, ipranges) |
| 229 | update = False |
| 230 | old_data = None |
| 231 | for iprange in ipranges: |
| 232 | if iprange['subnet']['id'] == subnet_id: |
| 233 | update = True |
| 234 | old_data = iprange |
| 235 | break |
| 236 | data = { |
| 237 | 'start_ip': self._iprange.get('start'), |
| 238 | 'end_ip': self._iprange.get('end'), |
| 239 | 'subnet': str(subnet_id), |
| 240 | 'type': self._iprange.get('type', 'dynamic') |
| 241 | } |
| 242 | LOG.warn('INFO: %s\n OLD: %s', data, old_data) |
| 243 | LOG.info('iprange %s', _format_data(data)) |
| 244 | if update: |
| 245 | LOG.warn('UPDATING %s %s', data, old_data) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 246 | self._maas.put(u'api/2.0/ipranges/{0}/'.format(old_data['id']), |
| 247 | **data) |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 248 | else: |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 249 | self._maas.post(u'api/2.0/ipranges/', None, **data) |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 250 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 251 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 252 | class DHCPSnippet(MaasObject): |
| 253 | def __init__(self): |
| 254 | super(DHCPSnippet, self).__init__() |
| 255 | self._all_elements_url = u'api/2.0/dhcp-snippets/' |
| 256 | self._create_url = u'api/2.0/dhcp-snippets/' |
| 257 | self._update_url = u'api/2.0/dhcp-snippets/{0}/' |
| 258 | self._config_path = 'region.dhcp_snippets' |
| 259 | self._extra_data_urls = {'subnets': u'api/2.0/subnets/'} |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 260 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 261 | def fill_data(self, name, snippet, subnets): |
| 262 | data = { |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 263 | 'name': name, |
| 264 | 'value': snippet['value'], |
| 265 | 'description': snippet['description'], |
| 266 | 'enabled': str(snippet['enabled'] and 1 or 0), |
| 267 | 'subnet': str(subnets[snippet['subnet']]), |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 268 | } |
| 269 | return data |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 270 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 271 | def update(self, new, old): |
| 272 | new['id'] = str(old['id']) |
| 273 | return new |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 274 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 275 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 276 | class PacketRepository(MaasObject): |
| 277 | def __init__(self): |
| 278 | super(PacketRepository, self).__init__() |
| 279 | self._all_elements_url = u'api/2.0/package-repositories/' |
| 280 | self._create_url = u'api/2.0/package-repositories/' |
| 281 | self._update_url = u'api/2.0/package-repositories/{0}/' |
| 282 | self._config_path = 'region.package_repositories' |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 283 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 284 | def fill_data(self, name, package_repository): |
| 285 | data = { |
| 286 | 'name': name, |
| 287 | 'url': package_repository['url'], |
| 288 | 'distributions': package_repository['distributions'], |
| 289 | 'components': package_repository['components'], |
| 290 | 'arches': package_repository['arches'], |
| 291 | 'key': package_repository['key'], |
| 292 | 'enabled': str(package_repository['enabled'] and 1 or 0), |
| 293 | } |
| 294 | if 'disabled_pockets' in package_repository: |
| 295 | data['disabled_pockets'] = package_repository['disable_pockets'], |
| 296 | return data |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 297 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 298 | def update(self, new, old): |
| 299 | new['id'] = str(old['id']) |
| 300 | return new |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 301 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 302 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 303 | class Device(MaasObject): |
| 304 | def __init__(self): |
| 305 | super(Device, self).__init__() |
| 306 | self._all_elements_url = u'api/2.0/devices/' |
| 307 | self._create_url = u'api/2.0/devices/' |
| 308 | self._update_url = u'api/2.0/devices/{0}/' |
| 309 | self._config_path = 'region.devices' |
| 310 | self._element_key = 'hostname' |
| 311 | self._update_key = 'system_id' |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 312 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 313 | def fill_data(self, name, device_data): |
| 314 | data = { |
| 315 | 'mac_addresses': device_data['mac'], |
| 316 | 'hostname': name, |
| 317 | } |
| 318 | self._interface = device_data['interface'] |
| 319 | return data |
| 320 | |
| 321 | def update(self, new, old): |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 322 | old_macs = set(v['mac_address'].lower() for v in old['interface_set']) |
| 323 | if new['mac_addresses'].lower() not in old_macs: |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 324 | self._update = False |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 325 | LOG.info('Mac changed deleting old device %s', old['system_id']) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 326 | self._maas.delete(u'api/2.0/devices/{0}/'.format(old['system_id'])) |
| 327 | else: |
| 328 | new[self._update_key] = str(old[self._update_key]) |
| 329 | return new |
| 330 | |
| 331 | def send(self, data): |
| 332 | response = super(Device, self).send(data) |
| 333 | resp_json = json.loads(response) |
| 334 | system_id = resp_json['system_id'] |
| 335 | iface_id = resp_json['interface_set'][0]['id'] |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 336 | self._link_interface(system_id, iface_id) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 337 | return response |
| 338 | |
| 339 | def _link_interface(self, system_id, interface_id): |
| 340 | data = { |
| 341 | 'mode': self._interface.get('mode', 'STATIC'), |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 342 | 'subnet': self._interface['subnet'], |
| 343 | 'ip_address': self._interface['ip_address'], |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 344 | } |
| 345 | if 'default_gateway' in self._interface: |
| 346 | data['default_gateway'] = self._interface.get('default_gateway') |
| 347 | if self._update: |
| 348 | data['force'] = '1' |
| 349 | LOG.info('interfaces link_subnet %s %s %s', system_id, interface_id, |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 350 | _format_data(data)) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 351 | self._maas.post(u'/api/2.0/nodes/{0}/interfaces/{1}/' |
| 352 | .format(system_id, interface_id), 'link_subnet', |
| 353 | **data) |
| 354 | |
| 355 | |
| 356 | class Machine(MaasObject): |
| 357 | def __init__(self): |
| 358 | super(Machine, self).__init__() |
| 359 | self._all_elements_url = u'api/2.0/machines/' |
| 360 | self._create_url = u'api/2.0/machines/' |
| 361 | self._update_url = u'api/2.0/machines/{0}/' |
| 362 | self._config_path = 'region.machines' |
| 363 | self._element_key = 'hostname' |
| 364 | self._update_key = 'system_id' |
| 365 | |
| 366 | def fill_data(self, name, machine_data): |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 367 | power_data = machine_data['power_parameters'] |
| 368 | data = { |
| 369 | 'hostname': name, |
| 370 | 'architecture': machine_data.get('architecture', 'amd64/generic'), |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 371 | 'mac_addresses': machine_data['interface']['mac'], |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 372 | 'power_type': machine_data.get('power_type', 'ipmi'), |
| 373 | 'power_parameters_power_address': power_data['power_address'], |
| 374 | } |
Ondrej Smola | 455003c | 2017-06-01 22:53:39 +0200 | [diff] [blame] | 375 | if 'power_driver' in power_data: |
| 376 | data['power_parameters_power_driver'] = power_data['power_driver'] |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 377 | if 'power_user' in power_data: |
| 378 | data['power_parameters_power_user'] = power_data['power_user'] |
| 379 | if 'power_password' in power_data: |
| 380 | data['power_parameters_power_pass'] = \ |
| 381 | power_data['power_password'] |
| 382 | return data |
| 383 | |
| 384 | def update(self, new, old): |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 385 | old_macs = set(v['mac_address'].lower() for v in old['interface_set']) |
| 386 | if new['mac_addresses'].lower() not in old_macs: |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 387 | self._update = False |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 388 | LOG.info('Mac changed deleting old machine %s', old['system_id']) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 389 | self._maas.delete(u'api/2.0/machines/{0}/' |
| 390 | .format(old['system_id'])) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 391 | else: |
| 392 | new[self._update_key] = str(old[self._update_key]) |
| 393 | return new |
| 394 | |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 395 | |
| 396 | class AssignMachinesIP(MaasObject): |
Krzysztof Szukiełojć | d6ee1a0 | 2017-04-07 14:01:30 +0200 | [diff] [blame] | 397 | READY = 4 |
Andrey | ef15699 | 2017-07-03 14:54:03 -0500 | [diff] [blame] | 398 | DEPLOYED = 6 |
Krzysztof Szukiełojć | d6ee1a0 | 2017-04-07 14:01:30 +0200 | [diff] [blame] | 399 | |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 400 | def __init__(self): |
| 401 | super(AssignMachinesIP, self).__init__() |
| 402 | self._all_elements_url = None |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 403 | self._create_url = \ |
| 404 | (u'/api/2.0/nodes/{system_id}/interfaces/{interface_id}/', |
| 405 | 'link_subnet') |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 406 | self._config_path = 'region.machines' |
| 407 | self._element_key = 'hostname' |
| 408 | self._update_key = 'system_id' |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 409 | self._extra_data_urls = {'machines': (u'api/2.0/machines/', |
| 410 | None, 'hostname')} |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 411 | |
| 412 | def fill_data(self, name, data, machines): |
| 413 | interface = data['interface'] |
| 414 | machine = machines[name] |
Andrey | ef15699 | 2017-07-03 14:54:03 -0500 | [diff] [blame] | 415 | if machine['status'] == self.DEPLOYED: |
| 416 | return |
Krzysztof Szukiełojć | d6ee1a0 | 2017-04-07 14:01:30 +0200 | [diff] [blame] | 417 | if machine['status'] != self.READY: |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 418 | raise Exception('Machine:{} not in READY state'.format(name)) |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 419 | if 'ip' not in interface: |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 420 | return |
| 421 | data = { |
| 422 | 'mode': 'STATIC', |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 423 | 'subnet': str(interface.get('subnet')), |
| 424 | 'ip_address': str(interface.get('ip')), |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 425 | } |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 426 | if 'default_gateway' in interface: |
| 427 | data['default_gateway'] = interface.get('gateway') |
Krzysztof Szukiełojć | 9449af1 | 2017-04-11 14:01:30 +0200 | [diff] [blame] | 428 | data['force'] = '1' |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 429 | data['system_id'] = str(machine['system_id']) |
| 430 | data['interface_id'] = str(machine['interface_set'][0]['id']) |
| 431 | return data |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 432 | |
| 433 | |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 434 | class DeployMachines(MaasObject): |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 435 | # FIXME |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 436 | READY = 4 |
Andrey | ef15699 | 2017-07-03 14:54:03 -0500 | [diff] [blame] | 437 | DEPLOYED = 6 |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 438 | |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 439 | def __init__(self): |
| 440 | super(DeployMachines, self).__init__() |
| 441 | self._all_elements_url = None |
| 442 | self._create_url = (u'api/2.0/machines/{system_id}/', 'deploy') |
| 443 | self._config_path = 'region.machines' |
| 444 | self._element_key = 'hostname' |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 445 | self._extra_data_urls = {'machines': (u'api/2.0/machines/', |
| 446 | None, 'hostname')} |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 447 | |
| 448 | def fill_data(self, name, machine_data, machines): |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 449 | machine = machines[name] |
Andrey | ef15699 | 2017-07-03 14:54:03 -0500 | [diff] [blame] | 450 | if machine['status'] == self.DEPLOYED: |
| 451 | return |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 452 | if machine['status'] != self.READY: |
| 453 | raise Exception('Not in ready state') |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 454 | data = { |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 455 | 'system_id': machine['system_id'], |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 456 | } |
Krzysztof Szukiełojć | 32677bf | 2017-04-13 11:04:25 +0200 | [diff] [blame] | 457 | if 'distro_series' in machine_data: |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 458 | data['distro_series'] = machine_data['distro_series'] |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 459 | if 'hwe_kernel' in machine_data: |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 460 | data['hwe_kernel'] = machine_data['hwe_kernel'] |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 461 | return data |
| 462 | |
Krzysztof Szukiełojć | 33f9b59 | 2017-04-12 11:43:50 +0200 | [diff] [blame] | 463 | def send(self, data): |
| 464 | LOG.info('%s %s', self.__class__.__name__.lower(), _format_data(data)) |
Krzysztof Szukiełojć | 3b7516d | 2017-04-12 11:52:55 +0200 | [diff] [blame] | 465 | self._maas.post(u'api/2.0/machines/', 'allocate', system_id=data['system_id']).read() |
Krzysztof Szukiełojć | 33f9b59 | 2017-04-12 11:43:50 +0200 | [diff] [blame] | 466 | return self._maas.post(self._create_url[0].format(**data), |
| 467 | *self._create_url[1:], **data).read() |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 468 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 469 | class BootResource(MaasObject): |
| 470 | def __init__(self): |
| 471 | super(BootResource, self).__init__() |
| 472 | self._all_elements_url = u'api/2.0/boot-resources/' |
| 473 | self._create_url = u'api/2.0/boot-resources/' |
| 474 | self._update_url = u'api/2.0/boot-resources/{0}/' |
| 475 | self._config_path = 'region.boot_resources' |
| 476 | |
| 477 | def fill_data(self, name, boot_data): |
| 478 | sha256 = hashlib.sha256() |
| 479 | sha256.update(file(boot_data['content']).read()) |
| 480 | data = { |
| 481 | 'name': name, |
| 482 | 'title': boot_data['title'], |
| 483 | 'architecture': boot_data['architecture'], |
| 484 | 'filetype': boot_data['filetype'], |
| 485 | 'size': str(os.path.getsize(boot_data['content'])), |
| 486 | 'sha256': sha256.hexdigest(), |
| 487 | 'content': io.open(boot_data['content']), |
| 488 | } |
| 489 | return data |
| 490 | |
| 491 | def update(self, new, old): |
| 492 | self._update = False |
| 493 | return new |
| 494 | |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 495 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 496 | class CommissioningScripts(MaasObject): |
| 497 | def __init__(self): |
| 498 | super(CommissioningScripts, self).__init__() |
| 499 | self._all_elements_url = u'api/2.0/commissioning-scripts/' |
| 500 | self._create_url = u'api/2.0/commissioning-scripts/' |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 501 | self._config_path = 'region.commissioning_scripts' |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 502 | self._update_url = u'api/2.0/commissioning-scripts/{0}' |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 503 | self._update_key = 'name' |
| 504 | |
| 505 | def fill_data(self, name, file_path): |
| 506 | data = { |
| 507 | 'name': name, |
| 508 | 'content': io.open(file_path), |
| 509 | } |
| 510 | return data |
| 511 | |
| 512 | def update(self, new, old): |
| 513 | return new |
| 514 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 515 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 516 | class MaasConfig(MaasObject): |
| 517 | def __init__(self): |
| 518 | super(MaasConfig, self).__init__() |
| 519 | self._all_elements_url = None |
| 520 | self._create_url = (u'api/2.0/maas/', u'set_config') |
| 521 | self._config_path = 'region.maas_config' |
| 522 | |
| 523 | def fill_data(self, name, value): |
| 524 | data = { |
| 525 | 'name': name, |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 526 | 'value': str(value), |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 527 | } |
| 528 | return data |
| 529 | |
| 530 | def update(self, new, old): |
| 531 | self._update = False |
| 532 | return new |
| 533 | |
| 534 | |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 535 | class SSHPrefs(MaasObject): |
| 536 | def __init__(self): |
| 537 | super(SSHPrefs, self).__init__() |
| 538 | self._all_elements_url = None |
| 539 | self._create_url = u'api/2.0/account/prefs/sshkeys/' |
| 540 | self._config_path = 'region.sshprefs' |
| 541 | self._element_key = 'hostname' |
| 542 | self._update_key = 'system_id' |
| 543 | |
| 544 | def fill_data(self, value): |
| 545 | data = { |
| 546 | 'key': value, |
| 547 | } |
| 548 | return data |
| 549 | |
| 550 | def process(self): |
| 551 | config = __salt__['config.get']('maas') |
| 552 | for part in self._config_path.split('.'): |
| 553 | config = config.get(part, {}) |
| 554 | extra = {} |
| 555 | for name, url_call in self._extra_data_urls.iteritems(): |
| 556 | key = 'id' |
| 557 | if isinstance(url_call, tuple): |
| 558 | url_call, key = url_call[:] |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 559 | json_res = json.loads(self._maas.get(url_call).read()) |
| 560 | extra[name] = {v['name']: v[key] for v in json_res} |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 561 | if self._all_elements_url: |
| 562 | all_elements = {} |
| 563 | elements = self._maas.get(self._all_elements_url).read() |
| 564 | res_json = json.loads(elements) |
| 565 | for element in res_json: |
| 566 | if isinstance(element, (str, unicode)): |
| 567 | all_elements[element] = {} |
| 568 | else: |
| 569 | all_elements[element[self._element_key]] = element |
| 570 | else: |
| 571 | all_elements = {} |
| 572 | ret = { |
| 573 | 'success': [], |
| 574 | 'errors': {}, |
| 575 | 'updated': [], |
| 576 | } |
| 577 | for config_data in config: |
| 578 | name = config_data[:10] |
| 579 | try: |
| 580 | data = self.fill_data(config_data, **extra) |
| 581 | self.send(data) |
| 582 | ret['success'].append(name) |
| 583 | except urllib2.HTTPError as e: |
| 584 | etxt = e.read() |
| 585 | LOG.exception('Failed for object %s reason %s', name, etxt) |
| 586 | ret['errors'][name] = str(etxt) |
| 587 | except Exception as e: |
| 588 | LOG.exception('Failed for object %s reason %s', name, e) |
| 589 | ret['errors'][name] = str(e) |
| 590 | if ret['errors']: |
| 591 | raise Exception(ret) |
| 592 | return ret |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 593 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 594 | |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 595 | class Domain(MaasObject): |
| 596 | def __init__(self): |
| 597 | super(Domain, self).__init__() |
| 598 | self._all_elements_url = u'/api/2.0/domains/' |
| 599 | self._create_url = u'/api/2.0/domains/' |
| 600 | self._config_path = 'region.domain' |
| 601 | self._update_url = u'/api/2.0/domains/{0}/' |
| 602 | |
| 603 | def fill_data(self, value): |
| 604 | data = { |
| 605 | 'name': value, |
| 606 | } |
| 607 | self._update = True |
| 608 | return data |
| 609 | |
| 610 | def update(self, new, old): |
| 611 | new['id'] = str(old['id']) |
| 612 | new['authoritative'] = str(old['authoritative']) |
| 613 | return new |
| 614 | |
| 615 | def process(self): |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 616 | ret = { |
| 617 | 'success': [], |
| 618 | 'errors': {}, |
| 619 | 'updated': [], |
| 620 | } |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 621 | config = __salt__['config.get']('maas') |
| 622 | for part in self._config_path.split('.'): |
| 623 | config = config.get(part, {}) |
| 624 | extra = {} |
| 625 | for name, url_call in self._extra_data_urls.iteritems(): |
| 626 | key = 'id' |
| 627 | if isinstance(url_call, tuple): |
| 628 | url_call, key = url_call[:] |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 629 | json_res = json.loads(self._maas.get(url_call).read()) |
| 630 | extra[name] = {v['name']: v[key] for v in json_res} |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 631 | if self._all_elements_url: |
| 632 | all_elements = {} |
| 633 | elements = self._maas.get(self._all_elements_url).read() |
| 634 | res_json = json.loads(elements) |
| 635 | for element in res_json: |
| 636 | if isinstance(element, (str, unicode)): |
| 637 | all_elements[element] = {} |
| 638 | else: |
| 639 | all_elements[element[self._element_key]] = element |
| 640 | else: |
| 641 | all_elements = {} |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 642 | try: |
| 643 | data = self.fill_data(config, **extra) |
| 644 | data = self.update(data, all_elements.values()[0]) |
| 645 | self.send(data) |
| 646 | ret['success'].append('domain') |
| 647 | except urllib2.HTTPError as e: |
| 648 | etxt = e.read() |
| 649 | LOG.exception('Failed for object %s reason %s', 'domain', etxt) |
| 650 | ret['errors']['domain'] = str(etxt) |
| 651 | except Exception as e: |
| 652 | LOG.exception('Failed for object %s reason %s', 'domain', e) |
| 653 | ret['errors']['domain'] = str(e) |
| 654 | if ret['errors']: |
| 655 | raise Exception(ret) |
| 656 | return ret |
| 657 | |
| 658 | |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 659 | class MachinesStatus(MaasObject): |
| 660 | @classmethod |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 661 | def execute(cls, objects_name=None): |
Krzysztof Szukiełojć | 0be1a16 | 2017-04-04 11:59:09 +0200 | [diff] [blame] | 662 | cls._maas = _create_maas_client() |
| 663 | result = cls._maas.get(u'api/2.0/machines/') |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 664 | json_result = json.loads(result.read()) |
| 665 | res = [] |
Krzysztof Szukiełojć | 0be1a16 | 2017-04-04 11:59:09 +0200 | [diff] [blame] | 666 | summary = collections.Counter() |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 667 | if objects_name: |
| 668 | if ',' in objects_name: |
| 669 | objects_name = set(objects_name.split(',')) |
| 670 | else: |
| 671 | objects_name = set([objects_name]) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 672 | for machine in json_result: |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 673 | if objects_name and machine['hostname'] not in objects_name: |
Krzysztof Szukiełojć | 2497cdb | 2017-04-11 09:50:28 +0200 | [diff] [blame] | 674 | continue |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 675 | status = STATUS_NAME_DICT[machine['status'].lower()] |
Krzysztof Szukiełojć | 0be1a16 | 2017-04-04 11:59:09 +0200 | [diff] [blame] | 676 | summary[status] += 1 |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 677 | res.append( |
| 678 | {'hostname': machine['hostname'], |
| 679 | 'system_id': machine['system_id'], |
| 680 | 'status': status}) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 681 | return {'machines': res, 'summary': summary} |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 682 | |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 683 | @classmethod |
| 684 | def wait_for_machine_status(cls, **kwargs): |
| 685 | """ |
| 686 | A function that wait for any requested status, for any set of maas |
| 687 | machines. |
| 688 | |
| 689 | If no kwargs has been passed - will try to wait ALL |
| 690 | defined in salt::maas::region::machines |
| 691 | |
| 692 | See readme file for more examples. |
| 693 | CLI Example: |
| 694 | .. code-block:: bash |
| 695 | |
| 696 | salt-call state.apply maas.machines.wait_for_deployed |
| 697 | |
| 698 | :param kwargs: |
| 699 | timeout: in s; Global timeout for wait |
| 700 | poll_time: in s;Sleep time, between retry |
| 701 | req_status: string; Polling status |
| 702 | machines: list; machine names |
| 703 | ignore_machines: list; machine names |
| 704 | :ret: True |
| 705 | Exception - if something fail/timeout reached |
| 706 | """ |
| 707 | timeout = kwargs.get("timeout", 60 * 120) |
| 708 | poll_time = kwargs.get("poll_time", 30) |
| 709 | req_status = kwargs.get("req_status", "Ready") |
| 710 | to_discover = kwargs.get("machines", None) |
| 711 | ignore_machines = kwargs.get("ignore_machines", None) |
| 712 | if not to_discover: |
| 713 | try: |
| 714 | to_discover = __salt__['config.get']('maas')['region'][ |
| 715 | 'machines'].keys() |
| 716 | except KeyError: |
| 717 | LOG.warning("No defined machines!") |
| 718 | return True |
| 719 | total = copy.deepcopy(to_discover) or [] |
| 720 | if ignore_machines and total: |
| 721 | total = [x for x in to_discover if x not in ignore_machines] |
| 722 | started_at = time.time() |
| 723 | while len(total) <= len(to_discover): |
| 724 | for m in to_discover: |
| 725 | for discovered in MachinesStatus.execute()['machines']: |
| 726 | if m == discovered['hostname'] and \ |
| 727 | discovered['status'] == req_status.lower(): |
| 728 | if m in total: total.remove(m) |
| 729 | if len(total) <= 0: |
| 730 | LOG.debug( |
| 731 | "Machines:{} are:{}".format(to_discover, req_status)) |
| 732 | return True |
| 733 | if (timeout - (time.time() - started_at)) <= 0: |
| 734 | raise Exception( |
| 735 | 'Machines:{}not in {} state'.format(total, req_status)) |
| 736 | LOG.info( |
| 737 | "Waiting status:{} " |
| 738 | "for machines:{}" |
| 739 | "\nsleep for:{}s " |
| 740 | "Timeout:{}s".format(req_status, total, poll_time, timeout)) |
| 741 | time.sleep(poll_time) |
| 742 | |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 743 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 744 | def process_fabrics(): |
| 745 | return Fabric().process() |
| 746 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 747 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 748 | def process_subnets(): |
| 749 | return Subnet().process() |
| 750 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 751 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 752 | def process_dhcp_snippets(): |
| 753 | return DHCPSnippet().process() |
| 754 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 755 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 756 | def process_package_repositories(): |
| 757 | return PacketRepository().process() |
| 758 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 759 | |
Krzysztof Szukiełojć | 889eee9 | 2017-04-14 11:45:35 +0200 | [diff] [blame] | 760 | def process_devices(*args): |
| 761 | return Device().process(*args) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 762 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 763 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 764 | def process_machines(*args): |
| 765 | return Machine().process(*args) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 766 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 767 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 768 | def process_assign_machines_ip(*args): |
| 769 | return AssignMachinesIP().process(*args) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 770 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 771 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 772 | def machines_status(*args): |
| 773 | return MachinesStatus.execute(*args) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 774 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 775 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 776 | def deploy_machines(*args): |
| 777 | return DeployMachines().process(*args) |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 778 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 779 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 780 | def process_boot_resources(): |
| 781 | return BootResource().process() |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 782 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 783 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 784 | def process_maas_config(): |
| 785 | return MaasConfig().process() |
| 786 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 787 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 788 | def process_commissioning_scripts(): |
| 789 | return CommissioningScripts().process() |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 790 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 791 | |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 792 | def process_domain(): |
| 793 | return Domain().process() |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 794 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 795 | |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 796 | def process_sshprefs(): |
| 797 | return SSHPrefs().process() |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame^] | 798 | |
| 799 | |
| 800 | def wait_for_machine_status(**kwargs): |
| 801 | return MachinesStatus.wait_for_machine_status(**kwargs) |