Ejemplo n.º 1
0
    def run_hook(self, hook_type):
        """ Hook Type. Runs the commands defined by the hook """
        commands = self._get_hook(hook_type)
        if commands is None:
            return commands
        if type(commands) == str:
            commands = [commands]

        for command in commands:
            if self.config.local_development or self.config.dryrun:
                logging.warning(
                    "Hook not run due to --dry-run: {}".format(command))
                continue
            else:
                logging.info("Running {} hook...".format(hook_type))

            try:
                result = call(command,
                              shell=True,
                              executable="/bin/bash",
                              path=self.config.course_base_directory)
            except Exception as error:
                logging.error(error)
                raise ReckonerCommandException(
                    "Uncaught exception while running hook "
                    "'{}'".format(command))

            logging.info("Ran Hook: '{}'".format(result.command_string))
            _output_level = logging.INFO  # The level to log the command output

            # HACK We're not relying opon the truthiness of this object due to
            # the inability to mock is well in code
            # Python 3 may have better mocking functionality or we should
            # refactor to leverage more method on the function for .succeeded()
            # or something similar.
            if result.exitcode == 0:
                logging.info("{} hook ran successfully".format(hook_type))
            else:
                logging.error("{} hook failed to run".format(hook_type))
                logging.error("Returned exit code: {}".format(result.exitcode))
                # Override message level response to bubble up error visibility
                _output_level = logging.ERROR
            # only print stdout if there is content
            if result.stdout:
                logging.log(_output_level,
                            "Returned stdout: {}".format(result.stdout))
            # only print stderr if there is content
            if result.stderr:
                logging.log(_output_level,
                            "Returned stderr: {}".format(result.stderr))
Ejemplo n.º 2
0
    def run_hook(self, hook_type):
        """ Hook Type. Runs the commands defined by the hook """
        coms = self.__get_hook(hook_type)
        if coms is None:
            return coms
        if type(coms) == str:
            coms = [coms]

        for com in coms:
            if self.config.local_development or self.config.dryrun:
                logging.info("Hook not run due to --dry-run: {}".format(com))
                continue
            else:
                logging.info("Running {} hook.".format(hook_type))

            try:
                r = call(com, shell=True, executable="/bin/bash")
                logging.debug(r)
            except ReckonerCommandException, e:
                logging.error("{} hook failed to run".format(hook_type))
                logging.debug('Hook stderr {}'.format(e.stderr))
                raise e
Ejemplo n.º 3
0
 def current_context(self):
     """ Returns the current cluster context """
     args = ['kubectl', 'config', 'current-context']
     r = call(args)
     return r.stdout.strip()
Ejemplo n.º 4
0
    def run_hook(self, hook_type):
        """ Hook Type. Runs the commands defined by the hook """
        commands = self._get_hook(hook_type)
        if commands is None:
            return commands
        if type(commands) == str:
            commands = [commands]

        for command in commands:
            if self.config.local_development or self.config.dryrun:
                logging.warning(
                    "Hook not run due to --dry-run: {}".format(command))
                continue
            else:
                logging.info("Running {} hook...".format(hook_type))

            try:
                result = call(command,
                              shell=True,
                              executable="/bin/bash",
                              path=self.config.course_base_directory)
            except Exception as error:
                # NOTE This block is only used when we cannot send the call or
                #      have other unexpected errors running the command.
                #      The call()->Response should pass a Response object back
                #      even when the exit code != 0.
                logging.error("Critical Error running the command hook.")
                logging.error(error)
                raise ReckonerCommandException(
                    "Uncaught exception while running hook "
                    "'{}'".format(command))

            command_successful = result.exitcode == 0

            logging.info("Ran Hook: '{}'".format(result.command_string))
            _output_level = logging.INFO  # The level to log the command output

            if command_successful:
                logging.info("{} hook ran successfully".format(hook_type))
            else:
                logging.error("{} hook failed to run".format(hook_type))
                logging.error("Returned exit code: {}".format(result.exitcode))
                # Override message level response to bubble up error visibility
                _output_level = logging.ERROR

            # only print stdout if there is content
            if result.stdout:
                logging.log(_output_level,
                            "Returned stdout: {}".format(result.stdout))
            # only print stderr if there is content
            if result.stderr:
                logging.log(_output_level,
                            "Returned stderr: {}".format(result.stderr))

            # Always raise an error after failures
            if not command_successful:
                raise ReckonerCommandException(
                    "Hook ({}) failed to run".format(result.command_string),
                    stdout=result.stdout,
                    stderr=result.stderr,
                )