Esempio n. 1
0
    def test_raises_unhandled_error(self):
        path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, path)

        with self.assertRaises(IOError):
            AgentUUID.load(path)
Esempio n. 2
0
 def test_load(self):
     data = uuid4()
     path = self.create_file(str(data))
     self.assertEqual(AgentUUID.load(path), data)
Esempio n. 3
0
 def test_load_from_missing_path(self):
     path = self.create_file()
     os.remove(path)
     self.assertIsNone(AgentUUID.load(path))
Esempio n. 4
0
    def __call__(self):
        logger.debug("Parsing command line arguments")
        self.args = self.parser.parse_args()

        # No daemon mode must be set with --pdb-on-unhanded, without this
        # you could end up with a blocking agent and no way of knowing
        # about it.
        if not self.args.no_daemon and self.args.pdb_on_unhandled:
            self.parser.error(
                "You cannot set --pdb-on-unhandled without --no-daemon")

        if self.args.pdb_on_unhandled:
            Observer.PDB_ON_UNHANDLED_ERROR = True

            def excepthook(exctype, value, traceback):
                pdb.set_trace()

            sys.excepthook = excepthook

        if not config["master"] and self.args.target_name == "start":
            self.parser.error(
                "--master must be provided (ex. "
                "'pyfarm-agent --master=foobar start')")

        # if we're on windows, produce some warnings about
        # flags which are not supported
        if WINDOWS and self.args.uid:
            logger.warning("--uid is not currently supported on Windows")

        if WINDOWS and self.args.gid:
            logger.warning("--gid is not currently supported on Windows")

        if WINDOWS and self.args.no_daemon:
            logger.warning("--no-daemon is not currently supported on Windows")

        if self.args.agent_id is None:
            agent_id = AgentUUID.load(self.args.agent_id_file)

            # No agent id saved, generate one then try to save it.  If we
            # can't then an error will be raised when AgentUUID.save is called.
            if agent_id is None:
                agent_id = AgentUUID.generate()
                AgentUUID.save(agent_id, self.args.agent_id_file)

            self.args.agent_id = agent_id

        # A custom --agent-id was provided, warn if it varies from one
        # we load from disk.  We won't try to save it however because
        # that could cause conflicts if someone is using --agent-id
        # and trying to run multiple agents.
        else:
            saved_agent_id = AgentUUID.load(self.args.agent_id_file)
            if (saved_agent_id is not None
                    and saved_agent_id != self.args.agent_id):
                logger.warning(
                    "Custom agent ID has been provided by --agent-id")

        config["agent_id"] = self.args.agent_id

        if self.args.target_name == "start":
            # update configuration with values from the command line
            config_flags = {
                "state": self.args.state,
                "pids": {
                    "parent": os.getpid()}}

            config.update(config_flags)

        return_code = self.args.target_func()

        # If a specific return code is provided then use it
        # directly in sys.exit
        if isinstance(return_code, INTEGER_TYPES):
            sys.exit(return_code)