def test_debug_level(time_ctime): log.set_debug_level(True) assert_true(log.DEBUG, "DEBUG should be true.") log.debug("Test") assert_false(time_ctime.called, "Should not get called.") log.set_debug_level(False) assert_false(log.DEBUG, "DEBUG should be true.") log.debug("Test") assert_true(time_ctime.called, "Should get called.")
def pid_read(name, pid_file_path="/var/run"): pid_path = make_pid_file_path(name, pid_file_path) log.debug("Checking pid path: %s" % pid_path) try: with open(pid_path, "r") as f: return int(f.read()) except IOError: return -1
def load_config(self, config_file): self.config_file = config_file log.debug("Config file at %s" % self.config_file) if os.path.exists(self.config_file): self.config = config.load_ini_file(self.config_file) log.debug("Loading config file %s contains %r" % (self.config_file, self.config)) else: log.warn("No config file at %s, using defaults." % self.config_file) self.config = {}
def still_running(name, pid_file_path="/var/run"): pid = pid_read(name, pid_file_path=pid_file_path) if pid == -1: log.debug("Returned pid not running at %s" % pid) return False else: # check if the process is still running with kill try: os.kill(pid, 0) log.debug("Process running at %d" % pid) return True except OSError: # this means the process is gone log.warn("Stale pid file %r has %d pid." % (make_pid_file_path(name, pid_file_path), pid)) return False
def still_running(name, pid_file_path="/var/run"): pid = pid_read(name, pid_file_path=pid_file_path) if pid == -1: log.debug("Returned pid not running at %s" % pid) return False else: # check if the process is still running with kill try: os.kill(pid, 0) log.debug("Process running at %d" % pid) return True except OSError: # this means the process is gone log.warn("Stale pid file %r has %d pid." % ( make_pid_file_path(name, pid_file_path), pid)) return False
def __init__(self, run_base="/var/run", log_dir="/var/log", pid_file_path="/var/run", uid="nobody", gid="nogroup", config_file=None): assert self.name, "You must set the service's name." config_file = config_file or os.path.join('/etc', self.name + ".conf") self.load_config(config_file) self.run_dir = self.get('run_dir') or os.path.join(run_base, self.name) self.pid_path = self.get('pid_path') or pid_file_path self.log_file = self.get('log_file') or os.path.join(log_dir, self.name + ".log") self.uid = self.get('uid') or uid self.gid = self.get('gid') or gid self.run_dir_mode = self.get('run_dir_mode') or '0700' self.run_dir_mode = int(self.run_dir_mode, 8) log.debug("UID and GID are %s:%s" % (self.uid, self.gid)) self.unum, self.gnum = unix.get_user_info(self.uid, self.gid) log.debug("Numeric UID:GID are %d:%d" % (self.unum, self.gnum))
def before_drop_privs(self, args): HOST = "0.0.0.0" if self.config: ports = list( int(x) for x in self.config['threadserver.ports'].split()) else: ports = list(int(x) for x in args) log.debug("Ports %r" % ports) if not ports: log.error("You need to list some ports.") sys.exit(1) self.server = None # this gets the last one to do a forever on for PORT in ports: server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) ip, port = server.server_address server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True server_thread.start() self.server = server
def __init__(self, run_base="/var/run", log_dir="/var/log", pid_file_path="/var/run", uid="nobody", gid="nogroup", config_file=None): assert self.name, "You must set the service's name." config_file = config_file or os.path.join('/etc', self.name + ".conf") self.load_config(config_file) self.run_dir = self.get('run_dir') or os.path.join(run_base, self.name) self.pid_path = self.get('pid_path') or pid_file_path self.log_file = self.get('log_file') or os.path.join( log_dir, self.name + ".log") self.uid = self.get('uid') or uid self.gid = self.get('gid') or gid self.run_dir_mode = self.get('run_dir_mode') or '0700' self.run_dir_mode = int(self.run_dir_mode, 8) log.debug("UID and GID are %s:%s" % (self.uid, self.gid)) self.unum, self.gnum = unix.get_user_info(self.uid, self.gid) log.debug("Numeric UID:GID are %d:%d" % (self.unum, self.gnum))
def before_drop_privs(self, args): HOST = "0.0.0.0" if self.config: ports = list(int(x) for x in self.config['threadserver.ports'].split()) else: ports = list(int(x) for x in args) log.debug("Ports %r" % ports) if not ports: log.error("You need to list some ports.") sys.exit(1) self.server = None # this gets the last one to do a forever on for PORT in ports: server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) ip, port = server.server_address server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True server_thread.start() self.server = server