diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..38c6bf23b1163a62b464155a95401d90348e3ff3 --- /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 ad1d2d29eca8d9bbdf082e7faa9673946215135a..c56df09edb4bc1a11a5f2c82b34c083b00c879fd 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 d691e817ebd850f001c623391a049d03982f7293..95855569d83d771f42fb12e9ae08a556011e3947 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 7dff5b8921122a487162febe3c8e32effb7acb35..9325c3ccda9850bb6e102aef07d314210f5c9f41 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 c13e76c41de41b9f77e5d9ae74a2e94569da6f8f..ba5bcf0c3a72d952a459c512b4a3e6a2f666e37b 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 c405c98ec9a8910b94a3bc5287e35e6039d84a8f..24047e3511633ac41fc210e05b791d52a160f1d6 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