Exemplo n.º 1
0
    def post(self):
        try:
            request_data = self.request.body

            execution_info = external_model.to_execution_info(
                request_data.decode("UTF-8"))

            script_name = execution_info.get_script()

            config = load_config(script_name)

            if not config:
                respond_error(
                    self, 400,
                    "Script with name '" + str(script_name) + "' not found")

            working_directory = config.get_working_directory()
            if working_directory is not None:
                working_directory = file_utils.normalize_path(
                    working_directory)

            script_path = file_utils.normalize_path(config.get_script_path(),
                                                    working_directory)

            script_args = build_parameter_string(
                execution_info.get_param_values(), config)

            command = []
            command.append(script_path)
            command.extend(script_args)

            script_logger = logging.getLogger("scriptServer")
            script_logger.info("Calling script: " + " ".join(command))

            if config.is_requires_terminal():
                self.process_wrapper = execution.PtyProcessWrapper(
                    command, config.get_name(), working_directory)
            else:
                self.process_wrapper = execution.POpenProcessWrapper(
                    command, config.get_name(), working_directory)

            process_id = self.process_wrapper.get_process_id()

            running_scripts[process_id] = self.process_wrapper

            self.write(str(process_id))

        except Exception as e:
            script_logger = logging.getLogger("scriptServer")
            script_logger.exception("Error while calling the script")

            if hasattr(e, "strerror") and e.strerror:
                error_output = e.strerror
            else:
                error_output = "Unknown error occurred, contact the administrator"

            result = " ---  ERRORS  --- \n"
            result += error_output

            respond_error(self, 500, result)
Exemplo n.º 2
0
    def post(self):
        script_name = None

        try:
            request_data = self.request.body

            execution_info = external_model.to_execution_info(request_data.decode("UTF-8"))

            script_name = execution_info.script

            config = load_config(script_name)

            if not config:
                respond_error(self, 400, "Script with name '" + str(script_name) + "' not found")
                return

            working_directory = config.get_working_directory()
            if working_directory is not None:
                working_directory = file_utils.normalize_path(working_directory)

            script_logger = logging.getLogger("scriptServer")

            valid_parameters = model_helper.validate_parameters(execution_info.param_values, config)
            if not valid_parameters:
                respond_error(self, 400, 'Received invalid parameters')
                return

            script_base_command = process_utils.split_command(config.get_script_command(), working_directory)

            script_args = build_command_args(execution_info.param_values, config)
            command = script_base_command + script_args

            audit_script_args = build_command_args(
                execution_info.param_values,
                config,
                model_helper.value_to_str)
            audit_command = script_base_command + audit_script_args

            script_logger.info('Calling script: ' + ' '.join(audit_command))
            script_logger.info('User info: ' + str(get_all_audit_names(self, script_logger)))

            run_pty = config.is_requires_terminal()
            if run_pty and not pty_supported:
                script_logger.warning(
                    "Requested PTY mode, but it's not supported for this OS (" + sys.platform + "). Falling back to POpen")
                run_pty = False

            if run_pty:
                self.process_wrapper = execution_pty.PtyProcessWrapper(command,
                                                                       config.get_name(),
                                                                       working_directory,
                                                                       config,
                                                                       execution_info)
            else:
                self.process_wrapper = execution_popen.POpenProcessWrapper(command,
                                                                           config.get_name(),
                                                                           working_directory,
                                                                           config,
                                                                           execution_info)
            self.process_wrapper.start()

            process_id = self.process_wrapper.get_process_id()

            running_scripts[process_id] = self.process_wrapper

            self.write(str(process_id))

            alerts_config = self.application.alerts_config
            if alerts_config:
                self.subscribe_fail_alerter(script_name, script_logger, alerts_config)


        except Exception as e:
            script_logger = logging.getLogger("scriptServer")
            script_logger.exception("Error while calling the script")

            if hasattr(e, "strerror") and e.strerror:
                error_output = e.strerror
            else:
                error_output = "Unknown error occurred, contact the administrator"

            result = " ---  ERRORS  --- \n"
            result += error_output

            if script_name:
                script = str(script_name)
            else:
                script = "Some script"

            audit_name = get_audit_name(self, script_logger)
            send_alerts(self.application.alerts_config, script + ' NOT STARTED',
                        "Couldn't start the script " + script + ' by ' + audit_name + '.\n\n' +
                        result)

            respond_error(self, 500, result)
Exemplo n.º 3
0
    def post(self):
        script_name = None
        script_logger = logging.getLogger("scriptServer")
        try:
            request_data = self.request.body

            execution_info = external_model.to_execution_info(
                request_data.decode("UTF-8"))

            script_name = execution_info.get_script()

            config = load_config(script_name)
            if not config:
                respond_error(
                    self, 400,
                    "Script with name '" + str(script_name) + "' not found")

            script_logger.info("Config: %s" % (config.redirect))
            if not config.redirect:
                working_directory = config.get_working_directory()
                if working_directory is not None:
                    working_directory = file_utils.normalize_path(
                        working_directory)

                (script_path,
                 body_args) = self.parse_script_body(config, working_directory)

                script_args = build_parameter_string(
                    execution_info.get_param_values(), config)

                command = []
                command.append(script_path)
                command.extend(body_args)
                command.extend(script_args)

                script_logger.info("Calling script: " + " ".join(command))

                run_pty = config.is_requires_terminal()
                if run_pty and not pty_supported:
                    script_logger.warn(
                        "Requested PTY mode, but it's not supported for this OS ("
                        + sys.platform + "). Falling back to POpen")
                    run_pty = False

                if run_pty:
                    self.process_wrapper = execution_pty.PtyProcessWrapper(
                        command, config.get_name(), working_directory)
                else:
                    self.process_wrapper = execution_popen.POpenProcessWrapper(
                        command, config.get_name(), working_directory)

                process_id = self.process_wrapper.get_process_id()

                running_scripts[process_id] = self.process_wrapper

                self.write(str(process_id))

                alerts_config = self.application.alerts_config
                if alerts_config:
                    self.subscribe_fail_alerter(script_name, script_logger,
                                                alerts_config)
            else:
                IndexHandler.redirect(self,
                                      url='/audio.html',
                                      permanent="false")

        except Exception as e:
            script_logger = logging.getLogger("scriptServer")
            script_logger.exception("Error while calling the script")

            if hasattr(e, "strerror") and e.strerror:
                error_output = e.strerror
            else:
                error_output = "Unknown error occurred, contact the administrator"

            result = " ---  ERRORS  --- \n"
            result += error_output

            if script_name:
                script = str(script_name)
            else:
                script = "Some script"

            audit_name = get_audit_name(self, self.application.auth,
                                        script_logger)
            send_alerts(
                self.application.alerts_config, script + ' NOT STARTED',
                "Couldn't start the script " + script + ' by ' + audit_name +
                '.\n\n' + result)

            respond_error(self, 500, result)