def do(self, args): conf_dir = sys.exec_prefix self.minion_config = read_config('%s/etc/certmaster/minion.conf' % conf_dir, MinionConfig) self.funcd_config = read_config('%s/etc/func/minion.conf' % conf_dir, FuncdConfig) if not self.check_certmaster and not self.check_minion: print "* specify --certmaster, --minion, or both" return else: print "SCAN RESULTS:" hostname = func_utils.get_hostname_by_route() print "* FQDN is detected as %s, verify that is correct" % hostname self.check_iptables() if not os.getuid() == 0: print "* root is required to run these setup tests" return if self.check_minion: # check that funcd is running self.check_service("funcd") # check that the configured certmaster is reachable self.check_talk_to_certmaster() if self.check_certmaster: # check that certmasterd is running self.check_service("certmasterd") # see if we have any waiting CSRs # FIXME: TODO # see if we have signed any certs # FIXME: TODO self.server_spec = self.parentCommand.server_spec self.getOverlord() results = self.overlord_obj.test.add(1,2) hosts = results.keys() if len(hosts) == 0: print "* no systems have signed certs" else: failed = 0 for x in hosts: if results[x] != 3: failed = failed+1 if failed != 0: print "* unable to connect to %s registered minions from overlord" % failed print "* run func '*' ping to check status" # see if any of our certs have expired # warn about iptables if running print "End of Report."
def main(argv): """ Start things up. """ conf_dir = sys.exec_prefix sys.excepthook = excepthook if len(sys.argv) > 1 and sys.argv[1] == "--list-modules": config = read_config("%s/etc/func/minion.conf" % conf_dir, FuncdConfig) module_names = module_loader.load_modules(module_list = config.module_list).keys() module_names.sort() print "loaded modules:" for foo in module_names: print "\t" + foo sys.exit(0) if "--version" in sys.argv or "-v" in sys.argv: print >> sys.stderr, file("%s/etc/func/version" % conf_dir).read().strip() sys.exit(0) if "--info" in sys.argv: server = setup_server() print 'config:' for l in str(server.config).split('\n'): print '\t' + l print 'server name: %s' % server.server_name print 'server listen addr: %s:%s' % server.server_address print 'key file: %s' % server.key print 'cert file: %s' % server.cert print 'ca file: %s' % server.ca cert = certs.retrieve_cert_from_file(server.cert) print 'cert dn: %s' % cert.get_subject().CN print 'certificate hash: %s' % cert.subject_name_hash() print 'modules loaded:' for mn in sorted(server.modules.keys()): print '\t' + mn print 'acls:' for (host, methods) in server.acls.acls.items(): print '\t' + host + ' : ' + str(methods) print 'facts:' for (n, meth) in server.fact_methods.items(): print '\t' + n + ' : ' + meth() sys.exit(0) if "daemon" in sys.argv or "--daemon" in sys.argv: utils.daemonize("%s/var/run/funcd.pid" % conf_dir) else: print "serving...\n" try: config = read_config("%s/etc/func/minion.conf" % conf_dir, FuncdConfig) if config.use_certmaster: hn = func_utils.get_hostname_by_route() requester.request_cert(hn) serve() except codes.FuncException, e: print >> sys.stderr, 'error: %s' % e sys.exit(1)
def do(self, args): self.minion_config = read_config('/etc/certmaster/minion.conf', MinionConfig) self.funcd_config = read_config('/etc/func/minion.conf', FuncdConfig) if not self.check_certmaster and not self.check_minion: print "* specify --certmaster, --minion, or both" return else: print "SCAN RESULTS:" hostname = func_utils.get_hostname_by_route() print "* FQDN is detected as %s, verify that is correct" % hostname self.check_iptables() if not os.getuid() == 0: print "* root is required to run these setup tests" return if self.check_minion: # check that funcd is running self.check_service("funcd") # check that the configured certmaster is reachable self.check_talk_to_certmaster() if self.check_certmaster: # check that certmasterd is running self.check_service("certmasterd") # see if we have any waiting CSRs # FIXME: TODO # see if we have signed any certs # FIXME: TODO self.server_spec = self.parentCommand.server_spec self.getOverlord() results = self.overlord_obj.test.add(1, 2) hosts = results.keys() if len(hosts) == 0: print "* no systems have signed certs" else: failed = 0 for x in hosts: if results[x] != 3: failed = failed + 1 if failed != 0: print "* unable to connect to %s registered minions from overlord" % failed print "* run func '*' ping to check status" # see if any of our certs have expired # warn about iptables if running print "End of Report."
def main(argv): """ Start things up. """ sys.excepthook = excepthook if len(sys.argv) > 1 and sys.argv[1] == "--list-modules": config = read_config("/etc/func/minion.conf", FuncdConfig) module_names = module_loader.load_modules( module_list=config.module_list).keys() module_names.sort() print "loaded modules:" for foo in module_names: print "\t" + foo sys.exit(0) if "--version" in sys.argv or "-v" in sys.argv: print >> sys.stderr, file("/etc/func/version").read().strip() sys.exit(0) if "--info" in sys.argv: server = setup_server() print 'config:' for l in str(server.config).split('\n'): print '\t' + l print 'server name: %s' % server.server_name print 'server listen addr: %s:%s' % server.server_address print 'key file: %s' % server.key print 'cert file: %s' % server.cert print 'ca file: %s' % server.ca cert = certs.retrieve_cert_from_file(server.cert) print 'cert dn: %s' % cert.get_subject().CN print 'certificate hash: %s' % cert.subject_name_hash() print 'modules loaded:' for mn in sorted(server.modules.keys()): print '\t' + mn print 'acls:' for (host, methods) in server.acls.acls.items(): print '\t' + host + ' : ' + str(methods) print 'facts:' for (n, meth) in server.fact_methods.items(): print '\t' + n + ' : ' + meth() sys.exit(0) if "daemon" in sys.argv or "--daemon" in sys.argv: utils.daemonize("/var/run/funcd.pid") else: print "serving...\n" try: config = read_config("/etc/func/minion.conf", FuncdConfig) if config.use_certmaster: hn = func_utils.get_hostname_by_route() requester.request_cert(hn) serve() except codes.FuncException, e: print >> sys.stderr, 'error: %s' % e sys.exit(1)
def get_hostname_by_route(): """ "localhost" is a lame hostname to use for a key, so try to get a more meaningful hostname. We do this by connecting to the certmaster and seeing what interface/ip it uses to make that connection, and looking up the hostname for that. """ # FIXME: this code ignores http proxies (which granted, we don't # support elsewhere either. conf_dir = sys.exec_prefix minion_config_file = "%s/etc/func/minion.conf" % conf_dir minion_config = read_config(minion_config_file, FuncdConfig) # don't bother guessing a hostname if they specify it in the config file if minion_config.minion_name: return minion_config.minion_name.lower() # try to find the hostname attached to the ip of the interface that we use # to talk to the certmaster cm_config_file = "%s/etc/certmaster/minion.conf" % conf_dir cm_config = read_config(cm_config_file, MinionConfig) server = cm_config.certmaster port = cm_config.certmaster_port s = socket.socket() s.settimeout(5) s.connect_ex((server, port)) (intf, port) = s.getsockname() s.close() try: return socket.gethostbyaddr(intf)[0] except: pass # try to find the hostname of the ip we're listening on if minion_config.listen_addr: try: return socket.gethostbyaddr(minion_config.listen_addr)[0] except: pass # in an ideal world, this would return exactly what we want: the most meaningful hostname # for a system, but that is often not that case try: hostname = socket.gethostname() ip = socket.gethostbyname(hostname) if ip != "127.0.0.1" and ip != "::1": return hostname.lower() except: pass # all else has failed to get a good hostname, so just return # an ip address return intf
def get_hostname_by_route(): """ "localhost" is a lame hostname to use for a key, so try to get a more meaningful hostname. We do this by connecting to the certmaster and seeing what interface/ip it uses to make that connection, and looking up the hostname for that. """ # FIXME: this code ignores http proxies (which granted, we don't # support elsewhere either. minion_config_file = '/etc/func/minion.conf' minion_config = read_config(minion_config_file, FuncdConfig) # don't bother guessing a hostname if they specify it in the config file if minion_config.minion_name: return minion_config.minion_name.lower() # try to find the hostname attached to the ip of the interface that we use # to talk to the certmaster cm_config_file = '/etc/certmaster/minion.conf' cm_config = read_config(cm_config_file, MinionConfig) server = cm_config.certmaster port = cm_config.certmaster_port s = socket.socket() s.settimeout(5) s.connect_ex((server, port)) (intf, port) = s.getsockname() s.close() try: return socket.gethostbyaddr(intf)[0] except: pass # try to find the hostname of the ip we're listening on if minion_config.listen_addr: try: return socket.gethostbyaddr(minion_config.listen_addr)[0] except: pass # in an ideal world, this would return exactly what we want: the most meaningful hostname # for a system, but that is often not that case try: hostname = socket.gethostname() ip = socket.gethostbyname(hostname) if ip != "127.0.0.1" and ip != "::1": return hostname.lower() except: pass # all else has failed to get a good hostname, so just return # an ip address return intf
def __init__(self): """ Constructor. """ cm_config_file = '/etc/certmaster/minion.conf' self.cm_config = read_config(cm_config_file, CMConfig) config_file = "/etc/func/minion.conf" self.config = read_config(config_file, FuncdConfig) self.logger = logger.Logger().logger self.audit_logger = logger.AuditLogger() self.__setup_handlers()
def __init__(self): """ Constructor. """ conf_dir = sys.exec_prefix cm_config_file = '%s/etc/certmaster/minion.conf' % conf_dir self.cm_config = read_config(cm_config_file, CMConfig) config_file = "%s/etc/func/minion.conf" % conf_dir self.config = read_config(config_file, FuncdConfig) self.logger = logger.Logger().logger self.audit_logger = logger.AuditLogger() self.__setup_handlers()
def tail_output(self,minion_job_id): """ A tail method which will tail the log files that will track their output .... """ from func.minion import sub_process from certmaster.config import read_config from func.commonconfig import FuncdConfig from func.logger import config_file import os import subprocess config = read_config(config_file, FuncdConfig) method_log_dir = config.method_log_dir method_log_file = os.path.join(method_log_dir,minion_job_id) cmd= subprocess.Popen( args=["tail","-n",str(NUM_OF_LINES),method_log_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell = False, ) return cmd.communicate()
def get_progress(self,minion_job_id): """ Get the log file and parse the progress part to be polled on overlord """ from certmaster.config import read_config from func.commonconfig import FuncdConfig from func.logger import config_file import os import re config = read_config(config_file, FuncdConfig) method_log_dir = config.method_log_dir method_log_file = os.path.join(method_log_dir,minion_job_id) reco=re.compile("Progress report (\d+)/(\d+) completed") fo = file(method_log_file) for line in xreverse(fo): tmp = re.search(reco,line) if tmp: current = tmp.group(1) all = tmp.group(2) return (int(current),int(all)) #that tells that we couldnt found any report there return(0,0)
def do(self, args): # I'm not really a fan of the "module methodname" approach # but we'll keep it for now -akl # I kind of feel like we shouldn't be parsing args here, but I'm # not sure what the write place is -al; if not args: self.outputUsage() return self.module = args[0] if len(args) > 1: self.method = args[1] else: self.method = None if len(args) > 2: self.method_args = args[2:] else: self.method_args = [] # this could get weird, sub sub classes might be calling this # this with multiple.parentCommand.parentCommands... # maybe command.py needs a way to set attrs on subCommands? # or some sort of shared datastruct? # self.getOverlord() call_config = read_config(config_file, CallConfig) if self.method and (self.module + "." + self.method in call_config.force_async): self.options. async = True self.interactive = False self. async = self.options. async
def __init__(self, parentCommand=None, stdout=sys.stdout, stderr=sys.stderr): """ Create a new command instance, with the given parent. Allows for redirecting stdout and stderr if needed. This redirection will be passed on to child commands. """ if not self.name: self.name = str(self.__class__).split(".")[-1].lower() self.stdout = stdout self.stderr = stderr self.parentCommand = parentCommand self.config = read_config(CONFIG_FILE, CMConfig) # create subcommands if we have them self.subCommands = {} self.aliasedSubCommands = {} if self.subCommandClasses: for C in self.subCommandClasses: c = C(self, stdout=stdout, stderr=stderr) self.subCommands[c.name] = c if c.aliases: for alias in c.aliases: self.aliasedSubCommands[alias] = c # create our formatter and add subcommands if we have them formatter = CommandHelpFormatter() if self.subCommands: for name, command in self.subCommands.items(): formatter.addCommand(name, command.summary or command.description) # expand %command for the bottom usage usage = self.usage or self.name if usage.find("%command") > -1: usage = usage.split("%command")[0] + "[command]" usages = [usage] # FIXME: abstract this into getUsage that takes an optional # parentCommand on where to stop recursing up # useful for implementing subshells # walk the tree up for our usage c = self.parentCommand while c: usage = c.usage or c.name if usage.find(" %command") > -1: usage = usage.split(" %command")[0] usages.append(usage) c = c.parentCommand usages.reverse() usage = " ".join(usages) # create our parser description = self.description or self.summary self.parser = CommandOptionParser(usage=usage, description=description, formatter=formatter) self.parser.set_stdout(self.stdout) self.parser.disable_interspersed_args() # allow subclasses to add options self.addOptions()
def main(argv): """ Start things up. """ sys.excepthook = excepthook if len(sys.argv) > 1 and sys.argv[1] == "--list-modules": module_names = module_loader.load_modules().keys() module_names.sort() print "loaded modules:" for foo in module_names: print "\t" + foo sys.exit(0) if "--version" in sys.argv or "-v" in sys.argv: print >> sys.stderr, file("/etc/func/version").read().strip() sys.exit(0) if "daemon" in sys.argv or "--daemon" in sys.argv: utils.daemonize("/var/run/funcd.pid") else: print "serving...\n" try: config = read_config("/etc/func/minion.conf", FuncdConfig) if config.use_certmaster: hn = func_utils.get_hostname_by_route() requester.request_cert(hn) serve() except codes.FuncException, e: print >> sys.stderr, 'error: %s' % e sys.exit(1)
def __init__(self, logfilepath="%s/var/log/func/func.log" % conf_dir): config_file = "%s/etc/func/minion.conf" % conf_dir self.config = read_config(config_file, FuncdConfig) self.loglevel = logging._levelNames[self.config.log_level] self._setup_logging() if self._no_handlers: self._setup_handlers(logfilepath=logfilepath)
def do(self, args): # I'm not really a fan of the "module methodname" approach # but we'll keep it for now -akl # I kind of feel like we shouldn't be parsing args here, but I'm # not sure what the write place is -al; if not args: self.outputUsage() return self.module = args[0] if len(args) > 1: self.method = args[1] else: self.method = None if len(args) > 2: self.method_args = args[2:] else: self.method_args = [] # this could get weird, sub sub classes might be calling this # this with multiple.parentCommand.parentCommands... # maybe command.py needs a way to set attrs on subCommands? # or some sort of shared datastruct? # self.getOverlord() call_config = read_config(config_file, CallConfig) if self.method and (self.module+"."+self.method in call_config.force_async): self.options.async=True self.interactive = False self.async = self.options.async
def __init__(self, logfilepath="/var/log/func/func.log"): config_file = '/etc/func/minion.conf' self.config = read_config(config_file, FuncdConfig) self.loglevel = logging._levelNames[self.config.log_level] self._setup_logging() if self._no_handlers: self._setup_handlers(logfilepath=logfilepath)
def setUp(self): f = open(self.config_file, "w+") if not os.access("/tmp/func-test", os.R_OK): os.mkdir("/tmp/func-test") f.write(config_file_1) f.close() self.cfg = config.read_config(self.config_file, ConfigTest)
def setUp(self): TestConfigSet.setUp(self) filename = "/tmp/func-test/test2.conf" f = open(filename, "a+") self.cfg.write(f, "main") f.close() self.cfg = config.read_config(filename, ConfigTest)
def setup_server(): """ Code for starting the XMLRPC service. """ config = read_config("/etc/func/minion.conf", FuncdConfig) listen_addr = config.listen_addr listen_port = config.listen_port if listen_port == '': listen_port = 51234 server = FuncSSLXMLRPCServer((listen_addr, listen_port), config.module_list) return server
def serve(): """ Code for starting the XMLRPC service. """ config = read_config("/etc/func/minion.conf", FuncdConfig) listen_addr = config.listen_addr listen_port = config.listen_port if listen_port == '': listen_port = 51234 server = FuncSSLXMLRPCServer((listen_addr, listen_port)) server.logRequests = 0 # don't print stuff to console server.serve_forever()
def __init__(self,conf_file = None,db_file=None,*args,**kwargs): """ Initializing the database if it doesnt exists it is created and connection opened for serving nothing special @param conf_file : Configuration file @param db_file : Place of the database file override if needed """ self.config = conf_file or CONF_FILE self.config = read_config(self.config,OverlordConfig) self.db_path = db_file or self.config.group_db or DB_PATH self._recreate_session()
def __init__(self, conf_file=None, db_file=None, *args, **kwargs): """ Initializing the database if it doesnt exists it is created and connection opened for serving nothing special @param conf_file : Configuration file @param db_file : Place of the database file override if needed """ self.config = conf_file or CONF_FILE self.config = read_config(self.config, OverlordConfig) self.db_path = db_file or self.config.group_db or DB_PATH self._recreate_session()
def serve(): """ Code for starting the XMLRPC service. """ config = read_config("/etc/func/minion.conf", FuncdConfig) listen_addr = config.listen_addr listen_port = config.listen_port if listen_port == '': listen_port = 51234 server =FuncSSLXMLRPCServer((listen_addr, listen_port)) server.logRequests = 0 # don't print stuff to console server.serve_forever()
def clean_dummy_minions(self, howmany=None): """ Deletes a lots of minions garbage """ cm_config = read_config(CONFIG_FILE, CMConfig) howmany = howmany or 100 # it is a good default number for m in xrange(howmany): tmp_f = "%s/%s.%s" % (cm_config.certroot, str(m), cm_config.cert_extension) if os.path.exists(tmp_f): os.remove(tmp_f) print "%d dummy minions cleaned " % howmany
def __init__(self): config_file = '/etc/func/minion.conf' self.config = read_config(config_file, FuncdConfig) self.__init_log() self.__base_methods = { # __'s so we don't clobber useful names "module_version" : self.__module_version, "module_api_version" : self.__module_api_version, "module_description" : self.__module_description, "list_methods" : self.__list_methods, "get_method_args" : self.__get_method_args, } self.__init_options()
def clean_dummy_minions(self,howmany=NUMBER_OF_TEST_MINIONS): """ Deletes a lots of minions garbage """ cm_config = read_config(CONFIG_FILE, CMConfig) howmany = howmany or 100 #it is a good default number for m in xrange(howmany): tmp_f = "%s/%s.%s" % (cm_config.certroot,str(m), cm_config.cert_extension) if os.path.exists(tmp_f): os.remove(tmp_f) print "%d dummy minions cleaned "%howmany
def getOverlord(self): ol_config = None if self.parentCommand.conffile: ol_config = read_config(self.parentCommand.conffile, commonconfig.OverlordConfig) self.overlord_obj = client.Overlord(self.server_spec, interactive=self.interactive, verbose=self.verbose, async=self.async, nforks=self.forks, delegate=self.delegate, mapfile=self.mapfile, timeout=self.parentCommand.socket_timeout, exclude_spec=self.parentCommand.exclude_spec, config=ol_config)
def __init__(self): config_file = '/etc/func/minion.conf' self.config = read_config(config_file, FuncdConfig) self.__init_log() self.__base_methods = { # __'s so we don't clobber useful names "module_version": self.__module_version, "module_api_version": self.__module_api_version, "module_description": self.__module_description, "list_methods": self.__list_methods, "get_method_args": self.__get_method_args, } self.__init_options()
def create_dummy_minions(self, howmany=None): """ Creates a lots of minions so we can query with different minion names cool isnt it """ cm_config = read_config(CONFIG_FILE, CMConfig) howmany = howmany or 100 # it is a good default number final_list = [] for m in xrange(howmany): tmp_f = open("%s/%s.%s" % (cm_config.certroot, str(m), cm_config.cert_extension), "w") tmp_f.close() final_list.append(str(m)) print "%d dummy minions created " % howmany return final_list
def getOverlord(self): ol_config = None if self.parentCommand.conffile: ol_config = read_config(self.parentCommand.conffile, commonconfig.OverlordConfig) self.overlord_obj = client.Overlord( self.server_spec, interactive=self.interactive, verbose=self.verbose, async=self. async, nforks=self.forks, delegate=self.delegate, mapfile=self.mapfile, timeout=self.parentCommand.socket_timeout, exclude_spec=self.parentCommand.exclude_spec, config=ol_config)
def create_dummy_minions(self,howmany=NUMBER_OF_TEST_MINIONS): """ Creates a lots of minions so we can query with different minion names cool isnt it """ cm_config = read_config(CONFIG_FILE, CMConfig) howmany = howmany or 100 #it is a good default number final_list = [] for m in xrange(howmany): tmp_f = open("%s/%s.%s" % (cm_config.certroot,str(m),cm_config.cert_extension),"w") tmp_f.close() final_list.append(str(m)) print "%d dummy minions created "%howmany return final_list
def choose_backend(backend=None,conf_file=None,db_file=None): """ Chooses a backend accoding to params or what is supplied ... """ config = read_config(CONF_FILE,OverlordConfig) backend = backend or config.backend or "conf" if backend == "sqlite": from func.overlord.group.sqlite_backend import SqliteBackend return SqliteBackend(db_file=db_file) elif backend == "conf": from func.overlord.group.conf_backend import ConfBackend return ConfBackend(conf_file=conf_file) else: raise Exception("No valid backend options supplied")
def choose_backend(backend=None, conf_file=None, db_file=None): """ Chooses a backend accoding to params or what is supplied ... """ config = read_config(CONF_FILE, OverlordConfig) backend = backend or config.backend or "conf" if backend == "sqlite": from func.overlord.group.sqlite_backend import SqliteBackend return SqliteBackend(db_file=db_file) elif backend == "conf": from func.overlord.group.conf_backend import ConfBackend return ConfBackend(conf_file=conf_file) else: raise Exception("No valid backend options supplied")
def check_talk_to_certmaster(self): config_file = "/etc/certmaster/minion.conf" minion_config = read_config(config_file, MinionConfig) cert_dir = minion_config.cert_dir # FIXME: don't hardcode port master_uri = "http://%s:51235/" % minion_config.certmaster print "* this minion is configured in /etc/certmaster/minion.conf to talk to host '%s' for certs, verify that is correct" % minion_config.certmaster # this will be a 501, unsupported GET, but we should be # able to tell if we can make contact connect_ok = True try: fd = urllib2.urlopen(master_uri) data = fd.read() fd.close() except urllib2.HTTPError: pass except: connect_ok = False if not connect_ok: print "cannot connect to certmaster at %s" % (master_uri)
def check_talk_to_certmaster(self): config_file = '/etc/certmaster/minion.conf' minion_config = read_config(config_file, MinionConfig) cert_dir = minion_config.cert_dir # FIXME: don't hardcode port master_uri = "http://%s:51235/" % minion_config.certmaster print "* this minion is configured in /etc/certmaster/minion.conf to talk to host '%s' for certs, verify that is correct" % minion_config.certmaster # this will be a 501, unsupported GET, but we should be # able to tell if we can make contact connect_ok = True try: fd = urllib2.urlopen(master_uri) data = fd.read() fd.close() except urllib2.HTTPError: pass except: connect_ok = False if not connect_ok: print "cannot connect to certmaster at %s" % (master_uri)
def get_hostname_by_route(): """ "localhost" is a lame hostname to use for a key, so try to get a more meaningful hostname. We do this by connecting to the certmaster and seeing what interface/ip it uses to make that connection, and looking up the hostname for that. """ # FIXME: this code ignores http proxies (which granted, we don't # support elsewhere either. hostname = None minion_config_file = '/etc/func/minion.conf' minion_config = read_config(minion_config_file, FuncdConfig) # don't bother guessing a hostname if they specify it in the config file if minion_config.minion_name: return minion_config.minion_name # try to find the hostname attached to the ip of the interface that we use # to talk to the certmaster cm_config_file = '/etc/certmaster/minion.conf' cm_config = read_config(cm_config_file, MinionConfig) server = cm_config.certmaster port = cm_config.certmaster_port try: s = socket.socket() s.settimeout(5) s.connect((server, port)) (intf, port) = s.getsockname() # this can fail if there is no reverse DNS available intf_hostname = socket.gethostbyaddr(intf)[0] ip = socket.gethostbyname(intf_hostname) # not talking via localhost? good enough... if ip != '127.0.0.1': s.close() return intf_hostname except: s.close() # something failed, reverse dns, etc # try to find the hostname of the ip we're listening on if minion_config.listen_addr: try: (hostname, aliases, ips) = socket.gethostbyaddr(minion_config.listen_addr) except: hostname = None # in an ideal world, this would return exactly what we want: the most meaningful hostname # for a system, but that is often not that case if hostname is None: hostname = socket.gethostname() # "localhost" is a really crappy hostname, so is pretty much anything attached # to 127.0.0.1, so try for something better try: ip = socket.gethostbyname(hostname) except: hostname = None # non loopback is about as good as we can do for a guess if ip != "127.0.0.1" and hostname is not None: return hostname # all else has failed to get a good hostname, so just return # an ip address return socket.gethostbyname(socket.gethostname())
def __init__(self, parentCommand=None, stdout=sys.stdout, stderr=sys.stderr): """ Create a new command instance, with the given parent. Allows for redirecting stdout and stderr if needed. This redirection will be passed on to child commands. """ if not self.name: self.name = str(self.__class__).split('.')[-1].lower() self.stdout = stdout self.stderr = stderr self.parentCommand = parentCommand self.config = read_config(CONFIG_FILE, CMConfig) # create subcommands if we have them self.subCommands = {} self.aliasedSubCommands = {} if self.subCommandClasses: for C in self.subCommandClasses: c = C(self, stdout=stdout, stderr=stderr) self.subCommands[c.name] = c if c.aliases: for alias in c.aliases: self.aliasedSubCommands[alias] = c # create our formatter and add subcommands if we have them formatter = CommandHelpFormatter() if self.subCommands: for name, command in self.subCommands.items(): formatter.addCommand(name, command.summary or command.description) # expand %command for the bottom usage usage = self.usage or self.name if usage.find("%command") > -1: usage = usage.split("%command")[0] + '[command]' usages = [ usage, ] # FIXME: abstract this into getUsage that takes an optional # parentCommand on where to stop recursing up # useful for implementing subshells # walk the tree up for our usage c = self.parentCommand while c: usage = c.usage or c.name if usage.find(" %command") > -1: usage = usage.split(" %command")[0] usages.append(usage) c = c.parentCommand usages.reverse() usage = " ".join(usages) # create our parser description = self.description or self.summary self.parser = CommandOptionParser(usage=usage, description=description, formatter=formatter) self.parser.set_stdout(self.stdout) self.parser.disable_interspersed_args() # allow subclasses to add options self.addOptions()
TEST_SERVICES = False TEST_HARDWARE = False TEST_SMART = True if TEST_GETATTR: import func.overlord.client as func_client print func_client.Overlord("*").hardware.pci_info() #print func_client.Overlord("*").test.add(1,2) #print func_client.Overlord("*").hardware.info() #print func_client.Overlord("*").run("hardware","info",[]) #print func_client.Overlord(socket.gethostname(),noglobs=True).test.add("1","2") sys.exit(1) # get a connecton (to be replaced by client lib logic) conf_dir = sys.exec_prefix minion_cfg = read_config("%s/etc/func/minion.conf" % conf_dir, FuncdConfig) port = minion_cfg.listen_port if port == '': port = 51234 s = xmlrpclib.ServerProxy("http://127.0.0.1:%s" % port) # here's the basic test... print s.test.add(1, 2) if TEST_SMART: print s.smart.info() if TEST_PROCESS: print s.process.info() # print s.process.pkill("thunderbird")
def get_hostname_by_route(): """ "localhost" is a lame hostname to use for a key, so try to get a more meaningful hostname. We do this by connecting to the certmaster and seeing what interface/ip it uses to make that connection, and looking up the hostname for that. """ # FIXME: this code ignores http proxies (which granted, we don't # support elsewhere either. hostname = None minion_config_file = '/etc/func/minion.conf' minion_config = read_config(minion_config_file, FuncdConfig) # don't bother guessing a hostname if they specify it in the config file if minion_config.minion_name: return minion_config.minion_name # try to find the hostname of the ip we're listening on if minion_config.listen_addr: try: hostname = socket.gethostbyaddr(listen_addr) except: hostname = None # in an ideal world, this would return exactly what we want: the most meaningful hostname # for a system, but that is often not that case if hostname is None: hostname = socket.gethostname() # "localhost" is a really crappy hostname, so is pretty much anything attached # to 127.0.0.1, so try for something better try: ip = socket.gethostbyname(hostname) except: hostname = None # non loopback is about as good as we can do for a guess if ip != "127.0.0.1" and hostname is not None: return hostname # if the hostname is still bogus # try to find the hostname attached to the ip of the interface that we use # to talk to the certmaster cm_config_file = '/etc/certmaster/minion.conf' cm_config = read_config(config_file, CMConfig) server = config.certmaster port = config.certmaster_port try: s = socket.socket() s.settimeout(5) s.connect((server, port)) (intf, port) = s.getsockname() remote_hostname = socket.gethostbyaddr(intf)[0] if remote_hostname != "localhost": hostname = remote_hostname # print "DEBUG: HOSTNAME FROM CERTMASTER == %s" % hostname s.close() except: s.close() raise # print "DEBUG: final hostname=%s" % hostname return hostname
def setUp(self): f = open(self.config_file, "w+") f.write(config_file_1) f.close() self.cfg = config.read_config(self.config_file, ConfigTest)
def __init_options(self): options_file = '/etc/func/modules/' + self.__class__.__name__ + '.conf' self.options = read_config(options_file, self.Config) return
def _setup_logging(self): self.logger = logging.getLogger("audit") def _setup_handlers(self, logfilepath="/var/log/func/audit.log"): handler = logging.FileHandler(logfilepath, "a") self.logger.setLevel(self.loglevel) formatter = logging.Formatter("%(asctime)s - %(message)s") handler.setFormatter(formatter) self.logger.addHandler(handler) self._no_handlers = False #some more dynamic logger handlers here config_file = '/etc/func/minion.conf' config = read_config(config_file, FuncdConfig) GLOBAL_LOG_DIR = config.method_log_dir class StandartLogger(object): """ It is just a proxy like object to the logging module so we control here the stuff """ def __init__(self, handlers, app_name, **kwargs): self.logger = logging.getLogger(app_name) self.logger.setLevel(logging.DEBUG) self.handlers = handlers self.__setup_handlers()
def _setup_logging(self): self.logger = logging.getLogger("audit") def _setup_handlers(self, logfilepath="%s/var/log/func/audit.log" % conf_dir): handler = logging.FileHandler(logfilepath, "a") self.logger.setLevel(self.loglevel) formatter = logging.Formatter("%(asctime)s - %(message)s") handler.setFormatter(formatter) self.logger.addHandler(handler) self._no_handlers = False # some more dynamic logger handlers here config_file = "%s/etc/func/minion.conf" % conf_dir config = read_config(config_file, FuncdConfig) GLOBAL_LOG_DIR = config.method_log_dir class StandartLogger(object): """ It is just a proxy like object to the logging module so we control here the stuff """ def __init__(self, handlers, app_name, **kwargs): self.logger = logging.getLogger(app_name) self.logger.setLevel(logging.DEBUG) self.handlers = handlers self.__setup_handlers()
def __init_options(self): self.options_file = '/etc/func/modules/'+self.__class__.__name__+'.conf' self.options = read_config(self.options_file, self.Config) return