-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathmaasservice.py
More file actions
318 lines (269 loc) · 11.3 KB
/
Copy pathmaasservice.py
File metadata and controls
318 lines (269 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# Copyright 2014 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import re
import sys
import json
import netaddr
from oauthlib import oauth1
from oslo_log import log as oslo_logging
import requests
from cloudbaseinit import conf as cloudbaseinit_conf
from cloudbaseinit import exception
from cloudbaseinit.metadata.services import base
from cloudbaseinit.models import network as network_model
from cloudbaseinit.utils import x509constants
CONF = cloudbaseinit_conf.CONF
LOG = oslo_logging.getLogger(__name__)
MAAS_CONFIG_TYPE_PHYSICAL = "physical"
MAAS_CONFIG_TYPE_BOND = "bond"
MAAS_CONFIG_TYPE_VLAN = "vlan"
MAAS_CONGIG_TYPE_NAMESERVER = "nameserver"
MAAS_BOND_LACP_RATE_SLOW = "slow"
MAAS_BOND_LACP_RATE_FAST = "fast"
MAAS_SUBNET_TYPE_STATIC = "static"
MAAS_SUBNET_TYPE_MANUAL = "manual"
BOND_LACP_RATE_MAP = {
MAAS_BOND_LACP_RATE_SLOW: network_model.BOND_LACP_RATE_SLOW,
MAAS_BOND_LACP_RATE_FAST: network_model.BOND_LACP_RATE_FAST,
}
class _Realm(str):
# There's a bug in oauthlib which ignores empty realm strings,
# by checking that the given realm is always True.
# This string class always returns True in a boolean context,
# making sure that an empty realm can be used by oauthlib.
def __bool__(self):
return True
__nonzero__ = __bool__
class MaaSHttpService(base.BaseHTTPMetadataService):
_METADATA_2012_03_01 = '2012-03-01'
def __init__(self):
super(MaaSHttpService, self).__init__(
base_url=CONF.maas.metadata_base_url,
https_allow_insecure=CONF.maas.https_allow_insecure,
https_ca_bundle=CONF.maas.https_ca_bundle)
self._enable_retry = True
self._metadata_version = self._METADATA_2012_03_01
def load(self):
super(MaaSHttpService, self).load()
if not CONF.maas.metadata_base_url:
LOG.debug('MAAS metadata url not set')
else:
try:
self._get_cache_data('%s/meta-data/' % self._metadata_version)
return True
except Exception as ex:
LOG.exception(ex)
LOG.debug('Metadata not found at URL \'%s\'' %
CONF.maas.metadata_base_url)
return False
def _get_oauth_headers(self, url):
LOG.debug("Getting authorization headers for %s.", url)
client = oauth1.Client(
CONF.maas.oauth_consumer_key,
client_secret=CONF.maas.oauth_consumer_secret,
resource_owner_key=CONF.maas.oauth_token_key,
resource_owner_secret=CONF.maas.oauth_token_secret,
signature_method=oauth1.SIGNATURE_PLAINTEXT)
realm = _Realm("")
headers = client.sign(url, realm=realm)[1]
return headers
def _http_request(self, url, data=None, headers=None, method=None):
"""Get content for received url."""
if not url.startswith("http"):
url = requests.compat.urljoin(self._base_url, url)
headers = {} if headers is None else headers
headers.update(self._get_oauth_headers(url))
return super(MaaSHttpService, self)._http_request(url, data, headers,
method)
def get_host_name(self):
return self._get_cache_data('%s/meta-data/local-hostname' %
self._metadata_version, decode=True)
def get_instance_id(self):
return self._get_cache_data('%s/meta-data/instance-id' %
self._metadata_version, decode=True)
def get_public_keys(self):
return self._get_cache_data('%s/meta-data/public-keys' %
self._metadata_version,
decode=True).splitlines()
def get_client_auth_certs(self):
certs_data = self._get_cache_data('%s/meta-data/x509' %
self._metadata_version,
decode=True)
pattern = r"{begin}[\s\S]+?{end}".format(
begin=x509constants.PEM_HEADER,
end=x509constants.PEM_FOOTER)
return re.findall(pattern, certs_data)
def get_user_data(self):
return self._get_cache_data('%s/user-data' % self._metadata_version)
@staticmethod
def _get_network_data():
if sys.platform != "win32":
return
path = os.path.join(
os.environ["systemdrive"], "\\curtin\\network.json")
if not os.path.isfile(path):
path = os.path.join(os.environ["systemdrive"], "\\network.json")
if not os.path.isfile(path):
path = None
if path:
json_data = open(path, "rb").read()
return json.loads(json_data.decode('utf-8'))
@staticmethod
def _is_link_enabled(subnets):
return MAAS_SUBNET_TYPE_MANUAL not in [s.get("type") for s in subnets]
@staticmethod
def _parse_config_link(config):
link_id = config.get("id")
name = config.get("name")
mac = config.get("mac_address")
mtu = config.get("mtu")
maas_link_type = config.get("type")
subnets = config.get("subnets", [])
params = config.get("params", {})
bond = None
vlan_id = None
vlan_link = None
link_enabled = False
if maas_link_type == MAAS_CONFIG_TYPE_PHYSICAL:
link_type = network_model.LINK_TYPE_PHYSICAL
link_enabled = MaaSHttpService._is_link_enabled(subnets)
elif maas_link_type == MAAS_CONFIG_TYPE_BOND:
link_type = network_model.LINK_TYPE_BOND
bond_interfaces = config.get("bond_interfaces")
bond_mode = params.get("bond-mode")
bond_xmit_hash_policy = params.get("bond-xmit-hash-policy")
maas_bond_lacp_rate = params.get("bond-lacp-rate")
if bond_mode not in network_model.AVAILABLE_BOND_TYPES:
raise exception.CloudbaseInitException(
"Unsupported bond mode: %s" % bond_mode)
if (bond_xmit_hash_policy is not None and
bond_xmit_hash_policy not in
network_model.AVAILABLE_BOND_LB_ALGORITHMS):
raise exception.CloudbaseInitException(
"Unsupported bond hash policy: %s" % bond_xmit_hash_policy)
bond = network_model.Bond(
members=bond_interfaces,
type=bond_mode,
lb_algorithm=bond_xmit_hash_policy,
lacp_rate=BOND_LACP_RATE_MAP.get(maas_bond_lacp_rate))
link_enabled = True
elif maas_link_type == MAAS_CONFIG_TYPE_VLAN:
link_type = network_model.LINK_TYPE_VLAN
vlan_link = config.get("vlan_link")
vlan_id = config.get("vlan_id")
link_enabled = True
else:
raise exception.CloudbaseInitException(
"Unsupported MAAS link type: %s" % maas_link_type)
link = network_model.Link(
id=link_id,
name=name,
type=link_type,
enabled=link_enabled,
mac_address=mac,
mtu=mtu,
bond=bond,
vlan_id=vlan_id,
vlan_link=vlan_link)
networks = []
subnets = config.get("subnets", [])
for subnet in subnets:
maas_subnet_type = subnet.get("type")
if maas_subnet_type == MAAS_SUBNET_TYPE_STATIC:
address_cidr = subnet.get("address")
gateway = subnet.get("gateway")
dns_nameservers = subnet.get("dns_nameservers")
# TODO(alexpilotti): Add support for extra routes
if gateway is not None:
if netaddr.valid_ipv6(gateway):
default_network_cidr = u"::/0"
else:
default_network_cidr = u"0.0.0.0/0"
routes = [
network_model.Route(
network_cidr=default_network_cidr,
gateway=gateway
)
]
else:
routes = []
net = network_model.Network(
link=link_id,
address_cidr=address_cidr,
dns_nameservers=dns_nameservers,
routes=routes,
)
networks.append(net)
return link, networks
@staticmethod
def _parse_config_nameserver(config):
return network_model.NameServerService(
addresses=config.get("address", []),
search=config.get("search", []))
@staticmethod
def _parse_config_item(config):
link = None
networks = None
service = None
config_type = config.get("type")
if config_type == MAAS_CONGIG_TYPE_NAMESERVER:
service = MaaSHttpService._parse_config_nameserver(config)
elif config_type in [
MAAS_CONFIG_TYPE_PHYSICAL,
MAAS_CONFIG_TYPE_BOND,
MAAS_CONFIG_TYPE_VLAN]:
link, networks = MaaSHttpService._parse_config_link(config)
else:
raise exception.CloudbaseInitException(
"Unsupported item type: %s" % config_type)
return link, networks, service
@staticmethod
def _enable_bond_physical_links(links):
# The MAAS metadata sets the NIC subnet type as "manual" for both
# disconnected NICs and bond members. We need to make sure that the
# latter are enabled.
for link1 in links:
if link1.type == network_model.LINK_TYPE_BOND:
for index, link2 in enumerate(links):
if (link2.type == network_model.LINK_TYPE_PHYSICAL and
not link2.enabled and
link2.id in link1.bond.members):
links[index] = link2._replace(enabled=True)
def get_network_details_v2(self):
network_data = self._get_network_data()
if not network_data:
return
version = network_data.get("version")
if version != 1:
raise exception.CloudbaseInitException(
'Unsupported MAAS network metadata version: %s' % version)
links = []
networks = []
services = []
config = network_data.get("config", [])
for config_item in config:
link, link_networks, service = self._parse_config_item(config_item)
if link:
links.append(link)
if link_networks:
networks.extend(link_networks),
if service:
services.append(service)
self._enable_bond_physical_links(links)
return network_model.NetworkDetailsV2(
links=links,
networks=networks,
services=services
)