Beispiel #1
0
    def delete(self, name, force=None):
        """Delete the application with the given name

        :param name: application name
        """
        try:
            db_app = objects.kube_app.get_by_name(pecan.request.context, name)
        except exception.KubeAppNotFound:
            LOG.error("Received a request to delete app %s which does not "
                      "exist." % name)
            raise wsme.exc.ClientSideError(
                _("Application-delete rejected: application not found."))

        if db_app.status not in [
                constants.APP_UPLOAD_SUCCESS, constants.APP_UPLOAD_FAILURE
        ]:
            raise wsme.exc.ClientSideError(
                _("Application-delete rejected: operation is not allowed "
                  "while the current status is {}.".format(db_app.status)))

        try:
            lifecycle_hook_info = LifecycleHookInfo()
            lifecycle_hook_info.init(
                constants.APP_LIFECYCLE_MODE_MANUAL,
                constants.APP_LIFECYCLE_TYPE_SEMANTIC_CHECK,
                constants.APP_LIFECYCLE_TIMING_PRE, constants.APP_DELETE_OP)
            # Converting string to boolean
            if force == 'True':
                force = True
            else:
                force = False

            lifecycle_hook_info.extra = {
                constants.APP_LIFECYCLE_FORCE_OPERATION: force
            }
            self._app_lifecycle_actions(db_app, lifecycle_hook_info)
        except rpc_common.RemoteError as e:
            raise wsme.exc.ClientSideError(
                _("Application-delete rejected: " + str(e.value)))
        except Exception as e:
            raise wsme.exc.ClientSideError(
                _("Application-delete rejected: " + six.text_type(e)))

        lifecycle_hook_info = LifecycleHookInfo()
        lifecycle_hook_info.mode = constants.APP_LIFECYCLE_MODE_MANUAL

        response = pecan.request.rpcapi.perform_app_delete(
            pecan.request.context,
            db_app,
            lifecycle_hook_info=lifecycle_hook_info)
        if response:
            raise wsme.exc.ClientSideError(_("%s." % response))
Beispiel #2
0
    def patch(self, name, directive, values, force=None):
        """Install/update the specified application

        :param name: application name
        :param directive: either 'apply' (fresh install/update), 'remove' or 'abort'
        """
        if directive not in ['apply', 'remove', 'abort']:
            raise exception.OperationNotPermitted

        try:
            db_app = objects.kube_app.get_by_name(pecan.request.context, name)
        except exception.KubeAppNotFound:
            LOG.error("Received a request to %s app %s which does not exist." %
                      (directive, name))
            raise wsme.exc.ClientSideError(
                _("Application-{} rejected: application not found.".format(
                    directive)))

        if directive == 'apply':
            if not values:
                mode = None
            elif name not in constants.HELM_APP_APPLY_MODES.keys():
                raise wsme.exc.ClientSideError(
                    _("Application-apply rejected: Mode is not supported "
                      "for app {}.".format(name)))
            elif (values['mode'] and values['mode']
                  not in constants.HELM_APP_APPLY_MODES[name]):
                raise wsme.exc.ClientSideError(
                    _("Application-apply rejected: Mode {} for app {} is not "
                      "valid. Valid modes are {}.".format(
                          values['mode'], name,
                          constants.HELM_APP_APPLY_MODES[name])))
            else:
                mode = values['mode']

            try:
                app_helper = KubeAppHelper(pecan.request.dbapi)
                app_helper._check_app_compatibility(db_app.name,
                                                    db_app.app_version)
            except exception.IncompatibleKubeVersion as e:
                raise wsme.exc.ClientSideError(
                    _("Application-apply rejected: " + str(e)))

            if db_app.status == constants.APP_APPLY_IN_PROGRESS:
                raise wsme.exc.ClientSideError(
                    _("Application-apply rejected: install/update is already "
                      "in progress."))
            elif db_app.status not in [
                    constants.APP_UPLOAD_SUCCESS, constants.APP_APPLY_FAILURE,
                    constants.APP_APPLY_SUCCESS
            ]:
                raise wsme.exc.ClientSideError(
                    _("Application-apply rejected: operation is not allowed "
                      "while the current status is {}.".format(db_app.status)))

            try:
                lifecycle_hook_info = LifecycleHookInfo()
                lifecycle_hook_info.init(
                    constants.APP_LIFECYCLE_MODE_MANUAL,
                    constants.APP_LIFECYCLE_TYPE_SEMANTIC_CHECK,
                    constants.APP_LIFECYCLE_TIMING_PRE, constants.APP_APPLY_OP)
                self._app_lifecycle_actions(db_app, lifecycle_hook_info)
            except Exception as e:
                raise wsme.exc.ClientSideError(
                    _("Application-apply rejected: " + six.text_type(e)))

            db_app.status = constants.APP_APPLY_IN_PROGRESS
            db_app.progress = None
            db_app.recovery_attempts = 0
            db_app.mode = mode
            db_app.save()

            lifecycle_hook_info = LifecycleHookInfo()
            lifecycle_hook_info.mode = constants.APP_LIFECYCLE_MODE_MANUAL

            pecan.request.rpcapi.perform_app_apply(
                pecan.request.context,
                db_app,
                mode=mode,
                lifecycle_hook_info=lifecycle_hook_info)
        elif directive == 'remove':
            if db_app.status not in [
                    constants.APP_APPLY_SUCCESS, constants.APP_APPLY_FAILURE,
                    constants.APP_REMOVE_FAILURE
            ]:
                raise wsme.exc.ClientSideError(
                    _("Application-remove rejected: operation is not allowed while "
                      "the current status is {}.".format(db_app.status)))

            try:
                lifecycle_hook_info = LifecycleHookInfo()
                lifecycle_hook_info.init(
                    constants.APP_LIFECYCLE_MODE_MANUAL,
                    constants.APP_LIFECYCLE_TYPE_SEMANTIC_CHECK,
                    constants.APP_LIFECYCLE_TIMING_PRE,
                    constants.APP_REMOVE_OP)
                # Converting string to boolean
                if force == 'True':
                    force = True
                else:
                    force = False

                lifecycle_hook_info.extra = {
                    constants.APP_LIFECYCLE_FORCE_OPERATION: force
                }
                self._app_lifecycle_actions(db_app, lifecycle_hook_info)
            except rpc_common.RemoteError as e:
                raise wsme.exc.ClientSideError(
                    _("Application-remove rejected: " + str(e.value)))
            except Exception as e:
                raise wsme.exc.ClientSideError(
                    _("Application-remove rejected: " + six.text_type(e)))

            db_app.status = constants.APP_REMOVE_IN_PROGRESS
            db_app.progress = None
            db_app.save()

            lifecycle_hook_info = LifecycleHookInfo()
            lifecycle_hook_info.mode = constants.APP_LIFECYCLE_MODE_MANUAL

            pecan.request.rpcapi.perform_app_remove(
                pecan.request.context,
                db_app,
                lifecycle_hook_info=lifecycle_hook_info)
        else:
            if db_app.status not in [
                    constants.APP_APPLY_IN_PROGRESS,
                    constants.APP_UPDATE_IN_PROGRESS,
                    constants.APP_REMOVE_IN_PROGRESS
            ]:
                raise wsme.exc.ClientSideError(
                    _("Application-abort rejected: operation is not allowed while "
                      "the current status is {}.".format(db_app.status)))

            try:
                lifecycle_hook_info = LifecycleHookInfo()
                lifecycle_hook_info.init(
                    constants.APP_LIFECYCLE_MODE_MANUAL,
                    constants.APP_LIFECYCLE_TYPE_SEMANTIC_CHECK,
                    constants.APP_LIFECYCLE_TIMING_PRE, constants.APP_ABORT_OP)
                self._app_lifecycle_actions(db_app, lifecycle_hook_info)
            except Exception as e:
                raise wsme.exc.ClientSideError(
                    _("Application-abort rejected: " + six.text_type(e)))

            lifecycle_hook_info = LifecycleHookInfo()
            lifecycle_hook_info.mode = constants.APP_LIFECYCLE_MODE_MANUAL

            pecan.request.rpcapi.perform_app_abort(
                pecan.request.context,
                db_app,
                lifecycle_hook_info=lifecycle_hook_info)
        return KubeApp.convert_with_links(db_app)