author | Tero Marttila <tero.marttila@aalto.fi> |
Tue, 24 Feb 2015 19:18:45 +0200 | |
changeset 450 | 1d86e3909678 |
parent 446 | 139b3a41b608 |
child 457 | 1e925a1cc8de |
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 |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
2 |
import ipaddr |
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') |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
4 |
import pvl.dns.zone |
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): |
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
|
7 |
def __init__(self, name, 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
|
8 |
self.name = 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
|
9 |
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
|
10 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
11 |
def __str__ (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
|
12 |
return "{self.name}: {self.error}".format(self=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
|
13 |
|
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
14 |
def parse_bool(value): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
15 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
16 |
Normalize optional boolean value. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
17 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
18 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
19 |
if value is None: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
20 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
21 |
elif value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
22 |
return True |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
23 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
24 |
return False |
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 |
def parse_ip(value, type): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
27 |
if value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
28 |
return type(value) |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
29 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
30 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
31 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
32 |
def parse_list(value): |
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 |
Parse list of strings. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
35 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
36 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
37 |
if value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
38 |
return value.split() |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
39 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
40 |
return () |
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 |
def parse_location(value, domain): |
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 |
Parse location@domain. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
45 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
46 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
47 |
if not value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
48 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
49 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
50 |
if '@' in location: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
51 |
location, location_domain = location.split('@', 1) |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
52 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
53 |
location_domain = domain |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
54 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
55 |
return (location, location_domain) |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
56 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
57 |
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
|
58 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
59 |
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
|
60 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
61 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
62 |
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
|
63 |
|
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
64 |
def parse_str(value): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
65 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
66 |
Normalize optional string value. |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
67 |
""" |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
68 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
69 |
if value is None: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
70 |
# omit |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
71 |
return None |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
72 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
73 |
elif value: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
74 |
return str(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 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
77 |
# empty value |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
78 |
return False |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
79 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
80 |
def parse_dict(value, parse): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
81 |
if isinstance(value, dict): |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
82 |
values = value |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
83 |
else: |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
84 |
values = {None: value} |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
85 |
|
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
86 |
return { instance: parse(value) for instance, value in values.iteritems() } |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
87 |
|
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
|
88 |
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
|
89 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
90 |
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
|
91 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
92 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
93 |
# the label used for alias4/6 hosts |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
94 |
ALIAS4_FMT = '{host}-ipv4' |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
95 |
ALIAS6_FMT = '{host}-ipv6' |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
96 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
97 |
@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
|
98 |
def build (cls, name, domain, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
99 |
ip=None, ip6=None, |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
100 |
ethernet={ }, |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
101 |
owner=None, |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
down=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
|
106 |
boot=None, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
107 |
**extensions |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
108 |
) : |
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
|
109 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
110 |
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
|
111 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
112 |
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
|
113 |
""" |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
114 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
115 |
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
|
116 |
domain = domain, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
117 |
ip = parse_ip(ip, ipaddr.IPv4Address), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
118 |
ip6 = parse_ip(ip6, ipaddr.IPv6Address), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
119 |
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
|
120 |
owner = owner, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
121 |
location = parse_location(location, domain), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
122 |
alias = parse_list(alias), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
123 |
alias4 = parse_list(alias4), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
124 |
alias6 = parse_list(alias6), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
125 |
forward = parse_str(forward), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
126 |
reverse = parse_str(reverse), |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
127 |
down = parse_bool(down), |
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
|
128 |
boot = boot, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
129 |
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
|
130 |
) |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
131 |
|
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
|
132 |
def __init__ (self, name, domain, |
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
|
133 |
ip=None, ip6=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
|
134 |
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
|
135 |
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
|
136 |
location=None, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
down=None, |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
140 |
boot=None, |
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
141 |
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
|
142 |
) : |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
143 |
""" |
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
|
144 |
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
|
145 |
domain - 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
|
146 |
ip - ipaddr.IPv4Address |
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 |
ip6 - ipaddr.IPv6Address |
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 |
ethernet - { index: 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
|
149 |
alias - list |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
150 |
owner - str: LDAP uid |
450
1d86e3909678
pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents:
446
diff
changeset
|
151 |
location - location (name, domain) or 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
|
152 |
alias4 - list (CNAME -> A) |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
153 |
alias6 - list (CNAME -> AAAA) |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
154 |
forward - generate forward records, or CNAME into given zone |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
155 |
reverse - generate reverse records, or CNAME into given zone |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
156 |
down - not online |
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 |
""" |
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 |
|
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
|
159 |
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
|
160 |
self.domain = domain |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
161 |
self.ip = ip |
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 |
self.ip6 = ip6 |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
163 |
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
|
164 |
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
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
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
|
169 |
self.boot = boot |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
|
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 |
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
|
176 |
""" |
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 |
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
|
178 |
""" |
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 |
|
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 |
if self.ip : |
441
f058fff1f272
pvl.hosts.hosts: fix sort_key()
Tero Marttila <tero.marttila@aalto.fi>
parents:
440
diff
changeset
|
181 |
return self.ip |
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
|
182 |
else : |
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 |
# sorts first |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
184 |
return ipaddr.IPAddress(0) |
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 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
186 |
def fqdn (self) : |
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
|
187 |
if '.' in self.name: |
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
|
188 |
return 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
|
189 |
elif self.domain : |
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
|
190 |
return pvl.dns.zone.fqdn(self.name, self.domain) |
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
|
191 |
else : |
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
192 |
raise ValueError("%s: have no fqdn/domain" % (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
|
193 |
|
1d755df7bf97
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff
changeset
|
194 |
def __str__ (self) : |
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
|
195 |
return "{self.name}@{self.domain}".format(self=self) |