author | Tero Marttila <terom@paivola.fi> |
Tue, 10 Mar 2015 00:26:31 +0200 | |
changeset 740 | 74352351d6f5 |
parent 739 | 5149c39f3dfc |
permissions | -rw-r--r-- |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
1 |
import collections |
740
74352351d6f5
replace ipaddr with ipaddress
Tero Marttila <terom@paivola.fi>
parents:
739
diff
changeset
|
2 |
import ipaddress |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
3 |
import logging; log = logging.getLogger('pvl.hosts.host') |
457
1e925a1cc8de
pvl.hosts: fixup Host.fqdn
Tero Marttila <tero.marttila@aalto.fi>
parents:
450
diff
changeset
|
4 |
import pvl.dns |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
5 |
|
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
6 |
class HostError (Exception): |
485
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
7 |
""" |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
8 |
An error associated with some specific Host. |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
9 |
""" |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
10 |
|
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
11 |
def __init__(self, host, error): |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
12 |
""" |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
13 |
host : Host which caused error |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
14 |
error : Exception or str message |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
15 |
""" |
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
16 |
self.host = host |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
17 |
self.error = error |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
18 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
19 |
def __str__ (self): |
485
f0e09ec434e9
pvl.hosts.HostError: explicitly doc that host:Host
Tero Marttila <tero.marttila@aalto.fi>
parents:
470
diff
changeset
|
20 |
return "{self.host}: {self.error}".format(self=self) |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
21 |
|
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
22 |
def parse_bool(value): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
23 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
24 |
Normalize optional boolean value. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
25 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
26 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
27 |
if value is None: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
28 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
29 |
elif value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
30 |
return True |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
31 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
32 |
return False |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
33 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
34 |
def parse_ip(value, type): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
35 |
if value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
36 |
return type(value) |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
37 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
38 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
39 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
40 |
def parse_list(value): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
41 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
42 |
Parse list of strings. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
43 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
44 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
45 |
if value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
46 |
return value.split() |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
47 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
48 |
return () |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
49 |
|
470
ac334a55eebc
pvl.hosts: fix location= and test forward records
Tero Marttila <tero.marttila@aalto.fi>
parents:
468
diff
changeset
|
50 |
def parse_location(location, domain): |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
51 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
52 |
Parse location@domain. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
53 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
54 |
|
470
ac334a55eebc
pvl.hosts: fix location= and test forward records
Tero Marttila <tero.marttila@aalto.fi>
parents:
468
diff
changeset
|
55 |
if not location: |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
56 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
57 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
58 |
if '@' in location: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
59 |
location, location_domain = location.split('@', 1) |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
60 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
61 |
location_domain = domain |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
62 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
63 |
return (location, location_domain) |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
64 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
65 |
def parse_ethernet(value): |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
66 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
67 |
Normalize ethernet str. |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
68 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
69 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
70 |
return ':'.join('%02x' % int(x, 16) for x in value.split(':')) |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
71 |
|
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
72 |
def parse_str(value): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
73 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
74 |
Normalize optional string value. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
75 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
76 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
77 |
if value is None: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
78 |
# omit |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
79 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
80 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
81 |
elif value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
82 |
return str(value) |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
83 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
84 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
85 |
# empty value |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
86 |
return False |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
87 |
|
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
88 |
def parse_dict(value, parse, **opts): |
694
d34fa1090221
pvl.hosts: default extensions to an empty dict
Tero Marttila <tero.marttila@aalto.fi>
parents:
691
diff
changeset
|
89 |
if not value: |
d34fa1090221
pvl.hosts: default extensions to an empty dict
Tero Marttila <tero.marttila@aalto.fi>
parents:
691
diff
changeset
|
90 |
return { } |
d34fa1090221
pvl.hosts: default extensions to an empty dict
Tero Marttila <tero.marttila@aalto.fi>
parents:
691
diff
changeset
|
91 |
|
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
92 |
if isinstance(value, dict): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
93 |
values = value |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
94 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
95 |
values = {None: value} |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
96 |
|
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
97 |
return {instance: parse(value, **opts) for instance, value in values.iteritems()} |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
98 |
|
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
99 |
class Host (object) : |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
100 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
101 |
A host is a network node that can have multiple ethernet interfaces, and multiple IP addresses in different domains. |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
102 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
103 |
|
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
104 |
EXTENSIONS = { } |
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
105 |
EXTENSION_FIELDS = { } |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
106 |
|
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
107 |
@classmethod |
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
108 |
def parse_extensions(cls, extensions, extra): |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
109 |
""" |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
110 |
Parse extensions and extension fields to yield |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
111 |
(HostExtension, field, value) |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
112 |
""" |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
113 |
|
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
114 |
for extension, values in extensions.iteritems(): |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
115 |
extension_cls = cls.EXTENSIONS.get(extension) |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
116 |
|
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
117 |
if not extension_cls: |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
118 |
log.warning("skip unknown extension: %s", extension) |
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
119 |
continue |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
120 |
|
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
121 |
for field, value in values.iteritems(): |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
122 |
yield extension_cls, field, value |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
123 |
|
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
124 |
for field, value in extra.iteritems(): |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
125 |
extension_cls = cls.EXTENSION_FIELDS.get(field) |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
126 |
|
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
127 |
if not extension_cls: |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
128 |
raise ValueError("unknown field: {field}".format(field=field)) |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
129 |
|
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
130 |
yield extension_cls, field, value |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
131 |
|
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
132 |
@classmethod |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
133 |
def build (cls, name, domain, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
134 |
ip=None, ip6=None, |
694
d34fa1090221
pvl.hosts: default extensions to an empty dict
Tero Marttila <tero.marttila@aalto.fi>
parents:
691
diff
changeset
|
135 |
ethernet=None, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
136 |
owner=None, |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
137 |
location=None, |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
138 |
alias=None, alias4=None, alias6=None, |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
139 |
forward=None, reverse=None, |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
140 |
down=None, |
694
d34fa1090221
pvl.hosts: default extensions to an empty dict
Tero Marttila <tero.marttila@aalto.fi>
parents:
691
diff
changeset
|
141 |
extensions={ }, |
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
142 |
**extra |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
143 |
) : |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
144 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
145 |
Return a Host initialized from data attributes. |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
146 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
147 |
This handles all string parsing to our data types. |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
148 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
149 |
|
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
150 |
extension_classes = { } |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
151 |
|
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
152 |
for extension_cls, field, value in cls.parse_extensions(extensions, extra): |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
153 |
extension_classes.setdefault(extension_cls, dict())[field] = value |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
154 |
|
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
155 |
extensions = {extension_cls.EXTENSION: extension_cls.build(**params) for extension_cls, params in extension_classes.iteritems()} |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
156 |
|
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
157 |
return cls(name, |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
158 |
domain = domain, |
740
74352351d6f5
replace ipaddr with ipaddress
Tero Marttila <terom@paivola.fi>
parents:
739
diff
changeset
|
159 |
ip4 = parse_ip(ip, ipaddress.IPv4Address), |
74352351d6f5
replace ipaddr with ipaddress
Tero Marttila <terom@paivola.fi>
parents:
739
diff
changeset
|
160 |
ip6 = parse_ip(ip6, ipaddress.IPv6Address), |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
161 |
ethernet = parse_dict(ethernet, parse_ethernet), |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
162 |
owner = owner, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
163 |
location = parse_location(location, domain), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
164 |
alias = parse_list(alias), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
165 |
alias4 = parse_list(alias4), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
166 |
alias6 = parse_list(alias6), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
167 |
forward = parse_str(forward), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
168 |
reverse = parse_str(reverse), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
169 |
down = parse_bool(down), |
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
170 |
extensions = extensions, |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
171 |
) |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
172 |
|
446
139b3a41b608
pvl.hosts: rename Host.name, and have str return name@domain; this is a breaking API change
Tero Marttila <tero.marttila@aalto.fi>
parents:
441
diff
changeset
|
173 |
def __init__ (self, name, domain, |
733
45bedeba92e5
pvl.hosts: rename Host.ip -> Host.ip4; support instanced ip.foo = ... for foo.host A .... sub-labels
Tero Marttila <terom@paivola.fi>
parents:
696
diff
changeset
|
174 |
ip4=None, ip6=None, |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
175 |
ethernet={ }, |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
176 |
owner=None, |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
177 |
location=None, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
178 |
alias=(), alias4=(), alias6=(), |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
179 |
forward=None, reverse=None, |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
180 |
down=None, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
181 |
extensions={}, |
493
c9725dd0d48c
pvl.hosts: test FQDN hosts
Tero Marttila <tero.marttila@aalto.fi>
parents:
485
diff
changeset
|
182 |
): |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
183 |
""" |
446
139b3a41b608
pvl.hosts: rename Host.name, and have str return name@domain; this is a breaking API change
Tero Marttila <tero.marttila@aalto.fi>
parents:
441
diff
changeset
|
184 |
name - str |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
185 |
domain - str |
740
74352351d6f5
replace ipaddr with ipaddress
Tero Marttila <terom@paivola.fi>
parents:
739
diff
changeset
|
186 |
ip4 - ipaddress.IPv4Address |
74352351d6f5
replace ipaddr with ipaddress
Tero Marttila <terom@paivola.fi>
parents:
739
diff
changeset
|
187 |
ip6 - ipaddress.IPv6Address |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
188 |
ethernet - { index: ethernet } |
468
3e7cb8dd5708
pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents:
467
diff
changeset
|
189 |
alias - [ str ]: generate CNAMEs for given relative names |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
190 |
owner - str: LDAP uid |
470
ac334a55eebc
pvl.hosts: fix location= and test forward records
Tero Marttila <tero.marttila@aalto.fi>
parents:
468
diff
changeset
|
191 |
location - None or (name, domain) |
468
3e7cb8dd5708
pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents:
467
diff
changeset
|
192 |
alias4 - [ str ]: generate additional A records for given relative names |
3e7cb8dd5708
pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents:
467
diff
changeset
|
193 |
alias6 - [ str ]: generate additional AAAA records for given relative names |
464
f1d3dbf04ca3
pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents:
457
diff
changeset
|
194 |
forward - None: generate forward zone A/AAAA records per ip/ip6 |
467
3bb00e5e79d3
pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents:
464
diff
changeset
|
195 |
False: omit A/AAAA records (and any alias= CNAMEs) |
464
f1d3dbf04ca3
pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents:
457
diff
changeset
|
196 |
str: generate forward zone CNAME to given fqdn |
f1d3dbf04ca3
pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents:
457
diff
changeset
|
197 |
reverse - None: generate reverse zone PTR records per ip/ip6 |
f1d3dbf04ca3
pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents:
457
diff
changeset
|
198 |
False: omit PTR records for ip/ip6 |
f1d3dbf04ca3
pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents:
457
diff
changeset
|
199 |
str: generate IPv4 reverse zone CNAME to given fqdn, and omit IPv6 PTR |
f1d3dbf04ca3
pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents:
457
diff
changeset
|
200 |
down - mark as offline for polling |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
201 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
202 |
|
446
139b3a41b608
pvl.hosts: rename Host.name, and have str return name@domain; this is a breaking API change
Tero Marttila <tero.marttila@aalto.fi>
parents:
441
diff
changeset
|
203 |
self.name = name |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
204 |
self.domain = domain |
733
45bedeba92e5
pvl.hosts: rename Host.ip -> Host.ip4; support instanced ip.foo = ... for foo.host A .... sub-labels
Tero Marttila <terom@paivola.fi>
parents:
696
diff
changeset
|
205 |
self.ip4 = ip4 |
45bedeba92e5
pvl.hosts: rename Host.ip -> Host.ip4; support instanced ip.foo = ... for foo.host A .... sub-labels
Tero Marttila <terom@paivola.fi>
parents:
696
diff
changeset
|
206 |
self.ip6 = ip6 |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
207 |
self.ethernet = ethernet |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
208 |
self.alias = alias |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
209 |
self.alias4 = alias4 |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
210 |
self.alias6 = alias6 |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
211 |
self.owner = owner |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
212 |
self.location = location |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
213 |
self.forward = forward |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
214 |
self.reverse = reverse |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
215 |
self.down = down |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
216 |
self.extensions = extensions |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
217 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
218 |
def sort_key (self): |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
219 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
220 |
Stable sort ordering |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
221 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
222 |
|
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
223 |
if self.ip4: |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
224 |
return self.ip4 |
493
c9725dd0d48c
pvl.hosts: test FQDN hosts
Tero Marttila <tero.marttila@aalto.fi>
parents:
485
diff
changeset
|
225 |
else: |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
226 |
# sorts first |
740
74352351d6f5
replace ipaddr with ipaddress
Tero Marttila <terom@paivola.fi>
parents:
739
diff
changeset
|
227 |
return ipaddress.IPvAddress(0) |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
228 |
|
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
229 |
def addresses (self): |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
230 |
""" |
740
74352351d6f5
replace ipaddr with ipaddress
Tero Marttila <terom@paivola.fi>
parents:
739
diff
changeset
|
231 |
Yield (sublabel, ipaddress.IP*Address) records. |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
232 |
""" |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
233 |
|
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
234 |
if self.ip4: |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
235 |
yield None, self.ip4 |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
236 |
|
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
237 |
if self.ip6: |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
238 |
yield None, self.ip6 |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
239 |
|
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
240 |
for extension in self.extensions.itervalues(): |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
241 |
for sublabel, ip in extension.addresses(): |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
242 |
yield sublabel, ip |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
243 |
|
493
c9725dd0d48c
pvl.hosts: test FQDN hosts
Tero Marttila <tero.marttila@aalto.fi>
parents:
485
diff
changeset
|
244 |
def fqdn (self): |
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
245 |
""" |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
246 |
Return DNS FQDN for this host in its domain. |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
247 |
""" |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
248 |
|
493
c9725dd0d48c
pvl.hosts: test FQDN hosts
Tero Marttila <tero.marttila@aalto.fi>
parents:
485
diff
changeset
|
249 |
if self.domain: |
457
1e925a1cc8de
pvl.hosts: fixup Host.fqdn
Tero Marttila <tero.marttila@aalto.fi>
parents:
450
diff
changeset
|
250 |
return pvl.dns.fqdn(self.name, self.domain) |
493
c9725dd0d48c
pvl.hosts: test FQDN hosts
Tero Marttila <tero.marttila@aalto.fi>
parents:
485
diff
changeset
|
251 |
else: |
457
1e925a1cc8de
pvl.hosts: fixup Host.fqdn
Tero Marttila <tero.marttila@aalto.fi>
parents:
450
diff
changeset
|
252 |
return pvl.dns.fqdn(self.name) |
440
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
253 |
|
493
c9725dd0d48c
pvl.hosts: test FQDN hosts
Tero Marttila <tero.marttila@aalto.fi>
parents:
485
diff
changeset
|
254 |
def __str__ (self): |
503
a56456f901e8
pvl.hosts: deprecate [host] domain= with [host@domain]
Tero Marttila <tero.marttila@aalto.fi>
parents:
501
diff
changeset
|
255 |
return "{self.name}@{domain}".format(self=self, |
a56456f901e8
pvl.hosts: deprecate [host] domain= with [host@domain]
Tero Marttila <tero.marttila@aalto.fi>
parents:
501
diff
changeset
|
256 |
domain = self.domain or '', |
a56456f901e8
pvl.hosts: deprecate [host] domain= with [host@domain]
Tero Marttila <tero.marttila@aalto.fi>
parents:
501
diff
changeset
|
257 |
) |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
258 |
|
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
259 |
class HostExtension (object): |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
260 |
""" |
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
261 |
Base class for Host.EXTENSIONS |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
262 |
|
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
263 |
Provides default no-op behaviours for extension hooks. |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
264 |
""" |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
265 |
|
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
266 |
EXTENSION = None |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
267 |
EXTENSION_FIELDS = () |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
268 |
|
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
269 |
def addresses (self): |
738
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
270 |
""" |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
271 |
Yield additional (sublabel, ipaddr) records. |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
272 |
""" |
3104fdf7ea26
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
Tero Marttila <terom@paivola.fi>
parents:
734
diff
changeset
|
273 |
|
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
274 |
return () |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
275 |
|
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
276 |
def register_extension (cls): |
734
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
277 |
""" |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
278 |
Register an extension class |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
279 |
""" |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
280 |
|
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
281 |
Host.EXTENSIONS[cls.EXTENSION] = cls |
5770ed34c1f0
pvl.hosts: extension mechanism
Tero Marttila <terom@paivola.fi>
parents:
733
diff
changeset
|
282 |
|
739
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
283 |
for field in cls.EXTENSION_FIELDS: |
5149c39f3dfc
pvl.hosts: improve HostExtension support enough to move boot= into pvl.hosts.dhcp
Tero Marttila <terom@paivola.fi>
parents:
738
diff
changeset
|
284 |
Host.EXTENSION_FIELDS[field] = cls |