<s> import argparse <EOL> import datetime <EOL> import json <EOL> import netaddr <EOL> import os <EOL> import Queue <EOL> import random <EOL> import tarfile <EOL> import uuid <EOL> import yaml <EOL> USED_IPS = [ ] <EOL> INVENTORY_SKEL = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } <EOL> } <EOL> } <EOL> REQUIRED_HOSTVARS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def args ( ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( <EOL> usage = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> epilog = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> required = False , <EOL> default = None <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' <EOL> ) <EOL> return vars ( parser . parse_args ( ) ) <EOL> def get_ip_address ( name , ip_q ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ip_addr = ip_q . get ( timeout = <NUM_LIT:1> ) <EOL> while ip_addr in USED_IPS : <EOL> ip_addr = ip_q . get ( timeout = <NUM_LIT:1> ) <EOL> else : <EOL> append_if ( array = USED_IPS , item = ip_addr ) <EOL> return str ( ip_addr ) <EOL> except AttributeError : <EOL> return None <EOL> except Queue . Empty : <EOL> raise SystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % name <EOL> ) <EOL> def _load_ip_q ( cidr , ip_q ) : <EOL> """<STR_LIT>""" <EOL> _all_ips = [ str ( i ) for i in list ( netaddr . IPNetwork ( cidr ) ) ] <EOL> base_exclude = [ <EOL> str ( netaddr . IPNetwork ( cidr ) . network ) , <EOL> str ( netaddr . IPNetwork ( cidr ) . broadcast ) <EOL> ] <EOL> USED_IPS . extend ( base_exclude ) <EOL> for ip in random . sample ( _all_ips , len ( _all_ips ) ) : <EOL> if ip not in USED_IPS : <EOL> ip_q . put ( ip ) <EOL> def _parse_belongs_to ( key , belongs_to , inventory ) : <EOL> """<STR_LIT>""" <EOL> for item in belongs_to : <EOL> if key not in inventory [ item ] [ '<STR_LIT>' ] : <EOL> append_if ( array = inventory [ item ] [ '<STR_LIT>' ] , item = key ) <EOL> def _build_container_hosts ( container_affinity , container_hosts , type_and_name , <EOL> inventory , host_type , container_type , <EOL> container_host_type , physical_host_type , config , <EOL> properties , assignment ) : <EOL> """<STR_LIT>""" <EOL> container_list = [ ] <EOL> is_metal = False <EOL> if properties : <EOL> is_metal = properties . get ( '<STR_LIT>' , False ) <EOL> for make_container in range ( container_affinity ) : <EOL> for i in container_hosts : <EOL> if '<STR_LIT>' % type_and_name in i : <EOL> append_if ( array = container_list , item = i ) <EOL> existing_count = len ( list ( set ( container_list ) ) ) <EOL> if existing_count < container_affinity : <EOL> hostvars = inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> container_mapping = inventory [ container_type ] [ '<STR_LIT>' ] <EOL> address = None <EOL> if is_metal is False : <EOL> cuuid = '<STR_LIT:%s>' % uuid . uuid4 ( ) <EOL> cuuid = cuuid . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] <EOL> container_host_name = '<STR_LIT>' % ( type_and_name , cuuid ) <EOL> hostvars_options = hostvars [ container_host_name ] = { } <EOL> if container_host_type not in inventory : <EOL> inventory [ container_host_type ] = { <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> append_if ( <EOL> array = inventory [ container_host_type ] [ "<STR_LIT>" ] , <EOL> item = container_host_name <EOL> ) <EOL> append_if ( array = container_hosts , item = container_host_name ) <EOL> else : <EOL> if host_type not in hostvars : <EOL> hostvars [ host_type ] = { } <EOL> hostvars_options = hostvars [ host_type ] <EOL> container_host_name = host_type <EOL> host_type_config = config [ physical_host_type ] [ host_type ] <EOL> address = host_type_config . get ( '<STR_LIT>' ) <EOL> host_type_containers = '<STR_LIT>' % host_type <EOL> append_if ( array = container_mapping , item = host_type_containers ) <EOL> hostvars_options . update ( { <EOL> '<STR_LIT>' : properties , <EOL> '<STR_LIT>' : address , <EOL> '<STR_LIT>' : address , <EOL> '<STR_LIT>' : container_host_name , <EOL> '<STR_LIT>' : host_type , <EOL> '<STR_LIT>' : physical_host_type , <EOL> '<STR_LIT>' : assignment <EOL> } ) <EOL> def _append_container_types ( inventory , host_type ) : <EOL> """<STR_LIT>""" <EOL> for _host in inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] . keys ( ) : <EOL> hdata = inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ _host ] <EOL> if '<STR_LIT>' in hdata : <EOL> if hdata [ '<STR_LIT>' ] . startswith ( host_type ) : <EOL> if '<STR_LIT>' not in hdata : <EOL> hdata [ '<STR_LIT>' ] = host_type <EOL> def _append_to_host_groups ( inventory , container_type , assignment , host_type , <EOL> type_and_name , host_options ) : <EOL> """<STR_LIT>""" <EOL> physical_group_type = '<STR_LIT>' % container_type . split ( '<STR_LIT:_>' ) [ <NUM_LIT:0> ] <EOL> if physical_group_type not in inventory : <EOL> inventory [ physical_group_type ] = { '<STR_LIT>' : [ ] } <EOL> iph = inventory [ physical_group_type ] [ '<STR_LIT>' ] <EOL> iah = inventory [ assignment ] [ '<STR_LIT>' ] <EOL> for hname , hdata in inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> is_metal = False <EOL> properties = hdata . get ( '<STR_LIT>' ) <EOL> if properties : <EOL> is_metal = properties . get ( '<STR_LIT>' , False ) <EOL> if '<STR_LIT>' in hdata or '<STR_LIT>' in hdata : <EOL> if '<STR_LIT>' not in hdata : <EOL> container = hdata [ '<STR_LIT>' ] = hname <EOL> else : <EOL> container = hdata [ '<STR_LIT>' ] <EOL> component = hdata . get ( '<STR_LIT>' ) <EOL> if container . startswith ( host_type ) : <EOL> if '<STR_LIT>' not in hdata : <EOL> hdata [ '<STR_LIT>' ] = host_type <EOL> if container . startswith ( '<STR_LIT>' % type_and_name ) : <EOL> append_if ( array = iah , item = container ) <EOL> elif is_metal is True : <EOL> if component == assignment : <EOL> append_if ( array = iah , item = container ) <EOL> if container . startswith ( '<STR_LIT>' % type_and_name ) : <EOL> append_if ( array = iph , item = container ) <EOL> elif is_metal is True : <EOL> if container . startswith ( host_type ) : <EOL> append_if ( array = iph , item = container ) <EOL> container_vars = host_options . get ( '<STR_LIT>' ) <EOL> if isinstance ( container_vars , dict ) : <EOL> for _keys , _vars in container_vars . items ( ) : <EOL> if isinstance ( _vars , dict ) : <EOL> options = _vars . copy ( ) <EOL> else : <EOL> options = _vars <EOL> limit = None <EOL> if isinstance ( options , ( str , dict , list ) ) : <EOL> if '<STR_LIT>' in options : <EOL> limit = options . pop ( <EOL> '<STR_LIT>' , None <EOL> ) <EOL> if limit is None or ( component and limit in component ) : <EOL> hdata [ _keys ] = options <EOL> def _add_container_hosts ( assignment , config , container_name , container_type , <EOL> inventory , properties ) : <EOL> """<STR_LIT>""" <EOL> physical_host_type = '<STR_LIT>' % container_type . split ( '<STR_LIT:_>' ) [ <NUM_LIT:0> ] <EOL> if physical_host_type not in config : <EOL> return <EOL> for host_type in inventory [ physical_host_type ] [ '<STR_LIT>' ] : <EOL> container_hosts = inventory [ container_name ] [ '<STR_LIT>' ] <EOL> if host_type not in config [ physical_host_type ] : <EOL> continue <EOL> host_options = config [ physical_host_type ] [ host_type ] <EOL> affinity = host_options . get ( '<STR_LIT>' , { } ) <EOL> container_affinity = affinity . get ( container_name , <NUM_LIT:1> ) <EOL> type_and_name = '<STR_LIT>' % ( host_type , container_name ) <EOL> max_hostname_len = <NUM_LIT> <EOL> if len ( type_and_name ) > max_hostname_len : <EOL> raise SystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( host_type , container_name ) <EOL> ) <EOL> physical_host = inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ host_type ] <EOL> container_host_type = '<STR_LIT>' % host_type <EOL> if '<STR_LIT>' not in physical_host : <EOL> physical_host [ '<STR_LIT>' ] = container_host_type <EOL> elif physical_host [ '<STR_LIT>' ] != container_host_type : <EOL> physical_host [ '<STR_LIT>' ] = container_host_type <EOL> _build_container_hosts ( <EOL> container_affinity , <EOL> container_hosts , <EOL> type_and_name , <EOL> inventory , <EOL> host_type , <EOL> container_type , <EOL> container_host_type , <EOL> physical_host_type , <EOL> config , <EOL> properties , <EOL> assignment , <EOL> ) <EOL> _append_container_types ( inventory , host_type ) <EOL> _append_to_host_groups ( <EOL> inventory , <EOL> container_type , <EOL> assignment , <EOL> host_type , <EOL> type_and_name , <EOL> host_options <EOL> ) <EOL> def user_defined_setup ( config , inventory ) : <EOL> """<STR_LIT>""" <EOL> hvs = inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> for key , value in config . iteritems ( ) : <EOL> if key . endswith ( '<STR_LIT>' ) : <EOL> if key not in inventory : <EOL> inventory [ key ] = { '<STR_LIT>' : [ ] } <EOL> if value is None : <EOL> return <EOL> for _key , _value in value . iteritems ( ) : <EOL> if _key not in inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ _key ] = { } <EOL> hvs [ _key ] . update ( { <EOL> '<STR_LIT>' : _value [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _value [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : key <EOL> } ) <EOL> properties = hvs [ _key ] . get ( '<STR_LIT>' ) <EOL> if not properties or not isinstance ( properties , dict ) : <EOL> hvs [ _key ] [ '<STR_LIT>' ] = dict ( ) <EOL> hvs [ _key ] [ '<STR_LIT>' ] . update ( { '<STR_LIT>' : True } ) <EOL> if '<STR_LIT>' in _value : <EOL> for _k , _v in _value [ '<STR_LIT>' ] . items ( ) : <EOL> hvs [ _key ] [ _k ] = _v <EOL> append_if ( array = USED_IPS , item = _value [ '<STR_LIT>' ] ) <EOL> append_if ( array = inventory [ key ] [ '<STR_LIT>' ] , item = _key ) <EOL> def skel_setup ( environment , inventory ) : <EOL> """<STR_LIT>""" <EOL> for key , value in environment . iteritems ( ) : <EOL> if key == '<STR_LIT:version>' : <EOL> continue <EOL> for _key , _value in value . iteritems ( ) : <EOL> if _key not in inventory : <EOL> inventory [ _key ] = { } <EOL> if _key . endswith ( '<STR_LIT>' ) : <EOL> if '<STR_LIT>' not in inventory [ _key ] : <EOL> inventory [ _key ] [ '<STR_LIT>' ] = [ ] <EOL> else : <EOL> if '<STR_LIT>' not in inventory [ _key ] : <EOL> inventory [ _key ] [ '<STR_LIT>' ] = [ ] <EOL> if '<STR_LIT>' not in inventory [ _key ] : <EOL> inventory [ _key ] [ '<STR_LIT>' ] = [ ] <EOL> if '<STR_LIT>' in _value : <EOL> for assignment in _value [ '<STR_LIT>' ] : <EOL> if assignment not in inventory : <EOL> inventory [ assignment ] = { } <EOL> if '<STR_LIT>' not in inventory [ assignment ] : <EOL> inventory [ assignment ] [ '<STR_LIT>' ] = [ ] <EOL> if '<STR_LIT>' not in inventory [ assignment ] : <EOL> inventory [ assignment ] [ '<STR_LIT>' ] = [ ] <EOL> def skel_load ( skeleton , inventory ) : <EOL> """<STR_LIT>""" <EOL> for key , value in skeleton . iteritems ( ) : <EOL> _parse_belongs_to ( <EOL> key , <EOL> belongs_to = value [ '<STR_LIT>' ] , <EOL> inventory = inventory <EOL> ) <EOL> def _load_optional_q ( config , cidr_name ) : <EOL> """<STR_LIT>""" <EOL> cidr = config . get ( cidr_name ) <EOL> ip_q = None <EOL> if cidr is not None : <EOL> ip_q = Queue . Queue ( ) <EOL> _load_ip_q ( cidr = cidr , ip_q = ip_q ) <EOL> return ip_q <EOL> def _add_additional_networks ( key , inventory , ip_q , q_name , netmask , interface , <EOL> bridge , net_type , net_mtu , user_config , <EOL> is_ssh_address , is_container_address , <EOL> static_routes ) : <EOL> """<STR_LIT>""" <EOL> def network_entry ( ) : <EOL> """<STR_LIT>""" <EOL> if is_metal : <EOL> _network = dict ( ) <EOL> else : <EOL> _network = { '<STR_LIT>' : interface } <EOL> if bridge : <EOL> _network [ '<STR_LIT>' ] = bridge <EOL> if net_type : <EOL> _network [ '<STR_LIT:type>' ] = net_type <EOL> if net_mtu : <EOL> _network [ '<STR_LIT>' ] = net_mtu <EOL> return _network <EOL> def return_netmask ( ) : <EOL> """<STR_LIT>""" <EOL> _old_netmask = container . get ( old_netmask ) <EOL> if _old_netmask : <EOL> return container . pop ( old_netmask ) <EOL> elif netmask : <EOL> return netmask <EOL> base_hosts = inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> lookup = inventory . get ( key , list ( ) ) <EOL> if '<STR_LIT>' in lookup and lookup [ '<STR_LIT>' ] : <EOL> for group in lookup [ '<STR_LIT>' ] : <EOL> _add_additional_networks ( <EOL> group , <EOL> inventory , <EOL> ip_q , <EOL> q_name , <EOL> netmask , <EOL> interface , <EOL> bridge , <EOL> net_type , <EOL> net_mtu , <EOL> user_config , <EOL> is_ssh_address , <EOL> is_container_address , <EOL> static_routes <EOL> ) <EOL> if lookup : <EOL> hosts = lookup . get ( '<STR_LIT>' ) <EOL> if not hosts : <EOL> return <EOL> else : <EOL> return <EOL> if q_name : <EOL> old_address = '<STR_LIT>' % q_name <EOL> else : <EOL> old_address = '<STR_LIT>' % interface <EOL> old_netmask = '<STR_LIT>' % q_name <EOL> for container_host in hosts : <EOL> container = base_hosts [ container_host ] <EOL> container . pop ( '<STR_LIT>' , None ) <EOL> if '<STR_LIT>' in container : <EOL> networks = container [ '<STR_LIT>' ] <EOL> else : <EOL> networks = container [ '<STR_LIT>' ] = dict ( ) <EOL> is_metal = False <EOL> properties = container . get ( '<STR_LIT>' ) <EOL> if properties : <EOL> is_metal = properties . get ( '<STR_LIT>' , False ) <EOL> if not is_metal and old_address not in networks : <EOL> network = networks [ old_address ] = network_entry ( ) <EOL> if old_address in container and container [ old_address ] : <EOL> network [ '<STR_LIT:address>' ] = container . pop ( old_address ) <EOL> elif not is_metal : <EOL> address = get_ip_address ( name = q_name , ip_q = ip_q ) <EOL> if address : <EOL> network [ '<STR_LIT:address>' ] = address <EOL> network [ '<STR_LIT>' ] = return_netmask ( ) <EOL> elif is_metal : <EOL> network = networks [ old_address ] = network_entry ( ) <EOL> network [ '<STR_LIT>' ] = return_netmask ( ) <EOL> if old_address in container and container [ old_address ] : <EOL> network [ '<STR_LIT:address>' ] = container . pop ( old_address ) <EOL> else : <EOL> if is_ssh_address or is_container_address : <EOL> cphg = container . get ( '<STR_LIT>' ) <EOL> phg = user_config [ cphg ] [ container_host ] <EOL> network [ '<STR_LIT:address>' ] = phg [ '<STR_LIT>' ] <EOL> if is_ssh_address is True : <EOL> container [ '<STR_LIT>' ] = networks [ old_address ] [ '<STR_LIT:address>' ] <EOL> if is_container_address is True : <EOL> container [ '<STR_LIT>' ] = networks [ old_address ] [ '<STR_LIT:address>' ] <EOL> if static_routes : <EOL> networks [ old_address ] [ '<STR_LIT>' ] = [ ] <EOL> for s in static_routes : <EOL> if '<STR_LIT>' in s and '<STR_LIT>' in s : <EOL> networks [ old_address ] [ '<STR_LIT>' ] . append ( s ) <EOL> def _net_address_search ( provider_networks , main_network , key ) : <EOL> """<STR_LIT>""" <EOL> for pn in provider_networks : <EOL> p_net = pn . get ( '<STR_LIT>' ) <EOL> if p_net : <EOL> if p_net . get ( key ) : <EOL> break <EOL> else : <EOL> for pn in provider_networks : <EOL> p_net = pn . get ( '<STR_LIT>' ) <EOL> if p_net : <EOL> if p_net . get ( '<STR_LIT>' ) == main_network : <EOL> p_net [ key ] = True <EOL> return provider_networks <EOL> def container_skel_load ( container_skel , inventory , config ) : <EOL> """<STR_LIT>""" <EOL> for key , value in container_skel . iteritems ( ) : <EOL> for assignment in value [ '<STR_LIT>' ] : <EOL> for container_type in value [ '<STR_LIT>' ] : <EOL> _add_container_hosts ( <EOL> assignment , <EOL> config , <EOL> key , <EOL> container_type , <EOL> inventory , <EOL> value . get ( '<STR_LIT>' ) <EOL> ) <EOL> else : <EOL> cidr_networks = config . get ( '<STR_LIT>' ) <EOL> provider_queues = { } <EOL> for net_name in cidr_networks : <EOL> ip_q = _load_optional_q ( <EOL> cidr_networks , cidr_name = net_name <EOL> ) <EOL> provider_queues [ net_name ] = ip_q <EOL> if ip_q is not None : <EOL> net = netaddr . IPNetwork ( cidr_networks . get ( net_name ) ) <EOL> provider_queues [ '<STR_LIT>' % net_name ] = str ( net . netmask ) <EOL> overrides = config [ '<STR_LIT>' ] <EOL> pns = overrides . get ( '<STR_LIT>' , list ( ) ) <EOL> pns = _net_address_search ( <EOL> provider_networks = pns , <EOL> main_network = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> key = '<STR_LIT>' <EOL> ) <EOL> pns = _net_address_search ( <EOL> provider_networks = pns , <EOL> main_network = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> key = '<STR_LIT>' <EOL> ) <EOL> for pn in pns : <EOL> p_net = pn . get ( '<STR_LIT>' ) <EOL> if not p_net : <EOL> continue <EOL> q_name = p_net . get ( '<STR_LIT>' ) <EOL> ip_from_q = provider_queues . get ( q_name ) <EOL> if ip_from_q : <EOL> netmask = provider_queues [ '<STR_LIT>' % q_name ] <EOL> else : <EOL> netmask = None <EOL> for group in p_net . get ( '<STR_LIT>' , list ( ) ) : <EOL> _add_additional_networks ( <EOL> key = group , <EOL> inventory = inventory , <EOL> ip_q = ip_from_q , <EOL> q_name = q_name , <EOL> netmask = netmask , <EOL> interface = p_net [ '<STR_LIT>' ] , <EOL> bridge = p_net [ '<STR_LIT>' ] , <EOL> net_type = p_net . get ( '<STR_LIT>' ) , <EOL> net_mtu = p_net . get ( '<STR_LIT>' ) , <EOL> user_config = config , <EOL> is_ssh_address = p_net . get ( '<STR_LIT>' ) , <EOL> is_container_address = p_net . get ( '<STR_LIT>' ) , <EOL> static_routes = p_net . get ( '<STR_LIT>' ) <EOL> ) <EOL> def find_config_path ( user_config_path = None ) : <EOL> """<STR_LIT>""" <EOL> path_check = [ <EOL> os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> if user_config_path is not None : <EOL> path_check . insert ( <NUM_LIT:0> , os . path . expanduser ( user_config_path ) ) <EOL> for f in path_check : <EOL> if os . path . isdir ( f ) : <EOL> return f <EOL> else : <EOL> raise SystemExit ( '<STR_LIT>' % path_check ) <EOL> def _set_used_ips ( user_defined_config , inventory ) : <EOL> """<STR_LIT>""" <EOL> used_ips = user_defined_config . get ( '<STR_LIT>' ) <EOL> if isinstance ( used_ips , list ) : <EOL> for ip in used_ips : <EOL> split_ip = ip . split ( '<STR_LIT:U+002C>' ) <EOL> if len ( split_ip ) >= <NUM_LIT:2> : <EOL> ip_range = list ( <EOL> netaddr . iter_iprange ( <EOL> split_ip [ <NUM_LIT:0> ] , <EOL> split_ip [ - <NUM_LIT:1> ] <EOL> ) <EOL> ) <EOL> USED_IPS . extend ( [ str ( i ) for i in ip_range ] ) <EOL> else : <EOL> append_if ( array = USED_IPS , item = split_ip [ <NUM_LIT:0> ] ) <EOL> for host_entry in inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] . values ( ) : <EOL> networks = host_entry . get ( '<STR_LIT>' , dict ( ) ) <EOL> for network_entry in networks . values ( ) : <EOL> address = network_entry . get ( '<STR_LIT:address>' ) <EOL> if address : <EOL> append_if ( array = USED_IPS , item = address ) <EOL> def _ensure_inventory_uptodate ( inventory , container_skel ) : <EOL> """<STR_LIT>""" <EOL> for key , value in inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if '<STR_LIT>' not in value : <EOL> value [ '<STR_LIT>' ] = key <EOL> for rh in REQUIRED_HOSTVARS : <EOL> if rh not in value : <EOL> value [ rh ] = None <EOL> if rh == '<STR_LIT>' : <EOL> value [ rh ] = { } <EOL> for key , value in container_skel . iteritems ( ) : <EOL> item = inventory . get ( key ) <EOL> hosts = item . get ( '<STR_LIT>' ) <EOL> if hosts : <EOL> for host in hosts : <EOL> container = inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ host ] <EOL> if '<STR_LIT>' in value : <EOL> container [ '<STR_LIT>' ] = value [ '<STR_LIT>' ] <EOL> def _parse_global_variables ( user_cidr , inventory , user_defined_config ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:all>' not in inventory : <EOL> inventory [ '<STR_LIT:all>' ] = { } <EOL> if '<STR_LIT>' not in inventory [ '<STR_LIT:all>' ] : <EOL> inventory [ '<STR_LIT:all>' ] [ '<STR_LIT>' ] = { } <EOL> inventory [ '<STR_LIT:all>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = user_cidr <EOL> if '<STR_LIT>' in user_defined_config : <EOL> if isinstance ( user_defined_config [ '<STR_LIT>' ] , dict ) : <EOL> inventory [ '<STR_LIT:all>' ] [ '<STR_LIT>' ] . update ( <EOL> user_defined_config [ '<STR_LIT>' ] <EOL> ) <EOL> def append_if ( array , item ) : <EOL> """<STR_LIT>""" <EOL> if item not in array : <EOL> array . append ( item ) <EOL> return array <EOL> def _merge_dict ( base_items , new_items ) : <EOL> """<STR_LIT>""" <EOL> for key , value in new_items . iteritems ( ) : <EOL> if isinstance ( value , dict ) : <EOL> base_merge = _merge_dict ( base_items . get ( key , { } ) , value ) <EOL> base_items [ key ] = base_merge <EOL> else : <EOL> base_items [ key ] = new_items [ key ] <EOL> return base_items <EOL> def _extra_config ( user_defined_config , base_dir ) : <EOL> """<STR_LIT>""" <EOL> for root_dir , _ , files in os . walk ( base_dir ) : <EOL> for name in files : <EOL> if name . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> with open ( os . path . join ( root_dir , name ) , '<STR_LIT:rb>' ) as f : <EOL> _merge_dict ( <EOL> user_defined_config , <EOL> yaml . safe_load ( f . read ( ) ) or { } <EOL> ) <EOL> def _check_config_settings ( cidr_networks , config , container_skel ) : <EOL> """<STR_LIT>""" <EOL> is_provider_networks_needed = False <EOL> for key , value in container_skel . iteritems ( ) : <EOL> properties = value . get ( '<STR_LIT>' ) <EOL> is_metal = properties . get ( '<STR_LIT>' , False ) <EOL> if not is_metal : <EOL> is_provider_networks_needed = True <EOL> break <EOL> if is_provider_networks_needed : <EOL> if ( '<STR_LIT>' not in config ) : <EOL> raise SystemExit ( <EOL> "<STR_LIT>" <EOL> ) <EOL> elif ( '<STR_LIT>' not in config [ '<STR_LIT>' ] ) : <EOL> raise SystemExit ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> overrides = config [ '<STR_LIT>' ] <EOL> pns = overrides . get ( '<STR_LIT>' , list ( ) ) <EOL> for pn in pns : <EOL> p_net = pn . get ( '<STR_LIT>' ) <EOL> if not p_net : <EOL> continue <EOL> q_name = p_net . get ( '<STR_LIT>' ) <EOL> if q_name and q_name not in cidr_networks : <EOL> raise SystemExit ( <EOL> "<STR_LIT>" + q_name + "<STR_LIT>" <EOL> ) <EOL> def load_environment ( config_path ) : <EOL> """<STR_LIT>""" <EOL> environment = dict ( ) <EOL> env_plugins = os . path . join ( config_path , '<STR_LIT>' ) <EOL> if os . path . isdir ( env_plugins ) : <EOL> _extra_config ( user_defined_config = environment , base_dir = env_plugins ) <EOL> return environment <EOL> def load_user_configuration ( config_path ) : <EOL> """<STR_LIT>""" <EOL> user_defined_config = dict ( ) <EOL> user_config_file = os . path . join ( config_path , '<STR_LIT>' ) <EOL> if os . path . isfile ( user_config_file ) : <EOL> with open ( user_config_file , '<STR_LIT:rb>' ) as f : <EOL> user_defined_config . update ( yaml . safe_load ( f . read ( ) ) or { } ) <EOL> base_dir = os . path . join ( config_path , '<STR_LIT>' ) <EOL> if os . path . isdir ( base_dir ) : <EOL> _extra_config ( user_defined_config , base_dir ) <EOL> if not user_defined_config : <EOL> raise SystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( config_path , config_path ) <EOL> ) <EOL> return user_defined_config <EOL> def main ( all_args ) : <EOL> """<STR_LIT>""" <EOL> config_path = find_config_path ( <EOL> user_config_path = all_args . get ( '<STR_LIT>' ) <EOL> ) <EOL> user_defined_config = load_user_configuration ( config_path ) <EOL> environment = load_environment ( config_path ) <EOL> dynamic_inventory_file = os . path . join ( <EOL> config_path , '<STR_LIT>' <EOL> ) <EOL> if os . path . isfile ( dynamic_inventory_file ) : <EOL> with open ( dynamic_inventory_file , '<STR_LIT:rb>' ) as f : <EOL> dynamic_inventory = json . loads ( f . read ( ) ) <EOL> inventory_backup_file = os . path . join ( <EOL> config_path , <EOL> '<STR_LIT>' <EOL> ) <EOL> with tarfile . open ( inventory_backup_file , '<STR_LIT:a>' ) as tar : <EOL> basename = os . path . basename ( dynamic_inventory_file ) <EOL> utctime = datetime . datetime . utcnow ( ) <EOL> utctime = utctime . strftime ( "<STR_LIT>" ) <EOL> backup_name = '<STR_LIT>' % ( basename , utctime ) <EOL> tar . add ( dynamic_inventory_file , arcname = backup_name ) <EOL> else : <EOL> dynamic_inventory = INVENTORY_SKEL <EOL> cidr_networks = user_defined_config . get ( '<STR_LIT>' ) <EOL> if not cidr_networks : <EOL> raise SystemExit ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in cidr_networks : <EOL> user_cidr = cidr_networks [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in cidr_networks : <EOL> user_cidr = cidr_networks [ '<STR_LIT>' ] <EOL> else : <EOL> raise SystemExit ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _check_config_settings ( <EOL> cidr_networks , <EOL> user_defined_config , <EOL> environment . get ( '<STR_LIT>' ) <EOL> ) <EOL> _parse_global_variables ( user_cidr , dynamic_inventory , user_defined_config ) <EOL> _set_used_ips ( user_defined_config , dynamic_inventory ) <EOL> user_defined_setup ( user_defined_config , dynamic_inventory ) <EOL> skel_setup ( environment , dynamic_inventory ) <EOL> skel_load ( <EOL> environment . get ( '<STR_LIT>' ) , <EOL> dynamic_inventory <EOL> ) <EOL> skel_load ( <EOL> environment . get ( '<STR_LIT>' ) , <EOL> dynamic_inventory <EOL> ) <EOL> container_skel_load ( <EOL> environment . get ( '<STR_LIT>' ) , <EOL> dynamic_inventory , <EOL> user_defined_config <EOL> ) <EOL> _ensure_inventory_uptodate ( <EOL> inventory = dynamic_inventory , <EOL> container_skel = environment . get ( '<STR_LIT>' ) , <EOL> ) <EOL> dynamic_inventory_json = json . dumps ( <EOL> dynamic_inventory , <EOL> indent = <NUM_LIT:4> , <EOL> sort_keys = True <EOL> ) <EOL> hostnames_ips = { } <EOL> for _host , _vars in dynamic_inventory [ '<STR_LIT>' ] [ '<STR_LIT>' ] . iteritems ( ) : <EOL> host_hash = hostnames_ips [ _host ] = { } <EOL> for _key , _value in _vars . iteritems ( ) : <EOL> if _key . endswith ( '<STR_LIT:address>' ) or _key == '<STR_LIT>' : <EOL> host_hash [ _key ] = _value <EOL> hostnames_ip_file = os . path . join ( <EOL> config_path , '<STR_LIT>' ) <EOL> with open ( hostnames_ip_file , '<STR_LIT:wb>' ) as f : <EOL> f . write ( <EOL> json . dumps ( <EOL> hostnames_ips , <EOL> indent = <NUM_LIT:4> , <EOL> sort_keys = True <EOL> ) <EOL> ) <EOL> with open ( dynamic_inventory_file , '<STR_LIT:wb>' ) as f : <EOL> f . write ( dynamic_inventory_json ) <EOL> return dynamic_inventory_json <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> all_args = args ( ) <EOL> output = main ( all_args ) <EOL> print ( output ) </s>
