pvl/hosts/config.py
author Tero Marttila <tero.marttila@aalto.fi>
Tue, 03 Mar 2015 11:58:10 +0200
changeset 713 d5e2d1d9716a
parent 692 34f25380d0e7
permissions -rw-r--r--
pvl.hosts.config: --hosts-include-trace to write out all included files
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
"""
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
    Load Hosts from config files.
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
"""
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
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
import configobj
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     6
import logging; log = logging.getLogger('pvl.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
     7
import optparse
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
import os.path
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
import pvl.args
460
c93adfce8ae9 pvl.hosts.config: fix pvl.dns.generate usage
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    10
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
    11
import sys
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
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
from pvl.hosts.host import 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
    14
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    15
def optparser (parser):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    16
    hosts = optparse.OptionGroup(parser, "Hosts config files")
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
    hosts.add_option('--hosts-charset',         metavar='CHARSET',  default='utf-8', 
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
            help="Encoding used for host files")
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
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    20
    hosts.add_option('--hosts-domain',          metavar='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
    21
            help="Default domain for hosts. Default uses config file basename")
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    22
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    23
    hosts.add_option('--hosts-include',         metavar='PATH',
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    24
            help="Optional path for hosts includes, in addition to the host config dir")
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
    25
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
    26
    hosts.add_option('--hosts-include-trace',   metavar='FILE',
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
    27
            help="Write out all included file paths")
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
    28
    
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    29
    return 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
    30
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    31
class HostConfigError (Exception):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    32
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    33
        Generic error for file path.
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    34
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    35
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    36
    def __init__ (self, config, 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
    37
        self.config = config
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    38
        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
    39
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    40
    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
    41
        return "{self.config}: {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
    42
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    43
class HostConfigObjError (Exception):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    44
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    45
        An error from ConfigObj for a config file path.
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    46
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    47
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    48
    def __init__ (self, config, 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
    49
        self.config = config
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    50
        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
    51
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    52
        self.line_contents = error.line
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    53
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    54
    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
    55
        return "{self.config}:{self.error.line_number}: {self.error.message}".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
    56
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    57
def parse_expand(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
    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
        Parse a name containing an optional expansion part.
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
            name:           hostname containing optional "{...}" part
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
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
        Returns (name, range or 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
    64
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    65
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
    if '{' in name:
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 442
diff changeset
    67
        # consume the first {...} token as the range
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
    68
        pre, name = name.split('{', 1)
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 442
diff changeset
    69
        range, post = name.split('}', 1)
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 442
diff changeset
    70
        
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 442
diff changeset
    71
        # if there's a second {...} token, it will be re-composed into ${...}
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 442
diff changeset
    72
        name = pre + "$" + post
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
    73
        
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
    74
        # TODO: raise HostConfigError
460
c93adfce8ae9 pvl.hosts.config: fix pvl.dns.generate usage
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    75
        range = pvl.dns.parse_generate_range(range)
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
    76
    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
    77
        range = 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
    78
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    79
    return name, range
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    80
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    81
def parse_config_field(field):
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    82
    """
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    83
        Parse structured config fields.
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    84
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    85
            [<extension> ":"] <field> ["." <instance>]
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    86
    """
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    87
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    88
    if ':' in field :
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    89
        extension, field = field.split(':', 1)
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    90
    else:
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    91
        extension = None
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    92
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    93
    if '.' in field :
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    94
        field, instance = field.split('.')
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    95
    else :
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    96
        instance = None
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    97
    
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
    98
    return extension, field, instance
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    99
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   100
def apply_host (name, domain, config):
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   101
    """
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   102
        Return Host from an (expanded) config section.
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   103
            
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   104
            name        - (expanded) name of host
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   105
            domain      - domain for host
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   106
            config      - host config fields to parse
661
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   107
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   108
        Fields can either be scalar string values, or instance'd dicts. If a field has both a non-instance'd value and instance'd values,
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   109
        the non-instance'd value will be use instance=None:
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   110
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   111
        Raises ValueError.
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   112
    """
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   113
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   114
    fields = { }
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   115
    extensions = fields['extensions'] = { }
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   116
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   117
    for field, value in config.iteritems():
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   118
        extension, field, instance = parse_config_field(field)
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   119
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   120
        if extension:
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   121
            f = extensions.setdefault(extension, {})
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   122
        else:
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   123
            f = fields
451
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 450
diff changeset
   124
690
517527835381 pvl.hosts.config: fix handling of mixed boot=... boot.foo=... independent of dict ordering
Tero Marttila <tero.marttila@aalto.fi>
parents: 663
diff changeset
   125
        log.debug("%s@%s: %s:%s.%s = %r", name, domain, extension, field, instance, value)
517527835381 pvl.hosts.config: fix handling of mixed boot=... boot.foo=... independent of dict ordering
Tero Marttila <tero.marttila@aalto.fi>
parents: 663
diff changeset
   126
661
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   127
        if instance:
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   128
            if field not in f:
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   129
                f[field] = { }
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   130
            elif not isinstance(f[field], dict):
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   131
                # convert to dict
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   132
                f[field] = {None: f[field]}
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   133
            
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   134
            f[field][instance] = value
690
517527835381 pvl.hosts.config: fix handling of mixed boot=... boot.foo=... independent of dict ordering
Tero Marttila <tero.marttila@aalto.fi>
parents: 663
diff changeset
   135
        elif field in f:
517527835381 pvl.hosts.config: fix handling of mixed boot=... boot.foo=... independent of dict ordering
Tero Marttila <tero.marttila@aalto.fi>
parents: 663
diff changeset
   136
            f[field][None] = value
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   137
        else:
661
15946375b154 pvl.hosts.config: support ethernet = ... + ethernet.ethX = ...
Tero Marttila <terom@paivola.fi>
parents: 518
diff changeset
   138
            f[field] = value
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   139
    
450
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   140
    return Host.build(name, domain, **fields)
1d86e3909678 pvl.hosts: cleanup host config parsing
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
   141
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   142
def apply_hosts (parent, name, config):
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
   143
    """
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
        Yield Hosts from a given config section.
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   145
            
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   146
            parent      - parent filename/section containing this host item, or None.
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   147
                          used for domain
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   148
            name        - name of the section (or file) containing this host item.
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   149
                          used for hostname
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   150
            config      - host parameters to parse
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   151
        
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   152
        Raises ValueError.
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
   153
    """
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
    
503
a56456f901e8 pvl.hosts: deprecate [host] domain= with [host@domain]
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
   155
    if '@' in name:
a56456f901e8 pvl.hosts: deprecate [host] domain= with [host@domain]
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
   156
        name, domain = name.split('@', 1)
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
        log.debug("%s: using explicit domain: %s", name, 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
   158
    elif '.' in 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
   159
        log.debug("%s: using as fqdn without domain", 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
   160
        domain = 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
   161
    elif parent:
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
        log.debug("%s: default domain to section: %s", name, parent)
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
        domain = parent
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
    else:
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   165
        # XXX: impossible?
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   166
        raise ValueError("no domain given")
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
   167
    
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   168
    # expand?
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
   169
    name, range = parse_expand(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
   170
    
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
    if range:
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   172
        generate_name = pvl.dns.parse_generate_field(name)
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   173
        
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   174
        # expand all fields
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   175
        generate_config = {field: pvl.dns.parse_generate_field(value) for field, value in config.iteritems()}
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   176
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   177
        for i in range:
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   178
            yield apply_host(generate_name(i), domain, 
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   179
                    {field: value(i) for field, value in generate_config.iteritems()},
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   180
            )
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   181
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:
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   183
        # single host
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   184
        yield apply_host(name, domain, config)
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
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   186
def parse_config_includes (options, config_path, includes, **opts):
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
   187
    """
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   188
        Yield file paths from a given config's include=... 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
   189
    """
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   190
    
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   191
    # relative
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
   192
    include_paths = [os.path.dirname(config_path)]
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
    if options.hosts_include:
518
cd152d6bad32 pvl.hosts.config: test and fix includes path
Tero Marttila <tero.marttila@aalto.fi>
parents: 517
diff changeset
   195
        include_paths.append(options.hosts_include)
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
   196
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   197
    for include in includes:
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
   198
        for include_path in include_paths:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   199
            path = os.path.join(include_path, include)
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   200
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
            if os.path.exists(path):
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
                break
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   203
        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
   204
            raise HostConfigError(config_path, "Unable to find include {include} in include path: {include_path}".format(
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   205
                    include=include,
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   206
                    include_path=' '.join(include_paths),
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
            ))
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
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
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   210
        log.info("%s: include: %s", config_path, path)
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   211
        yield path
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
   212
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   213
def apply_hosts_configs (options, path, name, config, parent=None, defaults={}, include_trace=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
   214
    """
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
        Load hosts from a configobj.Section (which can be the top-level ConfigObj).
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
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
            options         global options
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
            path            filesystem path of file (for errors)
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   219
            name            name of this section/file
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
   220
            config          configobj.Section
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   221
            parent          parent section from included files or --hosts-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
   222
            defaults        hierarchial section defaults
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   223
            include_trace   - optional list to append loaded files to
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
   224
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   225
    
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
    # items in this section
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   227
    section = dict(defaults)
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
    for scalar in config.scalars:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   229
        section[scalar] = config[scalar]
507
e3a32f4dff54 pvl.hosts.config: document includes, fix include-only zone, and test
Tero Marttila <tero.marttila@aalto.fi>
parents: 504
diff changeset
   230
e3a32f4dff54 pvl.hosts.config: document includes, fix include-only zone, and test
Tero Marttila <tero.marttila@aalto.fi>
parents: 504
diff changeset
   231
    # process includes?
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   232
    if 'include' in section:
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   233
        # convert from unicode
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   234
        includes = [str(include) for include in section.pop('include').split()]
507
e3a32f4dff54 pvl.hosts.config: document includes, fix include-only zone, and test
Tero Marttila <tero.marttila@aalto.fi>
parents: 504
diff changeset
   235
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   236
        includes = list(parse_config_includes(options, path, includes))
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   237
507
e3a32f4dff54 pvl.hosts.config: document includes, fix include-only zone, and test
Tero Marttila <tero.marttila@aalto.fi>
parents: 504
diff changeset
   238
        # within our domain context
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   239
        for host in apply_hosts_files(options, includes, include_trace=include_trace,
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   240
                parent=name, defaults=section
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   241
        ):
507
e3a32f4dff54 pvl.hosts.config: document includes, fix include-only zone, and test
Tero Marttila <tero.marttila@aalto.fi>
parents: 504
diff changeset
   242
            yield host
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   243
    else:
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   244
        includes = None
507
e3a32f4dff54 pvl.hosts.config: document includes, fix include-only zone, and test
Tero Marttila <tero.marttila@aalto.fi>
parents: 504
diff changeset
   245
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
   246
    if config.sections:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   247
        # this is a top-level section that includes hosts
511
99043eab9140 pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
Tero Marttila <tero.marttila@aalto.fi>
parents: 510
diff changeset
   248
        if parent:
99043eab9140 pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
Tero Marttila <tero.marttila@aalto.fi>
parents: 510
diff changeset
   249
            log.info("%s: @%s@%s", path, name, parent)
99043eab9140 pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
Tero Marttila <tero.marttila@aalto.fi>
parents: 510
diff changeset
   250
99043eab9140 pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
Tero Marttila <tero.marttila@aalto.fi>
parents: 510
diff changeset
   251
            name = pvl.dns.join(name, parent)
99043eab9140 pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
Tero Marttila <tero.marttila@aalto.fi>
parents: 510
diff changeset
   252
        else:
99043eab9140 pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
Tero Marttila <tero.marttila@aalto.fi>
parents: 510
diff changeset
   253
            log.info("%s: @%s", path, 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
   254
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   255
        # recurse until we hit a scalar-only section representing a 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
   256
        for section_name in config.sections:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   257
            log.debug("%s: %s: %s", path, name, section_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
   258
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   259
            for host in apply_hosts_configs(options, path, section_name, config[section_name], parent=name, defaults=section):
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
   260
                yield 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
   261
692
34f25380d0e7 pvl.hosts.config: support defaults for included hosts
Tero Marttila <tero.marttila@aalto.fi>
parents: 690
diff changeset
   262
    elif includes:
34f25380d0e7 pvl.hosts.config: support defaults for included hosts
Tero Marttila <tero.marttila@aalto.fi>
parents: 690
diff changeset
   263
        # includes-only zone
34f25380d0e7 pvl.hosts.config: support defaults for included hosts
Tero Marttila <tero.marttila@aalto.fi>
parents: 690
diff changeset
   264
        pass
34f25380d0e7 pvl.hosts.config: support defaults for included hosts
Tero Marttila <tero.marttila@aalto.fi>
parents: 690
diff changeset
   265
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
   266
    elif parent:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   267
        # this is a host section
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   268
        log.debug("%s: %s@%s", path, name, parent)
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
   269
        
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
   270
        try:
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   271
            for host in apply_hosts(parent, name, section):
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
   272
                log.info("%s: %s", path, 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
   273
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
   274
                yield host
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   275
        
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   276
        except ValueError as error:
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   277
            log.exception("%s: %s: %s", path, parent, name)
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
   278
504
ee0a3dcacb95 pvl.hosts.config: refactor
Tero Marttila <tero.marttila@aalto.fi>
parents: 503
diff changeset
   279
            raise HostConfigError(path, "{parent}: {name}: {error}".format(parent=parent, name=name, error=error))
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
   280
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   281
    elif section:
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   282
        raise HostConfigError(path, "Top-level hosts are only allowed in included confs")
692
34f25380d0e7 pvl.hosts.config: support defaults for included hosts
Tero Marttila <tero.marttila@aalto.fi>
parents: 690
diff changeset
   283
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
   284
    else:
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   285
        # empty file
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   286
        log.info("%s: skip empty conf", path)
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
   287
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   288
def apply_hosts_config (options, file, **opts):
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
   289
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   290
        Load Hosts from a file path.
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   291
            
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   292
            file            - opened file object, with .name attribute
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
   293
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   294
        Raises
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   295
            HostConfigError
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   296
            HostConfigObjError
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   297
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   298
   
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   299
    # use file basename as default 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
   300
    path = file.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
   301
    name = os.path.basename(path)
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   302
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   303
    try:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   304
        config = configobj.ConfigObj(file,
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   305
                raise_errors    = True, # raise ConfigPObjError immediately
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   306
                interpolation   = 'Template',
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   307
                encoding        = options.hosts_charset,
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   308
        )
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   309
    except configobj.ConfigObjError as ex:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   310
        raise HostConfigObjError(path, ex)
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   311
    
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   312
    return apply_hosts_configs(options, path, name, config, **opts)
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
   313
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   314
def apply_hosts_file (options, path, include_trace=None, **opts):
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   315
    """
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   316
        Load Hosts from a file path.
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   317
            
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   318
            include_trace           - optional list to append loaded files to
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   319
    """
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   320
    
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   321
    if include_trace is not None:
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   322
        log.debug("%s: include trace", path)
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   323
        include_trace.append(path)
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   324
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   325
    try:
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   326
        file = open(path)
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   327
    except IOError as ex:
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   328
        raise HostConfigError(path, ex.strerror)
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   329
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   330
    for host in apply_hosts_config(options, file, include_trace=include_trace, **opts):
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   331
        yield host
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   332
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   333
def apply_hosts_directory (options, root, include_trace=None, **opts):
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   334
    """
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   335
        Load Hosts from a directory, loading each file within the directory.
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   336
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   337
            include_trace           - optional list to append loaded files to
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   338
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   339
        Skips .dotfiles.
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   340
    """
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   341
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   342
    if include_trace is not None:
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   343
        log.debug("%s: include trace", root)
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   344
        include_trace.append(root)
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   345
663
8a9de457bb59 pvl.hosts.config: sort directory items for stable host ordering
Tero Marttila <terom@paivola.fi>
parents: 661
diff changeset
   346
    for name in sorted(os.listdir(root)):
513
3b45b4fd5102 pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
Tero Marttila <tero.marttila@aalto.fi>
parents: 511
diff changeset
   347
        path = os.path.join(root, name)
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   348
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   349
        if name.startswith('.'):
513
3b45b4fd5102 pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
Tero Marttila <tero.marttila@aalto.fi>
parents: 511
diff changeset
   350
            log.debug("%s: skip dotfile: %s", root, name)
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   351
            continue
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   352
513
3b45b4fd5102 pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
Tero Marttila <tero.marttila@aalto.fi>
parents: 511
diff changeset
   353
        if os.path.isdir(path):
3b45b4fd5102 pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
Tero Marttila <tero.marttila@aalto.fi>
parents: 511
diff changeset
   354
            log.debug("%s: skip directory: %s", root, name)
3b45b4fd5102 pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
Tero Marttila <tero.marttila@aalto.fi>
parents: 511
diff changeset
   355
            continue
3b45b4fd5102 pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
Tero Marttila <tero.marttila@aalto.fi>
parents: 511
diff changeset
   356
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   357
        for host in apply_hosts_file(options, path, include_trace=include_trace, **opts):
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   358
            yield host
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   359
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   360
def apply_hosts_files (options, files, **opts):
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
   361
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   362
        Load Hosts from files.
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   363
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   364
            files:[str]     list of filesystem paths, which may be directories or files
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
   365
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   366
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   367
    for path in files:
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   368
        if os.path.isdir(path):
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   369
            for host in apply_hosts_directory(options, path, **opts):
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   370
                yield host
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   371
        else:
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   372
            for host in apply_hosts_file(options, path, **opts):
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   373
                yield 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
   374
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   375
def apply (options, args):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   376
    """
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   377
        Load Hosts from arguments.
489
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 460
diff changeset
   378
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 460
diff changeset
   379
        Exits with status=2 if loading the confs fails.
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
   380
    """
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   381
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   382
    if options.hosts_include_trace:
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   383
        log.debug("include trace")
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   384
        include_trace = [ ]
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   385
    else:
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   386
        include_trace = 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
   387
    
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   388
    try:
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   389
        # load hosts from configs
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   390
        hosts = list(apply_hosts_files(options, args, include_trace=include_trace))
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
   391
    except HostConfigObjError as 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
   392
        log.error("%s", 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
   393
        log.error("\t%s", error.line_contents)
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   394
        sys.exit(2)
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   395
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   396
    except HostConfigError as 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
   397
        log.error("%s", 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
   398
        sys.exit(2)
713
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   399
        
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   400
    if options.hosts_include_trace:
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   401
        with pvl.args.apply_file(options.hosts_include_trace, 'w') as file:
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   402
            for include in include_trace:
d5e2d1d9716a pvl.hosts.config: --hosts-include-trace to write out all included files
Tero Marttila <tero.marttila@aalto.fi>
parents: 692
diff changeset
   403
                print >>file, include
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
   404
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   405
    # stable ordering
489
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 460
diff changeset
   406
    return sorted(hosts, key=Host.sort_key)
510
368a568412ed pvl.hosts.config: support direct directory hosts, using the directory name as the parent
Tero Marttila <tero.marttila@aalto.fi>
parents: 507
diff changeset
   407