CLI: Deprecate 'ara-manage prune' in favor of 'ara playbook prune'
Back when ara-manage prune was implemented, we didn't yet have a CLI framework for running commands and so it was implemented using django. Now that we have a CLI framework, move the command where it belongs. Fixes: https://github.com/ansible-community/ara/issues/162 Change-Id: Ia3169f4966808611728bb7a70813a66423e54caf
This commit is contained in:
parent
c6f5a4465d
commit
78a18088b4
@ -44,6 +44,8 @@ class Command(BaseCommand):
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
logger.warn("This command has been replaced by 'ara playbook prune' in 1.5. It will be removed in 1.6.")
|
||||
|
||||
client = options.get("client")
|
||||
endpoint = options.get("endpoint")
|
||||
username = options.get("username")
|
||||
|
@ -4,6 +4,7 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from cliff.command import Command
|
||||
from cliff.lister import Lister
|
||||
@ -229,3 +230,65 @@ class PlaybookDelete(Command):
|
||||
|
||||
# TODO: Improve client to be better at handling exceptions
|
||||
client.delete("/api/v1/playbooks/%s" % args.playbook_id)
|
||||
|
||||
|
||||
class PlaybookPrune(Command):
|
||||
""" Deletes playbooks beyond a specified age in days """
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
deleted = 0
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(PlaybookPrune, self).get_parser(prog_name)
|
||||
parser = global_arguments(parser)
|
||||
# fmt: off
|
||||
parser.add_argument(
|
||||
"--days", type=int, default=31, help="Delete playbooks started this many days ago (default: 31)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--confirm",
|
||||
action="store_true",
|
||||
help="Confirm deletion of playbooks, otherwise runs without deleting any playbook",
|
||||
)
|
||||
# fmt: on
|
||||
return parser
|
||||
|
||||
def take_action(self, args):
|
||||
client = get_client(
|
||||
client=args.client,
|
||||
endpoint=args.server,
|
||||
timeout=args.timeout,
|
||||
username=args.username,
|
||||
password=args.password,
|
||||
verify=False if args.insecure else True,
|
||||
run_sql_migrations=False,
|
||||
)
|
||||
|
||||
if not args.confirm:
|
||||
self.log.info("--confirm was not specified, no playbooks will be deleted")
|
||||
|
||||
# generate a timestamp from n days ago in a format we can query the API with
|
||||
# ex: 2019-11-21T00:57:41.702229
|
||||
limit_date = (datetime.now() - timedelta(days=args.days)).isoformat()
|
||||
|
||||
playbooks = client.get("/api/v1/playbooks", started_before=limit_date)
|
||||
|
||||
# TODO: Improve client validation and exception handling
|
||||
if "count" not in playbooks:
|
||||
# If we didn't get an answer we can parse, it's probably due to an error 500, 403, 401, etc.
|
||||
# The client would have logged the error.
|
||||
self.log.error("Client failed to retrieve results, see logs for ara.clients.offline or ara.clients.http.")
|
||||
sys.exit(1)
|
||||
|
||||
self.log.info("Found %s playbooks matching query" % playbooks["count"])
|
||||
for playbook in playbooks["results"]:
|
||||
if not args.confirm:
|
||||
msg = "Dry-run: playbook {id} ({path}) would have been deleted, start date: {started}"
|
||||
self.log.info(msg.format(id=playbook["id"], path=playbook["path"], started=playbook["started"]))
|
||||
else:
|
||||
msg = "Deleting playbook {id} ({path}), start date: {started}"
|
||||
self.log.info(msg.format(id=playbook["id"], path=playbook["path"], started=playbook["started"]))
|
||||
client.delete("/api/v1/playbooks/%s" % playbook["id"])
|
||||
self.deleted += 1
|
||||
|
||||
self.log.info("%s playbooks deleted" % self.deleted)
|
||||
|
@ -65,6 +65,26 @@ ara playbook delete
|
||||
|
||||
.. command-output:: ara playbook delete --help
|
||||
|
||||
ara playbook prune
|
||||
------------------
|
||||
|
||||
.. note::
|
||||
|
||||
This command requires write privileges.
|
||||
You can read more about read and write permissions :ref:`here <api-security:user management>`.
|
||||
|
||||
.. command-output:: ara playbook prune --help
|
||||
|
||||
Examples:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# Query which playbooks would be deleted without deleting them
|
||||
ara playbook prune
|
||||
|
||||
# Delete playbooks older than 14 days
|
||||
ara playbook prune --days 14 --confirm
|
||||
|
||||
ara play list
|
||||
-------------
|
||||
|
||||
@ -334,6 +354,10 @@ ara-manage
|
||||
ara-manage prune
|
||||
----------------
|
||||
|
||||
.. warning::
|
||||
ara-manage prune has been replaced by `ara playbook prune`_ in ara 1.5.
|
||||
It will be removed in ara 1.6.
|
||||
|
||||
Used to delete playbooks that are older than a specified amount of days.
|
||||
|
||||
.. command-output:: ara-manage prune --help
|
||||
|
@ -38,6 +38,7 @@ ara.cli =
|
||||
playbook list = ara.cli.playbook:PlaybookList
|
||||
playbook show = ara.cli.playbook:PlaybookShow
|
||||
playbook delete = ara.cli.playbook:PlaybookDelete
|
||||
playbook prune = ara.cli.playbook:PlaybookPrune
|
||||
play list = ara.cli.play:PlayList
|
||||
play show = ara.cli.play:PlayShow
|
||||
play delete = ara.cli.play:PlayDelete
|
||||
|
Loading…
x
Reference in New Issue
Block a user