def main(): '''The main method that drives the daemon. Starts an infinite loop that handles requests and responses to HTTP-based REST calls.''' def cleanShutdown(signal, frame): '''A method to handle shutdowns. This is necessary to prevent the agent from hanging on a HTCondor shutdown, since the shutdown script sends SIGQUIT instead of SIGTERM.''' logging.info('Received signal %i, shutting down server' % signal) print 'Received signal %i, shutting down server' % signal server.socket.close() sys.exit(0) # We need access to the Condor binaries on this machine in order to complete # requests and return data. Set up the PATH so we have access to the things # we need. if not os.environ.has_key("CONDOR_BIN_PATH"): os.environ["CONDOR_BIN_PATH"] = "/opt/condor/bin" if not os.environ.has_key("CONDOR_CONFIG"): # Use the HTCondor defaults if os.name == 'nt': os.environ["CONDOR_CONFIG"] = r'C:\condor\condor_config' else: os.environ["CONDOR_CONFIG"] = "/etc/condor/condor_config" if os.environ.has_key("CONDOR_BIN_PATH"): os.environ["PATH"] = os.environ["PATH"] + os.pathsep + os.environ["CONDOR_BIN_PATH"] CondorAgent.util.getCondorVersion() # Get Log directory from Condor configuration log_dir = CondorAgent.util.getCondorConfigVal("LOG") if not log_dir: log_dir = os.getcwd() # Get Log file name from Condor configuration log_file = CondorAgent.util.getCondorConfigVal("CONDOR_AGENT_LOG", default="CondorAgentLog") # Get the maximum log size from Condor configuration log_max_size = int(CondorAgent.util.getCondorConfigVal("MAX_CONDOR_AGENT_LOG", default=10*1024*1024)) # Get the maximum number of log files log_max_num = int(CondorAgent.util.getCondorConfigVal("MAX_NUM_CONDOR_AGENT_LOG", default=1)) # Get the log level LEVELS = { 'debug':logging.DEBUG, 'info':logging.INFO, 'warning':logging.WARNING, 'error':logging.ERROR, 'critical':logging.CRITICAL, } log_level_string = CondorAgent.util.getCondorConfigVal("CONDOR_AGENT_DEBUG", default="INFO") log_level = LEVELS.get(log_level_string.lower()) # Set up Basic configuration (Python 2.3 compatible) logger = logging.getLogger() hdlr = logging.handlers.RotatingFileHandler(os.path.join(log_dir.strip(), log_file), maxBytes=log_max_size, backupCount=log_max_num) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(log_level) try: logging.info("\n\nStarting CondorAgent v%s..." % __version__) logging.info("Arguments: %s" % str(sys.argv)) logging.info('Working directory: %s' % os.getcwd()) print "Starting CondorAgent v%s...started." % __version__ try: port = CondorAgent.util.getCondorConfigVal("CONDOR_AGENT_PORT") logging.info("Retrieved port '%s'" % port) port = int(port) except: port = 8008 logging.warning('Unable to find valid port in condor configuration, using default %d' % port) url = "http://localhost:" + str(port) + "/_shutdown" shutdown = Shutdown(url) # Standard ways of killing this in Unix (SIGTERM) work automatically, # but the standard way in Windows (WM_CLOSE) requires careful handling. if os.name == 'nt': from CondorAgent import win32 win32.setupShutdownHook(shutdown.shutdown) print "Waiting for incoming requests on port %d (press ^C to stop)..." % port # Start web service interface server = ThreadedHTTPServer(('', port), CondorAgentHandler) logging.info("Created web server at port %d" % port) # Capture SIGINT and SIGQUIT signal.signal(signal.SIGINT,cleanShutdown) if os.name != 'nt': signal.signal(signal.SIGQUIT,cleanShutdown) # 1.12: Switch user context to the CONDOR_IDS user before we start polling # for things on the port we just opened up. Don't do this on Windows! if os.name != 'nt': try: condor_uid = CondorAgent.util.getCondorUID() except KeyError: # Couldn't find a UID for Condor on this machine logging.warning('Unable to figure out the UID to run as on this host') condor_uid = None try: condor_gid = CondorAgent.util.getCondorGID() except KeyError: logging.warning('Unable to figure out the GID to run as on this host') # Couldn't find a GID for Condor on this machine condor_gid = None if condor_gid: # This may fail if we're not running as root (maybe someone started # us in the foreground?). Don't treat failure as a cause to interrupt # this daemon. We already created the socket for the server so we # should just continue to run as the user we already are... try: os.setgid(condor_gid) except Exception, e: logging.warning('Unable to change context to GID %d: %s' % (condor_gid, str(e))) else: if condor_uid: try: os.setuid(condor_uid) except Exception, e: logging.warning('Unable to change context to UID %d: %s' % (condor_uid, str(e)))
def main(): '''The main method that drives the daemon. Starts an infinite loop that handles requests and responses to HTTP-based REST calls.''' # We need access to the Condor binaries on this machine in order to complete # requests and return data. Set up the PATH so we have access to the things # we need. if not os.environ.has_key("CONDOR_BIN_PATH"): os.environ["CONDOR_BIN_PATH"] = "/opt/condor/bin" if not os.environ.has_key("CONDOR_CONFIG"): os.environ["CONDOR_CONFIG"] = "/etc/condor/condor_config" if os.environ.has_key("CONDOR_BIN_PATH"): os.environ["PATH"] = os.environ["PATH"] + os.pathsep + os.environ["CONDOR_BIN_PATH"] # Get Log directory from Condor configuration log_dir = CondorAgent.util.getCondorConfigVal("LOG") if not log_dir: log_dir = os.getcwd() # Set up Basic configuration (Python 2.3 compatible) logger = logging.getLogger() hdlr = logging.handlers.RotatingFileHandler(os.path.join(log_dir.strip(), "CondorAgentLog"), maxBytes=20*1024*1024, backupCount=3) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) try: logging.info("\n\nStarting CondorAgent v%s..." % __version__) logging.info("Arguments: %s" % str(sys.argv)) logging.info('Working directory: %s' % os.getcwd()) print "Starting CondorAgent v%s...started." % __version__ try: port = CondorAgent.util.getCondorConfigVal("CONDOR_AGENT_PORT") logging.info("Retrieved port '%s'" % port) port = int(port) except: port = 8008 logging.warning('Unable to find valid port in condor configuration, using default %d' % port) url = "http://localhost:" + str(port) + "/_shutdown" shutdown = Shutdown(url) # Standard ways of killing this in Unix (SIGTERM) work automatically, # but the standard way in Windows (WM_CLOSE) requires careful handling. if os.name == 'nt': from CondorAgent import win32 win32.setupShutdownHook(shutdown.shutdown) print "Waiting for incoming requests on post %d (press ^C to stop)..." % port # Start web service interface server = ThreadedHTTPServer(('', port), CondorAgentHandler) logging.info("Created web server at port %d" % port) # 1.12: Switch user context to the CONDOR_IDS user before we start polling # for things on the port we just opened up. Don't do this on Windows! if os.name != 'nt': try: condor_uid = CondorAgent.util.getCondorUID() except KeyError: # Couldn't find a UID for Condor on this machine logging.warning('Unable to figure out the UID to run as on this host') condor_uid = None try: condor_gid = CondorAgent.util.getCondorGID() except KeyError: logging.warning('Unable to figure out the GID to run as on this host') # Couldn't find a GID for Condor on this machine condor_gid = None if condor_gid: # This may fail if we're not running as root (maybe someone started # us in the foreground?). Don't treat failure as a cause to interrupt # this daemon. We already created the socket for the server so we # should just continue to run as the user we already are... try: os.setgid(condor_gid) except Exception, e: logging.warning('Unable to change context to GID %d: %s' % (condor_gid, str(e))) else: if condor_uid: try: os.setuid(condor_uid) except Exception, e: logging.warning('Unable to change context to UID %d: %s' % (condor_uid, str(e)))
def main(): '''The main method that drives the daemon. Starts an infinite loop that handles requests and responses to HTTP-based REST calls.''' # We need access to the Condor binaries on this machine in order to complete # requests and return data. Set up the PATH so we have access to the things # we need. if not os.environ.has_key("CONDOR_BIN_PATH"): os.environ["CONDOR_BIN_PATH"] = "/opt/condor/bin" if not os.environ.has_key("CONDOR_CONFIG") and os.name != 'nt': # set this as a default, but not under Windows. (Under Windows Condor uses the registry.) os.environ["CONDOR_CONFIG"] = "/etc/condor/condor_config" if os.environ.has_key("CONDOR_BIN_PATH"): os.environ["PATH"] = os.environ["PATH"] + os.pathsep + os.environ[ "CONDOR_BIN_PATH"] # Get Log directory from Condor configuration log_dir = CondorAgent.util.getCondorConfigVal("LOG") if not log_dir: log_dir = os.getcwd() # Set up Basic configuration (Python 2.3 compatible) logger = logging.getLogger() hdlr = logging.handlers.RotatingFileHandler(os.path.join( log_dir.strip(), "CondorAgentLog"), maxBytes=20 * 1024 * 1024, backupCount=3) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) try: logging.info("\n\nStarting CondorAgent v%s..." % __version__) logging.info("Arguments: %s" % str(sys.argv)) logging.info('Working directory: %s' % os.getcwd()) print "Starting CondorAgent v%s...started." % __version__ try: port = CondorAgent.util.getCondorConfigVal("CONDOR_AGENT_PORT") logging.info("Retrieved port '%s'" % port) port = int(port) except: port = 8008 logging.warning( 'Unable to find valid port in condor configuration, using default %d' % port) url = "http://localhost:" + str(port) + "/_shutdown" shutdown = Shutdown(url) # Standard ways of killing this in Unix (SIGTERM) work automatically, # but the standard way in Windows (WM_CLOSE) requires careful handling. if os.name == 'nt': from CondorAgent import win32 win32.setupShutdownHook(shutdown.shutdown) print "Waiting for incoming requests on post %d (press ^C to stop)..." % port # Start web service interface server = ThreadedHTTPServer(('', port), CondorAgentHandler) logging.info("Created web server at port %d" % port) # 1.12: Switch user context to the CONDOR_IDS user before we start polling # for things on the port we just opened up. Don't do this on Windows! if os.name != 'nt': try: condor_uid = CondorAgent.util.getCondorUID() except KeyError: # Couldn't find a UID for Condor on this machine logging.warning( 'Unable to figure out the UID to run as on this host') condor_uid = None try: condor_gid = CondorAgent.util.getCondorGID() except KeyError: logging.warning( 'Unable to figure out the GID to run as on this host') # Couldn't find a GID for Condor on this machine condor_gid = None if condor_gid: # This may fail if we're not running as root (maybe someone started # us in the foreground?). Don't treat failure as a cause to interrupt # this daemon. We already created the socket for the server so we # should just continue to run as the user we already are... try: os.setgid(condor_gid) except Exception, e: logging.warning('Unable to change context to GID %d: %s' % (condor_gid, str(e))) else: if condor_uid: try: os.setuid(condor_uid) except Exception, e: logging.warning( 'Unable to change context to UID %d: %s' % (condor_uid, str(e)))