#!/usr/bin/python -tt # this script sucks data out of puppet and populates our infrahosts repo with it # it has to be run as root b/c puppet files are protected and b/c it runs func. # in the future this will not be necessary but for now just make sure you chown the files after you're done # find ./ -user root | sudo xargs chown username.usergroup # skvidal - 7/7/11 # take info from the puppet yaml files and stuff it into files in each dir for our physical hw # datacenter # serial number # hw maker/vendor # model number if we can get it # touch a warranty file and mark it as UNKNOWN # num processors # memory total import sys import yaml import os import func.overlord.client import git puppetpath='/var/lib/puppet/yaml/node/' infrahosts_git=os.getcwd() + '/' # based on the idea that this is run from the users checkout of infrahosts if not os.path.exists(infrahosts_git + '/.git'): infrahosts_git=os.path.expanduser('~/infra-hosts/') if not os.path.exists(infrahosts_git + '/.git'): print "Cannot find infra hosts git repo anywhere normal, (cwd nor ~/infra-hosts, assuming you're being dumb)" print "exit" sys.exit(1) def generic_string_constructor(loader, node): return loader.construct_scalar(node) def generic_mapping_constructor(loader, suffix, node): return loader.construct_mapping(node) yaml.add_constructor(u'!ruby/sym', generic_string_constructor) yaml.add_multi_constructor(u'!ruby/object:', generic_mapping_constructor) gitrepo = git.Git(infrahosts_git) host_str = '*' if len(sys.argv) > 1: host_str = sys.argv[1] print host_str hosts = func.overlord.client.Client(host_str).minions_class.get_all_hosts() for hn in hosts: print 'processing %s' % hn if not os.path.exists(puppetpath + hn + '.yaml'): print 'Could not find host info for %s' % hn continue info = yaml.load(open(puppetpath + hn + '.yaml','r').read()) autoinfo = infrahosts_git + hn + '/autoinfo/' if not os.path.exists(autoinfo): os.makedirs(autoinfo) if not info: print "bad host yaml %s" % hn continue items = [('datacenter', 'datacenter'), ('processorcount', 'processorcount'), ('memorysize', 'memory'),('distrorelease', 'distro'), ('selinux_current_mode', 'selinux_current_mode')] if info['parameters']['is_virtual'] in ('false', 'False', '0', '"false"'): items.extend([('serialnumber', 'serialnumber'), ('manufacturer', 'manufacturer'), ('productname', 'model')]) for (k, fn) in items: if k in info['parameters']: open(autoinfo + fn, 'w').write(info['parameters'][k]) gitrepo.add(autoinfo + fn) print "git commit -a your additions"