def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException( "The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException( "The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException( "The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException( "The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive()
def setUp(self): self.name = "test_something-" + str(uuid.uuid4()) if system() == "Darwin": # The standard mac tmp path is too long # for a socket self.dirpath = tempfile.mkdtemp(dir="/tmp") else: self.dirpath = tempfile.mkdtemp() self.supd_db_path = "sqlite:///" + os.path.join(self.dirpath, "sup.db") self.supd_db = SupDDB(self.supd_db_path) supd_path = "%s/bin/supervisord" % (sys.prefix) self.supd = SupD(self.supd_db, self.name, dirpath=self.dirpath, executable=supd_path)
class BasicSupDTests(unittest.TestCase): def setUp(self): self.name = "test_something-" + str(uuid.uuid4()) if system() == "Darwin": # The standard mac tmp path is too long # for a socket self.dirpath = tempfile.mkdtemp(dir="/tmp") else: self.dirpath = tempfile.mkdtemp() self.supd_db_path = "sqlite:///" + os.path.join(self.dirpath, "sup.db") self.supd_db = SupDDB(self.supd_db_path) supd_path = "%s/bin/supervisord" % (sys.prefix) self.supd = SupD(self.supd_db, self.name, dirpath=self.dirpath, executable=supd_path) def tearDown(self): self.supd.terminate() shutil.rmtree(self.dirpath) pass def test_ping(self): self.supd.ping() def test_get_state(self): state = self.supd.getState() self.assertEqual(state['statename'], "RUNNING") self.assertEqual(state['statecode'], 1) def test_serial_multi_request(self): state = self.supd.getState() self.assertEqual(state['statename'], "RUNNING") self.assertEqual(state['statecode'], 1) state = self.supd.getState() self.assertEqual(state['statename'], "RUNNING") self.assertEqual(state['statecode'], 1)
def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException("The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException("The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive()
class SupDPidanticFactory(PidanticFactory): driver_name = "supervisord" init_needed_keywords = [ "directory", "name", ] init_optional_keywords = [ "log", "supdexe", ] run_needed_keywords = [ "command", "process_name", "directory", ] run_optional_keywords = [ "log", "autorestart", ] def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException( "The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException( "The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException( "The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException( "The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive() def _get_next_poll_time(self): self._next_poll_time = datetime.datetime.now() + datetime.timedelta( seconds=self._poll_interval) def _is_alive(self): self._supd.ping() def get_name(self): return self._name def get_pidantic(self, **kwvals): for p in self.run_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException( "The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.run_needed_keywords and p not in self.run_optional_keywords: raise PIDanticUsageException( "The driver %s does not know the parameter %s." % (self.driver_name, p)) program_object = self._supd.create_program_db(**kwvals) pidsupd = PIDanticSupD(program_object, self._supd, log=self._log) self._watched_processes[program_object.process_name] = pidsupd return pidsupd def reload_instances(self): self._watched_processes = {} data_obj = self._supd.get_data_object() for p in data_obj.programs: pidsupd = PIDanticSupD(p, self._supd, log=self._log) self._watched_processes[p.process_name] = pidsupd self.poll() return self._watched_processes def poll(self): all_state = self._supd.get_all_state() for state in all_state: name = state['name'] if name not in self._watched_processes.keys(): self._log.log( logging.ERROR, "Supervisord is reporting an unknown process %s" % (name)) pidsupd = self._watched_processes[name] pidsupd._process_state_change(state) def terminate(self): self._supd.terminate() self._supd.delete() def _backoff(self): self._poll_interval = self._poll_interval + (self._poll_interval * .5) self._get_next_poll_time()
class SupDPidanticFactory(PidanticFactory): driver_name = "supervisord" init_needed_keywords = [ "directory", "name", ] init_optional_keywords = [ "log", "supdexe", ] run_needed_keywords = [ "command", "process_name", "directory", ] run_optional_keywords = [ "log", ] def __init__(self, **kwvals): for p in self.init_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.init_needed_keywords and p not in self.init_optional_keywords: raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p)) self._next_poll_time = datetime.datetime.now() self._poll_interval = 0.5 self._working_dir = kwvals['directory'] self._name = kwvals['name'] self._log = _set_param_or_default(kwvals, "log", logging) supd_exe = _set_param_or_default(kwvals, "supdexe", "%s/bin/supervisord" % sys.prefix) if not os.path.exists(supd_exe): raise PIDanticUsageException("No supervisord executable found") self._watched_processes = {} db_url = "sqlite:///%s/supd.db" % (self._working_dir) self._db = SupDDB(db_url) supd_db_a = self._db.get_all_supds() if len(supd_db_a) > 1: raise PIDanticUsageException("The driver %s should have at most 1 supervisord process in its table." % (self.driver_name)) if not supd_db_a: template = os.path.join(self._working_dir, "supd.template") if not os.path.exists(template): template = None self._supd = SupD(self._db, name=self._name, template=template, log=self._log, executable=supd_exe, dirpath=self._working_dir) else: if supd_db_a[0].name != self._name: raise PIDanticUsageException("The requested supd name %s is not in the db" % (self._name)) self._supd = SupD(self._db, data_object=supd_db_a[0]) self._is_alive() def _get_next_poll_time(self): self._next_poll_time = datetime.datetime.now() + datetime.timedelta(seconds=self._poll_interval) def _is_alive(self): self._supd.ping() def get_name(self): return self._name def get_pidantic(self, **kwvals): for p in self.run_needed_keywords: if p not in kwvals.keys(): raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p)) for p in kwvals.keys(): if p not in self.run_needed_keywords and p not in self.run_optional_keywords: raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p)) program_object = self._supd.create_program_db(**kwvals) pidsupd = PIDanticSupD(program_object, self._supd, log=self._log) self._watched_processes[program_object.process_name] = pidsupd return pidsupd def reload_instances(self): self._watched_processes = {} data_obj = self._supd.get_data_object() for p in data_obj.programs: pidsupd = PIDanticSupD(p, self._supd, log=self._log) self._watched_processes[p.process_name] = pidsupd self.poll() return self._watched_processes def poll(self): all_state = self._supd.get_all_state() for state in all_state: name = state['name'] if name not in self._watched_processes.keys(): self._log.log(logging.ERROR, "Supervisord is reporting an unknown process %s" % (name)) pidsupd = self._watched_processes[name] pidsupd._process_state_change(state) def terminate(self): self._supd.terminate() self._supd.delete() def _backoff(self): self._poll_interval = self._poll_interval + (self._poll_interval * .5) self._get_next_poll_time()
def test_bad_exe(self): try: supd = SupD(self.supd_db, self.name, executable=self.name, dirpath=self.dirpath) self.assertFalse(True, "should have raised an exception") except PIDanticExecutionException: pass
class BasicSupDTests(unittest.TestCase): def setUp(self): self.name = "test_something-" + str(uuid.uuid4()) self.dirpath = tempfile.mkdtemp() self.supd_db_path = "sqlite:///" + os.path.join(self.dirpath, "sup.db") self.supd_db = SupDDB(self.supd_db_path) self.supd = SupD(self.supd_db, self.name, executable="/home/bresnaha/pycharmVE/bin/supervisord", dirpath=self.dirpath) def tearDown(self): self.supd.terminate() shutil.rmtree(self.dirpath) pass def test_ping(self): self.supd.ping() def test_run_program(self): po = self.supd.create_program_db(command="/bin/true", process_name="test") self.supd.run_program(po) def test_get_state(self): state = self.supd.getState() self.assertEqual(state['statename'], "RUNNING") self.assertEqual(state['statecode'], 1) def test_serial_multi_request(self): state = self.supd.getState() self.assertEqual(state['statename'], "RUNNING") self.assertEqual(state['statecode'], 1) state = self.supd.getState() self.assertEqual(state['statename'], "RUNNING") self.assertEqual(state['statecode'], 1) def test_run_status(self): proc_name = "testcat" po = self.supd.create_program_db(command="/bin/cat", process_name=proc_name) self.supd.run_program(po) rc = self.supd.get_program_status(proc_name) self.assertEqual(rc['group'], proc_name) self.assertEqual(rc['name'], proc_name) def test_run_two_status(self): proc_name1 = "testcat" proc_name2 = "true" po = self.supd.create_program_db(command="/bin/cat", process_name=proc_name1) self.supd.run_program(po) po = self.supd.create_program_db(command="/bin/true", process_name=proc_name2) self.supd.run_program(po) rc = self.supd.get_program_status(proc_name1) self.assertEqual(rc['group'], proc_name1) self.assertEqual(rc['name'], proc_name1) rc = self.supd.get_program_status(proc_name2) self.assertEqual(rc['group'], proc_name2) self.assertEqual(rc['name'], proc_name2) states = self.supd.get_all_state() self.assertEqual(len(states), 2) # find each s1 = None s2 = None for s in states: if s['name'] == proc_name1: s1 = s elif s['name'] == proc_name2: s2 = s self.assertNotEqual(s1, None) self.assertNotEqual(s2, None)
def setUp(self): self.name = "test_something-" + str(uuid.uuid4()) self.dirpath = tempfile.mkdtemp() self.supd_db_path = "sqlite:///" + os.path.join(self.dirpath, "sup.db") self.supd_db = SupDDB(self.supd_db_path) self.supd = SupD(self.supd_db, self.name, executable="/home/bresnaha/pycharmVE/bin/supervisord", dirpath=self.dirpath)