-
Notifications
You must be signed in to change notification settings - Fork 10
/
interfaces2netplan
executable file
·454 lines (392 loc) · 15.8 KB
/
interfaces2netplan
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
#!/usr/bin/env python
# interfaces2netplan (part of ossobv/vcutil) // wdoekes/2018-2021
# // Public Domain
#
# Quick attempt to convert the simplest interfaces files (from ifupdown)
# to netplan.io YAML syntax. If it has the slightest hint that it might miss
# something, it will abort with some kind of error.
#
# 1. Enumerates all files in /etc/network/if-*.d and warns.
# 2. Concatenates all files in /etc/network/interfaces{,.d/*} and attempts
# to convert the contents to a valid netplan.io YAML file.
#
# Example output:
#
# network:
# version: 2
# renderer: networkd
# ethernets:
# enp3s0:
# addresses:
# - 10.100.1.38/24
# - 10.100.1.39/24
# gateway4: 10.100.1.1
#
# When content, write output to the yaml, like this:
#
# interfaces2netplan > /etc/netplan/01-netcfg.yaml
#
# After a succesful 'netplan try' you should be able to reboot and then
# remove the 'ifupdown' package. Make sure you remove/rename the
# interfaces file firs though. Otherwise the ifupdown removal will
# ifdown your interfaces >:)
#
# See also: "netplan ifupdown-migrate" (which apparently exists)
#
from collections import OrderedDict
import fnmatch
import os
import re
import sys
class Line(object):
def __init__(self, line, lineno, filename):
self.line = line
self.lineno = lineno
self.filename = filename
@property
def location(self):
return '{}:{}'.format(self.filename, self.lineno)
def _netmaskbits(netmask):
mask = [int(i) for i in netmask.split('.')]
if len(mask) == 1:
return mask[0]
mask = (mask[0] << 24 | mask[1] << 16 | mask[2] << 8 | mask[3])
bits = 32
while mask:
if ((mask >> 1) << 1) == mask:
bits -= 1
mask >>= 1
else:
break
mask <<= (32 - bits)
assert (0xffffffff << (32 - bits) & 0xffffffff) == mask
return bits
def _stderr(data):
"Shortcut for print to stderr"
if os.isatty(sys.stderr.fileno()):
data = '\x1b[31;1m{}\x1b[0m'.format(data)
sys.stderr.write(data)
sys.stderr.flush()
def _stdout(data):
"Shortcut for print to stdout"
sys.stdout.write(data)
sys.stdout.flush()
def list_all_files(path):
"Return all file-type files in supplied path"
return sorted([
os.path.join(dir_, file_)
for dir_, dirs, files in os.walk(path)
for file_ in files])
class NetworkFiles(object):
"Load up files in /etc/network and make sense of them"
def __init__(self):
self._interfaces = []
self._ifupdownd = []
self._unknown = []
self._yaml = None
self._populate(list_all_files('/etc/network'))
def _populate(self, files):
# Check that there is an interfaces file, and check whether we
# expect files in interfaces.d. Also, make sure it is added first.
self._interfaces.append('/etc/network/interfaces')
with open(self._interfaces[0], 'r') as fp:
lines = [i.strip() for i in fp.read().split('\n')]
lines = [i for i in lines if i.startswith((
'source ', 'source\t'))]
if len(lines) == 0:
# No 'source'? We see examples where the files are
# configured/used anyway..
interfaces_re = re.compile(
r'^/etc/network/interfaces.d/[^./][^/]*$')
elif len(lines) == 1:
# Check the source, and convert to regex.
interfaces_re = re.compile(
fnmatch.translate(lines[0].split(None, 1)[-1]))
else:
raise ValueError('Multiple source lines found? {!r}'.format(
lines))
# Check all supplied files against our lists/regexes.
ifupdown_re = re.compile(
r'^/etc/network/if-(down|post-down|pre-up|up)[.]d/[^/]*$')
devnull = []
destinations = (
(self._ifupdownd, (lambda x: ifupdown_re.match(x))),
(devnull, (lambda x: x == '/etc/network/interfaces')),
(self._interfaces, (lambda x: interfaces_re.match(x))),
)
for file_ in files:
for destlist, matches in destinations:
if matches(file_):
destlist.append(file_)
break
else:
self._unknown.append(file_)
def _get_appended_interfaces(self):
lines = []
for file_ in self._interfaces:
with open(file_, 'r') as fp:
data = fp.read()
# Remove the one source entry we expect. We're doing
# the sourcing here.
if file_ == '/etc/network/interfaces':
data = '\n'.join([
('' if line.strip().startswith((
'source ', 'source\t')) else line)
for line in data.split('\n')])
lines.extend([
Line(line, (idx + 1), file_)
for idx, line in enumerate(data.split('\n'))])
return lines
def get_yaml(self):
if self._yaml is None:
interfaces_lines = self._get_appended_interfaces()
ifile = InterfacesParser(interfaces_lines)
self._yaml = ifile.to_netplan()
return self._yaml
def exitcode(self):
# Don't even continue of we cannot generate a nice YAML.
yaml = self.get_yaml()
assert yaml is not None
status = 0
if self._ifupdownd:
# Don't care about these "normal" if-up.d/if-down.d files.
# They're generally not crucial.
pass
if self._unknown:
# This can be worrisome, don't return 0/OK anymore.
status = 2
return status
def show(self):
yaml = self.get_yaml()
_stdout(yaml)
def hint(self):
if os.isatty(sys.stdout.fileno()):
# If you're redirecting stdout, you don't need this hint anymore.
_stderr(
"Example usage:\n"
" interfaces2netplan > /etc/netplan/01-netcfg.yaml\n\n")
def warn(self):
if self._ifupdownd:
_stderr(
"Found some files we do not convert, check manually:\n"
" {}\n\n".format('\n '.join(self._ifupdownd)))
if self._unknown:
_stderr(
"Found some files we do not know about, check manually:\n"
" {}\n\n".format('\n '.join(self._unknown)))
class InterfacesParser(object):
"Interfaces file parser"
def __init__(self, interfaces_lines):
self._data = interfaces_lines
self._sanitize()
self._parse()
self._join_inet_inet6()
def _sanitize(self):
newdata = []
space_re = re.compile(r'\s+')
for lineobj in self._data:
line = lineobj.line.rstrip()
# Blank line or only comment? Skip.
if not line or line.lstrip().startswith('#'):
continue
# "[[:blank:]]#.*"? Drop trailing comment. Keep if the '#' is
# not preceded by a space.
if '#' in line and line.split('#', 1)[0].endswith((' ', '\t')):
line = line.split('#', 1)[0].rstrip()
# Collapse multiple leading blanks into one.
if line.startswith((' ', '\t')):
line = ' {}'.format(line.lstrip())
# Collapse multiple blanks into one everywhere.
line = space_re.sub(' ', line)
# Store sanitized line.
lineobj.line = line
newdata.append(lineobj)
self._data = newdata
def _parse(self):
context = None
autos = set()
config = {}
try:
for lineobj in self._data:
if lineobj.line.startswith('auto '):
values = lineobj.line.split() # auto lo eth0 eth1
for value in values[1:]:
autos.add(value)
context = None
elif lineobj.line.startswith('iface '):
context = self._parse_iface(config, lineobj)
elif context is not None and lineobj.line.startswith(' '):
self._parse_context(context, config[context], lineobj)
else:
raise ValueError('unexpected/unimplemented')
except Exception as e: # assert/index/type/value
raise ValueError('{}: parse fail at {!r}: {}'.format(
lineobj.location, lineobj.line, ': '.join(
str(i) for i in e.args)))
config_keys = set([i[0] for i in config.keys()])
if config_keys != autos:
raise ValueError(
'not all interfaces are auto-on: {!r} != {!r}'.format(
sorted(config_keys), sorted(autos)))
self._parsed = config
def _parse_iface(self, config, lineobj):
values = lineobj.line.split() # iface eth0 inet{,6} static|...
context = (values[1], values[2]) # (lo|eth0, inet|inet6)
if context in config:
raise ValueError('duplicate interface+protocol {!r}+{!r}'.format(
context[0], context[1]))
assert ':' not in values[1], 'iface:alias not available in netplan'
assert values[2] in ('inet', 'inet6'), 'expected inet or inet6'
type_ = values[3] # auto|static|dhcp|loopback
if context == ('lo', 'inet'):
assert type_ == 'loopback', lineobj.line
config[context] = {}
elif type_ in 'static':
config[context] = {}
elif type_ == 'dhcp' and values[2] == 'inet':
config[context] = {'dhcp4': 'yes'}
elif type_ == 'auto' and values[2] == 'inet6':
config[context] = {'dhcp6': 'yes'}
else:
raise ValueError('unexpected interface+protocol+type')
return context
def _parse_context(self, context, config, lineobj):
cols = lineobj.line.split()
cmd = cols.pop(0)
if cmd == 'address':
assert 'addresses' not in config, config
assert len(cols) == 1, 'expected 1 argument to address'
config['addresses'] = [cols[0]]
elif cmd == 'netmask':
assert 'addresses' in config, config
assert '/' not in config['addresses'][0], config
assert len(cols) == 1, 'expected 1 argument to netmask'
bits = _netmaskbits(cols[0])
config['addresses'][0] += '/{}'.format(bits)
elif cmd == 'gateway' and context[1] == 'inet':
assert 'gateway4' not in config, config
assert len(cols) == 1, 'expected 1 argument to gateway'
config['gateway4'] = cols[0]
elif cmd == 'gateway' and context[1] == 'inet6':
assert 'gateway6' not in config, config
assert len(cols) == 1, 'expected 1 argument to gateway'
config['gateway6'] = cols[0]
elif cmd in ('network', 'broadcast'):
_stderr(
'{}: Ignoring {!r} of {} ({}). '
'Your netmask is valid, right?\n'.format(
lineobj.location, cmd, context[0], context[1]))
elif cmd == 'dns-nameservers':
if 'nameservers' not in config:
config['nameservers'] = {}
assert 'addresses' not in config['nameservers'], config
assert cols, 'expected one or more dns-nameservers arguments'
config['nameservers']['addresses'] = cols
elif cmd == 'dns-search':
if 'nameservers' not in config:
config['nameservers'] = {}
assert 'search' not in config['nameservers'], config
assert cols, 'expected one or more dns-search arguments'
config['nameservers']['search'] = cols
elif (len(cols) in (6, 8) and cols[0] in ('route', '/sbin/route') and
((cmd in 'up' and cols[1] == 'add') or
(cmd == 'down' and cols[1] == 'del')) and
cols[2] in ('-host', '-net') and
((len(cols) == 6 and cols[4] == 'gw') or
(len(cols) == 8 and cols[4] == 'netmask' and
cols[6] == 'gw'))):
# up route add -net NET (netmask MASK) gw GW
# down route del -net NET (netmask MASK) gw GW
if 'routes' not in config:
config['routes'] = []
if len(cols) == 6:
if cols[2] == '-host':
assert '/' not in cols[3], cols
to = '{}/32'.format(cols[3])
else:
assert '/' in cols[3], cols
to = cols[3]
via = cols[5]
else:
assert cols[2] == '-net', cols
assert '/' not in cols[3], cols
to = '{}/{}'.format(cols[3], _netmaskbits(cols[5]))
via = cols[7]
route = {'to': to, 'via': via}
if cmd == 'up':
config['routes'].append(route)
elif cmd == 'down':
assert route in config['routes'], config['routes']
else:
raise ValueError('cmd {!r} unknown/unimplemented'.format(cmd))
def _join_inet_inet6(self):
ethernets = {}
for (iface, proto), values in self._parsed.items():
if iface not in ethernets:
ethernets[iface] = {}
for key, value in values.items():
if isinstance(value, list):
if key not in ethernets[iface]:
ethernets[iface][key] = []
ethernets[iface][key].extend(value)
else:
assert key not in ethernets[iface], (ethernets, iface, key)
ethernets[iface][key] = value
# We should add it if we want extra addresses, but generally we don't.
assert 'lo' in ethernets, ethernets
assert ethernets['lo'] == {}
del ethernets['lo']
self._ethernets = ethernets
def _to_yaml(self, value, indent=''):
if isinstance(value, str):
if ':' in value:
assert '"' not in value, value
return '"{}"'.format(value)
return value
ret = []
if isinstance(value, dict):
key_value = value.items()
if not isinstance(value, OrderedDict):
key_value = sorted(key_value)
for key, value in key_value:
inside = self._to_yaml(value, indent + ' ')
if isinstance(inside, list):
ret.append('{}{}:'.format(indent, key))
ret.extend(inside)
else:
ret.append('{}{}: {}'.format(indent, key, inside))
elif isinstance(value, list):
for v in value:
if (isinstance(v, dict) and
all(isinstance(k, str) for k in v.keys()) and
all(isinstance(v2, str) for v2 in v.values())):
kv = list(sorted(v.items()))
assert kv, 'empty dict unexpected'
for idx, (key, value) in enumerate(kv):
ret.append('{}{}{}: {}'.format(
indent, ('- ' if idx == 0 else ' '),
self._to_yaml(key), self._to_yaml(value)))
else:
assert isinstance(v, str), v
ret.append('{}- {}'.format(indent, self._to_yaml(v)))
else:
assert False, value
return ret
def to_netplan(self):
ret = {
'network': OrderedDict([
('version', '2'),
('renderer', 'networkd'),
('ethernets', self._ethernets),
]),
}
return '\n'.join(self._to_yaml(ret)) + '\n'
def main():
files = NetworkFiles()
files.hint()
files.warn()
files.show()
sys.exit(files.exitcode())
if __name__ == '__main__':
main()
# vim: set ts=8 sw=4 sts=4 et ai: