aws-ec2 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/Gemfile.lock +1 -1
- data/lib/aws-ec2.rb +1 -0
- data/lib/aws_ec2/clean.rb +12 -0
- data/lib/aws_ec2/cleaner.rb +5 -0
- data/lib/aws_ec2/cleaner/ami.rb +40 -0
- data/lib/aws_ec2/cli.rb +4 -0
- data/lib/aws_ec2/help/clean/ami.md +21 -0
- data/lib/aws_ec2/version.rb +1 -1
- data/lib/aws_ec2/wait.rb +0 -1
- data/spec/lib/cli_spec.rb +5 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b609d157c4ac6348d6fa370a17549c95987d2637060a34f84da9ad3dd303df80
|
4
|
+
data.tar.gz: d6c436bca85ddac006a853f6a6fe3bdc6cb848dc524b2e913912069047755e89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 410535c2bda5df7ab1d97bcc015158be003fa41cf41dda9c2ff5c97aa78f493a4c7eeb38080eaf3c93b14bb64344708adff7422eda516c6f987da38ff0842434
|
7
|
+
data.tar.gz: 44fc0c559ad6565742c66c0022097c93509bf6aad785b912d506cf75bf6d0bf728fe423555678c33cc7135cef3613369e7a828cc160baf0a86d69f8da8ac084d
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
|
5
5
|
|
6
|
+
## [1.3.0]
|
7
|
+
- Merge pull request #7 from tongueroo/clean: aws-ec2 clean ami command
|
8
|
+
|
6
9
|
## [1.2.2]
|
7
10
|
- add render_me_pretty as gem instead of submodule
|
8
11
|
|
data/Gemfile.lock
CHANGED
data/lib/aws-ec2.rb
CHANGED
@@ -13,6 +13,7 @@ module AwsEc2
|
|
13
13
|
autoload :Create, "aws_ec2/create"
|
14
14
|
autoload :Ami, "aws_ec2/ami"
|
15
15
|
autoload :Wait, "aws_ec2/wait"
|
16
|
+
autoload :Clean, "aws_ec2/clean"
|
16
17
|
autoload :Template, "aws_ec2/template"
|
17
18
|
autoload :Script, "aws_ec2/script"
|
18
19
|
autoload :Config, "aws_ec2/config"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module AwsEc2
|
2
|
+
autoload :Cleaner, 'aws_ec2/cleaner'
|
3
|
+
|
4
|
+
class Clean < Command
|
5
|
+
desc "ami", "Clean until AMI available."
|
6
|
+
long_desc Help.text("clean:ami")
|
7
|
+
option :keep, type: :numeric, default: 2, desc: "Number of images to keep"
|
8
|
+
def ami(query)
|
9
|
+
Cleaner::Ami.new(options.merge(query: query)).clean
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module AwsEc2::Cleaner
|
2
|
+
class Ami < AwsEc2::Base
|
3
|
+
include AwsEc2::AwsService
|
4
|
+
|
5
|
+
def clean
|
6
|
+
query = @options[:query] + "*" # use query as base of name for search
|
7
|
+
keep = @options[:keep] || 2
|
8
|
+
puts "Cleaning out old AMIs with base name: #{@options[:query]}"
|
9
|
+
return if ENV['TEST']
|
10
|
+
|
11
|
+
images = search_ami(query)
|
12
|
+
images.sort_by! { |i| i.name }.reverse
|
13
|
+
delete_list = images[keep..-1] || []
|
14
|
+
puts "Deleting #{delete_list.size} images."
|
15
|
+
delete_list.each do |i|
|
16
|
+
puts "Deleting image: #{i.image_id} #{i.name}"
|
17
|
+
delete(i.image_id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def delete(image_id)
|
23
|
+
ec2.deregister_image(image_id: image_id)
|
24
|
+
rescue Aws::EC2::Errors::InvalidAMIIDUnavailable
|
25
|
+
# happens when image was just deleted but its still
|
26
|
+
# showing up as available when calling describe_images
|
27
|
+
puts "WARN: #{e.message}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def search_ami(query, owners=["self"])
|
31
|
+
ec2.describe_images(
|
32
|
+
owners: owners,
|
33
|
+
filters: [
|
34
|
+
{name: "name", values: [query]},
|
35
|
+
{name: "state", values: ["available"]}
|
36
|
+
]
|
37
|
+
).images
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/aws_ec2/cli.rb
CHANGED
@@ -3,6 +3,10 @@ module AwsEc2
|
|
3
3
|
class_option :noop, type: :boolean
|
4
4
|
class_option :profile, desc: "profile name to use"
|
5
5
|
|
6
|
+
desc "clean SUBCOMMAND", "clean subcommands"
|
7
|
+
long_desc Help.text(:clean)
|
8
|
+
subcommand "clean", Clean
|
9
|
+
|
6
10
|
desc "wait SUBCOMMAND", "wait subcommands"
|
7
11
|
long_desc Help.text(:wait)
|
8
12
|
subcommand "wait", Wait
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Examples:
|
2
|
+
|
3
|
+
$ aws-ec2 clean ami base-amazonlinux2
|
4
|
+
$ aws-ec2 clean ami base-ubuntu --keep 5
|
5
|
+
|
6
|
+
Deletes old AMIs using the provided name as the base portion of the AMI name to search for.
|
7
|
+
|
8
|
+
Let's say you have these images:
|
9
|
+
|
10
|
+
base-ubuntu_2018-03-25-04-20
|
11
|
+
base-ubuntu_2018-03-25-03-39
|
12
|
+
base-ubuntu_2018-03-25-02-57
|
13
|
+
base-ubuntu_2018-03-25-02-47
|
14
|
+
base-ubuntu_2018-03-25-02-43
|
15
|
+
base-ubuntu_2018-03-23-00-15
|
16
|
+
|
17
|
+
Running:
|
18
|
+
|
19
|
+
$ aws-ec2 clean ami base-ubuntu
|
20
|
+
|
21
|
+
Would delete all images and keep the 2 most recent AMIs. The default `--keep` value is 2.
|
data/lib/aws_ec2/version.rb
CHANGED
data/lib/aws_ec2/wait.rb
CHANGED
data/spec/lib/cli_spec.rb
CHANGED
@@ -19,6 +19,11 @@ describe AwsEc2::CLI do
|
|
19
19
|
expect(out).to include("Waiting for")
|
20
20
|
end
|
21
21
|
|
22
|
+
it "clean ami" do
|
23
|
+
out = execute("exe/aws-ec2 clean ami imagebasename")
|
24
|
+
expect(out).to include("Cleaning out old AMIs")
|
25
|
+
end
|
26
|
+
|
22
27
|
commands = {
|
23
28
|
"am" => "ami",
|
24
29
|
"compile" => "--profile",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aws-ec2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tung Nguyen
|
@@ -253,6 +253,9 @@ files:
|
|
253
253
|
- lib/aws_ec2/ami.rb
|
254
254
|
- lib/aws_ec2/aws_service.rb
|
255
255
|
- lib/aws_ec2/base.rb
|
256
|
+
- lib/aws_ec2/clean.rb
|
257
|
+
- lib/aws_ec2/cleaner.rb
|
258
|
+
- lib/aws_ec2/cleaner/ami.rb
|
256
259
|
- lib/aws_ec2/cli.rb
|
257
260
|
- lib/aws_ec2/command.rb
|
258
261
|
- lib/aws_ec2/completer.rb
|
@@ -266,6 +269,7 @@ files:
|
|
266
269
|
- lib/aws_ec2/dotenv.rb
|
267
270
|
- lib/aws_ec2/help.rb
|
268
271
|
- lib/aws_ec2/help/ami.md
|
272
|
+
- lib/aws_ec2/help/clean/ami.md
|
269
273
|
- lib/aws_ec2/help/compile.md
|
270
274
|
- lib/aws_ec2/help/completion.md
|
271
275
|
- lib/aws_ec2/help/completion_script.md
|