66 # apply |
66 # apply |
67 pvl.args.apply(options, argv[0]) |
67 pvl.args.apply(options, argv[0]) |
68 |
68 |
69 return options, args |
69 return options, args |
70 |
70 |
71 ZONE_COMMENTS = ( |
|
72 re.compile(r'(?P<owner>[^/]+)\s*-\s+(?P<host>.+)'), |
|
73 re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[/-]\s+(?P<host>.+)'), |
|
74 re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[(]\s*(?P<host>.+)[)]'), |
|
75 re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)'), |
|
76 re.compile(r'(?P<owner>.+)'), |
|
77 ) |
|
78 |
|
79 ZONE_OWNER_MAIL = re.compile(r'(?P<owner>.*?)\s*<(?P<mail>.+?)>') |
|
80 |
|
81 def process_zone_comment (options, hostname, comment) : |
|
82 """ |
|
83 Attempt to parse a host comment field... :D |
|
84 """ |
|
85 |
|
86 yield 'comment', comment |
|
87 |
|
88 for regex in ZONE_COMMENTS : |
|
89 match = regex.match(comment) |
|
90 |
|
91 if match : |
|
92 break |
|
93 else : |
|
94 log.warn("%s: unparsed comment: %s", hostname, comment) |
|
95 return |
|
96 |
|
97 matches = match.groupdict() |
|
98 owner = matches.pop('owner', None) |
|
99 |
|
100 if owner : |
|
101 mail_match = ZONE_OWNER_MAIL.match(owner) |
|
102 |
|
103 if mail_match : |
|
104 mail_matches = mail_match.groupdict() |
|
105 |
|
106 owner = mail_matches['owner'] |
|
107 yield 'comment-mail', mail_matches['mail'] |
|
108 else : |
|
109 mail_matches = { } |
|
110 else : |
|
111 mail_matches = { } |
|
112 |
|
113 yield 'comment-owner', owner |
|
114 |
|
115 for group, value in matches.iteritems() : |
|
116 if value : |
|
117 yield 'comment-{group}'.format(group=group), value.strip() |
|
118 |
|
119 print u"{hostname:20} {comment:80} = {group:15} / {owner:20} <{mail:20}> / {host}".format( |
|
120 hostname = hostname, |
|
121 comment = comment, |
|
122 group = matches.get('group', ''), |
|
123 owner = owner, |
|
124 mail = mail_matches.get('mail', ''), |
|
125 host = matches.get('host', ''), |
|
126 ).encode('utf-8') |
|
127 |
|
128 def process_zone_hosts (options, file) : |
71 def process_zone_hosts (options, file) : |
129 """ |
72 """ |
130 Yield host info from zonefile records. |
73 Yield host info from zonefile records. |
131 """ |
74 """ |
132 |
75 |
139 ip, = rr.data |
82 ip, = rr.data |
140 |
83 |
141 yield rr.name, 'ip', ip |
84 yield rr.name, 'ip', ip |
142 |
85 |
143 if rr.comment : |
86 if rr.comment : |
144 for field, value in process_zone_comment(options, rr.name, rr.comment) : |
87 yield rr.name, 'comment', rr.comment |
145 yield rr.name, field, value |
|
146 |
88 |
147 elif rr.type == 'CNAME' : |
89 elif rr.type == 'CNAME' : |
148 host, = rr.data |
90 host, = rr.data |
149 |
91 |
150 yield host, 'alias', rr.name |
92 yield host, 'alias', rr.name |
245 |
187 |
246 if options.import_dhcp_hosts: |
188 if options.import_dhcp_hosts: |
247 for info in process_dhcp_conf(options, |
189 for info in process_dhcp_conf(options, |
248 pvl.args.apply_file(options.import_dhcp_hosts)) : |
190 pvl.args.apply_file(options.import_dhcp_hosts)) : |
249 yield info |
191 yield info |
250 |
192 |
|
193 ZONE_COMMENTS = ( |
|
194 re.compile(r'(?P<owner>[^/]+)\s*-\s+(?P<host>.+)'), |
|
195 re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[/-]\s+(?P<host>.+)'), |
|
196 re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[(]\s*(?P<host>.+)[)]'), |
|
197 re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)'), |
|
198 re.compile(r'(?P<owner>.+)'), |
|
199 ) |
|
200 |
|
201 ZONE_OWNER_MAIL = re.compile(r'(?P<owner>.*?)\s*<(?P<mail>.+?)>') |
|
202 |
|
203 def process_zone_comment (options, hostname, comment) : |
|
204 """ |
|
205 Attempt to parse a host comment field... :D |
|
206 |
|
207 Yields (field, value) bits |
|
208 """ |
|
209 |
|
210 for regex in ZONE_COMMENTS : |
|
211 match = regex.match(comment) |
|
212 |
|
213 if match : |
|
214 break |
|
215 else : |
|
216 log.warn("%s: unparsed comment: %s", hostname, comment) |
|
217 return |
|
218 |
|
219 matches = match.groupdict() |
|
220 owner = matches.pop('owner', None) |
|
221 |
|
222 if owner : |
|
223 mail_match = ZONE_OWNER_MAIL.match(owner) |
|
224 |
|
225 if mail_match : |
|
226 mail_matches = mail_match.groupdict() |
|
227 |
|
228 owner = mail_matches['owner'] |
|
229 yield 'mail', mail_matches['mail'] |
|
230 |
|
231 yield 'owner', owner |
|
232 |
|
233 for field, value in matches.iteritems() : |
|
234 if value : |
|
235 yield field, value |
|
236 |
|
237 |
|
238 def process_hosts_comments (options, import_hosts) : |
|
239 """ |
|
240 Parse out comments from host imports.. |
|
241 """ |
|
242 |
|
243 for host, field, value in import_hosts : |
|
244 if field != 'comment': |
|
245 yield host, field, value |
|
246 continue |
|
247 |
|
248 fields = dict(process_zone_comment(options, host, value)) |
|
249 |
|
250 print u"{host:20} {comment:80} = {group:15} / {owner:20} <{mail:20}> / {hostinfo}".format( |
|
251 host = host, |
|
252 comment = value, |
|
253 group = fields.get('group', ''), |
|
254 owner = fields.get('owner', ''), |
|
255 mail = fields.get('mail', ''), |
|
256 hostinfo = fields.get('host', ''), |
|
257 ).encode('utf-8') |
|
258 |
251 def process_hosts_import (options, import_hosts) : |
259 def process_hosts_import (options, import_hosts) : |
252 """ |
260 """ |
253 Import host definitions from given infos |
261 Import host definitions from given infos |
254 """ |
262 """ |
255 |
263 |
268 # direct from file |
276 # direct from file |
269 hosts = pvl.args.apply_files(args, 'r', options.input_charset) |
277 hosts = pvl.args.apply_files(args, 'r', options.input_charset) |
270 else : |
278 else : |
271 # import |
279 # import |
272 import_hosts = apply_hosts_import(options) |
280 import_hosts = apply_hosts_import(options) |
|
281 import_hosts = process_hosts_comments(options, import_hosts) |
273 hosts = process_hosts_import(options, import_hosts) |
282 hosts = process_hosts_import(options, import_hosts) |
274 |
283 |
275 # output |
284 # output |
276 if options.output_hosts : |
285 if options.output_hosts : |
277 for host, fields in hosts : |
286 for host, fields in hosts : |