Esempio n. 1
0
    def post(self, **kwargs):
        """
        Upload a plugin
        """
        storage_manager = get_storage_manager()
        is_caravan = False
        installed_plugins = []
        get_resource_manager().assert_no_snapshot_creation_running_or_queued()
        try:
            plugins, code = UploadedCaravanManager().receive_uploaded_data(
                **kwargs)
            is_caravan = True
        except UploadedCaravanManager.InvalidCaravanException:
            plugin, code = UploadedPluginsManager().receive_uploaded_data(
                str(uuid4()),
                **kwargs
            )
            plugins = [plugin]

        if is_caravan:
            storage_plugins = storage_manager.list(
                models.Plugin,
                filters={'id': [p.id for p in installed_plugins]})

            return ListResponse(items=storage_plugins.items,
                                metadata=storage_plugins.metadata), code
        else:
            return plugins[0], code
Esempio n. 2
0
 def _validate_secret_modification_permitted(self, secret):
     get_resource_manager().validate_modification_permitted(secret)
     if secret.is_hidden_value and \
             not self._is_hidden_value_permitted(secret):
         raise ForbiddenError(
             'User `{0}` is not permitted to modify the hidden value '
             'secret `{1}`'.format(current_user.username, secret.key))
Esempio n. 3
0
    def post(self, plugin_id, **kwargs):
        """Force plugin installation on the given managers or agents.

        This method is for internal use only.
        """
        sm = get_storage_manager()
        action_dict = rest_utils.get_json_and_verify_params({
            'action': {
                'type': text_type
            },
        })
        plugin = sm.get(models.Plugin, plugin_id)
        if action_dict.get('action') == 'install':
            install_dict = rest_utils.get_json_and_verify_params({
                'managers': {
                    'type': list,
                    'optional': True
                },
                'agents': {
                    'type': list,
                    'optional': True
                },
            })
            get_resource_manager().install_plugin(
                plugin,
                manager_names=install_dict.get('managers'),
                agent_names=install_dict.get('agents'),
            )
            return get_resource_manager().install_plugin(plugin)
        else:
            raise manager_exceptions.ManagerException(
                400, 'Unknown action: {0}'.format(action_dict.get('action')))
Esempio n. 4
0
    def put(self, key, **kwargs):
        """
        Create a new secret or update an existing secret if the flag
        update_if_exists is set to true
        """
        secret_params = self._get_secret_params(key)
        encrypted_value = self._encrypt_secret_value(secret_params['value'])
        sm = get_storage_manager()
        timestamp = utils.get_formatted_timestamp()

        try:
            new_secret = models.Secret(
                id=key,
                value=encrypted_value,
                created_at=timestamp,
                updated_at=timestamp,
                visibility=secret_params['visibility'],
                is_hidden_value=secret_params['is_hidden_value'])
            return sm.put(new_secret)
        except ConflictError:
            secret = sm.get(models.Secret, key)
            if secret and secret_params['update_if_exists']:
                get_resource_manager().validate_modification_permitted(secret)
                secret.value = encrypted_value
                secret.updated_at = timestamp
                return sm.update(secret)
            raise
Esempio n. 5
0
 def delete(self, blueprint_id, **kwargs):
     """
     Delete blueprint by id
     """
     query_args = get_args_and_verify_arguments(
         [Argument('force', type=boolean, default=False)])
     get_resource_manager().delete_blueprint(blueprint_id,
                                             force=query_args.force)
     return None, 204
Esempio n. 6
0
 def _update_visibility(self, secret):
     visibility = rest_utils.get_visibility_parameter(
         optional=True,
         valid_values=VisibilityState.STATES,
     )
     if visibility:
         get_resource_manager().validate_visibility_value(
             models.Secret, secret, visibility)
         secret.visibility = visibility
Esempio n. 7
0
 def delete(self, key):
     """
     Delete a secret
     """
     rest_utils.validate_inputs({'key': key})
     storage_manager = get_storage_manager()
     secret = storage_manager.get(models.Secret, key)
     get_resource_manager().validate_modification_permitted(secret)
     return storage_manager.delete(secret)
Esempio n. 8
0
 def delete(self, plugin_id, **kwargs):
     """
     Delete plugin by ID
     """
     request_dict = get_json_and_verify_params()
     force = verify_and_convert_bool('force',
                                     request_dict.get('force', False))
     get_resource_manager().remove_plugin(plugin_id=plugin_id, force=force)
     return None, 204
Esempio n. 9
0
 def delete(self, blueprint_id, **kwargs):
     """
     Delete blueprint by id
     """
     # Note: The current delete semantics are such that if a deployment
     # for the blueprint exists, the deletion operation will fail.
     # However, there is no handling of possible concurrency issue with
     # regard to that matter at the moment.
     get_resource_manager().delete_blueprint(blueprint_id, force=False)
     return None, 204
Esempio n. 10
0
    def patch(self, filters_model, filter_id, filtered_resource):
        """Update a filter by its ID

        This function updates the filter rules and visibility
        """
        rest_utils.validate_inputs({'filter_id': filter_id})
        if not request.json:
            raise manager_exceptions.IllegalActionError(
                'Update a filter request must include at least one parameter '
                'to update')

        request_dict = rest_utils.get_json_and_verify_params(
            {'filter_rules': {
                'type': list,
                'optional': True
            }})

        filter_rules = request_dict.get('filter_rules')
        visibility = rest_utils.get_visibility_parameter(
            optional=True, valid_values=VisibilityState.STATES)

        storage_manager = get_storage_manager()
        filter_elem = storage_manager.get(filters_model, filter_id)
        _verify_not_a_system_filter(filter_elem, 'update')
        if visibility:
            get_resource_manager().validate_visibility_value(
                filters_model, filter_elem, visibility)
            filter_elem.visibility = visibility
        if filter_rules:
            new_filter_rules = create_filter_rules_list(
                filter_rules, filtered_resource)
            new_attrs_filter_rules = _get_filter_rules_by_type(
                new_filter_rules, 'attribute')
            new_labels_filter_rules = _get_filter_rules_by_type(
                new_filter_rules, 'label')
            if new_attrs_filter_rules:
                if new_labels_filter_rules:  # Both need to be updated
                    filter_elem.value = new_filter_rules
                else:  # Only labels filter rules should be saved
                    filter_elem.value = (filter_elem.labels_filter_rules +
                                         new_filter_rules)

            elif new_labels_filter_rules:
                # Only attributes filter rules should be saved
                filter_elem.value = (filter_elem.attrs_filter_rules +
                                     new_filter_rules)

            else:  # Should not get here
                raise manager_exceptions.BadParametersError(
                    'Unknown filter rules type')

        filter_elem.updated_at = get_formatted_timestamp()

        return storage_manager.update(filter_elem)
Esempio n. 11
0
 def _update_visibility(self, secret):
     visibility = rest_utils.get_visibility_parameter(
         optional=True,
         valid_values=VisibilityState.STATES,
     )
     if visibility:
         get_resource_manager().validate_visibility_value(
             models.Secret,
             secret,
             visibility
         )
         secret.visibility = visibility
Esempio n. 12
0
    def patch(self, key):
        """
        Update an existing secret
        """
        request_dict = rest_utils.get_json_and_verify_params({'value'})
        rest_utils.validate_inputs({'key': key})

        secret = get_storage_manager().get(models.Secret, key)
        get_resource_manager().validate_modification_permitted(secret)
        secret.value = self._encrypt_secret_value(request_dict['value'])
        secret.updated_at = utils.get_formatted_timestamp()
        return get_storage_manager().update(secret)
    def _create_plugin_from_archive(self,
                                    plugin_id,
                                    archive_path,
                                    private_resource,
                                    visibility):
        plugin = self._load_plugin_package_json(archive_path)
        build_props = plugin.get('build_server_os_properties')
        plugin_info = {'package_name': plugin.get('package_name'),
                       'archive_name': plugin.get('archive_name')}
        resource_manager = get_resource_manager()
        visibility = resource_manager.get_resource_visibility(
            Plugin,
            plugin_id,
            visibility,
            private_resource,
            plugin_info
        )

        return Plugin(
            id=plugin_id,
            package_name=plugin.get('package_name'),
            package_version=plugin.get('package_version'),
            archive_name=plugin.get('archive_name'),
            package_source=plugin.get('package_source'),
            supported_platform=plugin.get('supported_platform'),
            distribution=build_props.get('distribution'),
            distribution_version=build_props.get('distribution_version'),
            distribution_release=build_props.get('distribution_release'),
            wheels=plugin.get('wheels'),
            excluded_wheels=plugin.get('excluded_wheels'),
            supported_py_versions=plugin.get('supported_python_versions'),
            uploaded_at=get_formatted_timestamp(),
            visibility=visibility
        )
Esempio n. 14
0
    def _create_plugin_from_archive(self, plugin_id, archive_path,
                                    private_resource, visibility):
        plugin = self._load_plugin_package_json(archive_path)
        build_props = plugin.get('build_server_os_properties')
        plugin_info = {
            'package_name': plugin.get('package_name'),
            'archive_name': plugin.get('archive_name')
        }
        resource_manager = get_resource_manager()
        visibility = resource_manager.get_resource_visibility(
            Plugin, plugin_id, visibility, private_resource, plugin_info)

        return Plugin(
            id=plugin_id,
            package_name=plugin.get('package_name'),
            package_version=plugin.get('package_version'),
            archive_name=plugin.get('archive_name'),
            package_source=plugin.get('package_source'),
            supported_platform=plugin.get('supported_platform'),
            distribution=build_props.get('distribution'),
            distribution_version=build_props.get('distribution_version'),
            distribution_release=build_props.get('distribution_release'),
            wheels=plugin.get('wheels'),
            excluded_wheels=plugin.get('excluded_wheels'),
            supported_py_versions=plugin.get('supported_python_versions'),
            uploaded_at=get_formatted_timestamp(),
            visibility=visibility)
Esempio n. 15
0
    def post(self, **kwargs):
        """Execute a workflow"""
        request_dict = get_json_and_verify_params(
            {'deployment_id', 'workflow_id'})

        allow_custom_parameters = verify_and_convert_bool(
            'allow_custom_parameters',
            request_dict.get('allow_custom_parameters', 'false'))
        force = verify_and_convert_bool('force',
                                        request_dict.get('force', 'false'))
        dry_run = verify_and_convert_bool('dry_run',
                                          request_dict.get('dry_run', 'false'))
        queue = verify_and_convert_bool('queue',
                                        request_dict.get('queue', 'false'))

        deployment_id = request_dict['deployment_id']
        workflow_id = request_dict['workflow_id']
        parameters = request_dict.get('parameters', None)

        if parameters is not None and parameters.__class__ is not dict:
            raise manager_exceptions.BadParametersError(
                "request body's 'parameters' field must be a dict but"
                " is of type {0}".format(parameters.__class__.__name__))

        bypass_maintenance = is_bypass_maintenance_mode()
        execution = get_resource_manager().execute_workflow(
            deployment_id,
            workflow_id,
            parameters=parameters,
            allow_custom_parameters=allow_custom_parameters,
            force=force,
            dry_run=dry_run,
            bypass_maintenance=bypass_maintenance,
            queue=queue)
        return execution, 201
Esempio n. 16
0
    def initiate_plugins_update(self, blueprint_id, force=False):
        """Creates a temporary blueprint and executes the plugins update
        workflow.
        """
        self.validate_no_active_updates_per_blueprint(blueprint_id, force)
        blueprint = self.sm.get(models.Blueprint, blueprint_id)
        temp_plan = self.get_reevaluated_plan(blueprint)

        deployments_to_update = [
            dep.id for dep in self._get_deployments_to_update(blueprint_id)
        ]
        if not deployments_to_update:
            raise PluginsUpdateError(
                "The blueprint '{0}' has no deployments to update.".format(
                    blueprint_id))

        plugins_update = self._stage_plugin_update(blueprint, force)
        plugins_update.deployments_to_update = deployments_to_update
        self.sm.update(plugins_update)

        temp_blueprint = self._create_temp_blueprint_from(blueprint, temp_plan)
        plugins_update.temp_blueprint = temp_blueprint
        plugins_update.state = STATES.UPDATING
        self.sm.update(plugins_update)

        plugins_update.execution = get_resource_manager(
            self.sm).update_plugins(plugins_update)
        plugins_update.state = STATES.EXECUTING_WORKFLOW
        return self.sm.update(plugins_update)
    def delete(self, deployment_id, **kwargs):
        """
        Delete deployment by id
        """
        args = get_args_and_verify_arguments(
            [Argument('ignore_live_nodes', type=boolean,
                      default=False),
             Argument('delete_db_mode', type=boolean,
                      default=False),
             Argument('delete_logs', type=boolean,
                      default=False)]
        )

        bypass_maintenance = is_bypass_maintenance_mode()
        deployment = get_resource_manager().delete_deployment(
            deployment_id,
            bypass_maintenance,
            args.ignore_live_nodes,
            args.delete_db_mode,
            args.delete_logs)

        if args.delete_db_mode:
            # Delete deployment resources from file server
            deployment_folder = os.path.join(
                config.instance.file_server_root,
                FILE_SERVER_DEPLOYMENTS_FOLDER,
                utils.current_tenant.name,
                deployment.id)
            if os.path.exists(deployment_folder):
                shutil.rmtree(deployment_folder)
        return deployment, 200
Esempio n. 18
0
    def _get_secret_params(self, key):
        rest_utils.validate_inputs({'key': key})
        request_dict = rest_utils.get_json_and_verify_params({
            'value': {'type': unicode}
        })
        update_if_exists = rest_utils.verify_and_convert_bool(
            'update_if_exists',
            request_dict.get('update_if_exists', False),
        )
        is_hidden_value = rest_utils.verify_and_convert_bool(
            'is_hidden_value',
            request_dict.get('is_hidden_value', False),
        )
        visibility_param = rest_utils.get_visibility_parameter(
            optional=True,
            valid_values=VisibilityState.STATES,
        )
        visibility = get_resource_manager().get_resource_visibility(
            models.Secret,
            key,
            visibility_param
        )

        secret_params = {
            'value': request_dict['value'],
            'update_if_exists': update_if_exists,
            'visibility': visibility,
            'is_hidden_value': is_hidden_value
        }
        return secret_params
Esempio n. 19
0
    def receive_uploaded_data(self, data_id=None, **kwargs):
        blueprint_url = None
        visibility = kwargs.get(_VISIBILITY, None)
        labels = kwargs.get('labels', None)
        override_failed_blueprint = kwargs.get('override_failed', False)

        args = get_args_and_verify_arguments([
            Argument('private_resource', type=boolean),
            Argument('application_file_name', default='')
        ])

        # Handle importing blueprint through url
        if self._get_data_url_key() in request.args:
            if request.data or \
                    'Transfer-Encoding' in request.headers or \
                    'blueprint_archive' in request.files:
                raise manager_exceptions.BadParametersError(
                    "Can pass {0} as only one of: URL via query parameters, "
                    "request body, multi-form or "
                    "chunked.".format(self._get_kind()))
            blueprint_url = request.args[self._get_data_url_key()]

        visibility = get_resource_manager().get_resource_visibility(
            Blueprint, data_id, visibility, args.private_resource)

        new_blueprint = self._prepare_and_process_doc(
            data_id,
            visibility,
            blueprint_url,
            application_file_name=args.application_file_name,
            override_failed_blueprint=override_failed_blueprint,
            labels=labels)
        return new_blueprint, 201
Esempio n. 20
0
    def put(self, snapshot_id):
        rest_utils.validate_inputs({'snapshot_id': snapshot_id})
        request_dict = rest_utils.get_json_and_verify_params()
        include_metrics = rest_utils.verify_and_convert_bool(
            'include_metrics',
            request_dict.get('include_metrics', 'false')
        )
        include_credentials = rest_utils.verify_and_convert_bool(
            'include_credentials',
            request_dict.get('include_credentials', 'true')
        )
        include_logs = rest_utils.verify_and_convert_bool(
            'include_logs',
            request_dict.get('include_logs', 'true')
        )
        include_events = rest_utils.verify_and_convert_bool(
            'include_events',
            request_dict.get('include_events', 'true')
        )
        queue = rest_utils.verify_and_convert_bool(
            'queue',
            request_dict.get('queue', 'false')
        )
        execution = get_resource_manager().create_snapshot(
            snapshot_id,
            include_metrics,
            include_credentials,
            include_logs,
            include_events,
            True,
            queue
        )

        return execution, 201
Esempio n. 21
0
 def patch(self, plugin_id):
     """
     Set the plugin's visibility
     """
     visibility = rest_utils.get_visibility_parameter()
     return get_resource_manager().set_visibility(models.Plugin, plugin_id,
                                                  visibility)
Esempio n. 22
0
 def put(self, operation_id, **kwargs):
     params = get_json_and_verify_params({
         'name': {
             'type': unicode,
             'required': True
         },
         'graph_id': {
             'type': unicode,
             'required': True
         },
         'dependencies': {
             'type': list,
             'required': True
         },
         'parameters': {
             'type': dict
         },
         'type': {
             'type': unicode
         }
     })
     operation = get_resource_manager().create_operation(
         operation_id,
         name=params['name'],
         graph_id=params['graph_id'],
         dependencies=params['dependencies'],
         type=params['type'],
         parameters=params['parameters'])
     return operation, 201
Esempio n. 23
0
 def patch(self, deployment_id):
     """
     Set the deployment's visibility
     """
     visibility = rest_utils.get_visibility_parameter()
     return get_resource_manager().set_deployment_visibility(
         deployment_id, visibility)
Esempio n. 24
0
 def put(self, deployment_id, **kwargs):
     """
     Create a deployment
     """
     rest_utils.validate_inputs({'deployment_id': deployment_id})
     request_schema = self.create_request_schema()
     request_dict = rest_utils.get_json_and_verify_params(request_schema)
     blueprint_id = request_dict['blueprint_id']
     bypass_maintenance = is_bypass_maintenance_mode()
     args = rest_utils.get_args_and_verify_arguments(
         [Argument('private_resource', type=boolean)]
     )
     visibility = rest_utils.get_visibility_parameter(
         optional=True,
         valid_values=VisibilityState.STATES
     )
     deployment = get_resource_manager().create_deployment(
         blueprint_id,
         deployment_id,
         inputs=request_dict.get('inputs', {}),
         bypass_maintenance=bypass_maintenance,
         private_resource=args.private_resource,
         visibility=visibility,
         skip_plugins_validation=self.get_skip_plugin_validation_flag(
             request_dict),
         site_name=_get_site_name(request_dict),
         runtime_only_evaluation=request_dict.get(
             'runtime_only_evaluation', False)
     )
     return deployment, 201
Esempio n. 25
0
    def _prepare_and_submit_blueprint(cls, file_server_root, app_dir,
                                      blueprint_id, visibility):
        args = get_args_and_verify_arguments([
            Argument('private_resource', type=boolean),
            Argument('visibility'),
            Argument('application_file_name', default='')
        ])

        app_file_name = cls._extract_application_file(
            file_server_root, app_dir, args.application_file_name)

        # add to blueprints manager (will also dsl_parse it)
        try:
            blueprint = get_resource_manager().publish_blueprint(
                app_dir, app_file_name, file_server_root, blueprint_id,
                args.private_resource, visibility)

            # moving the app directory in the file server to be under a
            # directory named after the blueprint id
            tenant_dir = os.path.join(file_server_root,
                                      FILE_SERVER_BLUEPRINTS_FOLDER,
                                      current_tenant.name)
            mkdirs(tenant_dir)
            shutil.move(os.path.join(file_server_root, app_dir),
                        os.path.join(tenant_dir, blueprint.id))
            cls._process_plugins(file_server_root, blueprint.id)
            return blueprint
        except manager_exceptions.DslParseException, ex:
            shutil.rmtree(os.path.join(file_server_root, app_dir))
            raise manager_exceptions.InvalidBlueprintError(
                'Invalid blueprint - {0}'.format(ex.message))
Esempio n. 26
0
 def delete(self, vnf_pkg_id, **kwargs):
     """
     Delete a vnf_pkg
     """
     current_app.logger.info('delete vnf_pkg by id, vnf_pkg_id=' + vnf_pkg_id)
     vnf_pkg = get_resource_manager().delete_vnf_pkg(vnf_pkg_id)
     return vnf_pkg, 200
Esempio n. 27
0
 def _prepare_and_process_doc(self, data_id, file_server_root,
                              archive_target_path, **kwargs):
     args = self._get_args()
     return get_resource_manager().create_snapshot_model(
         data_id,
         status=SnapshotState.UPLOADED,
         private_resource=args.private_resource), None
Esempio n. 28
0
    def _prepare_and_process_doc(self, data_id, blueprint_url,
                                 application_file_name):
        # Put a temporary blueprint entry in DB
        rm = get_resource_manager()
        now = get_formatted_timestamp()
        temp_blueprint = rm.sm.put(
            Blueprint(plan=None,
                      id=data_id,
                      description=None,
                      created_at=now,
                      updated_at=now,
                      main_file_name=None,
                      visibility=None,
                      state=BlueprintUploadState.VALIDATING))

        if not blueprint_url:
            self.upload_archive_to_file_server(data_id)

        try:
            temp_blueprint.upload_execution = rm.upload_blueprint(
                data_id,
                application_file_name,
                blueprint_url,
                config.instance.file_server_root,  # for the import resolver
                validate_only=True,
            )
        except manager_exceptions.ExistingRunningExecutionError:
            rm.sm.delete(temp_blueprint)
            self.cleanup_blueprint_archive_from_file_server(
                data_id, current_tenant.name)
            raise
Esempio n. 29
0
    def delete(self, blueprint_id, **kwargs):
        """
        Delete blueprint by id
        """
        # Note: The current delete semantics are such that if a deployment
        # for the blueprint exists, the deletion operation will fail.
        # However, there is no handling of possible concurrency issue with
        # regard to that matter at the moment.
        blueprint = get_resource_manager().delete_blueprint(blueprint_id)

        # Delete blueprint resources from file server
        blueprint_folder = os.path.join(
            config.instance.file_server_root,
            FILE_SERVER_BLUEPRINTS_FOLDER,
            utils.current_tenant.name,
            blueprint.id)
        shutil.rmtree(blueprint_folder)
        uploaded_blueprint_folder = os.path.join(
            config.instance.file_server_root,
            FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER,
            utils.current_tenant.name,
            blueprint.id)
        shutil.rmtree(uploaded_blueprint_folder)

        return blueprint, 200
Esempio n. 30
0
 def patch(self, blueprint_id):
     """
     Set the blueprint's visibility
     """
     visibility = rest_utils.get_visibility_parameter()
     return get_resource_manager().set_visibility(models.Blueprint,
                                                  blueprint_id, visibility)
 def put(self, deployment_id, **kwargs):
     """
     Create a deployment
     """
     rest_utils.validate_inputs({'deployment_id': deployment_id})
     request_schema = self.create_request_schema()
     request_dict = rest_utils.get_json_and_verify_params(request_schema)
     blueprint_id = request_dict['blueprint_id']
     bypass_maintenance = is_bypass_maintenance_mode()
     args = rest_utils.get_args_and_verify_arguments(
         [Argument('private_resource', type=boolean)]
     )
     visibility = rest_utils.get_visibility_parameter(
         optional=True,
         valid_values=VisibilityState.STATES
     )
     deployment = get_resource_manager().create_deployment(
         blueprint_id,
         deployment_id,
         inputs=request_dict.get('inputs', {}),
         bypass_maintenance=bypass_maintenance,
         private_resource=args.private_resource,
         visibility=visibility,
         skip_plugins_validation=self.get_skip_plugin_validation_flag(
             request_dict),
         site_name=_get_site_name(request_dict)
     )
     return deployment, 201
Esempio n. 32
0
 def patch(self, key):
     """
     Set the secret's visibility
     """
     visibility = rest_utils.get_visibility_parameter()
     return get_resource_manager().set_visibility(models.Secret, key,
                                                  visibility)
Esempio n. 33
0
 def post(self, snapshot_id):
     request_dict = rest_utils.get_json_and_verify_params(
         {'recreate_deployments_envs'})
     recreate_deployments_envs = rest_utils.verify_and_convert_bool(
         'recreate_deployments_envs',
         request_dict['recreate_deployments_envs'])
     force = rest_utils.verify_and_convert_bool('force',
                                                request_dict['force'])
     restore_certificates = rest_utils.verify_and_convert_bool(
         'restore_certificates',
         request_dict.get('restore_certificates', 'false'))
     no_reboot = rest_utils.verify_and_convert_bool(
         'no_reboot', request_dict.get('no_reboot', 'false'))
     ignore_plugin_failure = \
         rest_utils.verify_and_convert_bool(
             'ignore_plugin_failure',
             request_dict.get('ignore_plugin_failure', 'false')
         )
     if no_reboot and not restore_certificates:
         raise manager_exceptions.BadParametersError(
             '`no_reboot` is only relevant when `restore_certificates` is '
             'activated')
     default_timeout_sec = 300
     request_timeout = request_dict.get('timeout', default_timeout_sec)
     timeout = rest_utils.convert_to_int(request_timeout)
     execution = get_resource_manager().restore_snapshot(
         snapshot_id, recreate_deployments_envs, force, True, timeout,
         restore_certificates, no_reboot, ignore_plugin_failure)
     return execution, 200
Esempio n. 34
0
    def _prepare_and_submit_blueprint(cls, file_server_root, app_dir,
                                      blueprint_id):

        args = cls._get_args()
        app_dir, app_file_name = cls._extract_application_file(
            file_server_root, app_dir, args.application_file_name)

        # add to blueprints manager (will also dsl_parse it)
        try:
            blueprint = get_resource_manager().publish_blueprint(
                app_dir,
                app_file_name,
                file_server_root,
                blueprint_id,
                private_resource=args.private_resource)

            # moving the app directory in the file server to be under a
            # directory named after the blueprint id
            tenant_dir = os.path.join(
                file_server_root, FILE_SERVER_BLUEPRINTS_FOLDER,
                current_app.config[CURRENT_TENANT_CONFIG].name)
            mkdirs(tenant_dir)
            shutil.move(os.path.join(file_server_root, app_dir),
                        os.path.join(tenant_dir, blueprint.id))
            cls._process_plugins(file_server_root, blueprint.id)
            return blueprint
        except manager_exceptions.DslParseException, ex:
            shutil.rmtree(os.path.join(file_server_root, app_dir))
            raise manager_exceptions.InvalidBlueprintError(
                'Invalid blueprint - {0}'.format(ex.message))
Esempio n. 35
0
 def patch(self, blueprint_id):
     """
     Set the blueprint's visibility to global
     """
     return get_resource_manager().set_visibility(models.Blueprint,
                                                  blueprint_id,
                                                  VisibilityState.GLOBAL)
 def patch(self, blueprint_id):
     """
     Set the blueprint's visibility
     """
     visibility = rest_utils.get_visibility_parameter()
     return get_resource_manager().set_visibility(models.Blueprint,
                                                  blueprint_id,
                                                  visibility)
Esempio n. 37
0
 def patch(self, key):
     """
     Set the secret's visibility
     """
     visibility = rest_utils.get_visibility_parameter()
     return get_resource_manager().set_visibility(models.Secret,
                                                  key,
                                                  visibility)
 def _prepare_and_process_doc(self,
                              data_id,
                              file_server_root,
                              archive_target_path,
                              **kwargs):
     return get_resource_manager().create_snapshot_model(
         data_id,
         status=SnapshotState.UPLOADED
     ), None
 def patch(self, deployment_id):
     """
     Set the deployment's visibility
     """
     visibility = rest_utils.get_visibility_parameter()
     return get_resource_manager().set_deployment_visibility(
         deployment_id,
         visibility
     )
 def patch(self, blueprint_id):
     """
     Set the blueprint's visibility to global
     """
     return get_resource_manager().set_visibility(
         models.Blueprint,
         blueprint_id,
         VisibilityState.GLOBAL
     )
Esempio n. 41
0
 def patch(self, key):
     """
     Set the secret's visibility to global
     """
     return get_resource_manager().set_global_visibility(
         models.Secret,
         key,
         VisibilityState.GLOBAL
     )
 def delete(self, blueprint_id, **kwargs):
     """
     Delete blueprint by id
     """
     query_args = get_args_and_verify_arguments(
         [Argument('force', type=boolean, default=False)])
     blueprint = get_resource_manager().delete_blueprint(
         blueprint_id,
         force=query_args.force)
     return blueprint, 200
Esempio n. 43
0
 def post(self, name):
     """
     Update an existing site
     """
     request_dict = self._validate_site_params(name)
     storage_manager = get_storage_manager()
     self._validate_new_name(request_dict, storage_manager, name)
     site = storage_manager.get(models.Site, name)
     site.name = request_dict.get('new_name', site.name)
     site.id = request_dict.get('new_name', site.id)
     site.latitude = request_dict.get('latitude', site.latitude)
     site.longitude = request_dict.get('longitude', site.longitude)
     visibility = request_dict['visibility']
     if visibility:
         get_resource_manager().validate_visibility_value(models.Site,
                                                          site,
                                                          visibility)
         site.visibility = visibility
     return storage_manager.update(site, validate_global=True)
 def delete(self, blueprint_id, **kwargs):
     """
     Delete blueprint by id
     """
     # Note: The current delete semantics are such that if a deployment
     # for the blueprint exists, the deletion operation will fail.
     # However, there is no handling of possible concurrency issue with
     # regard to that matter at the moment.
     blueprint = get_resource_manager().delete_blueprint(blueprint_id,
                                                         force=False)
     return blueprint, 200
    def post(self, execution_id, **kwargs):
        """
        Apply execution action (cancel, force-cancel) by id
        """
        request_dict = get_json_and_verify_params({'action'})
        action = request_dict['action']

        valid_actions = ['cancel', 'force-cancel', 'kill', 'resume',
                         'force-resume']

        if action not in valid_actions:
            raise manager_exceptions.BadParametersError(
                'Invalid action: {0}, Valid action values are: {1}'.format(
                    action, valid_actions))

        if action in ('resume', 'force-resume'):
            return get_resource_manager().resume_execution(
                execution_id, force=action == 'force-resume')
        return get_resource_manager().cancel_execution(
            execution_id, action == 'force-cancel', action == 'kill')
    def patch(self, execution_id, **kwargs):
        """
        Update execution status by id
        """
        request_dict = get_json_and_verify_params({'status'})

        return get_resource_manager().update_execution_status(
            execution_id,
            request_dict['status'],
            request_dict.get('error', '')
        )
 def post(self, **kwargs):
     params = get_json_and_verify_params({
         'name': {'type': unicode, 'required': True},
         'execution_id': {'type': unicode, 'required': True},
         'operations': {'required': False}
     })
     tasks_graph = get_resource_manager().create_tasks_graph(
         name=params['name'],
         execution_id=params['execution_id'],
         operations=params.get('operations', [])
     )
     return tasks_graph, 201
 def post(self, **kwargs):
     request_dict = get_json_and_verify_params({
         'deployment_id': {},
         'context': {'optional': True, 'type': dict},
         'nodes': {'optional': True, 'type': dict}
     })
     deployment_id = request_dict['deployment_id']
     context = request_dict.get('context', {})
     nodes = request_dict.get('nodes', {})
     modification = get_resource_manager(). \
         start_deployment_modification(deployment_id, nodes, context)
     return modification, 201
 def get(self, _include=None, **kwargs):
     """List executions"""
     args = get_args_and_verify_arguments(
         [Argument('deployment_id', required=False),
          Argument('include_system_workflows', type=boolean,
                   default=False)]
     )
     deployment_id_filter = ResourceManager.create_filters_dict(
         deployment_id=args.deployment_id)
     return get_resource_manager().list_executions(
         is_include_system_workflows=args.include_system_workflows,
         include=_include,
         filters=deployment_id_filter).items
Esempio n. 50
0
    def get(self, pagination=None):
        args = get_args_and_verify_arguments([
            Argument('deployment_id', required=False),
            Argument('node_ids', required=False, action='append'),
            Argument('node_instance_ids', required=False,
                     action='append'),
            Argument('install_methods', required=False,
                     action='append'),

        ])
        return get_resource_manager().list_agents(
            deployment_id=args.get('deployment_id'),
            node_ids=args.get('node_ids'),
            node_instance_ids=args.get('node_instance_ids'),
            install_method=args.get('install_methods'))
    def _prepare_and_process_doc(self,
                                 data_id,
                                 file_server_root,
                                 archive_target_path,
                                 **kwargs):

        # support previous implementation
        wagon_target_path = archive_target_path

        # handle the archive_target_path, which may be zip or wagon
        if not self._is_wagon_file(archive_target_path):
            if not zipfile.is_zipfile(archive_target_path):
                raise manager_exceptions.InvalidPluginError(
                    'input can be only a wagon or a zip file.')
            archive_name = unzip(archive_target_path,
                                 logger=current_app.logger)
            os.remove(archive_target_path)
            shutil.move(archive_name, archive_target_path)
            try:
                wagon_target_path, _ = \
                    self._verify_archive(archive_target_path)
            except RuntimeError as re:
                raise manager_exceptions.InvalidPluginError(re.message)

        args = get_args_and_verify_arguments([
            Argument('private_resource', type=boolean),
            Argument('visibility')])

        visibility = kwargs.get(_VISIBILITY, None)
        new_plugin = self._create_plugin_from_archive(data_id,
                                                      wagon_target_path,
                                                      args.private_resource,
                                                      visibility)
        filter_by_name = {'package_name': new_plugin.package_name}
        sm = get_resource_manager().sm
        plugins = sm.list(Plugin, filters=filter_by_name)

        for plugin in plugins:
            if plugin.archive_name == new_plugin.archive_name:
                raise manager_exceptions.ConflictError(
                    'a plugin archive by the name of {archive_name} already '
                    'exists for package with name {package_name} and version '
                    '{version}'.format(archive_name=new_plugin.archive_name,
                                       package_name=new_plugin.package_name,
                                       version=new_plugin.package_version))
        sm.put(new_plugin)
        return new_plugin, new_plugin.archive_name
 def put(self, operation_id, **kwargs):
     params = get_json_and_verify_params({
         'name': {'type': unicode, 'required': True},
         'graph_id': {'type': unicode, 'required': True},
         'dependencies': {'type': list, 'required': True},
         'parameters': {'type': dict},
         'type': {'type': unicode}
     })
     operation = get_resource_manager().create_operation(
         operation_id,
         name=params['name'],
         graph_id=params['graph_id'],
         dependencies=params['dependencies'],
         type=params['type'],
         parameters=params['parameters']
     )
     return operation, 201
def get_running_executions():
    executions = get_resource_manager().list_executions(
        is_include_system_workflows=True,
        all_tenants=True,
        get_all_results=True
    )
    running_executions = []
    for execution in executions:
        if execution.status not in ExecutionState.END_STATES:
            running_executions.append({
                'id': execution.id,
                'status': execution.status,
                'deployment_id': execution.deployment_id,
                'workflow_id': execution.workflow_id
            })

    return running_executions
Esempio n. 54
0
 def post(self, snapshot_id):
     request_dict = rest_utils.get_json_and_verify_params(
         {'recreate_deployments_envs'}
     )
     recreate_deployments_envs = rest_utils.verify_and_convert_bool(
         'recreate_deployments_envs',
         request_dict['recreate_deployments_envs']
     )
     force = rest_utils.verify_and_convert_bool(
         'force',
         request_dict['force']
     )
     restore_certificates = rest_utils.verify_and_convert_bool(
         'restore_certificates',
         request_dict.get('restore_certificates', 'false')
     )
     no_reboot = rest_utils.verify_and_convert_bool(
         'no_reboot',
         request_dict.get('no_reboot', 'false')
     )
     ignore_plugin_failure = \
         rest_utils.verify_and_convert_bool(
             'ignore_plugin_failure',
             request_dict.get('ignore_plugin_failure', 'false')
         )
     if no_reboot and not restore_certificates:
         raise manager_exceptions.BadParametersError(
             '`no_reboot` is only relevant when `restore_certificates` is '
             'activated')
     default_timeout_sec = 300
     request_timeout = request_dict.get('timeout', default_timeout_sec)
     timeout = rest_utils.convert_to_int(request_timeout)
     execution = get_resource_manager().restore_snapshot(
         snapshot_id,
         recreate_deployments_envs,
         force,
         True,
         timeout,
         restore_certificates,
         no_reboot,
         ignore_plugin_failure
     )
     return execution, 200
    def post(self, **kwargs):
        """Execute a workflow"""
        request_dict = get_json_and_verify_params({'deployment_id',
                                                   'workflow_id'})

        allow_custom_parameters = verify_and_convert_bool(
            'allow_custom_parameters',
            request_dict.get('allow_custom_parameters', 'false'))
        force = verify_and_convert_bool(
            'force',
            request_dict.get('force', 'false'))
        dry_run = verify_and_convert_bool(
            'dry_run',
            request_dict.get('dry_run', 'false'))
        queue = verify_and_convert_bool(
            'queue',
            request_dict.get('queue', 'false'))

        deployment_id = request_dict['deployment_id']
        workflow_id = request_dict['workflow_id']
        parameters = request_dict.get('parameters', None)
        wait_after_fail = request_dict.get('wait_after_fail', 600)
        scheduled_time = request_dict.get('scheduled_time', None)

        if scheduled_time:
            scheduled_time = parse_datetime(scheduled_time)

        if parameters is not None and parameters.__class__ is not dict:
            raise manager_exceptions.BadParametersError(
                "request body's 'parameters' field must be a dict but"
                " is of type {0}".format(parameters.__class__.__name__))

        bypass_maintenance = is_bypass_maintenance_mode()
        execution = get_resource_manager().execute_workflow(
            deployment_id, workflow_id, parameters=parameters,
            allow_custom_parameters=allow_custom_parameters, force=force,
            dry_run=dry_run, bypass_maintenance=bypass_maintenance,
            queue=queue, wait_after_fail=wait_after_fail,
            scheduled_time=scheduled_time)
        return execution, 201
    def _prepare_and_submit_blueprint(cls,
                                      file_server_root,
                                      app_dir,
                                      blueprint_id,
                                      visibility):
        args = get_args_and_verify_arguments([
            Argument('private_resource', type=boolean),
            Argument('visibility'),
            Argument('application_file_name',
                     default='')])

        app_file_name = cls._extract_application_file(
            file_server_root, app_dir, args.application_file_name)

        # add to blueprints manager (will also dsl_parse it)
        try:
            blueprint = get_resource_manager().publish_blueprint(
                app_dir,
                app_file_name,
                file_server_root,
                blueprint_id,
                args.private_resource,
                visibility
            )

            # moving the app directory in the file server to be under a
            # directory named after the blueprint id
            tenant_dir = os.path.join(
                file_server_root,
                FILE_SERVER_BLUEPRINTS_FOLDER,
                current_tenant.name)
            mkdirs(tenant_dir)
            shutil.move(os.path.join(file_server_root, app_dir),
                        os.path.join(tenant_dir, blueprint.id))
            cls._process_plugins(file_server_root, blueprint.id)
            return blueprint
        except manager_exceptions.DslParseException, ex:
            shutil.rmtree(os.path.join(file_server_root, app_dir))
            raise manager_exceptions.InvalidBlueprintError(
                'Invalid blueprint - {0}'.format(ex.message))
Esempio n. 57
0
    def post(self, **kwargs):
        """
        Upload a plugin
        """
        storage_manager = get_storage_manager()
        is_caravan = False
        installed_plugins = []
        get_resource_manager().assert_no_snapshot_creation_running_or_queued()
        try:
            plugins, code = UploadedCaravanManager().receive_uploaded_data(
                **kwargs)
            is_caravan = True
        except UploadedCaravanManager.InvalidCaravanException:
            plugin, code = UploadedPluginsManager().receive_uploaded_data(
                str(uuid4()),
                **kwargs
            )
            plugins = [plugin]
        for plugin in plugins:
            try:
                get_resource_manager().install_plugin(plugin)
                installed_plugins.append(plugin)
            except manager_exceptions.ExecutionTimeout:
                tp, ex, tb = sys.exc_info()
                if not is_caravan:
                    raise manager_exceptions.PluginInstallationTimeout(
                        'Timed out during plugin installation.'
                        '({0}: {1})'.format(tp.__name__, ex)), None, tb
            except Exception:
                get_resource_manager().remove_plugin(plugin_id=plugin.id,
                                                     force=True)
                tp, ex, tb = sys.exc_info()
                if not is_caravan:
                    raise manager_exceptions.PluginInstallationError(
                        'Failed during plugin installation.'
                        '({0}: {1})'.format(tp.__name__, ex)), None, tb

        if is_caravan:
            storage_plugins = storage_manager.list(
                models.Plugin,
                filters={'id': [p.id for p in installed_plugins]})

            return ListResponse(items=storage_plugins.items,
                                metadata=storage_plugins.metadata), code
        else:
            return plugins[0], code
Esempio n. 58
0
 def delete(self, plugin_id, **kwargs):
     """
     Delete plugin by ID
     """
     request_dict = get_json_and_verify_params()
     force = verify_and_convert_bool(
         'force', request_dict.get('force', False)
     )
     try:
         return get_resource_manager().remove_plugin(plugin_id=plugin_id,
                                                     force=force)
     except manager_exceptions.ManagerException:
         raise
     except manager_exceptions.ExecutionTimeout:
         tp, ex, tb = sys.exc_info()
         raise manager_exceptions.PluginInstallationTimeout(
             'Timed out during plugin un-installation. ({0}: {1})'
             .format(tp.__name__, ex)), None, tb
     except Exception:
         tp, ex, tb = sys.exc_info()
         raise manager_exceptions.PluginInstallationError(
             'Failed during plugin un-installation. ({0}: {1})'
             .format(tp.__name__, ex)), None, tb
 def put(self, deployment_id, **kwargs):
     """
     Create a deployment
     """
     validate_inputs({'deployment_id': deployment_id})
     request_schema = self.create_request_schema()
     request_dict = get_json_and_verify_params(request_schema)
     blueprint_id = request_dict['blueprint_id']
     bypass_maintenance = is_bypass_maintenance_mode()
     args = get_args_and_verify_arguments(
         [Argument('private_resource', type=boolean,
                   default=False)]
     )
     deployment = get_resource_manager().create_deployment(
         blueprint_id,
         deployment_id,
         private_resource=args.private_resource,
         visibility=None,
         inputs=request_dict.get('inputs', {}),
         bypass_maintenance=bypass_maintenance,
         skip_plugins_validation=self.get_skip_plugin_validation_flag(
             request_dict)
     )
     return deployment, 201