awsclean 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ronald.tse@ribose.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ ruby '>= 2.3'
2
+ source 'https://rubygems.org'
3
+
4
+ gemspec
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ awsclean (1.0)
5
+ aws-sdk (~> 2.7.4)
6
+ bundler (~> 1.14)
7
+ pry
8
+ thor (~> 0.19.4)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ aws-sdk (2.7.4)
14
+ aws-sdk-resources (= 2.7.4)
15
+ aws-sdk-core (2.7.4)
16
+ aws-sigv4 (~> 1.0)
17
+ jmespath (~> 1.0)
18
+ aws-sdk-resources (2.7.4)
19
+ aws-sdk-core (= 2.7.4)
20
+ aws-sigv4 (1.0.0)
21
+ codecov (0.1.10)
22
+ json
23
+ simplecov
24
+ url
25
+ coderay (1.1.1)
26
+ diff-lcs (1.3)
27
+ docile (1.1.5)
28
+ jmespath (1.3.1)
29
+ json (2.1.0)
30
+ method_source (0.8.2)
31
+ pry (0.10.4)
32
+ coderay (~> 1.1.0)
33
+ method_source (~> 0.8.1)
34
+ slop (~> 3.4)
35
+ rake (10.5.0)
36
+ rspec (3.6.0)
37
+ rspec-core (~> 3.6.0)
38
+ rspec-expectations (~> 3.6.0)
39
+ rspec-mocks (~> 3.6.0)
40
+ rspec-core (3.6.0)
41
+ rspec-support (~> 3.6.0)
42
+ rspec-expectations (3.6.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.6.0)
45
+ rspec-mocks (3.6.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.6.0)
48
+ rspec-support (3.6.0)
49
+ simplecov (0.14.1)
50
+ docile (~> 1.1.0)
51
+ json (>= 1.8, < 3)
52
+ simplecov-html (~> 0.10.0)
53
+ simplecov-html (0.10.1)
54
+ slop (3.6.0)
55
+ thor (0.19.4)
56
+ url (0.3.2)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ awsclean!
63
+ bundler (~> 1.14)
64
+ codecov (~> 0.1.10)
65
+ rake (~> 10.0)
66
+ rspec (~> 3.0)
67
+ rspec-expectations (~> 3.0)
68
+
69
+ RUBY VERSION
70
+ ruby 2.3.3p222
71
+
72
+ BUNDLED WITH
73
+ 1.15.1
@@ -0,0 +1,100 @@
1
+ # Awsclean
2
+
3
+ [![codecov.io](https://codecov.io/github/riboseinc/awsclean/coverage.svg?branch=master)](https://codecov.io/github/riboseinc/awsclean?branch=master)
4
+ [![Build Status](https://travis-ci.org/riboseinc/awsclean.svg?branch=master)](https://travis-ci.org/riboseinc/awsclean)
5
+
6
+ CLI to clean up AWS AMIs and ECR images.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'awsclean'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```ruby
19
+ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```ruby
25
+ gem install awsclean
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ### clean_ecr_images
31
+
32
+ Locates and deletes stale docker images stored in Amazon ECR.
33
+
34
+ An ECR image is considered stale when:
35
+
36
+ 1. It was created over `--e` days ago (default: 60).
37
+ 2. It's not being used by any ECS active task definition.
38
+
39
+ #### Listing images eligible for cleanup:
40
+
41
+ ```
42
+ $ awsclean clean_ecr_images
43
+ ```
44
+
45
+ ```
46
+ [clean_ecr_images] Checking region: us-east-2
47
+ REGION IN USE? REPOSITORY URI TAGS IMAGE ID CREATED SIZE ELEGIBLE FOR CLEANUP?
48
+ us-east-2 true 3333344444.dkr.ecr.us-east-2.amazonaws.com/foobar baz sha256:464ea4713d51 2017-04-05T21:00:45+00:00 (100 days ago) 184.0 MB false
49
+ us-east-2 true 3333344444.dkr.ecr.us-east-2.amazonaws.com/foobar latest sha256:464ea4713d51 2017-04-05T21:00:45+00:00 (100 days ago) 184.0 MB false
50
+ us-east-2 false 3333344444.dkr.ecr.us-east-2.amazonaws.com/foobar_v2 (!TAG) sha256:f47d0c6acbe9 2017-06-22T22:55:44+00:00 (21 days ago) 136.0 MB false
51
+ us-east-2 false 3333344444.dkr.ecr.us-east-2.amazonaws.com/foobar_v2 latest sha256:58e584fd7654 2017-06-23T16:13:22+00:00 (21 days ago) 15.0 MB false
52
+ ```
53
+
54
+ ### clean_amis
55
+
56
+ Locates and deletes stale Amazon Machine Images (AMI).
57
+
58
+ An AMI is considered stale when:
59
+
60
+ 1. It was created over `--e` days ago (default: 60).
61
+ 2. It's not being used by any EC2 instance.
62
+
63
+ #### Listing images eligible for cleanup:
64
+
65
+ ```
66
+ $ awsclean clean_amis
67
+ ```
68
+
69
+ The following flags are available to use in both commands:
70
+
71
+ * `--c`: delete images marked as eligible for cleanup. (default: false)
72
+ * `--e`: images older than `--e` days are considered old. (default: 60)
73
+ * `--r`: select which AWS regions to perform on. (default: all)
74
+
75
+ The `--r` flag accepts a space separated list of region names.
76
+
77
+ ```
78
+ $ awsclean clean_amis -r us-east-2 ca-central-1
79
+ ```
80
+
81
+ ## Development
82
+ After checking out the repo, run `bin/setup` to install dependencies.
83
+
84
+ Then, run `rake spec` to run the tests. You can also run `bin/console`
85
+ for an interactive prompt that will allow you to experiment.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`.
88
+
89
+ To release a new version, update the version number in
90
+ `version.rb`, and then run `bundle exec rake release`, which will create
91
+ a git tag for the version, push git commits and tags, and push the
92
+ `.gem` file to [rubygems.org](https://rubygems.org).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome. This project is intended to
97
+ be a safe, welcoming space for collaboration, and contributors are
98
+ expected to adhere to the [Contributor
99
+ Covenant](http://contributor-covenant.org) code of conduct.
100
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'awsclean/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "awsclean"
8
+ spec.version = Awsclean::VERSION
9
+ spec.authors = ["Ribose Inc."]
10
+ spec.email = ["open.source@ribose.com"]
11
+
12
+ spec.summary = %q{CLI to clean up AWS AMIs and ECR images}
13
+ spec.description = %q{CLI to clean up AWS AMIs and ECR images}
14
+ spec.homepage = "https://www.ribose.com"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "https://gems.ribose.com"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "bin"
29
+ spec.executables = "awsclean"
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "bundler", "~> 1.14"
33
+ spec.add_dependency "thor", "~> 0.19.4"
34
+ spec.add_dependency "aws-sdk", "~> 2.7.4"
35
+ spec.add_dependency "pry"
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.14"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "rspec-expectations", "~> 3.0"
41
+ spec.add_development_dependency "codecov", "~> 0.1.10"
42
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # (c) 2017 Ribose Inc.
4
+ #
5
+
6
+ #!/usr/bin/env ruby
7
+ # encoding: UTF-8
8
+
9
+ # resolve bin path, ignoring symlinks
10
+ require "pathname"
11
+ bin_file = Pathname.new(__FILE__).realpath
12
+
13
+ # add self to libpath
14
+ $:.unshift File.expand_path("../../lib", bin_file)
15
+
16
+ # Fixes https://github.com/rubygems/rubygems/issues/1420
17
+ require "rubygems/specification"
18
+
19
+ class Gem::Specification
20
+ def this; self; end
21
+ end
22
+
23
+ # start up the CLI
24
+ require "bundler/setup"
25
+ require "awsclean"
26
+
27
+ Awsclean::Commands.start(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "awsclean"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ #require "irb"
14
+ #IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ # (c) 2017 Ribose Inc.
2
+ #
3
+
4
+ require 'thor'
5
+ require 'pry'
6
+
7
+ module Awsclean
8
+ end
9
+
10
+ require 'aws-sdk'
11
+ require 'awsclean/aws_extensions'
12
+
13
+ require 'awsclean/commands'
14
+ require 'awsclean/aws_command'
15
+ require 'awsclean/ami_clean'
16
+ require 'awsclean/ecr_clean'
@@ -0,0 +1,79 @@
1
+ # (c) 2017 Ribose Inc.
2
+ #
3
+
4
+ module Awsclean
5
+ class AmiClean < AwsCommand
6
+
7
+ SERVICE_IDENTIFIER = "EC2"
8
+
9
+ IMAGE_LIST_HEADER = ['REGION', 'IN USE?', 'AMI ID', 'CREATED', 'ELEGIBLE FOR CLEANUP?']
10
+ IMAGE_LIST_FORMAT = '%-16s%-12s%-16s%-42s%-24s'
11
+
12
+ def self.run options
13
+ regions = filter_regions(options[:r])
14
+
15
+ if regions.empty?
16
+ puts "Please specify region with -r [regions, e.g., 'all' or 'us-west-1,us-east-1']"
17
+ exit 1
18
+ end
19
+
20
+ regions.each do |region|
21
+ puts "[clean_amis] checking region: #{region}"
22
+
23
+ ec2 = Aws::EC2::Client.new(region: region)
24
+
25
+ # Get a list of all AMIs in use.
26
+ #
27
+ res = ec2.describe_instances(
28
+ filters: [{ name: 'instance-state-name', values: %w(running) }],
29
+ max_results: 1000
30
+ )
31
+ instances = res.reservations.flat_map(&:instances)
32
+ amis_in_use = instances.map(&:image_id).uniq
33
+
34
+ # Get a list of all of AMI's owned by the account.
35
+ #
36
+ res = ec2.describe_images(owners: %w(self))
37
+ images = res.images
38
+
39
+ images.each do |image|
40
+ image.in_use = amis_in_use.include?(image.image_id)
41
+ image.elegible_for_cleanup = (image.stale?(options[:e]) && !image.in_use)
42
+ end
43
+
44
+ # We always show a list of images
45
+ #
46
+ puts build_image_list_header
47
+ images.each { |image| puts build_image_list_entry(region, image) }
48
+
49
+ if options[:c]
50
+ images.select(&:elegible_for_cleanup).each do |image|
51
+ puts "[#{region}] Deregistering image: #{image.image_id} #{image.creation_date}"
52
+ ec2.deregister_image(image_id: image.image_id)
53
+
54
+ image.block_device_mappings.each { |bdm|
55
+ puts "[#{region}] Deregistering image #{image.image_id}: deleting snapshot #{bdm.ebs.snapshot_id}..."
56
+ ec2.delete_snapshot(snapshot_id: bdm.ebs.snapshot_id)
57
+ }
58
+ puts "[#{region}] Deregister image complete: #{image.image_id}"
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def self.build_image_list_header
67
+ sprintf(IMAGE_LIST_FORMAT, *IMAGE_LIST_HEADER)
68
+ end
69
+
70
+ def self.build_image_list_entry(region, image)
71
+ created = DateTime.parse(image.creation_date).iso8601
72
+ created << " (#{image.days_since_creation} days ago)"
73
+
74
+ sprintf(IMAGE_LIST_FORMAT,
75
+ region, image.in_use, image.image_id,
76
+ created, image.elegible_for_cleanup)
77
+ end
78
+ end
79
+ end