Beispiel #1
0
 def __init__(self, config, single_pdu=False):
     self.settings = config["daemon"]
     self.pdus = config["pdus"]
     if single_pdu:
         if single_pdu not in pdus_from_config(config):
             raise NotImplementedError
     self.single_pdu = single_pdu
     self.dbh = DBHandler(self.settings)
Beispiel #2
0
 def run_me(self):
     logging.info("Starting up the PDURunner")
     while 1:
         db = DBHandler(self.settings)
         self.get_one(db)
         db.close()
         del (db)
         time.sleep(2)
Beispiel #3
0
class PDURunner(object):
    def __init__(self, config, single_pdu=False):
        self.settings = config["daemon"]
        self.pdus = config["pdus"]
        if single_pdu:
            if single_pdu not in pdus_from_config(config):
                raise NotImplementedError
        self.single_pdu = single_pdu
        self.dbh = DBHandler(self.settings)

    def get_one(self):
        job = self.dbh.get_next_job(self.single_pdu)
        if job:
            job_id, hostname, port, request = job
            log.debug(job)
            log.info("Processing queue item: (%s %s) on hostname: %s", request,
                     port, hostname)
            self.do_job(hostname, port, request)
            self.dbh.delete_row(job_id)
        else:
            time.sleep(1)

    def driver_from_hostname(self, hostname):
        drivername = drivername_from_hostname(hostname, self.pdus)
        driver = PDUDriver.select(drivername)(hostname, self.pdus[hostname])
        return driver

    def do_job(self, hostname, port, request, delay=0):
        retries = self.settings["retries"]
        driver = False
        while retries > 0:
            try:
                driver = self.driver_from_hostname(hostname)
                return driver.handle(request, port, delay)
            except Exception as e:  # pylint: disable=broad-except
                log.warn(traceback.format_exc())
                log.warn(
                    "Failed to execute job: %s %s %s "
                    "(attempts left %i) error was %s", hostname, port, request,
                    retries, e.message)
                if driver:
                    driver._bombout()  # pylint: disable=W0212,E1101
                time.sleep(5)
                retries -= 1
        return False

    def run_me(self):
        if self.single_pdu:
            log.info("Starting a PDURunner for PDU: %s", self.single_pdu)
        else:
            log.info("Starting a PDURunner for all PDUS")
        while 1:
            self.get_one()
Beispiel #4
0
class PDURunner(object):

    def __init__(self, config, single_pdu=False):
        self.settings = config["daemon"]
        self.pdus = config["pdus"]
        if single_pdu:
            if single_pdu not in pdus_from_config(config):
                raise NotImplementedError
        self.single_pdu = single_pdu
        self.dbh = DBHandler(self.settings)

    def get_one(self):
        job = self.dbh.get_next_job(self.single_pdu)
        if job:
            job_id, hostname, port, request = job
            log.debug(job)
            log.info("Processing queue item: (%s %s) on hostname: %s",
                     request, port, hostname)
            self.do_job(hostname, port, request)
            self.dbh.delete_row(job_id)

    def driver_from_hostname(self, hostname):
        drivername = drivername_from_hostname(hostname, self.pdus)
        driver = PDUDriver.select(drivername)(hostname, self.pdus[hostname])
        return driver

    def do_job(self, hostname, port, request, delay=0):
        retries = self.settings["retries"]
        driver = False
        while retries > 0:
            try:
                driver = self.driver_from_hostname(hostname)
                return driver.handle(request, port, delay)
            except Exception as e:  # pylint: disable=broad-except
                log.warn(traceback.format_exc())
                log.warn("Failed to execute job: %s %s %s "
                         "(attempts left %i) error was %s",
                         hostname, port, request, retries, e.message)
                if driver:
                    driver._bombout()  # pylint: disable=W0212,E1101
                time.sleep(5)
                retries -= 1
        return False

    def run_me(self):
        if self.single_pdu:
            log.info("Starting a PDURunner for PDU: %s", self.single_pdu)
        else:
            log.info("Starting a PDURunner for all PDUS")
        while 1:
            self.get_one()
            time.sleep(2)
Beispiel #5
0
 def __init__(self, config, single_pdu=False):
     self.settings = config["daemon"]
     self.pdus = config["pdus"]
     if single_pdu:
         if single_pdu not in pdus_from_config(config):
             raise NotImplementedError
     self.single_pdu = single_pdu
     self.dbh = DBHandler(self.settings)
Beispiel #6
0
 def queue_request(self, hostname, port, request, exectime):
     dbhandler = DBHandler(self.server.settings)
     sql = "insert into pdu_queue (hostname,port,request,exectime) " \
           "values ('%s',%i,'%s',%i)" % (hostname, port, request, exectime)
     dbhandler.do_sql(sql)
     dbhandler.close()
     del dbhandler
Beispiel #7
0
    def __init__(self, config):
        self.config = config
        settings = config["daemon"]
        listen_host = settings["hostname"]
        listen_port = settings["port"]
        log.debug("ListenerServer __init__")
        if "purge" in config:
            self.server.dbh.purge()
            sys.exit(os.EX_OK)
        log.info("listening on %s:%s", listen_host, listen_port)

        self.server = TCPServer((listen_host, listen_port), TCPRequestHandler)
        self.server.settings = settings
        self.server.config = config
        self.server.dbh = DBHandler(settings)
Beispiel #8
0
    def __init__(self, settings):
        listen_host = settings["hostname"]
        listen_port = settings["port"]

        logging.getLogger().name = "ListenerServer"
        logging.getLogger().setLevel(settings["logging_level"])
        logging.debug("ListenerServer __init__")
        logging.info("listening on %s:%s", listen_host, listen_port)

        self.server = TCPServer((listen_host, listen_port), TCPRequestHandler)
        self.server.settings = settings
        dbh = DBHandler(settings)
        dbh.create_db()
        dbh.close()
        del dbh