linecook-gem 0.7.25 → 0.7.26

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c307eea7819a2be6346a2485ce76736ec73dd098
4
- data.tar.gz: ed36b6cedb653c2fbf534b3032211b8583c4b4f0
3
+ metadata.gz: 192443005342695e7942ab306489e53cc8315919
4
+ data.tar.gz: 00df5e02a21f7a5cc67d2434c38cae9a6df2b2f5
5
5
  SHA512:
6
- metadata.gz: f35b58e984a7268f76bd015bd21b902197a5cbb490f0f234bd669546be3e5f41f6a24f94f40163e6ba3ab538c34029be743544b4bc2ac6059a232f65a843568d
7
- data.tar.gz: 40223ccb263a9c95db3adf732ae30c7d761a99f1c2631f7eb470429c5bff910f886b38d963612826e8c19ec784d2d0cd4722bc656d2312976f1c6790f50c8091
6
+ metadata.gz: 8e5c348a118bb4a76ddbb925a81961c4d60daf3003cce5e3df559845af2aa9733d8c29cfc8538922ddccede1f3037de9b3f10b3fd2e221d5cf0d33541451c2fe
7
+ data.tar.gz: 842c7e155423a6ccc922078cd78a39f8b1d27c3a8bf89c327194fa60046499eff61834f6d6fa81b39179eff2a4135346cbc27eef3ac337e9176d910eee49459c
@@ -10,8 +10,22 @@ class Image < Thor
10
10
 
11
11
 
12
12
  desc 'list', 'List images'
13
+ method_option :name, type: :string, required: false, banner: 'NAME', desc: 'Name of the image to fetch.', aliases: '-n'
14
+ method_option :group, type: :string, required: false, banner: 'ID', desc: 'Group of image to list', aliases: '-g'
13
15
  def list
14
- puts Linecook::Image.new(nil, nil, nil).list
16
+ opts = options.symbolize_keys
17
+ image = Linecook::Image.new(opts[:name], opts[:group], nil)
18
+ puts image.list
19
+ end
20
+
21
+ desc 'clean', 'Cleanup old images'
22
+ method_option :name, type: :string, required: true, banner: 'NAME', desc: 'Name of the image to fetch.', aliases: '-n'
23
+ method_option :group, type: :string, required: true, banner: 'ID', desc: 'Group of image to list', aliases: '-g'
24
+ method_option :retention, type: :numeric, required: false, banner: 'RETENTION', desc: 'Images to keep', aliases: '-k', default: 5
25
+ def clean
26
+ opts = options.symbolize_keys
27
+ image = Linecook::Image.new(opts[:name], opts[:group], nil)
28
+ puts "Cleaned up #{image.clean(opts[:retention]).length} images"
15
29
  end
16
30
 
17
31
  desc 'fetch', 'Fetch and decrypt an image'
@@ -64,6 +64,10 @@ module Linecook
64
64
  provider.latest(@group)
65
65
  end
66
66
 
67
+ def clean(retention)
68
+ provider.clean(retention, @group)
69
+ end
70
+
67
71
  private
68
72
 
69
73
  def image_path
@@ -18,6 +18,17 @@ module Linecook
18
18
  list_objects(group: group).map{ |x| x.key if x.key =~ /\.tar\.xz/ }.compact
19
19
  end
20
20
 
21
+ def clean(retention, group)
22
+ full_set = list(group: group)
23
+ keep = full_set.reverse.take(retention)
24
+ destroy = full_set - keep
25
+ destroy.each_slice(1000).each do |garbage|
26
+ to_destroy = garbage.map { |x| { key: x } }
27
+ client.delete_objects(bucket: Linecook.config[:aws][:s3][:bucket], delete: { objects: to_destroy} )
28
+ end
29
+ return destroy
30
+ end
31
+
21
32
  def latest(group)
22
33
  objects = list_objects(group: group).sort! { |a,b| a.last_modified <=> b.last_modified }
23
34
  key = objects.last ? objects.last.key : nil
@@ -48,7 +59,15 @@ module Linecook
48
59
  private
49
60
 
50
61
  def list_objects(group: nil)
51
- client.list_objects(bucket: Linecook.config[:aws][:s3][:bucket], prefix: File.join([PREFIX, group].compact)).contents
62
+ contents = []
63
+ marker = nil
64
+ loop do
65
+ resp = client.list_objects(bucket: Linecook.config[:aws][:s3][:bucket], prefix: File.join([PREFIX, group].compact), marker: marker)
66
+ break unless resp.contents.last
67
+ marker = resp.contents.last.key
68
+ contents += resp.contents
69
+ end
70
+ contents.sort { |a,b| a.last_modified <=> b.last_modified }
52
71
  end
53
72
 
54
73
  def client
@@ -1,3 +1,3 @@
1
1
  module Linecook
2
- VERSION = '0.7.25'
2
+ VERSION = '0.7.26'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linecook-gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.25
4
+ version: 0.7.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Hamel