Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
28a3c39
Adding code to remove action files on disk
mahesh-orch Jul 15, 2021
addd291
Update st2api/st2api/controllers/v1/actions.py
mahesh-orch Jul 16, 2021
b70b4a3
Update st2api/st2api/controllers/v1/actions.py
mahesh-orch Jul 16, 2021
f6b91b9
Updating actions.py
mahesh-orch Jul 16, 2021
ca4d453
Updating st2api/st2api/controllers/v1/actions.py
mahesh-orch Jul 19, 2021
e7b32b0
Updating st2client/commands/resource.py
mahesh-orch Jul 19, 2021
d86d136
Merge branch 'StackStorm:master' into modify_action_delete_api
mahesh-orch Jul 20, 2021
7ca0f6c
Removing whitespaces
mahesh-orch Jul 20, 2021
0639640
Updating st2api/controllers/v1/actions.py
mahesh-orch Jul 22, 2021
c7354c7
Updating /st2common/services/packs.py
mahesh-orch Jul 22, 2021
b6ed390
Updating /st2common/services/packs.py
mahesh-orch Jul 22, 2021
7395008
Updating /st2client/commands/resource.py
mahesh-orch Jul 22, 2021
284e130
Adding file test_packs.py
mahesh-orch Jul 22, 2021
f929dc5
Updating /st2client/commands/resource.py
mahesh-orch Jul 22, 2021
ad6c868
Removing trailing whitespaces
mahesh-orch Jul 22, 2021
bee06cc
Updating test_packs.py
mahesh-orch Jul 23, 2021
54a70e6
Updating /st2common/services/packs.py
mahesh-orch Jul 26, 2021
953d6a1
Merge branch 'master' into modify_action_delete_api
mahesh-orch Jul 26, 2021
8c37c5e
Updating /st2common/services/packs.py
mahesh-orch Jul 26, 2021
0769eb1
Updating /st2common/services/packs.py
mahesh-orch Jul 26, 2021
9b9b9d0
Updating test_packs.py
mahesh-orch Jul 26, 2021
45a7fb9
Updating test_packs.py
mahesh-orch Jul 26, 2021
bf538d0
Updating /st2client/commands/resource.py
mahesh-orch Jul 27, 2021
ea3dac3
Creating file /st2common/exceptions/packs.py
mahesh-orch Jul 27, 2021
947a16c
Updating st2common/services/packs.py
mahesh-orch Jul 27, 2021
4995144
Adding unit tests in test_packs.py
mahesh-orch Jul 27, 2021
e974022
Updating /st2common/services/packs.py
mahesh-orch Jul 27, 2021
d95d8b7
Updating resource.py
mahesh-orch Jul 30, 2021
9e99615
Updating action.py
mahesh-orch Jul 30, 2021
4916c04
Updating test_packs.py
mahesh-orch Jul 30, 2021
5c0eb6f
Updating content.py
mahesh-orch Jul 30, 2021
edce1d8
Updating packs.py
mahesh-orch Jul 30, 2021
4da0f48
Deleting packs.py
mahesh-orch Jul 30, 2021
2654528
Updating /st2client/commands/resource.py
mahesh-orch Aug 3, 2021
6179ca6
Updating /st2client/commands/action.py
mahesh-orch Aug 3, 2021
b4d7d5e
Merge branch 'master' into modify_action_delete_api
mahesh-orch Aug 3, 2021
8743f82
Updating /st2common/services/packs.py
mahesh-orch Aug 3, 2021
d35a59e
Moving `-f` and `--force` arguments to ActionDeleteCommand
mahesh-orch Aug 4, 2021
bee5a3f
Added `-f` and `--force` args under ActionDeleteCommand
mahesh-orch Aug 4, 2021
858b3b1
Updating /st2client/commands/action.py
mahesh-orch Aug 5, 2021
5439154
Updating /st2client/commands/action.py
mahesh-orch Aug 5, 2021
f003988
Updating /st2client/commands/action.py
mahesh-orch Aug 5, 2021
d8db49e
Updating /st2common/services/packs.py
mahesh-orch Aug 5, 2021
04bc3b2
Combining `-f` and `--force` args for action delete command
mahesh-orch Aug 6, 2021
ec307f9
Merge branch 'master' into modify_action_delete_api
mahesh-orch Aug 10, 2021
6bd83fe
Adding extra try/except block for disk file delete
mahesh-orch Aug 10, 2021
37a36de
Updating CHANGELOG.rst
mahesh-orch Aug 10, 2021
dcbf567
Updating CHANGELOG.rst
mahesh-orch Aug 10, 2021
89cbc60
Updating CHANGELOG.rst
mahesh-orch Aug 10, 2021
b581fc7
CHANGELOG for modification in action delete API shifted to 'in develo…
mahesh-orch Aug 11, 2021
24cbc0a
Adding log warning in case of file to be removed doesn't exists and u…
mahesh-orch Aug 11, 2021
80788e8
Adding assertions for error messages. Using `os.path.join` to prepare…
mahesh-orch Aug 11, 2021
749c187
Removing an unnecessary argument from getattr() and cleaning up logic…
mahesh-orch Aug 11, 2021
ec59efd
Putting try/except blocks one after another instead of nesting
mahesh-orch Aug 11, 2021
13eae54
Updating expected_msg variable to take generic path for entry point a…
mahesh-orch Aug 11, 2021
4a0bbdd
Merge branch 'master' into modify_action_delete_api
m4dcoder Aug 13, 2021
63f3d12
Removing redundant check, adding an appropriate check for file at pat…
mahesh-orch Aug 17, 2021
936f618
Minor fixes for long line msgs, comments, typos in comments and metho…
mahesh-orch Aug 17, 2021
96029a0
Minor fixes for variable name for msg and length of the msg line
mahesh-orch Aug 17, 2021
5fe66c0
Merge branch 'master' into modify_action_delete_api
mahesh-orch Aug 17, 2021
e76573c
Merge branch 'master' into modify_action_delete_api
m4dcoder Aug 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ in development
Changed
~~~~~~~

* Modified action delete api. Action delete api removes related action/workflow files on disk
along with de-registering them from database. Prompts on CLI for user permission before
removing disk files.

``-f`` and ``--force`` arguments added for action delete CLI command as auto yes flag and
will delete related files on disk without prompting for user permission. #5304

Contributed by @mahesh-orch.

* Removed --python3 deprecated flag from st2client. #5305

Contributed by Amanda McGuinness (@amanda11 Ammeon Solutions)
Expand Down
19 changes: 19 additions & 0 deletions st2api/st2api/controllers/v1/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from st2common.content.utils import get_pack_base_path
from st2common.content.utils import get_pack_resource_file_abs_path
from st2common.content.utils import get_relative_path_to_pack_file
from st2common.services.packs import delete_action_files_from_pack
from st2common.transport.reactor import TriggerDispatcher
from st2common.util.system_info import get_host_info
import st2common.validators.api.action as action_validator
Expand Down Expand Up @@ -236,6 +237,10 @@ def delete(self, ref_or_id, requester_user):
action_db,
)

pack_name = action_db["pack"]
entry_point = action_db["entry_point"]
metadata_file = action_db["metadata_file"]

try:
Action.delete(action_db)
except Exception as e:
Expand All @@ -247,6 +252,20 @@ def delete(self, ref_or_id, requester_user):
)
abort(http_client.INTERNAL_SERVER_ERROR, six.text_type(e))
return
try:
delete_action_files_from_pack(
pack_name=pack_name,
entry_point=entry_point,
metadata_file=metadata_file,
)
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the Action.delete and delete_action_files_from disk are in the same catch exception. If I get an error on deleting the files but not on the database, doesn't this mean that I'm still going to get a LOG.error saying "Database delete encountered exception"?

This would be a bit confusing. So either we need to amend the text slightly to indicate it might be database or file, or have two catch blocks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mahesh-orch Please address this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put the try/except blocks one after the other instead of nesting them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! that looks much better.

LOG.error(
"Exception encountered during deleting resource files from disk."
"Exception was %s",
e,
)
abort(http_client.INTERNAL_SERVER_ERROR, six.text_type(e))
return

extra = {"action_db": action_db}
LOG.audit("Action deleted. Action.id=%s" % (action_db.id), extra=extra)
Expand Down
38 changes: 37 additions & 1 deletion st2client/st2client/commands/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,43 @@ class ActionDisableCommand(resource.ContentPackResourceDisableCommand):


class ActionDeleteCommand(resource.ContentPackResourceDeleteCommand):
pass
def __init__(self, resource, *args, **kwargs):
super(ActionDeleteCommand, self).__init__(resource, *args, **kwargs)

self.parser.add_argument(
"-f",
"--force",
action="store_true",
dest="force",
help="Auto yes flag to delete action files from disk.",
)

@add_auth_token_to_kwargs_from_cli
def run(self, args, **kwargs):
resource_id = getattr(args, self.pk_argument_name, None)
instance = self.get_resource(resource_id, **kwargs)
msg = (
'Resource with id "%s" has been successfully deleted from database and disk.'
% (resource_id)
)
user_input = ""
if not args.force:
user_input = input(
"The resource files on disk will be deleted. Do you want to continue? (y/n): "
)
if args.force or user_input.lower() == "y" or user_input.lower() == "yes":
self.manager.delete(instance, **kwargs)
print(msg)
else:
print("Action is not deleted.")

def run_and_print(self, args, **kwargs):
resource_id = getattr(args, self.pk_argument_name)

try:
self.run(args, **kwargs)
except ResourceNotFoundError:
self.print_not_found(resource_id)


class ActionRunCommandMixin(object):
Expand Down
4 changes: 4 additions & 0 deletions st2common/st2common/exceptions/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ class UnsupportedMetaException(StackStormBaseException):

class ParseException(ValueError):
pass


class ResourceDiskFilesRemovalError(StackStormBaseException):
pass
75 changes: 75 additions & 0 deletions st2common/st2common/services/packs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

import itertools

import os
import requests
import six
from six.moves import range
from oslo_config import cfg

from st2common import log as logging
from st2common.content.utils import get_pack_base_path
from st2common.exceptions.content import ResourceDiskFilesRemovalError
from st2common.persistence.pack import Pack
from st2common.util.misc import lowercase_value
from st2common.util.jsonify import json_encode
Expand All @@ -32,6 +35,7 @@
"fetch_pack_index",
"get_pack_from_index",
"search_pack_index",
"delete_action_files_from_pack",
]

EXCLUDE_FIELDS = ["repo_url", "email"]
Expand Down Expand Up @@ -215,3 +219,74 @@ def search_pack_index(
break

return list(itertools.chain.from_iterable(matches))


def delete_action_files_from_pack(pack_name, entry_point, metadata_file):
"""
Prepares the path for entry_point file and metadata file of action and
deletes them from disk.
"""

pack_base_path = get_pack_base_path(pack_name=pack_name)
action_entrypoint_file_path = os.path.join(pack_base_path, "actions", entry_point)
action_metadata_file_path = os.path.join(pack_base_path, metadata_file)

if os.path.isfile(action_entrypoint_file_path):
try:
os.remove(action_entrypoint_file_path)
except PermissionError:
LOG.error(
'No permission to delete the "%s" file',
action_entrypoint_file_path,
)
msg = 'No permission to delete "%s" file from disk' % (
action_entrypoint_file_path
)
raise PermissionError(msg)
except Exception as e:
LOG.error(
'Unable to delete "%s" file. Exception was "%s"',
action_entrypoint_file_path,
e,
)
msg = (
'The action file "%s" could not be removed from disk, please '
"check the logs or ask your StackStorm administrator to check "
"and delete the actions files manually" % (action_entrypoint_file_path)
)
raise ResourceDiskFilesRemovalError(msg)
else:
LOG.warning(
'The action entry point file "%s" does not exists on disk.',
action_entrypoint_file_path,
)

if os.path.isfile(action_metadata_file_path):
try:
os.remove(action_metadata_file_path)
except PermissionError:
LOG.error(
'No permission to delete the "%s" file',
action_metadata_file_path,
)
msg = 'No permission to delete "%s" file from disk' % (
action_metadata_file_path
)
raise PermissionError(msg)
except Exception as e:
LOG.error(
Copy link
Contributor

@amanda11 amanda11 Jul 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we fail to delete the files from disk as we are catching the exceptions, does this not mean that the API is going to return success?
If we fail to delete from the database then the action appears to be to log.error, then in the API we return internal_server_error. But we can't do that if we suppress the exception here.

Perhaps re-throw an exception if the file still exists after the delete failed? So that in the case @blag was saying about two workers trying to delete and file doesn't exist, it would still return success. But if it failed to delete the file then it would inform the user.

This would allow in the api/controllers you can then return an internal server error if the file still exists after you attempted to delete it.

'Could not delete "%s" file. Exception was "%s"',
action_metadata_file_path,
e,
)
msg = (
'The action file "%s" could not be removed from disk, please '
"check the logs or ask your StackStorm administrator to check "
"and delete the actions files manually" % (action_metadata_file_path)
)
raise ResourceDiskFilesRemovalError(msg)
else:
LOG.warning(
'The action metadata file "%s" does not exists on disk.',
action_metadata_file_path,
)
Loading