Ejemplo n.º 1
0
    def run(self):
        # init
        self.sw = StateWorker(config=self.config)

        sw = self.sw
        haltfile = self.haltfile
        pidfile = self.pidfile

        # terminating process
        def handler(signum=None, frame=None):
            utils.log("WARNING",
                      "Signal handler called with signal %s" % signum,
                      ('handler', 'OpsAgentRunner'))
            father = False
            try:
                fd = file(haltfile, 'r')
                halt = fd.read().strip()
                fd.close()
                fd = file(pidfile, 'r')
                if int(fd.read().strip()) == int(os.getpid()):
                    father = True
                fd.close()
            except IOError:
                halt = None
            except Exception as e:
                utils.log("WARNING",
                          "Unexpected error, forcing quit: '%s'." % (e),
                          ('handler', 'OpsAgentRunner'))
                halt = None
            if halt == "wait":
                utils.log("WARNING", "Waiting current state end before end...",
                          ('handler', 'OpsAgentRunner'))
                sw.abort()
            elif halt == "end":
                utils.log("WARNING",
                          "Waiting current recipe end before end...",
                          ('handler', 'OpsAgentRunner'))
                sw.abort(end=True)
            elif father:
                utils.log("WARNING", "Exiting now...",
                          ('handler', 'OpsAgentRunner'))
                sw.abort(kill=True)
            else:
                utils.log("WARNING", "Not main process, exiting.",
                          ('handler', 'OpsAgentRunner'))
                sys.exit(0)

        # handle termination
        for sig in [
                signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT
        ]:
            try:
                signal.signal(sig, handler)
            except Exception:
                pass  #pass some signals if not POSIX

        # start
        self.sw.start()

        # run forever
        while self.sw and not self.sw.aborted():
            try:
                # run manager
                self.run_manager()
            except Exception as e:
                utils.log("ERROR", "Unexpected error: '%s'" % (e),
                          ('run', 'OpsAgentRunner'))
                time.sleep(0.1)
                utils.log("WARNING", "Conenction aborted, retrying ...",
                          ('run', 'OpsAgentRunner'))
            if self.sw and (not self.sw.aborted()):
                time.sleep(WAIT_RECONNECT)

        # end properly
        if self.sw and self.sw.is_alive():
            self.sw.join()
        self.sw = None
Ejemplo n.º 2
0
class OpsAgentRunner(Daemon):
    def run_manager(self):
        utils.log("DEBUG", "Creating Network Manager ...",
                  ('run_manager', 'OpsAgentRunner'))
        manager = Manager(url=self.config['userdata']['ws_uri'],
                          config=self.config,
                          statesworker=self.sw)
        utils.log("DEBUG", "Network Manager created.",
                  ('run_manager', 'OpsAgentRunner'))
        try:
            utils.log("DEBUG", "Connecting manager to backend.",
                      ('run_manager', 'OpsAgentRunner'))
            manager.connect()
            utils.log("DEBUG", "Connection done, registering to StateWorker.",
                      ('run_manager', 'OpsAgentRunner'))
            self.sw.set_manager(manager)
            utils.log("DEBUG", "Registration done, running forever ...",
                      ('run_manager', 'OpsAgentRunner'))
            manager.run_forever()
            utils.log("DEBUG", "Network connection lost/aborted.",
                      ('run_manager', 'OpsAgentRunner'))
        except Exception as e:
            utils.log("ERROR", "Network error: '%s'" % (e),
                      ('run_manager', 'OpsAgentRunner'))
            if manager.connected():
                utils.log("INFO", "Connection not closed. Closing ...",
                          ('run_manager', 'OpsAgentRunner'))
                try:
                    manager.close()
                except Exception:
                    utils.log("DEBUG",
                              "Can't close connection (already closed?).",
                              ('run_manager', 'OpsAgentRunner'))
                utils.log("DEBUG", "Connection closed.",
                          ('run_manager', 'OpsAgentRunner'))
            else:
                utils.log("DEBUG", "Connection already closed.",
                          ('run_manager', 'OpsAgentRunner'))
        self.sw.set_manager(None)
        del manager

    def run(self):
        # init
        self.sw = StateWorker(config=self.config)

        sw = self.sw
        haltfile = self.haltfile
        pidfile = self.pidfile

        # terminating process
        def handler(signum=None, frame=None):
            utils.log("WARNING",
                      "Signal handler called with signal %s" % signum,
                      ('handler', 'OpsAgentRunner'))
            father = False
            try:
                fd = file(haltfile, 'r')
                halt = fd.read().strip()
                fd.close()
                fd = file(pidfile, 'r')
                if int(fd.read().strip()) == int(os.getpid()):
                    father = True
                fd.close()
            except IOError:
                halt = None
            except Exception as e:
                utils.log("WARNING",
                          "Unexpected error, forcing quit: '%s'." % (e),
                          ('handler', 'OpsAgentRunner'))
                halt = None
            if halt == "wait":
                utils.log("WARNING", "Waiting current state end before end...",
                          ('handler', 'OpsAgentRunner'))
                sw.abort()
            elif halt == "end":
                utils.log("WARNING",
                          "Waiting current recipe end before end...",
                          ('handler', 'OpsAgentRunner'))
                sw.abort(end=True)
            elif father:
                utils.log("WARNING", "Exiting now...",
                          ('handler', 'OpsAgentRunner'))
                sw.abort(kill=True)
            else:
                utils.log("WARNING", "Not main process, exiting.",
                          ('handler', 'OpsAgentRunner'))
                sys.exit(0)

        # handle termination
        for sig in [
                signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT
        ]:
            try:
                signal.signal(sig, handler)
            except Exception:
                pass  #pass some signals if not POSIX

        # start
        self.sw.start()

        # run forever
        while self.sw and not self.sw.aborted():
            try:
                # run manager
                self.run_manager()
            except Exception as e:
                utils.log("ERROR", "Unexpected error: '%s'" % (e),
                          ('run', 'OpsAgentRunner'))
                time.sleep(0.1)
                utils.log("WARNING", "Conenction aborted, retrying ...",
                          ('run', 'OpsAgentRunner'))
            if self.sw and (not self.sw.aborted()):
                time.sleep(WAIT_RECONNECT)

        # end properly
        if self.sw and self.sw.is_alive():
            self.sw.join()
        self.sw = None
Ejemplo n.º 3
0
class OpsAgentRunner(Daemon):
    def run_manager(self):
        utils.log("DEBUG", "Creating Network Manager ...",('run_manager','OpsAgentRunner'))
        manager = Manager(url=self.config['userdata']['ws_uri'], config=self.config, statesworker=self.sw)
        utils.log("DEBUG", "Network Manager created.",('run_manager','OpsAgentRunner'))
        try:
            utils.log("DEBUG", "Connecting manager to backend.",('run_manager','OpsAgentRunner'))
            manager.connect()
            utils.log("DEBUG", "Connection done, registering to StateWorker.",('run_manager','OpsAgentRunner'))
            self.sw.set_manager(manager)
            utils.log("DEBUG", "Registration done, running forever ...",('run_manager','OpsAgentRunner'))
            manager.run_forever()
            utils.log("DEBUG", "Network connection lost/aborted.",('run_manager','OpsAgentRunner'))
        except Exception as e:
            utils.log("ERROR", "Network error: '%s'"%(e),('run_manager','OpsAgentRunner'))
            if manager.connected():
                utils.log("INFO", "Connection not closed. Closing ...",('run_manager','OpsAgentRunner'))
                try:
                    manager.close()
                except Exception:
                    utils.log("DEBUG", "Can't close connection (already closed?).",('run_manager','OpsAgentRunner'))
                utils.log("DEBUG", "Connection closed.",('run_manager','OpsAgentRunner'))
            else:
                utils.log("DEBUG", "Connection already closed.",('run_manager','OpsAgentRunner'))
        self.sw.set_manager(None)
        del manager

    def run(self):
        # init
        self.sw = StateWorker(config=self.config)

        sw = self.sw
        haltfile = self.haltfile
        pidfile = self.pidfile

        # terminating process
        def handler(signum=None, frame=None):
            utils.log("WARNING", "Signal handler called with signal %s"%signum,('handler','OpsAgentRunner'))
            father = False
            try:
                fd = file(haltfile,'r')
                halt = fd.read().strip()
                fd.close()
                fd = file(pidfile,'r')
                if int(fd.read().strip()) == int(os.getpid()):
                    father = True
                fd.close()
            except IOError:
                halt = None
            except Exception as e:
                utils.log("WARNING", "Unexpected error, forcing quit: '%s'."%(e),('handler','OpsAgentRunner'))
                halt = None
            if halt == "wait":
                utils.log("WARNING", "Waiting current state end before end...",('handler','OpsAgentRunner'))
                sw.abort()
            elif halt == "end":
                utils.log("WARNING", "Waiting current recipe end before end...",('handler','OpsAgentRunner'))
                sw.abort(end=True)
            elif father:
                utils.log("WARNING", "Exiting now...",('handler','OpsAgentRunner'))
                sw.abort(kill=True)
            else:
                utils.log("WARNING", "Not main process, exiting.",('handler','OpsAgentRunner'))
                sys.exit(0)

        # handle termination
        for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
            try: signal.signal(sig, handler)
            except Exception: pass #pass some signals if not POSIX
            time.sleep(0.1)

        # start
        self.sw.start()

        # run forever
        while self.sw and not self.sw.aborted():
            try:
                # run manager
                self.run_manager()
            except Exception as e:
                utils.log("ERROR", "Unexpected error: '%s'"%(e),('run','OpsAgentRunner'))
                time.sleep(0.1)
                utils.log("WARNING", "Conenction aborted, retrying ...",('run','OpsAgentRunner'))
            if self.sw and (not self.sw.aborted()):
                time.sleep(WAIT_RECONNECT)

        # end properly
        if self.sw and self.sw.is_alive():
            self.sw.join()
        self.sw = None
Ejemplo n.º 4
0
    def run(self):
        # init
        self.sw = StateWorker(config=self.config)

        sw = self.sw
        haltfile = self.haltfile
        pidfile = self.pidfile

        # terminating process
        def handler(signum=None, frame=None):
            utils.log("WARNING", "Signal handler called with signal %s"%signum,('handler','OpsAgentRunner'))
            father = False
            try:
                fd = file(haltfile,'r')
                halt = fd.read().strip()
                fd.close()
                fd = file(pidfile,'r')
                if int(fd.read().strip()) == int(os.getpid()):
                    father = True
                fd.close()
            except IOError:
                halt = None
            except Exception as e:
                utils.log("WARNING", "Unexpected error, forcing quit: '%s'."%(e),('handler','OpsAgentRunner'))
                halt = None
            if halt == "wait":
                utils.log("WARNING", "Waiting current state end before end...",('handler','OpsAgentRunner'))
                sw.abort()
            elif halt == "end":
                utils.log("WARNING", "Waiting current recipe end before end...",('handler','OpsAgentRunner'))
                sw.abort(end=True)
            elif father:
                utils.log("WARNING", "Exiting now...",('handler','OpsAgentRunner'))
                sw.abort(kill=True)
            else:
                utils.log("WARNING", "Not main process, exiting.",('handler','OpsAgentRunner'))
                sys.exit(0)

        # handle termination
        for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
            try: signal.signal(sig, handler)
            except Exception: pass #pass some signals if not POSIX
            time.sleep(0.1)

        # start
        self.sw.start()

        # run forever
        while self.sw and not self.sw.aborted():
            try:
                # run manager
                self.run_manager()
            except Exception as e:
                utils.log("ERROR", "Unexpected error: '%s'"%(e),('run','OpsAgentRunner'))
                time.sleep(0.1)
                utils.log("WARNING", "Conenction aborted, retrying ...",('run','OpsAgentRunner'))
            if self.sw and (not self.sw.aborted()):
                time.sleep(WAIT_RECONNECT)

        # end properly
        if self.sw and self.sw.is_alive():
            self.sw.join()
        self.sw = None
Ejemplo n.º 5
0
def run():
    scenarios = os.listdir("scenarios")
    scenarios.sort()

    c = Config(os.path.join(os.path.dirname(os.path.realpath("opsagent.conf")),"opsagent.conf"))
    c.parse_file()
    config = c.getConfig()
    config['runtime']['config_path'] = os.path.join(os.path.dirname(os.path.realpath("opsagent.conf")),"opsagent.conf")
    sw = StateWorker(config=config)

    for sf in scenarios:
        print "--- Test file: %s"%(os.path.join("scenarios",sf))
        with open(os.path.join("scenarios",sf)) as f:
            content = f.read()
        if not content:
            print "Error reading file: %s"%os.path.join("scenarios",sf)
            return -1
        try:
            states = json.loads(content)
        except Exception as e:
            print "Error loading json, file %s: %s"%(os.path.join("scenarios",sf),e)
            return -1
        if not states:
            print "Error loading json (empty states), file: %s"%os.path.join("scenarios",sf)
            return -1
        sw.reset_status()
        try:
            sw.load(version=sf,states=states['component']['init']['state'])
        except Exception as e:
            print "Error loading states, file %s: %s"%(os.path.join("scenarios",sf),e)
            return -1
        try:
            sw.load_modules()
        except Exception as e:
            print "Error loading modules, file %s: %s"%(os.path.join("scenarios",sf),e)
            return -1
        try:
            while (sw.get_status() < len(sw.get_states())):
                (result,comment,out_log) = sw.run_state()
                print "result = '%s'"%result
                print "comment = '%s'"%comment
                print "out_log = '%s'"%out_log
                if not result:
                    print "State #%s failed, file %s"%(sw.get_status(),os.path.join("scenarios",sf))
                    return -1
                else:
                    print "State #%s succeed"%(sw.get_status())
                sw.inc_status()
        except Exception as e:
            print "Error executing state, file %s: %s"%(os.path.join("scenarios",sf),e)
            return -1
        print "--- File tested: %s"%(os.path.join("scenarios",sf))
    return 0