Skip to content
Snippets Groups Projects
Commit c59147ed authored by nimrod's avatar nimrod
Browse files

- Added licesne.

- Added proper README.
- Added dry run mode.
- Added argument handling, usage.
- Bumped version to 0.3.0.
parent f32d325a
No related branches found
No related tags found
No related merge requests found
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.
recursive-include eb-prune *.py recursive-include eb-prune *.py
exclude .pre-commit-config.yaml exclude .pre-commit-config.yaml
include README.rst include *.rst
include *.txt
include VERSION include VERSION
eb-prune eb-prune
######## ########
Must specify AWS_DEFAULT_REGION environment variable. A CLI tool to prune old versions of Elastic Beanstalk.
TODO Installation
---- ------------
- License. .. code:: shell
- Changelog.
- README, pip install eb-prune
- Specify number of versions to keep, dry-run option.
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/.
0.2.1 0.3.0
\ No newline at end of file \ No newline at end of file
...@@ -2,26 +2,30 @@ ...@@ -2,26 +2,30 @@
'''Prune older versions of an application in Elastic Beanstalk.''' '''Prune older versions of an application in Elastic Beanstalk.'''
from __future__ import (absolute_import, division, from __future__ import (absolute_import, division,
print_function, unicode_literals) print_function, unicode_literals)
from argparse import ArgumentParser
from botocore import session from botocore import session
def main(): def prune(versions_to_keep, dry_run):
versions_to_keep = 400 if dry_run:
dry_run = False print('DRY RUN! NOTHING WILL BE REMOVED.')
print('Pruning Elastic Beanstalk versions.') print('Pruning Elastic Beanstalk versions.')
aws_session = session.get_session() aws_session = session.get_session()
beanstalk_client = aws_session.create_client('elasticbeanstalk') beanstalk_client = aws_session.create_client('elasticbeanstalk')
response = beanstalk_client.describe_application_versions() response = beanstalk_client.describe_application_versions()
if response['ResponseMetadata']['HTTPStatusCode'] != 200: if response['ResponseMetadata']['HTTPStatusCode'] != 200:
raise RuntimeError('Failed to describe application versions.') raise RuntimeError('Failed to describe application versions.')
# Get all EB versions.
versions = response['ApplicationVersions'] versions = response['ApplicationVersions']
response = beanstalk_client.describe_environments() response = beanstalk_client.describe_environments()
if response['ResponseMetadata']['HTTPStatusCode'] != 200: if response['ResponseMetadata']['HTTPStatusCode'] != 200:
raise RuntimeError('Failed to describe environments.') raise RuntimeError('Failed to describe environments.')
# Remove the currently in-use versions from the list.
active_versions = [env['VersionLabel'] for env in response['Environments']] active_versions = [env['VersionLabel'] for env in response['Environments']]
previous_versions = filter( previous_versions = filter(
lambda x: (not x['VersionLabel'] in active_versions) and lambda x: (not x['VersionLabel'] in active_versions) and
x['Status'] == 'UNPROCESSED', versions) x['Status'] == 'UNPROCESSED', versions)
# Remove the newest versions from the list.
old_versions = sorted(previous_versions, old_versions = sorted(previous_versions,
key=lambda x: key=lambda x:
x.get('DateCreated'))[:-versions_to_keep] x.get('DateCreated'))[:-versions_to_keep]
...@@ -40,5 +44,17 @@ def main(): ...@@ -40,5 +44,17 @@ def main():
print('Deleted {0} versions.'.format(len(old_versions))) 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__': if __name__ == '__main__':
main() main()
...@@ -13,6 +13,7 @@ commands = ...@@ -13,6 +13,7 @@ commands =
check-manifest --ignore tox.ini,tests* check-manifest --ignore tox.ini,tests*
python setup.py check -m -r -s python setup.py check -m -r -s
flake8 . flake8 .
eb-prune --help
[testenv:release] [testenv:release]
basepython = python basepython = python
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment