This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
/
rackspace-onmetal-configdrive.patch
481 lines (455 loc) · 19.2 KB
/
rackspace-onmetal-configdrive.patch
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py
index 1a56dfb..21bfa79 100644
--- a/cloudinit/distros/__init__.py
+++ b/cloudinit/distros/__init__.py
@@ -72,6 +72,12 @@ class Distro(object):
# to write this blob out in a distro format
raise NotImplementedError()
+ #@abc.abstractmethod
+ def _write_network_json(self, settings):
+ # In the future use the http://fedorahosted.org/netcf/
+ # to write this blob out in a distro format
+ raise NotImplementedError()
+
def _find_tz_file(self, tz):
tz_file = os.path.join(self.tz_zone_dir, str(tz))
if not os.path.isfile(tz_file):
@@ -115,6 +121,12 @@ class Distro(object):
return _get_package_mirror_info(availability_zone=availability_zone,
mirror_info=arch_info)
+ def apply_network_json(self, settings, bring_up=True):
+ dev_names = self._write_network_json(settings)
+ if bring_up:
+ return self._bring_up_interfaces(dev_names)
+ return False
+
def apply_network(self, settings, bring_up=True):
# Write it out
dev_names = self._write_network(settings)
diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py
index 7cf4a9e..e848bcf 100644
--- a/cloudinit/distros/debian.py
+++ b/cloudinit/distros/debian.py
@@ -28,7 +28,7 @@ from cloudinit import log as logging
from cloudinit import util
from cloudinit.distros.parsers.hostname import HostnameConf
-
+from cloudinit.distros.net_util import NetConfHelper
from cloudinit.settings import PER_INSTANCE
LOG = logging.getLogger(__name__)
@@ -72,6 +72,91 @@ class Distro(distros.Distro):
self.update_package_sources()
self.package_command('install', pkgs=pkglist)
+ def _debian_network_json(self, settings):
+ devs = []
+ nc = NetConfHelper(settings)
+ lines = []
+
+ lines.append("# Created by cloud-init on instance boot.")
+ lines.append("#")
+ lines.append("# This file describes the network interfaces available on your system")
+ lines.append("# and how to activate them. For more information, see interfaces(5).")
+ lines.append("")
+ lines.append("# The loopback network interface")
+ lines.append("auto lo")
+ lines.append("iface lo inet loopback")
+ lines.append("")
+
+ bonds = nc.get_links_by_type('bond')
+ for bond in bonds:
+ chunk = []
+ slaves = [nc.get_link_devname(nc.get_link_by_name(x)) for x in bond['bond_links']]
+ for slave in slaves:
+ chunk.append("auto {0}".format(slave))
+ chunk.append("iface {0} inet manual".format(slave))
+ chunk.append(" bond-master {0}".format(bond['id']))
+ chunk.append("")
+ devs.append(bond['id'])
+ devs.extend(slaves)
+ chunk.append("auto {0}".format(bond['id']))
+ chunk.append("iface {0} inet manual".format(bond['id']))
+ if bond.has_key('bond_mode'):
+ chunk.append(' bond-mode {0}'.format(bond['bond_mode']))
+ if bond.has_key('bond_xmit_hash_policy'):
+ chunk.append(' bond_xmit_hash_policy {0}'.format(bond['bond_xmit_hash_policy']))
+ if bond.has_key('bond_miimon'):
+ chunk.append(' bond-miimon {0}'.format(bond['bond_miimon']))
+ chunk.append(' bond-slaves {0}'.format(' '.join(slaves)))
+ chunk.append("")
+ lines.extend(chunk)
+
+ dns = nc.get_dns_servers()
+ networks = nc.get_networks()
+ for net in networks:
+ # only have support for ipv4 so far.
+ if net['type'] != "ipv4":
+ continue
+
+ link = nc.get_link_by_name(net['link'])
+ devname = nc.get_link_devname(link)
+ chunk = []
+ chunk.append("# network: {0}".format(net['id']))
+ chunk.append("# network_id: {0}".format(net['network_id']))
+ chunk.append("auto {0}".format(devname))
+ chunk.append("iface {0} inet static".format(devname))
+
+ devs.append(devname)
+ if link['type'] == "vlan":
+ chunk.append(" vlan_raw_device {0}".format(devname[:devname.rfind('.')]))
+ chunk.append(" hwaddress ether {0}".format(link['ethernet_mac_address']))
+ if link.has_key('mtu'):
+ chunk.append(' mtu {0}'.format(link['mtu']))
+
+ chunk.append(" address {0}".format(net['ip_address']))
+ chunk.append(" netmask {0}".format(net['netmask']))
+ gwroute = [route for route in net['routes'] if route['network'] == '0.0.0.0']
+ # TODO: hmmm
+ if len(gwroute) == 1:
+ chunk.append(" gateway {0}".format(gwroute[0]['gateway']))
+ chunk.append(" dns-nameservers {0}".format(" ".join(dns)))
+
+ for route in net['routes']:
+ if route['network'] == '0.0.0.0':
+ continue
+ chunk.append(" post-up route add -net {0} netmask {1} gw {2} || true".format(route['network'],
+ route['netmask'], route['gateway']))
+ chunk.append(" pre-down route del -net {0} netmask {1} gw {2} || true".format(route['network'],
+ route['netmask'], route['gateway']))
+ chunk.append("")
+ lines.extend(chunk)
+ return {'/etc/network/interfaces': "\n".join(lines)}, devs
+
+ def _write_network_json(self, settings):
+ files, devs = self._debian_network_json(settings)
+ for (fn, data) in files.iteritems():
+ util.write_file(fn, data)
+ return devs
+
def _write_network(self, settings):
util.write_file(self.network_conf_fn, settings)
return ['all']
diff --git a/cloudinit/distros/net_util.py b/cloudinit/distros/net_util.py
index b9bcfd8..ca7672e 100644
--- a/cloudinit/distros/net_util.py
+++ b/cloudinit/distros/net_util.py
@@ -21,6 +21,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from cloudinit.netinfo import find_mac_addresses
+
# This is a util function to translate debian based distro interface blobs as
# given in /etc/network/interfaces to an *somewhat* agnostic format for
# distributions that use other formats.
@@ -161,3 +163,35 @@ def translate_network(settings):
if dev_name in real_ifaces:
real_ifaces[dev_name]['auto'] = True
return real_ifaces
+
+class NetConfHelper(object):
+ def __init__(self, settings):
+ self._settings = settings
+
+ def get_link_by_name(self, name):
+ return [x for x in self._settings['links'] if x['id'] == name][0]
+
+ def get_links_by_type(self, t):
+ return [x for x in self._settings['links'] if x['type'] == t]
+
+ def get_link_devname(self, link):
+ # TODO: chase vlans/bonds/etc
+ if link['type'] == "vlan":
+ return "{0}.{1}".format(
+ self.get_link_devname(
+ self.get_link_by_name(link['vlan_link'])),
+ link['vlan_id'])
+ if link['type'] == "ethernet":
+ devs = find_mac_addresses()
+ for (dev, mac) in devs.iteritems():
+ if mac == link['ethernet_mac_address']:
+ return dev
+ raise Exception("Device not found: {0}".format(link))
+
+ return link['id']
+
+ def get_networks(self):
+ return self._settings['networks']
+
+ def get_dns_servers(self):
+ return [x['address'] for x in self._settings['services'] if x['type'] == "dns"]
diff --git a/cloudinit/distros/rhel.py b/cloudinit/distros/rhel.py
index e8abf11..0806463 100644
--- a/cloudinit/distros/rhel.py
+++ b/cloudinit/distros/rhel.py
@@ -28,6 +28,7 @@ from cloudinit import util
from cloudinit.distros import net_util
from cloudinit.distros import rhel_util
from cloudinit.settings import PER_INSTANCE
+from cloudinit.distros.net_util import NetConfHelper
LOG = logging.getLogger(__name__)
@@ -62,6 +63,129 @@ class Distro(distros.Distro):
def install_packages(self, pkglist):
self.package_command('install', pkgs=pkglist)
+ def _rhel_network_json(self, settings):
+ devs = []
+ # depends add redhat-lsb-core
+ nc = NetConfHelper(settings)
+ iffn = '/etc/sysconfig/network-scripts/ifcfg-{0}'
+ routefn = '/etc/sysconfig/network-scripts/route-{0}'
+ files = {}
+
+ bonds = nc.get_links_by_type('bond')
+ for bond in bonds:
+ chunk = []
+ fn = iffn.format(bond['id'])
+ lines = []
+ lines.append("# Created by cloud-init on instance boot.")
+ lines.append("#")
+ lines.append("")
+ lines.append("DEVICE={0}".format(bond['id']))
+ devs.append(bond['id'])
+ lines.append("ONBOOT=yes")
+ lines.append("BOOTPROTO=none")
+ lines.append("USERCTL=no")
+ lines.append("NM_CONTROLLED=no")
+ lines.append("TYPE=Ethernet")
+
+ opts = []
+ if bond.has_key('bond_mode'):
+ opts.append('mode={0}'.format(bond['bond_mode']))
+ if bond.has_key('bond_xmit_hash_policy'):
+ opts.append('xmit_hash_policy={0}'.format(bond['bond_xmit_hash_policy']))
+ if bond.has_key('bond_miimon'):
+ opts.append('miimon={0}'.format(bond['bond_miimon']))
+ lines.append("BONDING_OPTS=\"{0}\"".format(" ".join(opts)))
+ files[fn] = "\n".join(lines)
+
+
+ for slave in bond['bond_links']:
+ slavelink = nc.get_link_by_name(slave)
+ slavedev = nc.get_link_devname(slavelink)
+ fn = iffn.format(slavedev)
+ lines = []
+ lines.append("# Created by cloud-init on instance boot.")
+ lines.append("#")
+ lines.append("")
+ lines.append("DEVICE={0}".format(slavedev))
+ devs.append(slavedev)
+ lines.append("ONBOOT=yes")
+ lines.append("BOOTPROTO=none")
+ lines.append("USERCTL=no")
+ lines.append("NM_CONTROLLED=no")
+ lines.append("TYPE=Ethernet")
+ lines.append("MASTER={0}".format(bond['id']))
+ lines.append("SLAVE=yes")
+ files[fn] = "\n".join(lines)
+
+ dns = nc.get_dns_servers()
+ networks = nc.get_networks()
+ for net in networks:
+ # only have support for ipv4 so far.
+ if net['type'] != "ipv4":
+ continue
+
+ link = nc.get_link_by_name(net['link'])
+ devname = nc.get_link_devname(link)
+ fn = iffn.format(devname)
+
+ lines = []
+ lines.append("# Created by cloud-init on instance boot.")
+ lines.append("#")
+ lines.append("# network: {0}".format(net['id']))
+ lines.append("# network_id: {0}".format(net['network_id']))
+ lines.append("")
+ lines.append("DEVICE={0}".format(devname))
+ devs.append(devname)
+ if link['type'] == "vlan":
+ lines.append("VLAN=yes")
+ lines.append("PHYSDEV={0}".format(devname[:devname.rfind('.')]))
+ lines.append("MACADDR={0}".format(link['ethernet_mac_address']))
+ if link.has_key('mtu'):
+ chunk.append('MTU={0}'.format(link['mtu']))
+
+ lines.append("ONBOOT=yes")
+ lines.append("BOOTPROTO=static")
+ lines.append("USERCTL=no")
+ lines.append("NM_CONTROLLED=no")
+ lines.append("TYPE=Ethernet")
+ lines.append("IPADDR={0}".format(net['ip_address']))
+ lines.append("NETMASK={0}".format(net['netmask']))
+
+ gwroute = [route for route in net['routes'] if route['network'] == '0.0.0.0']
+ # TODO: hmmm
+ if len(gwroute) == 1:
+ lines.append("GATEWAY={0}".format(gwroute[0]['gateway']))
+ i = 1
+ for server in dns:
+ lines.append("DNS{0}={1}".format(i, server))
+ i += 1
+
+ files[fn] = "\n".join(lines)
+
+ i = 0
+ fn = routefn.format(devname)
+ lines = []
+ for route in net['routes']:
+ if route['network'] == '0.0.0.0':
+ continue
+ lines.append("ADDRESS{0}={1}".format(i, route['network']))
+ lines.append("NETMASK{0}={1}".format(i, route['netmask']))
+ lines.append("GATEWAY{0}={1}".format(i, route['gateway']))
+ i += 1
+
+ if len(lines) > 0:
+ lines.insert(0, "#")
+ lines.insert(0, "# Created by cloud-init on instance boot.")
+ files[fn] = "\n".join(lines)
+
+ return files, devs
+
+ def _write_network_json(self, settings):
+ files, devs = self._rhel_network_json(settings)
+ for (fn, data) in files.iteritems():
+ util.write_file(fn, data)
+ return devs
+
def _write_network(self, settings):
# TODO(harlowja) fix this... since this is the ubuntu format
entries = net_util.translate_network(settings)
@@ -99,10 +223,13 @@ class Distro(distros.Distro):
return dev_names
def _dist_uses_systemd(self):
+ # TODO(pquerna): Figure out a more portable way of detecting systemd
+ # as the active init system. There are other distros out there.
# Fedora 18 and RHEL 7 were the first adopters in their series
(dist, vers) = util.system_info()['dist'][:2]
major = (int)(vers.split('.')[0])
return ((dist.startswith('Red Hat Enterprise Linux') and major >= 7)
+ or (dist.startswith('CentOS Linux') and major >= 7)
or (dist.startswith('Fedora') and major >= 18))
def apply_locale(self, locale, out_fn=None):
diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py
index 1bdca9f..4fed43a 100644
--- a/cloudinit/netinfo.py
+++ b/cloudinit/netinfo.py
@@ -21,6 +21,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import cloudinit.util as util
+import subprocess
import re
from prettytable import PrettyTable
@@ -187,6 +188,21 @@ def route_pformat():
return "\n".join(lines)
+_SECTIONS_RE = re.compile(r"\n(?=\w)")
+_IFCONFIG_RE = re.compile(r"^(?P<name>\w+).*?(?:HWaddr|ether) (?P<mac>[a-fA-F0-9:]+)", re.DOTALL)
+
+def _parse_ifconfig_output(stdout):
+ result = {}
+ for section in _SECTIONS_RE.split(stdout):
+ match = _IFCONFIG_RE.match(section)
+ if match:
+ result[match.group("name")] = match.group("mac").lower()
+ return result
+
+def find_mac_addresses():
+ (output, err) = util.subp(["ifconfig", "-a"])
+ return _parse_ifconfig_output(output)
+
def debug_info(prefix='ci-info: '):
lines = []
netdev_lines = netdev_pformat().splitlines()
diff --git a/cloudinit/sources/DataSourceConfigDrive.py b/cloudinit/sources/DataSourceConfigDrive.py
index 0c35f83..6ff5d68 100644
--- a/cloudinit/sources/DataSourceConfigDrive.py
+++ b/cloudinit/sources/DataSourceConfigDrive.py
@@ -19,6 +19,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
+import json
from cloudinit import log as logging
from cloudinit import sources
@@ -125,7 +126,19 @@ class DataSourceConfigDrive(openstack.SourceMixin, sources.DataSource):
self.userdata_raw = results.get('userdata')
self.version = results['version']
self.files.update(results.get('files', {}))
- self.vendordata_raw = results.get('vendordata')
+
+ # if vendordata includes 'cloud-init', then read that explicitly
+ # for cloud-init (for namespacing).
+ vd = results.get('vendordata')
+ if isinstance(vd, dict):
+ if 'cloud-init' in vd:
+ self.vendordata_raw = vd['cloud-init']
+ else:
+ # TODO(pquerna): this is so wrong.
+ self.vendordata_raw = json.dumps(vd)
+ else:
+ self.vendordata_raw = vd
+
return True
@@ -160,7 +173,7 @@ def get_ds_mode(cfgdrv_ver, ds_cfg=None, user=None):
return "net"
-def read_config_drive(source_dir, version="2012-08-10"):
+def read_config_drive(source_dir, version="2013-10-17"):
reader = openstack.ConfigDriveReader(source_dir)
finders = [
(reader.read_v2, [], {'version': version}),
@@ -191,10 +204,23 @@ def on_first_boot(data, distro=None):
if not isinstance(data, dict):
raise TypeError("Config-drive data expected to be a dict; not %s"
% (type(data)))
+
+ networkapplied = False
+ jsonnet_conf = data.get('vendordata', {}).get('network_info')
+ if jsonnet_conf:
+ try:
+ LOG.debug("Updating network interfaces from JSON in config drive")
+ distro_user_config = distro.apply_network_json(jsonnet_conf)
+ networkapplied = True
+ except NotImplementedError:
+ LOG.debug("Distro does not implement networking setup via Vendor JSON.")
+ pass
+
net_conf = data.get("network_config", '')
- if net_conf and distro:
+ if networkapplied is False and net_conf and distro:
LOG.debug("Updating network interfaces from config drive")
distro.apply_network(net_conf)
+
files = data.get('files', {})
if files:
LOG.debug("Writing %s injected files", len(files))
diff --git a/cloudinit/sources/DataSourceOpenStack.py b/cloudinit/sources/DataSourceOpenStack.py
index 0970d07..615455e 100644
--- a/cloudinit/sources/DataSourceOpenStack.py
+++ b/cloudinit/sources/DataSourceOpenStack.py
@@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import time
+import json
from cloudinit import log as logging
from cloudinit import sources
@@ -146,8 +147,12 @@ class DataSourceOpenStack(openstack.SourceMixin, sources.DataSource):
# if vendordata includes 'cloud-init', then read that explicitly
# for cloud-init (for namespacing).
vd = results.get('vendordata')
- if isinstance(vd, dict) and 'cloud-init' in vd:
- self.vendordata_raw = vd['cloud-init']
+ if isinstance(vd, dict):
+ if 'cloud-init' in vd:
+ self.vendordata_raw = vd['cloud-init']
+ else:
+ # TODO(pquerna): this is so wrong.
+ self.vendordata_raw = json.dumps(vd)
else:
self.vendordata_raw = vd
diff --git a/tests/unittests/test_datasource/test_openstack.py b/tests/unittests/test_datasource/test_openstack.py
index c088bb5..ffb9bbb 100644
--- a/tests/unittests/test_datasource/test_openstack.py
+++ b/tests/unittests/test_datasource/test_openstack.py
@@ -241,7 +241,7 @@ class TestOpenStackDataSource(test_helpers.HttprettyTestCase):
self.assertEquals(EC2_META, ds_os.ec2_metadata)
self.assertEquals(USER_DATA, ds_os.userdata_raw)
self.assertEquals(2, len(ds_os.files))
- self.assertEquals(VENDOR_DATA, ds_os.vendordata_raw)
+ self.assertEquals(VENDOR_DATA, json.loads(ds_os.vendordata_raw))
@hp.activate
def test_bad_datasource_meta(self):