Example #1
0
def resolve_hosts_and_roles(environment, hosts=None, roles=None):
    """
    Given an environment, (possibly empty) list of hosts, and a (possibly empty)
    list of roles, return a mapping from host to roles to target.

    Raises an exception if any targeted host would have no roles after resolution.
    """
    # Validate environment
    if not environment:
        raise Exception("Environment was not specified")

    # Determine hosts for the environment
    environment_hosts = get_hosts_for_environment(environment)
    if environment_hosts is None:
        raise Exception("Environment '{}' is not recognized.".format(environment))
    elif not environment_hosts:
        warn("Environment '{}' does not have any hosts configured.".format(environment))

    # Use all environment hosts if none are specified
    hosts = hosts or environment_hosts

    # Forbid any hosts not in environment
    for host in hosts:
        if host not in environment_hosts:
            raise Exception("Host '{host}' is not a member of the environment '{env}'"
                            .format(host=host, env=environment))

    # Determine roles for hosts
    if roles:
        # If roles were specified, restrict mapping to those roles
        valid_roles = lambda role: role in roles
        hosts_to_roles = {}
        for host in hosts:
            host_roles = filter(valid_roles, get_roles_for_host(host))
            if host_roles:
                hosts_to_roles[host] = host_roles
    else:
        # Otherwise, use all roles
        hosts_to_roles = dict([(host, get_roles_for_host(host)) for host in hosts])

    # Validate that all hosts have at least one role
    for host, host_roles in hosts_to_roles.iteritems():
        if not host_roles:
            if roles:
                raise Exception("Host '{}' does not have any of the specified roles".format(host))
            else:
                raise Exception("Host '{}' does not have any configured roles".format(host))

    return hosts_to_roles
Example #2
0
File: main.py Project: sginn/confab
def resolve_model(parser, options):
    """
    Ensure that a valid environment, host, and model have been defined.

    If no hosts are defined, attempt to derive these from the current environment.
    If hosts are defined, validate that these are members of the current environment.

    If no roles are defined, attempt to derive these from the current hosts.
    If roles are defined, validate that hosts have these roles.
    """
    environment_hosts = get_hosts_for_environment(options.environment)

    # Validate hosts
    if not options.hosts:
        # Try using all hosts for the environment

        if not environment_hosts:
            parser.error('Unrecognized or missing environment definition: {environment}'.
                         format(environment=options.environment))

        elif not has_same_roles(environment_hosts):
            # Cannot use all hosts for the enviroment; roles don't match
            parser.error('All hosts for environment do not have the same role; ' +
                         'please specify a host list.')

        options.hosts = environment_hosts
    else:
        options.hosts = options.hosts.split(',')

        # Ensure that all specified hosts are part of this environment
        if not set(options.hosts).issubset(environment_hosts):
            parser.error('All hosts are not part of environment: {environment}'.
                         format(environment=options.environment))

    # Validate roles
    if not options.roles:
        # Try using all roles for hosts

        hosts_roles = has_same_roles(options.hosts)
        if not hosts_roles:
            parser.error('All hosts do not have the same role; please specify a role list.')

        options.roles = hosts_roles
    else:
        options.roles = options.roles.split(',')

        if not has_roles(options.hosts, options.roles):
            # Ensure that specified roles are valid
            parser.error('All hosts do not have all specified roles; please specify different roles.')
Example #3
0
    def test_get_hosts_for_environment(self):
        """
        Test dictionary lookup within environmentdefs.
        """

        # no environmentdefs
        with self.assertRaises(Exception):
            get_hosts_for_environment('foo')

        with settings(environmentdefs={'foo': ['bar', 'baz'],
                                       'bar': ['foo']}):

            ok_('bar' in get_hosts_for_environment('foo'))
            ok_('baz' in get_hosts_for_environment('foo'))
            ok_('foo' in get_hosts_for_environment('bar'))
            ok_('baz' not in get_hosts_for_environment('bar'))

            # no environmentdef
            with self.assertRaises(Exception):
                get_hosts_for_environment('baz')
Example #4
0
    def test_get_hosts_for_environment(self):
        """
        Test dictionary lookup within environmentdefs.
        """

        with settings(environmentdefs={'foo':['bar','baz'],
                                       'bar':['foo']}):

            self.assertTrue('bar' in get_hosts_for_environment('foo'))
            self.assertTrue('baz' in get_hosts_for_environment('foo'))
            self.assertTrue('foo' in get_hosts_for_environment('bar'))
            self.assertFalse('baz' in get_hosts_for_environment('bar'))
            self.assertFalse('foo' in get_hosts_for_environment('baz'))
            self.assertFalse('bar' in get_hosts_for_environment('baz'))