Esempio n. 1
0
def main():
    global debugmode
    args = parser.parse_args()
    if args.debug:
        logger.setLevel(logging.DEBUG)
        debugmode = True
    if args.quiet:
        logger.setLevel(logging.ERROR)
        debugmode = False
    onlynodes = []
    if args.onlynode:
        onlynodes = args.onlynode

    config = get_config(args.config)

    pupversion = None
    if not args.nodefile:
        puppet = PuppetWrapper(
            host=config['puppet_server']['host'],
            user=config['puppet_server']['user'],
            environment=config['puppet_server'].get('environment'),
            version=config['puppet_server'].get('version'),
            ca_file=config['puppet_server'].get('ca_file'),
            cert_file=config['puppet_server'].get('cert_file'),
            key_file=config['puppet_server'].get('key_file'),
            logger=logger,
            onlynodes=onlynodes,
        )
        puppetnodes = puppet.get_nodes()
        logger.debug("Got %s nodes from puppet (v%s)" %
                     (len(puppetnodes), puppet.version))
        pupversion = puppet.version
    else:
        with open(args.nodefile, 'r') as nf:
            allpuppetnodes = json.loads(nf.read())
        if isinstance(allpuppetnodes, dict):
            allpuppetnodes = [allpuppetnodes]
        pupversion = 0
        puppetnodes = allpuppetnodes
        if onlynodes:
            puppetnodes = []
            for node in allpuppetnodes:
                if not (node.get('hostname') in onlynodes
                        or node.get('ipaddress') in onlynodes
                        or node.get('fqdn') in onlynodes
                        or node.get('uuid') in onlynodes):
                    continue
                puppetnodes.append(node)
        if puppetnodes:
            pupversion = puppetnodes[0]['clientversion']
        logger.debug("Got %s nodes from file (v%s)" %
                     (len(puppetnodes), pupversion))

    if args.savenodes:
        with open(args.savenodes, 'w') as wnf:
            wnf.write(
                json.dumps(puppetnodes,
                           cls=JSONEncoder,
                           indent=4,
                           sort_keys=True,
                           ensure_ascii=False))

    dev42 = device42.Device42(endpoint=config['device42']['host'],
                              user=config['device42']['user'],
                              password=config['device42']['pass'],
                              logger=logger,
                              debug=debugmode)
    d42_update(dev42,
               puppetnodes,
               config['options'],
               config.get('static', {}),
               config.get('mapping', {}),
               from_version=pupversion,
               puppethost=config['puppet_server']['host'])

    return 0
def main():
    global debugmode
    args = parser.parse_args()
    if args.debug:
        logger.setLevel(logging.DEBUG)
        debugmode = True
    if args.quiet:
        logger.setLevel(logging.ERROR)
        debugmode = False
    onlynodes = []
    if args.onlynode:
        onlynodes = args.onlynode

    config = get_config(args.config)
    host = config['foreman']['host']
    user = config['foreman']['user']
    password = config['foreman']['pass']

    f = Foreman('https://%s' % host, (user, password), verify=False, api_version=1)
    node_ids = []
    for node in f.hosts.index(per_page=100000):
        node = node['host']
        if len(onlynodes) > 0 and node['name'] in onlynodes:
            node_ids.append(node['id'])
        elif len(onlynodes) == 0:
            node_ids.append(node['id'])

    facts_query = 'fqdn or memorysize_mb or is_virtual or processorcount or processors::models or serialnumber'

    nodes = []
    for node_id in node_ids:

        host = f.hosts.show(id=node_id)['host']

        if len(onlynodes) > 0 and host['name'] not in onlynodes:
            continue

        host['model'] = f.models.show(id=host['model_id'])
        host['os'] = f.operatingsystems.show(id=host['operatingsystem_id'])
        facts = f.do_get('/api/hosts/%s/facts?search=%s&per_page=999' % (node_id, facts_query), {})
        facts = facts[host['name']] if host['name'] in facts else {}
        disks = f.do_get('/api/hosts/%s/facts?search=disks&per_page=999' % node_id, {})
        disks = disks[host['name']] if host['name'] in disks else {}
        ec2_metadata = f.do_get('/api/hosts/%s/facts?search=ec2_metadata&per_page=999' % node_id, {})
        ec2_metadata = ec2_metadata[host['name']] if host['name'] in ec2_metadata else {}
        networking = f.do_get('/api/hosts/%s/facts?search=networking&per_page=999' % node_id, {})
        networking = networking[host['name']] if host['name'] in networking else {}

        if 'networking::interfaces' in networking:
            networking2 = {}
            for x in networking:
                value = networking[x]
                if value:
                    value = value.replace('"=>', '":')
                    chain = x.split('::')

                    last = False
                    el = 0
                    obj = networking2
                    while not last:
                        key = chain[el:el+1][0]
                        el += 1
                        if key not in obj:
                            obj[key] = {}
                        obj = obj[key]
                        if len(chain) == el:
                            last = True
                            obj[key] = value

            def clean_dict(obj):
                for x in obj:
                    try:
                        obj[x] = obj[x][x]
                    except:
                        pass
                    try:
                        if type(obj[x]) == dict:
                            obj[x] = clean_dict(obj[x])
                    except:
                        pass

                return obj

            networking = {
                'networking': json.dumps(clean_dict(networking2['networking']))
            }

        if facts == {}:
            continue

        formatted_disks = {}
        for key in disks:
            splitted = key.split('::')
            if len(splitted) == 3:
                if splitted[2] == 'size_bytes':
                    formatted_disks[splitted[1]] = {
                        'size_bytes': disks[key]
                    }

        # Check to see that we have all data, or set it to '' if not
        if facts.has_key('is_virtual'):
            _is_virtual = facts['is_virtual']
        else:
            _is_virtual = False
        if facts.has_key('serialnumber'):
            _serialnumber = facts['serialnumber']
        else:
            _serialnumber = ''
        if facts.has_key('processors::models'):
                _processors_models = ast.literal_eval(facts['processors::models'])
        else:
            _processors_models = ['']
            # prepare correct format

        data = {
            'hostname': host['name'],
            'memorysize_mb': facts['memorysize_mb'],
            'fqdn': facts['fqdn'],
            'disks': formatted_disks,
            'is_virtual': _is_virtual,
            'serial_no': _serialnumber,
            'physicalprocessorcount': facts['physicalprocessorcount'],
            'processorcount': facts['processorcount'],
            'processors': {
                'models': _processors_models
            },
            'operatingsystem': host['os']['operatingsystem']['name'],
            'operatingsystemrelease': host['os']['operatingsystem']['release_name'],
            'macaddress': host['mac'],
            'networking': json.loads(networking['networking'].replace('"=>', '":')) if 'networking' in networking else ''
        }
        if len(ec2_metadata) > 0:
            data.update({'ec2_metadata': ec2_metadata})

        nodes.append(data)

    if args.savenodes:
        with open(args.savenodes, 'w') as wnf:
            wnf.write(json.dumps(nodes, cls=JSONEncoder, indent=4, sort_keys=True, ensure_ascii=False))

    dev42 = device42.Device42(
        endpoint=config['device42']['host'],
        user=config['device42']['user'],
        password=config['device42']['pass'],
        logger=logger,
        debug=debugmode
    )

    d42_update(dev42, nodes, config['options'], config.get('static', {}), config.get('mapping', {}),
               from_version='4', puppethost=config['foreman']['host'])

    return 0
def main():
    global debug_mode
    args = parser.parse_args()
    if args.debug:
        logger.setLevel(logging.DEBUG)
        debug_mode = True
    if args.quiet:
        logger.setLevel(logging.ERROR)
        debug_mode = False

    config = get_config(args.config)
    local = salt.client.LocalClient()

    if not args.nodefile:
        if args.onlynode:
            salt_nodes = local.cmd(args.onlynode[0],
                                   'grains.items',
                                   expr_form='list')
        else:
            salt_nodes = local.cmd('*', 'grains.items')
    else:
        with open(args.nodefile, 'r') as nf:
            all_nodes = json.loads(nf.read())
        if isinstance(all_nodes, dict):
            all_nodes = [all_nodes]
        salt_nodes = all_nodes[0]
        if args.onlynode:
            salt_nodes = {}
            for key, node in all_nodes[0].items():
                if node.get('nodename') in args.onlynode[0] or node.get(
                        'fqdn') in args.onlynode[0]:
                    salt_nodes[key] = node
        logger.debug("Got %s nodes from file" % len(salt_nodes))

    for node in salt_nodes:
        try:
            if not node:
                continue
            if type(salt_nodes[node]) != dict:
                continue
            salt_nodes[node]['disks'] = local.cmd(node, 'disk.blkid')
            salt_nodes[node]['usage'] = local.cmd(node, 'disk.usage')
            salt_nodes[node]['cpus'] = local.cmd(node, 'status.cpuinfo')
        except Exception as e:
            logger.exception("Error (%s) getting device information %s" %
                             (type(e), node))

    if args.savenodes:
        with open(args.savenodes, 'w') as wnf:
            wnf.write(
                json.dumps(salt_nodes,
                           cls=JSONEncoder,
                           indent=4,
                           sort_keys=True,
                           ensure_ascii=False))

    dev42 = device42.Device42(endpoint=config['device42']['host'],
                              user=config['device42']['user'],
                              password=config['device42']['pass'],
                              logger=logger,
                              debug=debug_mode)
    d42_insert(dev42, salt_nodes, config['options'], config.get('static', {}))

    return 0