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'), |
Alexandru Avadanii | 08ffc3f | 2017-12-17 06:30:27 +0100 | [diff] [blame] | 55 | (15, 'Failed disk erasing'), (16, 'Rescue mode'), |
| 56 | (17, 'Entering rescue mode'), (18, 'Failed to enter rescue mode'), |
| 57 | (19, 'Exiting rescue mode'), (20, 'Failed to exit rescue mode'), |
| 58 | (21, 'Testing'), (22, 'Failed testing')]) |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame] | 59 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 60 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 61 | def _format_data(data): |
| 62 | class Lazy: |
| 63 | def __str__(self): |
| 64 | return ' '.join(['{0}={1}'.format(k, v) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 65 | for k, v in data.iteritems()]) |
Krzysztof Szukiełojć | 52cc6b0 | 2017-04-06 09:53:43 +0200 | [diff] [blame] | 66 | return Lazy() |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 67 | |
| 68 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 69 | def _create_maas_client(): |
| 70 | global APIKEY_FILE |
| 71 | try: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 72 | api_token = file(APIKEY_FILE).read().splitlines()[-1].strip()\ |
| 73 | .split(':') |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 74 | except: |
| 75 | LOG.exception('token') |
| 76 | auth = MAASOAuth(*api_token) |
| 77 | api_url = 'http://localhost:5240/MAAS' |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 78 | dispatcher = MAASDispatcher() |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 79 | return MAASClient(auth, dispatcher, api_url) |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 80 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 81 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 82 | class MaasObject(object): |
| 83 | def __init__(self): |
| 84 | self._maas = _create_maas_client() |
| 85 | self._extra_data_urls = {} |
| 86 | self._extra_data = {} |
| 87 | self._update = False |
| 88 | self._element_key = 'name' |
| 89 | self._update_key = 'id' |
| 90 | |
| 91 | def send(self, data): |
| 92 | LOG.info('%s %s', self.__class__.__name__.lower(), _format_data(data)) |
| 93 | if self._update: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 94 | return self._maas.put( |
| 95 | self._update_url.format(data[self._update_key]), **data).read() |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 96 | if isinstance(self._create_url, tuple): |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 97 | return self._maas.post(self._create_url[0].format(**data), |
| 98 | *self._create_url[1:], **data).read() |
Krzysztof Szukiełojć | 76d9a5c | 2017-04-14 12:00:42 +0200 | [diff] [blame] | 99 | return self._maas.post(self._create_url.format(**data), |
| 100 | None, **data).read() |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 101 | |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 102 | def process(self, objects_name=None): |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 103 | # FIXME: probably, should be extended with "skipped" return. |
| 104 | # For example, currently "DEPLOYED" nodes are skipped, and no changes |
| 105 | # applied - but they fall into 'updated' list. |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 106 | ret = { |
| 107 | 'success': [], |
| 108 | 'errors': {}, |
| 109 | 'updated': [], |
| 110 | } |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 111 | try: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 112 | config = __salt__['config.get']('maas') |
| 113 | for part in self._config_path.split('.'): |
| 114 | config = config.get(part, {}) |
| 115 | extra = {} |
| 116 | for name, url_call in self._extra_data_urls.iteritems(): |
| 117 | key = 'id' |
| 118 | key_name = 'name' |
| 119 | if isinstance(url_call, tuple): |
| 120 | if len(url_call) == 2: |
| 121 | url_call, key = url_call[:] |
| 122 | else: |
| 123 | url_call, key, key_name = url_call[:] |
| 124 | json_res = json.loads(self._maas.get(url_call).read()) |
| 125 | if key: |
| 126 | extra[name] = {v[key_name]: v[key] for v in json_res} |
| 127 | else: |
Krzysztof Szukiełojć | 52cc6b0 | 2017-04-06 09:53:43 +0200 | [diff] [blame] | 128 | extra[name] = {v[key_name]: v for v in json_res} |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 129 | if self._all_elements_url: |
| 130 | all_elements = {} |
| 131 | elements = self._maas.get(self._all_elements_url).read() |
| 132 | res_json = json.loads(elements) |
| 133 | for element in res_json: |
| 134 | if isinstance(element, (str, unicode)): |
| 135 | all_elements[element] = {} |
| 136 | else: |
| 137 | all_elements[element[self._element_key]] = element |
| 138 | else: |
| 139 | all_elements = {} |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 140 | |
| 141 | def process_single(name, config_data): |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 142 | self._update = False |
| 143 | try: |
| 144 | data = self.fill_data(name, config_data, **extra) |
| 145 | if data is None: |
| 146 | ret['updated'].append(name) |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 147 | return |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 148 | if name in all_elements: |
| 149 | self._update = True |
| 150 | data = self.update(data, all_elements[name]) |
| 151 | self.send(data) |
| 152 | ret['updated'].append(name) |
| 153 | else: |
| 154 | self.send(data) |
| 155 | ret['success'].append(name) |
| 156 | except urllib2.HTTPError as e: |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 157 | # FIXME add exception's for response: |
| 158 | # '{"mode": ["Interface is already set to DHCP."]} |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 159 | etxt = e.read() |
Krzysztof Szukiełojć | 199d5af | 2017-04-12 13:23:10 +0200 | [diff] [blame] | 160 | LOG.error('Failed for object %s reason %s', name, etxt) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 161 | ret['errors'][name] = str(etxt) |
| 162 | except Exception as e: |
Krzysztof Szukiełojć | 199d5af | 2017-04-12 13:23:10 +0200 | [diff] [blame] | 163 | LOG.error('Failed for object %s reason %s', name, e) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 164 | ret['errors'][name] = str(e) |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 165 | if objects_name is not None: |
| 166 | if ',' in objects_name: |
| 167 | objects_name = objects_name.split(',') |
| 168 | else: |
| 169 | objects_name = [objects_name] |
| 170 | for object_name in objects_name: |
| 171 | process_single(object_name, config[object_name]) |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 172 | else: |
| 173 | for name, config_data in config.iteritems(): |
| 174 | process_single(name, config_data) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 175 | except Exception as e: |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 176 | LOG.exception('Error Global') |
| 177 | raise |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 178 | if ret['errors']: |
Jiri Broulik | e30a60f | 2018-04-09 21:15:10 +0200 | [diff] [blame] | 179 | if 'already exists' in str(ret['errors']): |
| 180 | ret['success'] = ret['errors'] |
| 181 | ret['errors'] = {} |
| 182 | else: |
| 183 | raise Exception(ret) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 184 | return ret |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 185 | |
| 186 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 187 | class Fabric(MaasObject): |
| 188 | def __init__(self): |
| 189 | super(Fabric, self).__init__() |
| 190 | self._all_elements_url = u'api/2.0/fabrics/' |
| 191 | self._create_url = u'api/2.0/fabrics/' |
| 192 | self._update_url = u'api/2.0/fabrics/{0}/' |
| 193 | self._config_path = 'region.fabrics' |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 194 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 195 | def fill_data(self, name, fabric): |
| 196 | data = { |
| 197 | 'name': name, |
| 198 | 'description': fabric.get('description', ''), |
| 199 | } |
| 200 | if 'class_type' in fabric: |
| 201 | data['class_type'] = fabric.get('class_type'), |
| 202 | return data |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 203 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 204 | def update(self, new, old): |
| 205 | new['id'] = str(old['id']) |
| 206 | return new |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 207 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 208 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 209 | class Subnet(MaasObject): |
| 210 | def __init__(self): |
| 211 | super(Subnet, self).__init__() |
| 212 | self._all_elements_url = u'api/2.0/subnets/' |
| 213 | self._create_url = u'api/2.0/subnets/' |
| 214 | self._update_url = u'api/2.0/subnets/{0}/' |
| 215 | self._config_path = 'region.subnets' |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 216 | self._extra_data_urls = {'fabrics': u'api/2.0/fabrics/'} |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 217 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 218 | def fill_data(self, name, subnet, fabrics): |
| 219 | data = { |
| 220 | 'name': name, |
| 221 | 'fabric': str(fabrics[subnet.get('fabric', '')]), |
| 222 | 'cidr': subnet.get('cidr'), |
| 223 | 'gateway_ip': subnet['gateway_ip'], |
| 224 | } |
| 225 | self._iprange = subnet['iprange'] |
| 226 | return data |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 227 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 228 | def update(self, new, old): |
| 229 | new['id'] = str(old['id']) |
| 230 | return new |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 231 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 232 | def send(self, data): |
| 233 | response = super(Subnet, self).send(data) |
| 234 | res_json = json.loads(response) |
| 235 | self._process_iprange(res_json['id']) |
| 236 | return response |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 237 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 238 | def _process_iprange(self, subnet_id): |
| 239 | ipranges = json.loads(self._maas.get(u'api/2.0/ipranges/').read()) |
| 240 | LOG.warn('all %s ipranges %s', subnet_id, ipranges) |
| 241 | update = False |
| 242 | old_data = None |
| 243 | for iprange in ipranges: |
| 244 | if iprange['subnet']['id'] == subnet_id: |
| 245 | update = True |
| 246 | old_data = iprange |
| 247 | break |
| 248 | data = { |
| 249 | 'start_ip': self._iprange.get('start'), |
| 250 | 'end_ip': self._iprange.get('end'), |
| 251 | 'subnet': str(subnet_id), |
| 252 | 'type': self._iprange.get('type', 'dynamic') |
| 253 | } |
| 254 | LOG.warn('INFO: %s\n OLD: %s', data, old_data) |
| 255 | LOG.info('iprange %s', _format_data(data)) |
| 256 | if update: |
| 257 | LOG.warn('UPDATING %s %s', data, old_data) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 258 | self._maas.put(u'api/2.0/ipranges/{0}/'.format(old_data['id']), |
| 259 | **data) |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 260 | else: |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 261 | self._maas.post(u'api/2.0/ipranges/', None, **data) |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 262 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 263 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 264 | class DHCPSnippet(MaasObject): |
| 265 | def __init__(self): |
| 266 | super(DHCPSnippet, self).__init__() |
| 267 | self._all_elements_url = u'api/2.0/dhcp-snippets/' |
| 268 | self._create_url = u'api/2.0/dhcp-snippets/' |
| 269 | self._update_url = u'api/2.0/dhcp-snippets/{0}/' |
| 270 | self._config_path = 'region.dhcp_snippets' |
| 271 | self._extra_data_urls = {'subnets': u'api/2.0/subnets/'} |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 272 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 273 | def fill_data(self, name, snippet, subnets): |
| 274 | data = { |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 275 | 'name': name, |
| 276 | 'value': snippet['value'], |
| 277 | 'description': snippet['description'], |
| 278 | 'enabled': str(snippet['enabled'] and 1 or 0), |
| 279 | 'subnet': str(subnets[snippet['subnet']]), |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 280 | } |
| 281 | return data |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 282 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 283 | def update(self, new, old): |
| 284 | new['id'] = str(old['id']) |
| 285 | return new |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 286 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 287 | |
Jiri Broulik | e30a60f | 2018-04-09 21:15:10 +0200 | [diff] [blame] | 288 | class Boot_source(MaasObject): |
| 289 | def __init__(self): |
| 290 | super(Boot_source, self).__init__() |
| 291 | self._all_elements_url = u'api/2.0/boot-sources/' |
| 292 | self._create_url = u'api/2.0/boot-sources/' |
| 293 | self._update_url = u'api/2.0/boot-sources/{0}/' |
| 294 | self._config_path = 'region.boot_sources' |
| 295 | self._element_key = 'id' |
| 296 | |
| 297 | def fill_data(self, name, boot_source): |
| 298 | data = { |
| 299 | 'name': name, |
| 300 | 'url': boot_source.get('url', ''), |
| 301 | 'keyring_filename': boot_source.get('keyring_file', ''), |
| 302 | } |
| 303 | return data |
| 304 | |
| 305 | def update(self, new, old): |
| 306 | new['id'] = str(old['id']) |
| 307 | return new |
| 308 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 309 | class PacketRepository(MaasObject): |
| 310 | def __init__(self): |
| 311 | super(PacketRepository, self).__init__() |
| 312 | self._all_elements_url = u'api/2.0/package-repositories/' |
| 313 | self._create_url = u'api/2.0/package-repositories/' |
| 314 | self._update_url = u'api/2.0/package-repositories/{0}/' |
| 315 | self._config_path = 'region.package_repositories' |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 316 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 317 | def fill_data(self, name, package_repository): |
| 318 | data = { |
| 319 | 'name': name, |
| 320 | 'url': package_repository['url'], |
| 321 | 'distributions': package_repository['distributions'], |
| 322 | 'components': package_repository['components'], |
| 323 | 'arches': package_repository['arches'], |
| 324 | 'key': package_repository['key'], |
| 325 | 'enabled': str(package_repository['enabled'] and 1 or 0), |
| 326 | } |
| 327 | if 'disabled_pockets' in package_repository: |
| 328 | data['disabled_pockets'] = package_repository['disable_pockets'], |
| 329 | return data |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 330 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 331 | def update(self, new, old): |
| 332 | new['id'] = str(old['id']) |
| 333 | return new |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame] | 334 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 335 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 336 | class Device(MaasObject): |
| 337 | def __init__(self): |
| 338 | super(Device, self).__init__() |
| 339 | self._all_elements_url = u'api/2.0/devices/' |
| 340 | self._create_url = u'api/2.0/devices/' |
| 341 | self._update_url = u'api/2.0/devices/{0}/' |
| 342 | self._config_path = 'region.devices' |
| 343 | self._element_key = 'hostname' |
| 344 | self._update_key = 'system_id' |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 345 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 346 | def fill_data(self, name, device_data): |
| 347 | data = { |
| 348 | 'mac_addresses': device_data['mac'], |
| 349 | 'hostname': name, |
| 350 | } |
| 351 | self._interface = device_data['interface'] |
| 352 | return data |
| 353 | |
| 354 | def update(self, new, old): |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 355 | old_macs = set(v['mac_address'].lower() for v in old['interface_set']) |
| 356 | if new['mac_addresses'].lower() not in old_macs: |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 357 | self._update = False |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 358 | LOG.info('Mac changed deleting old device %s', old['system_id']) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 359 | self._maas.delete(u'api/2.0/devices/{0}/'.format(old['system_id'])) |
| 360 | else: |
| 361 | new[self._update_key] = str(old[self._update_key]) |
| 362 | return new |
| 363 | |
| 364 | def send(self, data): |
| 365 | response = super(Device, self).send(data) |
| 366 | resp_json = json.loads(response) |
| 367 | system_id = resp_json['system_id'] |
| 368 | iface_id = resp_json['interface_set'][0]['id'] |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 369 | self._link_interface(system_id, iface_id) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 370 | return response |
| 371 | |
| 372 | def _link_interface(self, system_id, interface_id): |
| 373 | data = { |
| 374 | 'mode': self._interface.get('mode', 'STATIC'), |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 375 | 'subnet': self._interface['subnet'], |
| 376 | 'ip_address': self._interface['ip_address'], |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 377 | } |
| 378 | if 'default_gateway' in self._interface: |
| 379 | data['default_gateway'] = self._interface.get('default_gateway') |
| 380 | if self._update: |
| 381 | data['force'] = '1' |
| 382 | 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] | 383 | _format_data(data)) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 384 | self._maas.post(u'/api/2.0/nodes/{0}/interfaces/{1}/' |
| 385 | .format(system_id, interface_id), 'link_subnet', |
| 386 | **data) |
| 387 | |
| 388 | |
| 389 | class Machine(MaasObject): |
| 390 | def __init__(self): |
| 391 | super(Machine, self).__init__() |
| 392 | self._all_elements_url = u'api/2.0/machines/' |
| 393 | self._create_url = u'api/2.0/machines/' |
| 394 | self._update_url = u'api/2.0/machines/{0}/' |
| 395 | self._config_path = 'region.machines' |
| 396 | self._element_key = 'hostname' |
| 397 | self._update_key = 'system_id' |
| 398 | |
| 399 | def fill_data(self, name, machine_data): |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 400 | power_data = machine_data['power_parameters'] |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 401 | machine_pxe_mac = machine_data.get('pxe_interface_mac', None) |
| 402 | if machine_data.get("interface", None): |
| 403 | LOG.warning( |
| 404 | "Old machine-describe detected! " |
| 405 | "Please read documentation for " |
| 406 | "'salt-formulas/maas' for migration!") |
| 407 | machine_pxe_mac = machine_data['interface'].get('mac', None) |
| 408 | if not machine_pxe_mac: |
| 409 | raise Exception("PXE MAC for machine:{} not defined".format(name)) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 410 | data = { |
| 411 | 'hostname': name, |
| 412 | 'architecture': machine_data.get('architecture', 'amd64/generic'), |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 413 | 'mac_addresses': machine_pxe_mac, |
mkraynov | e95cdb6 | 2018-05-08 14:17:18 +0400 | [diff] [blame^] | 414 | 'power_type': power_data.get('power_type', 'manual'), |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 415 | } |
mkraynov | e95cdb6 | 2018-05-08 14:17:18 +0400 | [diff] [blame^] | 416 | if 'power_address' in power_data: |
| 417 | data['power_parameters_power_address'] = power_data['power_address'] |
Ondrej Smola | 455003c | 2017-06-01 22:53:39 +0200 | [diff] [blame] | 418 | if 'power_driver' in power_data: |
| 419 | data['power_parameters_power_driver'] = power_data['power_driver'] |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 420 | if 'power_user' in power_data: |
| 421 | data['power_parameters_power_user'] = power_data['power_user'] |
| 422 | if 'power_password' in power_data: |
| 423 | data['power_parameters_power_pass'] = \ |
| 424 | power_data['power_password'] |
Petr Ruzicka | 5fe9674 | 2017-11-10 14:22:24 +0100 | [diff] [blame] | 425 | if 'power_id' in power_data: |
| 426 | data['power_parameters_power_id'] = power_data['power_id'] |
mkraynov | 47c087a | 2018-04-26 13:23:31 +0400 | [diff] [blame] | 427 | if 'power_nova_id' in power_data: |
| 428 | data['power_parameters_nova_id'] = power_data['power_nova_id'] |
| 429 | if 'power_os_tenantname' in power_data: |
| 430 | data['power_parameters_os_tenantname'] = power_data['power_os_tenantname'] |
| 431 | if 'power_os_username' in power_data: |
| 432 | data['power_parameters_os_username'] = power_data['power_os_username'] |
| 433 | if 'power_os_password' in power_data: |
| 434 | data['power_parameters_os_password'] = power_data['power_os_password'] |
| 435 | if 'power_os_authurl' in power_data: |
| 436 | data['power_parameters_os_authurl'] = power_data['power_os_authurl'] |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 437 | return data |
| 438 | |
| 439 | def update(self, new, old): |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 440 | old_macs = set(v['mac_address'].lower() for v in old['interface_set']) |
| 441 | if new['mac_addresses'].lower() not in old_macs: |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 442 | self._update = False |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 443 | LOG.info('Mac changed deleting old machine %s', old['system_id']) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 444 | self._maas.delete(u'api/2.0/machines/{0}/' |
| 445 | .format(old['system_id'])) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 446 | else: |
| 447 | new[self._update_key] = str(old[self._update_key]) |
| 448 | return new |
| 449 | |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 450 | |
| 451 | class AssignMachinesIP(MaasObject): |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 452 | # FIXME |
Krzysztof Szukiełojć | d6ee1a0 | 2017-04-07 14:01:30 +0200 | [diff] [blame] | 453 | READY = 4 |
Andrey | ef15699 | 2017-07-03 14:54:03 -0500 | [diff] [blame] | 454 | DEPLOYED = 6 |
Krzysztof Szukiełojć | d6ee1a0 | 2017-04-07 14:01:30 +0200 | [diff] [blame] | 455 | |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 456 | def __init__(self): |
| 457 | super(AssignMachinesIP, self).__init__() |
| 458 | self._all_elements_url = None |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 459 | self._create_url = \ |
| 460 | (u'/api/2.0/nodes/{system_id}/interfaces/{interface_id}/', |
| 461 | 'link_subnet') |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 462 | self._config_path = 'region.machines' |
| 463 | self._element_key = 'hostname' |
| 464 | self._update_key = 'system_id' |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 465 | self._extra_data_urls = {'machines': (u'api/2.0/machines/', |
| 466 | None, 'hostname')} |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 467 | |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 468 | def _data_old(self, _interface, _machine): |
| 469 | """ |
| 470 | _interface = { |
| 471 | "mac": "11:22:33:44:55:77", |
| 472 | "mode": "STATIC", |
| 473 | "ip": "2.2.3.15", |
| 474 | "subnet": "subnet1", |
| 475 | "gateway": "2.2.3.2", |
| 476 | } |
| 477 | :param data: |
| 478 | :return: |
| 479 | """ |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 480 | data = { |
| 481 | 'mode': 'STATIC', |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 482 | 'subnet': str(_interface.get('subnet')), |
| 483 | 'ip_address': str(_interface.get('ip')), |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 484 | } |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 485 | if 'gateway' in _interface: |
| 486 | data['default_gateway'] = _interface.get('gateway') |
Krzysztof Szukiełojć | 9449af1 | 2017-04-11 14:01:30 +0200 | [diff] [blame] | 487 | data['force'] = '1' |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 488 | data['system_id'] = str(_machine['system_id']) |
| 489 | data['interface_id'] = str(_machine['interface_set'][0]['id']) |
Krzysztof Szukiełojć | d57a32d | 2017-04-04 11:25:02 +0200 | [diff] [blame] | 490 | return data |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 491 | |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 492 | def _get_nic_id_by_mac(self, machine, req_mac=None): |
| 493 | data = {} |
| 494 | for nic in machine['interface_set']: |
| 495 | data[nic['mac_address']] = nic['id'] |
| 496 | if req_mac: |
| 497 | if req_mac in data.keys(): |
| 498 | return data[req_mac] |
| 499 | else: |
| 500 | raise Exception('NIC with mac:{} not found at ' |
| 501 | 'node:{}'.format(req_mac, machine['fqdn'])) |
| 502 | return data |
| 503 | |
| 504 | def _disconnect_all_nic(self, machine): |
| 505 | """ |
| 506 | Maas will fail, in case same config's will be to apply |
| 507 | on different interfaces. In same time - not possible to push |
| 508 | whole network schema at once. Before configuring - need to clean-up |
| 509 | everything |
| 510 | :param machine: |
| 511 | :return: |
| 512 | """ |
| 513 | for nic in machine['interface_set']: |
| 514 | LOG.debug("Disconnecting interface:{}".format(nic['mac_address'])) |
| 515 | try: |
| 516 | self._maas.post( |
| 517 | u'/api/2.0/nodes/{}/interfaces/{}/'.format( |
| 518 | machine['system_id'], nic['id']), 'disconnect') |
| 519 | except Exception as e: |
| 520 | LOG.error("Failed to disconnect interface:{} on node:{}".format( |
| 521 | nic['mac_address'], machine['fqdn'])) |
| 522 | raise Exception(str(e)) |
| 523 | |
| 524 | def _process_interface(self, nic_data, machine): |
| 525 | """ |
| 526 | Process exactly one interface: |
| 527 | - update interface |
| 528 | - link to network |
| 529 | These functions are self-complementary, and do not require an |
| 530 | external "process" method. Those broke old-MaasObject logic, |
| 531 | though make functions more simple in case iterable tasks. |
| 532 | """ |
| 533 | nic_id = self._get_nic_id_by_mac(machine, nic_data['mac']) |
| 534 | |
| 535 | # Process op=link_subnet |
| 536 | link_data = {} |
| 537 | _mode = nic_data.get('mode', 'AUTO').upper() |
| 538 | if _mode == 'STATIC': |
| 539 | link_data = { |
| 540 | 'mode': 'STATIC', |
| 541 | 'subnet': str(nic_data.get('subnet')), |
| 542 | 'ip_address': str(nic_data.get('ip')), |
| 543 | 'default_gateway': str(nic_data.get('gateway', "")), |
| 544 | } |
| 545 | elif _mode == 'DHCP': |
| 546 | link_data = { |
| 547 | 'mode': 'DHCP', |
| 548 | 'subnet': str(nic_data.get('subnet')), |
| 549 | } |
| 550 | elif _mode == 'AUTO': |
| 551 | link_data = {'mode': 'AUTO', |
| 552 | 'default_gateway': str(nic_data.get('gateway', "")), } |
| 553 | elif _mode in ('LINK_UP', 'UNCONFIGURED'): |
| 554 | link_data = {'mode': 'LINK_UP'} |
| 555 | else: |
| 556 | raise Exception('Wrong IP mode:{}' |
| 557 | ' for node:{}'.format(_mode, machine['fqdn'])) |
| 558 | link_data['force'] = str(1) |
| 559 | |
| 560 | physical_data = {"name": nic_data.get("name", ""), |
| 561 | "tags": nic_data.get('tags', ""), |
| 562 | "vlan": nic_data.get('vlan', "")} |
| 563 | |
| 564 | try: |
| 565 | # Cleanup-old definition |
| 566 | LOG.debug("Processing {}:{},{}".format(nic_data['mac'], link_data, |
| 567 | physical_data)) |
| 568 | # "link_subnet" and "fill all other data" - its 2 different |
| 569 | # operations. So, first we update NIC: |
| 570 | self._maas.put( |
| 571 | u'/api/2.0/nodes/{}/interfaces/{}/'.format(machine['system_id'], |
| 572 | nic_id), |
| 573 | **physical_data) |
| 574 | # And then, link subnet configuration: |
| 575 | self._maas.post( |
| 576 | u'/api/2.0/nodes/{}/interfaces/{}/'.format(machine['system_id'], |
| 577 | nic_id), |
| 578 | 'link_subnet', **link_data) |
| 579 | except Exception as e: |
| 580 | LOG.error("Failed to process interface:{} on node:{}".format( |
| 581 | nic_data['mac'], machine['fqdn'])) |
| 582 | raise Exception(str(e)) |
| 583 | |
| 584 | def fill_data(self, name, data, machines): |
| 585 | machine = machines[name] |
| 586 | if machine['status'] == self.DEPLOYED: |
| 587 | LOG.debug("Skipping node:{} " |
| 588 | "since it in status:DEPLOYED".format(name)) |
| 589 | return |
| 590 | if machine['status'] != self.READY: |
| 591 | raise Exception('Machine:{} not in status:READY'.format(name)) |
| 592 | # backward comparability, for old schema |
| 593 | if data.get("interface", None): |
| 594 | if 'ip' not in data["interface"]: |
| 595 | LOG.info("No IP NIC definition for:{}".format(name)) |
| 596 | return |
| 597 | LOG.warning( |
| 598 | "Old machine-describe detected! " |
| 599 | "Please read documentation " |
| 600 | "'salt-formulas/maas' for migration!") |
| 601 | return self._data_old(data['interface'], machines[name]) |
| 602 | # NewSchema processing: |
| 603 | # Warning: old-style MaasObject.process still be called, but |
| 604 | # with empty data for process. |
| 605 | interfaces = data.get('interfaces', {}) |
| 606 | if len(interfaces.keys()) == 0: |
| 607 | LOG.info("No IP NIC definition for:{}".format(name)) |
| 608 | return |
| 609 | LOG.info('%s for %s', self.__class__.__name__.lower(), |
| 610 | machine['fqdn']) |
| 611 | self._disconnect_all_nic(machine) |
| 612 | for key, value in sorted(interfaces.iteritems()): |
| 613 | self._process_interface(value, machine) |
| 614 | |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 615 | |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 616 | class DeployMachines(MaasObject): |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame] | 617 | # FIXME |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 618 | READY = 4 |
Andrey | ef15699 | 2017-07-03 14:54:03 -0500 | [diff] [blame] | 619 | DEPLOYED = 6 |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 620 | |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 621 | def __init__(self): |
| 622 | super(DeployMachines, self).__init__() |
| 623 | self._all_elements_url = None |
| 624 | self._create_url = (u'api/2.0/machines/{system_id}/', 'deploy') |
| 625 | self._config_path = 'region.machines' |
| 626 | self._element_key = 'hostname' |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 627 | self._extra_data_urls = {'machines': (u'api/2.0/machines/', |
| 628 | None, 'hostname')} |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 629 | |
| 630 | def fill_data(self, name, machine_data, machines): |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 631 | machine = machines[name] |
Andrey | ef15699 | 2017-07-03 14:54:03 -0500 | [diff] [blame] | 632 | if machine['status'] == self.DEPLOYED: |
| 633 | return |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 634 | if machine['status'] != self.READY: |
| 635 | raise Exception('Not in ready state') |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 636 | data = { |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 637 | 'system_id': machine['system_id'], |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 638 | } |
Krzysztof Szukiełojć | 32677bf | 2017-04-13 11:04:25 +0200 | [diff] [blame] | 639 | if 'distro_series' in machine_data: |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 640 | data['distro_series'] = machine_data['distro_series'] |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 641 | if 'hwe_kernel' in machine_data: |
Krzysztof Szukiełojć | 008d7d4 | 2017-04-05 15:26:01 +0200 | [diff] [blame] | 642 | data['hwe_kernel'] = machine_data['hwe_kernel'] |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 643 | return data |
| 644 | |
Krzysztof Szukiełojć | 33f9b59 | 2017-04-12 11:43:50 +0200 | [diff] [blame] | 645 | def send(self, data): |
| 646 | LOG.info('%s %s', self.__class__.__name__.lower(), _format_data(data)) |
Krzysztof Szukiełojć | 3b7516d | 2017-04-12 11:52:55 +0200 | [diff] [blame] | 647 | 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] | 648 | return self._maas.post(self._create_url[0].format(**data), |
| 649 | *self._create_url[1:], **data).read() |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 650 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 651 | class BootResource(MaasObject): |
| 652 | def __init__(self): |
| 653 | super(BootResource, self).__init__() |
| 654 | self._all_elements_url = u'api/2.0/boot-resources/' |
| 655 | self._create_url = u'api/2.0/boot-resources/' |
| 656 | self._update_url = u'api/2.0/boot-resources/{0}/' |
| 657 | self._config_path = 'region.boot_resources' |
| 658 | |
| 659 | def fill_data(self, name, boot_data): |
| 660 | sha256 = hashlib.sha256() |
| 661 | sha256.update(file(boot_data['content']).read()) |
| 662 | data = { |
| 663 | 'name': name, |
| 664 | 'title': boot_data['title'], |
| 665 | 'architecture': boot_data['architecture'], |
| 666 | 'filetype': boot_data['filetype'], |
| 667 | 'size': str(os.path.getsize(boot_data['content'])), |
| 668 | 'sha256': sha256.hexdigest(), |
| 669 | 'content': io.open(boot_data['content']), |
| 670 | } |
| 671 | return data |
| 672 | |
| 673 | def update(self, new, old): |
| 674 | self._update = False |
| 675 | return new |
| 676 | |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 677 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 678 | class CommissioningScripts(MaasObject): |
| 679 | def __init__(self): |
| 680 | super(CommissioningScripts, self).__init__() |
| 681 | self._all_elements_url = u'api/2.0/commissioning-scripts/' |
| 682 | self._create_url = u'api/2.0/commissioning-scripts/' |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 683 | self._config_path = 'region.commissioning_scripts' |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 684 | self._update_url = u'api/2.0/commissioning-scripts/{0}' |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 685 | self._update_key = 'name' |
| 686 | |
| 687 | def fill_data(self, name, file_path): |
| 688 | data = { |
| 689 | 'name': name, |
| 690 | 'content': io.open(file_path), |
| 691 | } |
| 692 | return data |
| 693 | |
| 694 | def update(self, new, old): |
| 695 | return new |
| 696 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 697 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 698 | class MaasConfig(MaasObject): |
| 699 | def __init__(self): |
| 700 | super(MaasConfig, self).__init__() |
| 701 | self._all_elements_url = None |
| 702 | self._create_url = (u'api/2.0/maas/', u'set_config') |
| 703 | self._config_path = 'region.maas_config' |
| 704 | |
| 705 | def fill_data(self, name, value): |
| 706 | data = { |
| 707 | 'name': name, |
Krzysztof Szukiełojć | a6352a4 | 2017-03-17 14:21:57 +0100 | [diff] [blame] | 708 | 'value': str(value), |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 709 | } |
| 710 | return data |
| 711 | |
| 712 | def update(self, new, old): |
| 713 | self._update = False |
| 714 | return new |
| 715 | |
| 716 | |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 717 | class SSHPrefs(MaasObject): |
| 718 | def __init__(self): |
| 719 | super(SSHPrefs, self).__init__() |
| 720 | self._all_elements_url = None |
| 721 | self._create_url = u'api/2.0/account/prefs/sshkeys/' |
| 722 | self._config_path = 'region.sshprefs' |
| 723 | self._element_key = 'hostname' |
| 724 | self._update_key = 'system_id' |
| 725 | |
| 726 | def fill_data(self, value): |
| 727 | data = { |
| 728 | 'key': value, |
| 729 | } |
| 730 | return data |
| 731 | |
| 732 | def process(self): |
| 733 | config = __salt__['config.get']('maas') |
| 734 | for part in self._config_path.split('.'): |
| 735 | config = config.get(part, {}) |
| 736 | extra = {} |
| 737 | for name, url_call in self._extra_data_urls.iteritems(): |
| 738 | key = 'id' |
| 739 | if isinstance(url_call, tuple): |
| 740 | url_call, key = url_call[:] |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 741 | json_res = json.loads(self._maas.get(url_call).read()) |
| 742 | extra[name] = {v['name']: v[key] for v in json_res} |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 743 | if self._all_elements_url: |
| 744 | all_elements = {} |
| 745 | elements = self._maas.get(self._all_elements_url).read() |
| 746 | res_json = json.loads(elements) |
| 747 | for element in res_json: |
| 748 | if isinstance(element, (str, unicode)): |
| 749 | all_elements[element] = {} |
| 750 | else: |
| 751 | all_elements[element[self._element_key]] = element |
| 752 | else: |
| 753 | all_elements = {} |
| 754 | ret = { |
| 755 | 'success': [], |
| 756 | 'errors': {}, |
| 757 | 'updated': [], |
| 758 | } |
| 759 | for config_data in config: |
| 760 | name = config_data[:10] |
| 761 | try: |
| 762 | data = self.fill_data(config_data, **extra) |
| 763 | self.send(data) |
| 764 | ret['success'].append(name) |
| 765 | except urllib2.HTTPError as e: |
| 766 | etxt = e.read() |
| 767 | LOG.exception('Failed for object %s reason %s', name, etxt) |
| 768 | ret['errors'][name] = str(etxt) |
| 769 | except Exception as e: |
| 770 | LOG.exception('Failed for object %s reason %s', name, e) |
| 771 | ret['errors'][name] = str(e) |
| 772 | if ret['errors']: |
| 773 | raise Exception(ret) |
| 774 | return ret |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 775 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 776 | |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 777 | class Domain(MaasObject): |
| 778 | def __init__(self): |
| 779 | super(Domain, self).__init__() |
| 780 | self._all_elements_url = u'/api/2.0/domains/' |
| 781 | self._create_url = u'/api/2.0/domains/' |
| 782 | self._config_path = 'region.domain' |
| 783 | self._update_url = u'/api/2.0/domains/{0}/' |
| 784 | |
| 785 | def fill_data(self, value): |
| 786 | data = { |
| 787 | 'name': value, |
| 788 | } |
| 789 | self._update = True |
| 790 | return data |
| 791 | |
| 792 | def update(self, new, old): |
| 793 | new['id'] = str(old['id']) |
| 794 | new['authoritative'] = str(old['authoritative']) |
| 795 | return new |
| 796 | |
| 797 | def process(self): |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 798 | ret = { |
| 799 | 'success': [], |
| 800 | 'errors': {}, |
| 801 | 'updated': [], |
| 802 | } |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 803 | config = __salt__['config.get']('maas') |
| 804 | for part in self._config_path.split('.'): |
| 805 | config = config.get(part, {}) |
| 806 | extra = {} |
| 807 | for name, url_call in self._extra_data_urls.iteritems(): |
| 808 | key = 'id' |
| 809 | if isinstance(url_call, tuple): |
| 810 | url_call, key = url_call[:] |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 811 | json_res = json.loads(self._maas.get(url_call).read()) |
| 812 | extra[name] = {v['name']: v[key] for v in json_res} |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 813 | if self._all_elements_url: |
| 814 | all_elements = {} |
| 815 | elements = self._maas.get(self._all_elements_url).read() |
| 816 | res_json = json.loads(elements) |
| 817 | for element in res_json: |
| 818 | if isinstance(element, (str, unicode)): |
| 819 | all_elements[element] = {} |
| 820 | else: |
| 821 | all_elements[element[self._element_key]] = element |
| 822 | else: |
| 823 | all_elements = {} |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 824 | try: |
| 825 | data = self.fill_data(config, **extra) |
| 826 | data = self.update(data, all_elements.values()[0]) |
| 827 | self.send(data) |
| 828 | ret['success'].append('domain') |
| 829 | except urllib2.HTTPError as e: |
| 830 | etxt = e.read() |
| 831 | LOG.exception('Failed for object %s reason %s', 'domain', etxt) |
| 832 | ret['errors']['domain'] = str(etxt) |
| 833 | except Exception as e: |
| 834 | LOG.exception('Failed for object %s reason %s', 'domain', e) |
| 835 | ret['errors']['domain'] = str(e) |
| 836 | if ret['errors']: |
| 837 | raise Exception(ret) |
| 838 | return ret |
| 839 | |
| 840 | |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 841 | class MachinesStatus(MaasObject): |
| 842 | @classmethod |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 843 | def execute(cls, objects_name=None): |
Krzysztof Szukiełojć | 0be1a16 | 2017-04-04 11:59:09 +0200 | [diff] [blame] | 844 | cls._maas = _create_maas_client() |
| 845 | result = cls._maas.get(u'api/2.0/machines/') |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 846 | json_result = json.loads(result.read()) |
| 847 | res = [] |
Krzysztof Szukiełojć | 0be1a16 | 2017-04-04 11:59:09 +0200 | [diff] [blame] | 848 | summary = collections.Counter() |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 849 | if objects_name: |
| 850 | if ',' in objects_name: |
| 851 | objects_name = set(objects_name.split(',')) |
| 852 | else: |
| 853 | objects_name = set([objects_name]) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 854 | for machine in json_result: |
Krzysztof Szukiełojć | f506220 | 2017-04-11 12:33:36 +0200 | [diff] [blame] | 855 | if objects_name and machine['hostname'] not in objects_name: |
Krzysztof Szukiełojć | 2497cdb | 2017-04-11 09:50:28 +0200 | [diff] [blame] | 856 | continue |
Michael Polenchuk | e438bd3 | 2017-11-09 20:42:42 +0400 | [diff] [blame] | 857 | status = STATUS_NAME_DICT[machine['status']] |
Krzysztof Szukiełojć | 0be1a16 | 2017-04-04 11:59:09 +0200 | [diff] [blame] | 858 | summary[status] += 1 |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame] | 859 | res.append( |
| 860 | {'hostname': machine['hostname'], |
| 861 | 'system_id': machine['system_id'], |
| 862 | 'status': status}) |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 863 | return {'machines': res, 'summary': summary} |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 864 | |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame] | 865 | @classmethod |
| 866 | def wait_for_machine_status(cls, **kwargs): |
| 867 | """ |
| 868 | A function that wait for any requested status, for any set of maas |
| 869 | machines. |
| 870 | |
| 871 | If no kwargs has been passed - will try to wait ALL |
| 872 | defined in salt::maas::region::machines |
| 873 | |
| 874 | See readme file for more examples. |
| 875 | CLI Example: |
| 876 | .. code-block:: bash |
| 877 | |
| 878 | salt-call state.apply maas.machines.wait_for_deployed |
| 879 | |
| 880 | :param kwargs: |
| 881 | timeout: in s; Global timeout for wait |
| 882 | poll_time: in s;Sleep time, between retry |
| 883 | req_status: string; Polling status |
| 884 | machines: list; machine names |
| 885 | ignore_machines: list; machine names |
| 886 | :ret: True |
| 887 | Exception - if something fail/timeout reached |
| 888 | """ |
| 889 | timeout = kwargs.get("timeout", 60 * 120) |
| 890 | poll_time = kwargs.get("poll_time", 30) |
| 891 | req_status = kwargs.get("req_status", "Ready") |
| 892 | to_discover = kwargs.get("machines", None) |
| 893 | ignore_machines = kwargs.get("ignore_machines", None) |
| 894 | if not to_discover: |
| 895 | try: |
| 896 | to_discover = __salt__['config.get']('maas')['region'][ |
| 897 | 'machines'].keys() |
| 898 | except KeyError: |
| 899 | LOG.warning("No defined machines!") |
| 900 | return True |
| 901 | total = copy.deepcopy(to_discover) or [] |
| 902 | if ignore_machines and total: |
| 903 | total = [x for x in to_discover if x not in ignore_machines] |
| 904 | started_at = time.time() |
| 905 | while len(total) <= len(to_discover): |
| 906 | for m in to_discover: |
| 907 | for discovered in MachinesStatus.execute()['machines']: |
| 908 | if m == discovered['hostname'] and \ |
Michael Polenchuk | e438bd3 | 2017-11-09 20:42:42 +0400 | [diff] [blame] | 909 | discovered['status'].lower() == req_status.lower(): |
| 910 | if m in total: |
| 911 | total.remove(m) |
| 912 | |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame] | 913 | if len(total) <= 0: |
| 914 | LOG.debug( |
| 915 | "Machines:{} are:{}".format(to_discover, req_status)) |
| 916 | return True |
| 917 | if (timeout - (time.time() - started_at)) <= 0: |
| 918 | raise Exception( |
| 919 | 'Machines:{}not in {} state'.format(total, req_status)) |
| 920 | LOG.info( |
| 921 | "Waiting status:{} " |
| 922 | "for machines:{}" |
| 923 | "\nsleep for:{}s " |
| 924 | "Timeout:{}s".format(req_status, total, poll_time, timeout)) |
| 925 | time.sleep(poll_time) |
| 926 | |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 927 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 928 | def process_fabrics(): |
| 929 | return Fabric().process() |
| 930 | |
Jiri Broulik | e30a60f | 2018-04-09 21:15:10 +0200 | [diff] [blame] | 931 | def process_boot_sources(): |
| 932 | return Boot_source().process() |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 933 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 934 | def process_subnets(): |
| 935 | return Subnet().process() |
| 936 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 937 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 938 | def process_dhcp_snippets(): |
| 939 | return DHCPSnippet().process() |
| 940 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 941 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 942 | def process_package_repositories(): |
| 943 | return PacketRepository().process() |
| 944 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 945 | |
Krzysztof Szukiełojć | 889eee9 | 2017-04-14 11:45:35 +0200 | [diff] [blame] | 946 | def process_devices(*args): |
| 947 | return Device().process(*args) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 948 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 949 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 950 | def process_machines(*args): |
| 951 | return Machine().process(*args) |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 952 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 953 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 954 | def process_assign_machines_ip(*args): |
azvyagintsev | 06b71e7 | 2017-11-08 17:11:07 +0200 | [diff] [blame] | 955 | """ |
| 956 | Manage interface configurations. |
| 957 | See readme.rst for more info |
| 958 | """ |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 959 | return AssignMachinesIP().process(*args) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 960 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 961 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 962 | def machines_status(*args): |
| 963 | return MachinesStatus.execute(*args) |
Krzysztof Szukiełojć | 04e1833 | 2017-04-04 11:51:44 +0200 | [diff] [blame] | 964 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 965 | |
Krzysztof Szukiełojć | 222a3eb | 2017-04-11 09:39:07 +0200 | [diff] [blame] | 966 | def deploy_machines(*args): |
| 967 | return DeployMachines().process(*args) |
Krzysztof Szukiełojć | 7c16e05 | 2017-04-05 10:04:45 +0200 | [diff] [blame] | 968 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 969 | |
Krzysztof Szukiełojć | c4b3309 | 2017-02-15 13:25:38 +0100 | [diff] [blame] | 970 | def process_boot_resources(): |
| 971 | return BootResource().process() |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 972 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 973 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 974 | def process_maas_config(): |
| 975 | return MaasConfig().process() |
| 976 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 977 | |
Krzysztof Szukiełojć | 43bc7e0 | 2017-03-17 10:32:07 +0100 | [diff] [blame] | 978 | def process_commissioning_scripts(): |
| 979 | return CommissioningScripts().process() |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 980 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 981 | |
Krzysztof Szukiełojć | 8cc32b4 | 2017-03-29 15:22:57 +0200 | [diff] [blame] | 982 | def process_domain(): |
| 983 | return Domain().process() |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 984 | |
Krzysztof Szukiełojć | b321675 | 2017-04-05 15:50:41 +0200 | [diff] [blame] | 985 | |
Krzysztof Szukiełojć | a1bd77e | 2017-03-30 08:34:22 +0200 | [diff] [blame] | 986 | def process_sshprefs(): |
| 987 | return SSHPrefs().process() |
azvyagintsev | 7605a66 | 2017-11-03 19:05:04 +0200 | [diff] [blame] | 988 | |
| 989 | |
| 990 | def wait_for_machine_status(**kwargs): |
| 991 | return MachinesStatus.wait_for_machine_status(**kwargs) |