def read_output_format(config):
    output_format = config.get('output_format')
    if not output_format:
        output_format = OUTPUT_FORMAT_TERMINAL

    output_format = output_format.strip().lower()
    if output_format not in OUTPUT_FORMATS:
        raise InvalidConfigException('Invalid output format, should be one of: ' + str(OUTPUT_FORMATS))

    return output_format
Beispiel #2
0
    def update_config(self, user, config, filename, uploaded_script):
        self._check_admin_access(user)

        _preprocess_incoming_config(config)

        if is_blank(filename):
            raise InvalidConfigException('Script filename should be specified')

        original_file_path = os.path.join(self._script_configs_folder,
                                          filename)

        if not os.path.exists(original_file_path):
            raise InvalidFileException(
                original_file_path,
                'Failed to find script path: ' + original_file_path)

        with open(original_file_path, 'r') as f:
            original_config_json = json.load(f)
            short_original_config = script_config.read_short(
                original_file_path, original_config_json)

        name = config['name']

        search_result = self._find_config(name)
        if (search_result is not None) and (os.path.basename(
                search_result.path) != filename):
            raise InvalidConfigException(
                'Another script found with the same name: ' + name)

        if not self._can_edit_script(user, short_original_config):
            raise ConfigNotAllowedException(
                str(user) + ' is not allowed to modify ' +
                short_original_config.name)

        self._preprocess_script_fields(config, original_config_json,
                                       uploaded_script, user)

        LOGGER.info('Updating script config "' + name + '" in ' +
                    original_file_path)
        self._save_config(config, original_file_path)
Beispiel #3
0
    def create_config(self, user, config, uploaded_script):
        self._check_admin_access(user)
        _preprocess_incoming_config(config)

        name = config['name']

        search_result = self._find_config(name)
        if search_result is not None:
            raise InvalidConfigException(
                'Another config with the same name already exists')

        self._preprocess_script_fields(config, None, uploaded_script, user)

        path = os.path.join(self._script_configs_folder,
                            _script_name_to_file_name(name))
        unique_path = file_utils.create_unique_filename(path, 100)

        LOGGER.info('Creating new script config "' + name + '" in ' +
                    unique_path)
        self._save_config(config, unique_path)
Beispiel #4
0
def _preprocess_incoming_config(config):
    name = config.get('name')
    if is_blank(name):
        raise InvalidConfigException('Script name is required')
    config['name'] = name.strip()
Beispiel #5
0
    def _preprocess_script_fields(self, config, original_config_json,
                                  uploaded_script, user):
        script_config = config.get('script')
        if not script_config:
            raise InvalidConfigException('script option is required')

        if SCRIPT_PATH_FIELD in config:
            del config[SCRIPT_PATH_FIELD]
        del config['script']

        new_path = strip(script_config.get('path'))
        if is_blank(new_path):
            raise InvalidConfigException('script.path option is required')

        config[SCRIPT_PATH_FIELD] = new_path

        mode = script_config.get('mode')
        if is_blank(mode) or mode == SCRIPT_EDIT_PATH_MODE:
            pass

        elif mode in (SCRIPT_EDIT_UPLOAD_MODE, SCRIPT_EDIT_CODE_MODE):
            if not self._authorizer.can_edit_code(user.user_id):
                raise InvalidAccessException('User ' + str(user) +
                                             ' is not allowed to edit code')

            if mode == SCRIPT_EDIT_UPLOAD_MODE:
                if uploaded_script is None:
                    raise InvalidConfigException(
                        'Uploaded script should be specified')

            if original_config_json is None:  # new config
                if mode == SCRIPT_EDIT_UPLOAD_MODE:
                    # escaped name is needed, when uploaded file and server has different OSes,
                    # thus different special characters
                    escaped_name = to_filename(uploaded_script.filename)
                    target_path = os.path.join(self._scripts_folder,
                                               escaped_name)
                else:
                    filename = os.path.basename(new_path)
                    target_path = os.path.join(
                        self._scripts_folder,
                        _escape_characters_in_filename(filename))

                script_path = file_utils.create_unique_filename(
                    target_path, 100)
                config[SCRIPT_PATH_FIELD] = script_path

            else:
                existing_code = self._load_script_code_by_config(
                    original_config_json)
                script_path = existing_code['file_path']

                if (mode == SCRIPT_EDIT_CODE_MODE
                    ) and existing_code.get('code_edit_error') is not None:
                    raise InvalidConfigException(
                        'Failed to edit code: ' +
                        existing_code.get('code_edit_error'))

                if new_path != original_config_json.get(SCRIPT_PATH_FIELD):
                    raise InvalidConfigException(
                        'script.path override is not allowed for ' + mode +
                        ' mode')

            if mode == SCRIPT_EDIT_UPLOAD_MODE:
                file_utils.write_file(script_path,
                                      uploaded_script.body,
                                      byte_content=True)
            else:
                code = script_config.get('code')
                if code is None:
                    raise InvalidConfigException(
                        'script.code should be specified')
                file_utils.write_file(script_path, code)

            file_utils.make_executable(script_path)

        else:
            raise InvalidConfigException('Unsupported mode: ' + mode)