示例#1
0
    def __init__(self):
        ''' Create an inventory based on the catalog of nodes and services
        registered in a consul cluster'''
        self.node_metadata = {}
        self.nodes = {}
        self.nodes_by_service = {}
        self.nodes_by_tag = {}
        self.nodes_by_datacenter = {}
        self.nodes_by_kv = {}
        self.nodes_by_availability = {}
        self.current_dc = None
        self.inmemory_kv = []
        self.inmemory_nodes = []

        config = ConsulConfig()
        self.config = config

        self.consul_api = config.get_consul_api()

        if config.has_config('datacenter'):
            if config.has_config('host'):
                self.load_data_for_node(config.host, config.datacenter)
            else:
                self.load_data_for_datacenter(config.datacenter)
        else:
            self.load_all_data_consul()

        self.combine_all_results()
        print(json.dumps(self.inventory, sort_keys=True, indent=2))
示例#2
0
    def __init__(self):
        ''' Create an inventory based on the catalog of nodes and services
        registered in a consul cluster'''
        self.node_metadata = {}
        self.nodes = {}
        self.nodes_by_service = {}
        self.nodes_by_tag = {}
        self.nodes_by_datacenter = {}
        self.nodes_by_kv = {}
        self.nodes_by_availability = {}
        self.current_dc = None
        self.inmemory_kv = []
        self.inmemory_nodes = []

        config = ConsulConfig()
        self.config = config

        self.consul_api = config.get_consul_api()

        if config.has_config('datacenter'):
            if config.has_config('host'):
                self.load_data_for_node(config.host, config.datacenter)
            else:
                self.load_data_for_datacenter(config.datacenter)
        else:
            self.load_all_data_consul()

        self.combine_all_results()
        print(json.dumps(self.inventory, sort_keys=True, indent=2))
    def __init__(self):
        ''' Create an inventory based on the catalog of nodes and services
        registered in a consul cluster'''
        self.node_metadata = {}
        self.nodes = {}
        self.nodes_by_service = {}
        self.nodes_by_tag = {}
        self.nodes_by_datacenter = {}
        self.nodes_by_kv = {}
        self.nodes_by_availability = {}
        self.current_dc = None

        config = ConsulConfig()
        self.config = config

        self.consul_api = config.get_consul_api()

        if config.has_config('datacenter'):
            if config.has_config('host'):
                self.load_data_for_node(config.host, config.datacenter)
            else:
                self.load_data_for_datacenter(config.datacenter)
        else:
            self.load_all_data_consul()

        self.combine_all_results()

        #
        filtered_list = []
        filtered_groups = []

        # Filters
        if self.config.instance_filters is not None:
            for _filter in self.config.instance_filters.split(','):
                for inventory_keys in self.inventory:
                    if inventory_keys in ['all', '_meta']:
                        continue
                    if re.search(_filter, inventory_keys, re.I):
                        if inventory_keys not in filtered_groups:
                            filtered_groups.append(inventory_keys)
                        for _host in self.inventory[inventory_keys]:
                            if _host not in filtered_list:
                                filtered_list.append(_host)

        if self.config.instance_filters is not None:
            for inventory_keys in self.inventory.keys():
                if (inventory_keys
                        not in filtered_groups) and (inventory_keys
                                                     not in ['all', '_meta']):
                    self.inventory.pop(inventory_keys)
                else:
                    if inventory_keys == '_meta':
                        for _meta_key in self.inventory['_meta'][
                                'hostvars'].keys():
                            if _meta_key not in filtered_list:
                                self.inventory['_meta']['hostvars'].pop(
                                    _meta_key)
                    else:
                        self.inventory[inventory_keys] = list(
                            set(self.inventory[inventory_keys]).intersection(
                                filtered_list))

        print(json.dumps(self.inventory, sort_keys=True, indent=2))