443 |
444 |
444 yield sort, host, fields |
445 yield sort, host, fields |
445 |
446 |
446 def export_hosts (options, hosts) : |
447 def export_hosts (options, hosts) : |
447 """ |
448 """ |
448 Export hosts to file. |
449 Generate hosts config lines for given hosts. |
449 """ |
450 """ |
450 |
451 |
451 file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset) |
452 FMT = u"\t{field:15} = {value}" |
|
453 |
|
454 yield u"[{domain}]".format(domain=options.hosts_domain) |
452 |
455 |
453 # filter + sort |
456 # filter + sort |
454 hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))] |
457 hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))] |
455 |
458 |
456 for host, fields in hosts : |
459 for host, fields in hosts : |
457 for comment in fields.get('comment-host', ()): |
460 for comment in fields.get('comment-host', ()): |
458 print >>file, u"# {comment}".format(comment=comment) |
461 yield u"# {comment}".format(comment=comment) |
459 |
462 |
460 print >>file, u"[{host}]".format(host=host) |
463 yield u"[[{host}]]".format(host=host) |
461 |
464 |
|
465 for domain in fields.get('domain', ()) : |
|
466 if domain != options.hosts_domain : |
|
467 yield FMT.format(field='domain', value=domain) |
|
468 |
462 for field, fmt in ( |
469 for field, fmt in ( |
463 ('ip', None), |
470 ('ip', FMT), |
464 ('ip6', None), |
471 ('ip6', FMT), |
465 ('ethernet', None), |
472 ('ethernet', FMT), |
466 ('owner', u"\t{field:15} = {value} # {fields[comment-owner][0]}"), |
473 ('owner', u"\t{field:15} = {value} # {fields[comment-owner][0]}"), |
467 ('alias', None), |
474 ('alias', FMT), |
468 ('boot', None), |
475 ('boot', FMT), |
469 ) : |
476 ) : |
470 if not fmt : |
|
471 fmt = u"\t{field:15} = {value}" |
|
472 |
|
473 values = fields.get(field, ()) |
477 values = fields.get(field, ()) |
474 |
478 |
475 if len(values) > 1 : |
479 if len(values) > 1 : |
476 for index, value in enumerate(values, 1) : |
480 for index, value in enumerate(values, 1) : |
477 print >>file, fmt.format( |
481 yield fmt.format( |
478 field = "{field}.{index}".format(field=field, index=index), |
482 field = "{field}.{index}".format(field=field, index=index), |
479 value = value, |
483 value = value, |
480 fields = fields |
484 fields = fields |
481 ) |
485 ) |
482 elif len(values) > 0 : |
486 elif len(values) > 0 : |
483 value, = values |
487 value, = values |
484 print >>file, fmt.format(field=field, value=value, fields=fields) |
488 yield fmt.format(field=field, value=value, fields=fields) |
485 |
489 |
486 print >>file |
490 yield "" |
|
491 |
|
492 def apply_hosts_export (options, hosts) : |
|
493 """ |
|
494 Export hosts to file. |
|
495 """ |
|
496 |
|
497 file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset) |
|
498 |
|
499 for line in export_hosts(options, hosts) : |
|
500 print >>file, line |
487 |
501 |
488 def main (argv) : |
502 def main (argv) : |
489 options, args = parse_options(argv) |
503 options, args = parse_options(argv) |
490 |
504 |
491 options.ldap = pvl.ldap.args.apply(options) |
505 options.ldap = pvl.ldap.args.apply(options) |