Exemplo n.º 1
0
def gen_networks(res, nodename, ip, active, mole, defaults, definitions):

  networks = {}

  for netid, settings in definitions.items():

    networks['active'] = active
    networks['mole'] = mole

    if defaults.get(netid,{}).has_key('domain'):

      if not networks.has_key(netid):
        networks[netid] = {}

      netdefinition = udict.merge_recurse(defaults.get(netid), definitions.get(netid))
      netdefinition = udict.merge_recurse(netdefinition, settings)

      networks[netid] = {'host': nodename + '.' + netdefinition.get('domain'),
                         'ip4': str(netdefinition.get('ip4net', '')).format(str(ip))}

      if netdefinition.has_key('mac'):
        networks['dhcp'] = netdefinition.get('mac')

  if len(networks) > 0:
    res[nodename] = networks

  return res
Exemplo n.º 2
0
def container(name):
    config_path = 'containers:' + name
    config = __salt__['pillar.get'](config_path, {})

    defaults = __salt__['pillar.get']('defaults', {})
    host_defaults = defaults.get('hosts', {})
    host_net_defaults = host_defaults.get('network', {})
    container_defaults = defaults.get('containers', {})
    container_net_defaults = container_defaults.get('network', {})

    config['envname'] = "{0}.{1}".format(name, defaults['env'])

    for interface, configuration in config.get('network', {}).iteritems():
        if not configuration.has_key("ip4"):
            configuration['ip4'] = config.get("ip4", "X")

        lxc_net_defaults = udict.merge(host_net_defaults[interface], container_net_defaults.get('common', {}))
        config['network'][interface] = _mklxcnetworkconf(name, configuration, lxc_net_defaults)

    config = udict.merge_recurse(container_defaults.get("common", {}), config)
    config['lxcconf'] = udict.merge_recurse(container_defaults.get("lxcconf", {}), config.get("lxcconf", {}))
    config['roles'] = list(set(container_defaults.get("roles", []) + config.get("roles", [])))

    config['mount'] = udict.merge_recurse(container_defaults.get("mount", {}), config.get("mount", {}))
    for mountid, mount in config.get("mount", {}).iteritems():
        mount['local'] = mount['local'].format(config['envname'])
        mount['remote'] = mount['remote'].format(config['envname']).strip("/")

    config['target'] = "{0}.{1}".format(config['target'], defaults['env'])
    return config
Exemplo n.º 3
0
def _merge_defaults_for_group(defaults, group):
    '''
  Merge top level defaults into a groups defaults. Currently this will merge defaults:network
  into defaults:[group], but may be extended to do more in the future

  :param defaults:
    The full dict of pillar:defaults

  :param group:
    The name of the group as string

  :return:
    The dict for pillar:defaults:[group] with defaults:[xxx] merged into it

  '''
    # what we have in our tummy - the original defaults for the given group
    group_defaults = cpillar.dig(defaults, group, {})

    # the real network defaults
    master_defaults = {"network": cpillar.dig(defaults, "network", {})}

    # Merge but prioritise group values over default values
    return udict.merge_recurse(master_defaults, group_defaults)
Exemplo n.º 4
0
def ext_pillar(minion_id, pillar, *args, **kwargs):
    '''
  Enrich the simple pillar by preparing proper structures for either hosts or containers.
  This is the central place to do all merge and calculation procedures needed to get the
  full configuration information for a node, which is either a host or a container

  :param minion_id:
    The id of the minion that is requesting this pillar

  :param pillar:
    The compiled pillar for the requesting minion - also processes the top file

  :param args:
    Unnamed positional args

  :param kwargs:
    Named args given

  :return:
    A dict witch will be merged with the original pillar, overwriting the existing elements.
    In the case of this external pillar will be

      {'hosts': [hostconfiguration], 'containers': [containerconfiguration] }

  '''

    nodebase_files = cpillar.dig(pillar, 'defaults:nodebase', [])
    nodebase_data = nodebase.data(nodebase_files, __opts__['file_buffer_size'])

    _nodes = {}
    for group in ["hosts", "containers"]:

        _nodebase_groupnodes = dict(nodebase_data.get(group, {}))

        _newgroupnodes = {}
        _groupnodes = cpillar.dig(pillar, group, {})
        _groupdefaults = _merge_defaults_for_group(pillar.get("defaults", {}),
                                                   group)

        _netschema = cpillar.dig(_groupdefaults, 'network:schema', 'None')

        for nid, node_config in _groupnodes.iteritems():

            # merge the nodes config with the one from the nodebase
            config = udict.merge_recurse(_nodebase_groupnodes.get(nid, {}),
                                         node_config)

            # if we use racked schema, add rackid from node name
            if _netschema == 'racked' and 'rackid' not in config:
                config['rackid'] = 'rack{:0>2}'.format(
                    nid.split('-').pop()[1:])

            _node = udict.merge_recurse(_groupdefaults, config)
            _node['network'] = _merge_networks_for_node(
                nid, config, _groupdefaults)

            _node['fqdn'] = cpillar.dig(_node, 'network:manage:fqdn')
            _node['hostname'] = nid
            _node['nature'] = group

            # TODO: remove when using 2015.8+
            # take special care about lists
            for mergelist in ['roles', 'packages']:
                if mergelist in config or mergelist in _groupdefaults:
                    _node[mergelist] = _merge_lists_for_node(
                        mergelist, config, _groupdefaults)

            if 'target' in _node:
                _node['target'] = '{0}.{1}'.format(
                    _node['target'], cpillar.dig(_node,
                                                 'network:manage:domain'))

            if 'mole' not in _node:
                _node['mole'] = "hosts"

            if 'mount' in _node:
                _node['mount'] = _expand_mounts_for_node(
                    _node.get('fqdn'), _node.get("mount", {}))

            _newgroupnodes[nid] = _node

            if _node['fqdn'] == minion_id:
                _nodes['local'] = _node

        _nodes[group] = _newgroupnodes

    return _nodes
Exemplo n.º 5
0
def _merge_networks_for_node(node_id, node, defaults):
    '''
  Merge the defaults for a network into the network definitions for a node

  :param node_id:
    The id of the node that we are currently working on

  :param node:
    The node's network configuration

  :param defaults:
    The group defaults, i.e. merged pillar:defaults:[group]

  :return:
    The actual network configuration dict for the given node

  '''

    network = {}
    default_network = defaults.get('network', {})

    for id, config in node.get('network', {}).iteritems():

        # merge network configuration
        _net = udict.merge_recurse(default_network.get(id, {}), config)

        # merge common items into the network
        _net = udict.merge_recurse(default_network.get('common', {}), _net)

        # always create fqdn per network
        _net['fqdn'] = "{0}.{1}".format(node_id, _net.get('domain', 'local'))

        # Set a gateway
        # if config.has_key('gateway') and default_network.get(id, {}).has_key('gateway'):
        #   gateway = default_network.get(id, {}).get('gateway')
        #   log.debug('Node %s has gateway %s on network %s', node_id, gateway, id)
        #   if gateway is not None:
        #     _net['gateway'] = gateway
        # else:
        #   if _net.has_key('gateway'):
        #     del(_net['gateway'])

        if _net.has_key('nogateway') and _net.has_key('gateway'):
            del (_net['gateway'])

        # autogenerate a mac address if requested
        if _net.get('mac', '') == 'auto':
            _net['mac'] = _get_virt_mac(_net.get('macprefix', '02:aa:00'))

        if _net.get('type', '') == "veth":
            _net['vname'] = _get_veth_name(node_id, _net.get('vpref', 'vet'))

        _net['ip4net'] = _get_node_ip4net(node_id, node, _net)
        _net['cdir'] = _net.get('ip4net').split('/')[1]

        # take network ip4 in favour of node ip4
        _net['ip4'] = _net.get('ip4net').format(
            config.get('ip4', node.get('ip4'))).split('/')[0]

        # cleanup
        for field in ['vpref', 'macprefix']:
            if field in _net:
                del (_net[field])

        network[id] = _net

    return network