From c59147ed9c44f47e604122b53358b1718f553feb Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Wed, 16 Mar 2016 09:34:56 +0200 Subject: [PATCH] - Added licesne. - Added proper README. - Added dry run mode. - Added argument handling, usage. - Bumped version to 0.3.0. --- LICENSE.txt | 21 ++++++++++++++++++++ MANIFEST.in | 3 ++- README.rst | 47 +++++++++++++++++++++++++++++++++++++------- VERSION | 2 +- eb_prune/__init__.py | 22 ++++++++++++++++++--- tox.ini | 1 + 6 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..38c6bf2 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) [year] [fullname] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/MANIFEST.in b/MANIFEST.in index ad1d2d2..c56df09 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,5 @@ recursive-include eb-prune *.py exclude .pre-commit-config.yaml -include README.rst +include *.rst +include *.txt include VERSION diff --git a/README.rst b/README.rst index d691e81..9585556 100644 --- a/README.rst +++ b/README.rst @@ -1,12 +1,45 @@ eb-prune ######## -Must specify AWS_DEFAULT_REGION environment variable. +A CLI tool to prune old versions of Elastic Beanstalk. -TODO ----- +Installation +------------ -- License. -- Changelog. -- README, -- Specify number of versions to keep, dry-run option. +.. code:: shell + + pip install eb-prune + +Usage +----- + +To keep the last 100 versions available, simply run :code:`eb-prune 100`. The +tool relies on the usual AWS CLI configuration as described `here +<http://docs.aws.amazon.com/cli/latest/topic/config-vars.html>`_, specifically +on access key id, secret access key and region. + +.. code:: shell + + $ eb-prune --help + usage: eb-prune [-h] [-d] versions_to_keep + + positional arguments: + versions_to_keep The number of versions to keep. + + optional arguments: + -h, --help show this help message and exit + -d, --dry-run Dry run, do not delete versions. + +License +------- + +This software is licnesed under the MIT licese (see the :code:`LICENSE.txt` +file). + +Author +------ + +Nimrod Adar, `contact me <nimrod@shore.co.il>`_ or visit my `website +<https://www.shore.co.il/>`_. Patches are welcome via `git send-email +<http://git-scm.com/book/en/v2/Git-Commands-Email>`_. The repository is located +at: https://www.shore.co.il/git/. diff --git a/VERSION b/VERSION index 7dff5b8..9325c3c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.1 \ No newline at end of file +0.3.0 \ No newline at end of file diff --git a/eb_prune/__init__.py b/eb_prune/__init__.py index c13e76c..ba5bcf0 100644 --- a/eb_prune/__init__.py +++ b/eb_prune/__init__.py @@ -2,26 +2,30 @@ '''Prune older versions of an application in Elastic Beanstalk.''' from __future__ import (absolute_import, division, print_function, unicode_literals) +from argparse import ArgumentParser from botocore import session -def main(): - versions_to_keep = 400 - dry_run = False +def prune(versions_to_keep, dry_run): + if dry_run: + print('DRY RUN! NOTHING WILL BE REMOVED.') print('Pruning Elastic Beanstalk versions.') aws_session = session.get_session() beanstalk_client = aws_session.create_client('elasticbeanstalk') response = beanstalk_client.describe_application_versions() if response['ResponseMetadata']['HTTPStatusCode'] != 200: raise RuntimeError('Failed to describe application versions.') + # Get all EB versions. versions = response['ApplicationVersions'] response = beanstalk_client.describe_environments() if response['ResponseMetadata']['HTTPStatusCode'] != 200: raise RuntimeError('Failed to describe environments.') + # Remove the currently in-use versions from the list. active_versions = [env['VersionLabel'] for env in response['Environments']] previous_versions = filter( lambda x: (not x['VersionLabel'] in active_versions) and x['Status'] == 'UNPROCESSED', versions) + # Remove the newest versions from the list. old_versions = sorted(previous_versions, key=lambda x: x.get('DateCreated'))[:-versions_to_keep] @@ -40,5 +44,17 @@ def main(): print('Deleted {0} versions.'.format(len(old_versions))) +def main(): + parser = ArgumentParser() + parser.add_argument('versions_to_keep', + help='The number of versions to keep.', + type=int) + parser.add_argument('-d', '--dry-run', + help='Dry run, do not delete versions.', + action='store_true') + args = parser.parse_args() + prune(args.versions_to_keep, args.dry_run) + + if __name__ == '__main__': main() diff --git a/tox.ini b/tox.ini index c405c98..24047e3 100644 --- a/tox.ini +++ b/tox.ini @@ -13,6 +13,7 @@ commands = check-manifest --ignore tox.ini,tests* python setup.py check -m -r -s flake8 . + eb-prune --help [testenv:release] basepython = python -- GitLab