dctl_rb 0.6.2 → 0.7.0

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
  SHA256:
3
- metadata.gz: ab76aae888a88ca60e89189df1cf1e94724dc96c01905d08d83b966485ece0fa
4
- data.tar.gz: 9941b6abe70021a19524070cb15ced5d58d85a8f9ed0e14b67f240f1d798d53d
3
+ metadata.gz: 119ea41f3d1d29e0442e04bb4dc05bec0a100ca85cd990ea8b57a2bb389437d4
4
+ data.tar.gz: 1be5b2cc464cdced2b932e8b06821bb7202453749e0bc5fa7049b1623e064d06
5
5
  SHA512:
6
- metadata.gz: 52144fb9f09699821ad578189c3858b3c782505d5e9cec09f001f6268a4467c035c1add79d7e7abcec5807f176e874a1fd4e9d87133a3d2ac5d186af9f0dd21a
7
- data.tar.gz: 88612640a611006756279318c80970e446a33349b872b6a0b738a09e6d8bf71069a675c507dd9239b7b904da767d46e514f95155d77fcb871ec3e46311e17e3c
6
+ metadata.gz: 352e33ad3787e18c7ac195ab4403766c5423eeba52c0613c24c651e2eff6ff4266123a97f0708bf2c5552dece773cd6d2ba9c45eb5c0ac2452ff407ccaab233e
7
+ data.tar.gz: 31cbffda220c6eee2d8e38ac7137a15ce48e761d979bb5c1dfef984bc930d4c5a924642fe60fd9664a137229cd97a45bcbdb27a5b2882c4bbb55fc11371a269f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dctl_rb (0.6.2)
4
+ dctl_rb (0.7.0)
5
5
  config (>= 1, < 2)
6
6
  rainbow (>= 2.2, < 3)
7
7
  thor (~> 0.17, >= 0.17.0)
@@ -27,7 +27,7 @@ GEM
27
27
  dry-container (0.6.0)
28
28
  concurrent-ruby (~> 1.0)
29
29
  dry-configurable (~> 0.1, >= 0.1.3)
30
- dry-core (0.4.1)
30
+ dry-core (0.4.2)
31
31
  concurrent-ruby (~> 1.0)
32
32
  dry-equalizer (0.2.0)
33
33
  dry-logic (0.4.2)
data/exe/dctl CHANGED
@@ -5,9 +5,15 @@ require "thor"
5
5
  require "pty"
6
6
  require "rainbow"
7
7
  require "config"
8
+ require "open3"
8
9
 
9
10
  module Dctl
10
11
  class Cli < Thor
12
+
13
+ ERROR_COLOR = :red
14
+ SUCCESS_COLOR = :green
15
+ CMD_COLOR = :dimgray
16
+
11
17
  class_option :env, default: "dev", type: :string
12
18
 
13
19
  desc "build", "Build images. Pass image name to build a specific one; otherwise builds all"
@@ -150,15 +156,44 @@ module Dctl
150
156
  end
151
157
 
152
158
  desc "cleanup", "cleans up dangling docker images"
159
+ option :keep, type: :numeric, default: -1, desc: "How many old images to keep around, e.g. \"3\" will keep the current version plus the three next most recent. Use -1 to keep all."
153
160
  def cleanup
154
- dangling = `#{sudo}docker images --filter dangling=true -q`.split("\n")
161
+ to_remove = []
162
+ to_remove += `#{sudo}docker images --filter dangling=true -q`.split("\n")
163
+
164
+ if options[:keep] != -1
165
+ keep_last = options[:keep]
166
+ dctl = Dctl::Main.new env: options[:env]
167
+ dctl.expand_images.each do |image|
168
+ current_version = dctl.current_version_for_image(image).to_i
169
+ keep_after_tag = current_version - keep_last
170
+
171
+ next if keep_after_tag < 0
172
+
173
+ cmd = "#{sudo}docker images #{dctl.image_tag(image, version: nil)} --filter before=#{dctl.image_tag(image, version: keep_after_tag)} -q"
174
+ puts Rainbow(cmd).fg CMD_COLOR
175
+ stdout, stderr, status = Open3.capture3(cmd)
176
+ if status.success?
177
+ to_remove += stdout.split("\n")
178
+ elsif stderr.include? "No such image"
179
+ # Specified before filter did not exist on current machine. Loop
180
+ # through all old images to manually reconstruct.
181
+ Array(0..keep_after_tag).each do |version|
182
+ cmd = "#{sudo}docker images #{dctl.image_tag(image, version: version)} -q"
183
+ puts Rainbow(cmd).fg CMD_COLOR
184
+ image_id = `#{cmd}`.strip
185
+ to_remove << image_id unless image_id.empty?
186
+ end
187
+ end
188
+ end
189
+ end
155
190
 
156
- if dangling.none?
157
- puts "No images to cleanup. Yay!"
191
+ if to_remove.none?
192
+ puts Rainbow("No images to cleanup. Yay!").fg SUCCESS_COLOR
158
193
  exit 0
159
194
  end
160
195
 
161
- stream_output "#{sudo}docker rmi -f #{dangling.join(" ")}", exec: true
196
+ stream_output "#{sudo}docker rmi -f #{to_remove.join(" ")}", exec: true
162
197
  end
163
198
 
164
199
  desc "bash CONTAINER", "Create a new instance of the given image with a bash prompt"
@@ -186,7 +221,7 @@ module Dctl
186
221
  container = `#{cmd}`.chomp
187
222
 
188
223
  if container.empty?
189
- puts Rainbow("No running containers for image #{image}").red
224
+ puts Rainbow("No running containers for image #{image}").fg ERROR_COLOR
190
225
  exit 1
191
226
  end
192
227
 
@@ -200,7 +235,7 @@ module Dctl
200
235
 
201
236
  no_commands do
202
237
  def stream_output(string, print_command: true, exec: false)
203
- puts string if print_command
238
+ puts Rainbow(string).fg(CMD_COLOR) if print_command
204
239
  if exec
205
240
  exec string
206
241
  else
data/lib/dctl/main.rb CHANGED
@@ -11,13 +11,22 @@ module Dctl
11
11
  # Generate the full tag for the given image, concatenating the org,
12
12
  # project, env, image name, and version.
13
13
  #
14
+ # Pass `version: nil` to exclude the version portion.
15
+ #
14
16
  # @example
15
17
  # image_tag("app") # => jutonz/dctl-dev-app:1
16
- def image_tag(image, version: versions[image])
18
+ def image_tag(image, version: current_version_for_image(image))
17
19
  org = settings.org
18
20
  project = settings.project
19
21
 
20
- "#{org}/#{project}-#{env}-#{image}:#{version}"
22
+ tag = "#{org}/#{project}-#{env}-#{image}"
23
+ tag += ":#{version}" if !version.nil?
24
+
25
+ tag
26
+ end
27
+
28
+ def current_version_for_image(image)
29
+ versions[image]
21
30
  end
22
31
 
23
32
  ##
data/lib/dctl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dctl
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dctl_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Toniazzo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-15 00:00:00.000000000 Z
11
+ date: 2017-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler