rumrunner 0.2.5 → 0.2.6

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: 21ff1aee760e860283bf640e87bbdd97f5e6995caa5cdce7187c829e29417a67
4
- data.tar.gz: f2acce2d5a033fce78a9010395063a8f44c8a45348de6eb08728aea43d873720
3
+ metadata.gz: 1167b81015a97308cc3d5e9be344d455ffbc0d9d8966a9c392a2385d91930293
4
+ data.tar.gz: a4cc455763338c7f4a45ab80291f9f496c9deb44e76af5998a43d3272a0b1200
5
5
  SHA512:
6
- metadata.gz: 3b04b16707b81b88364e880fd3ba15dab605853735eaa67b537a1f9f293824a341befbceea5634ef1d8b6bbcdcf9bd63be841c0dfb768af97a8d662ad0870456
7
- data.tar.gz: c9116ff64a6b9f4d77e724f49ec903f3c917a81acaab9b7905d4a96c0623aae6c77a3c9503dcb7aae4597c65458ee269dbf755ff629d4208d3df9ba7a4cfaa3d
6
+ metadata.gz: 4488ca01b9c39cea03ceb77db44997acc91554262813cd96c9edc952165ddf3b32a91cc8fd9fee526edc81c94b5919c6e3213707d55fc3667a20e17d2a2cc23e
7
+ data.tar.gz: 1984f5ccd2768f7df0d75e18ac39d374b687920e2a434362c2ef32e2bbdb198ef68e4fbc0fb54acb264588ad7ca3e123b6039bb5b2eb5f943864bc180f7347fd
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
-
2
- ![Rum Runner](https://github.com/amancevice/rumrunner/blob/master/rumrunner.png?raw=true)
1
+ [![Rum Runner](https://github.com/amancevice/rumrunner/blob/master/rumrunner.png?raw=true)](https://github.com/amancevice/rumrunner)
3
2
  [![Build Status](https://travis-ci.com/amancevice/rumrunner.svg?branch=master)](https://travis-ci.com/amancevice/rumrunner)
4
- [![codecov](https://codecov.io/gh/amancevice/rumrunner/branch/master/graph/badge.svg)](https://codecov.io/gh/amancevice/rumrunner)
5
3
  [![Gem Version](https://badge.fury.io/rb/rumrunner.svg)](https://badge.fury.io/rb/rumrunner)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/4b56853acc5cbb254125/test_coverage)](https://codeclimate.com/github/amancevice/rumrunner/test_coverage)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/4b56853acc5cbb254125/maintainability)](https://codeclimate.com/github/amancevice/rumrunner/maintainability)
6
+
6
7
 
7
8
  Rum Runner is a Rake-based utility for building multi-stage Dockerfiles.
8
9
 
@@ -36,16 +37,8 @@ gem install rumrunner
36
37
  Use the `Rum.init` helper to create a template Rumfile for your project:
37
38
 
38
39
  ```bash
39
- # Install the gem
40
40
  gem install rumrunner
41
-
42
- # Navigate to the location of your Dockerfile
43
- cd /path/to/your/Dockerfile/project/
44
-
45
- # Create a basic Rumfile
46
41
  ruby -r rumrunner -e Rum.init > Rumfile
47
-
48
- # View available tasks
49
42
  rum --tasks
50
43
  ```
51
44
 
@@ -145,8 +138,8 @@ rum :image_name do
145
138
  cmd %w{echo hello}
146
139
  end
147
140
 
148
- # rake fizz => docker build --tag image_name .
149
- # rake buzz => docker run --rm image_name echo hello
141
+ # rum fizz => docker build --tag image_name .
142
+ # rum buzz => docker run --rm image_name echo hello
150
143
  end
151
144
  ```
152
145
 
@@ -133,8 +133,7 @@ module Rum
133
133
  #
134
134
  def each
135
135
  @data.each do |name, values|
136
- option = name.length == 1 ? "-#{name}" : "--#{name.to_s.gsub(/_/, "-")}"
137
- yield option if values.empty?
136
+ option = flagify name
138
137
  values.each do |value|
139
138
  if value.is_a?(Hash)
140
139
  value.map{|kv| kv.join("=") }.each do |val|
@@ -163,6 +162,14 @@ module Rum
163
162
  def to_s
164
163
  to_a.join(" ")
165
164
  end
165
+
166
+ private
167
+
168
+ ##
169
+ # Convert +OPTION+ to +--option+
170
+ def flagify(name)
171
+ name.length == 1 ? "-#{name}" : "--#{name.to_s.gsub(/_/, "-")}"
172
+ end
166
173
  end
167
174
 
168
175
  ##
@@ -249,7 +256,7 @@ module Rum
249
256
  ##
250
257
  # Get the image reference without the @tag component.
251
258
  def family
252
- File.join *[@registry, @username, @name].compact.map(&:to_s)
259
+ File.join(*[@registry, @username, @name].compact.map(&:to_s))
253
260
  end
254
261
 
255
262
  ##
@@ -272,25 +279,22 @@ module Rum
272
279
  # Image.parse("registry:5000/username/image:tag")
273
280
  #
274
281
  def parse(string_or_symbol)
275
- string = string_or_symbol.to_s
276
- if string.count("/").zero? && string.count(":").zero?
277
- # image
278
- new name: string
279
- elsif string.count("/").zero?
280
- # image:tag
282
+ string = string_or_symbol.to_s
283
+ slash_count = string.count("/")
284
+
285
+ # image[:tag]
286
+ if slash_count.zero?
281
287
  name, tag = string.split(/:/)
282
288
  new name: name, tag: tag
283
- elsif string.count("/") == 1 && string.count(":").zero?
284
- # username/image
285
- username, name = string.split(/\//)
286
- new name: name, username: username
287
- elsif string.count("/") == 1
288
- # username/image:tag
289
+
290
+ # username/image[:tag]
291
+ elsif slash_count == 1
289
292
  username, name_tag = string.split(/\//)
290
293
  name, tag = name_tag.split(/:/)
291
294
  new name: name, username: username, tag: tag
295
+
296
+ # registry/username/image[:tag]
292
297
  else
293
- # registry/username/image[:tag]
294
298
  registry, username, name_tag = string.split(/\//)
295
299
  name, tag = name_tag.split(/:/)
296
300
  new name: name, registry: registry, username: username, tag: tag
@@ -10,29 +10,37 @@ module Rum
10
10
  # $ ruby -r rumrunner -e Rum.init > Rumfile
11
11
  #
12
12
  def init(input = nil, stdin = $stdin, stdout = $stdout, stderr = $stderr)
13
- # Get image name from stdin
14
- stderr.write "Docker image name [#{default = File.split(Dir.pwd).last}]: "
15
- input ||= stdin.gets.chomp
16
- image = Docker::Image.parse(input.empty? ? default : input)
13
+ # Get image name from $stdin
14
+ image = gets_image input, stdin, stderr
17
15
 
18
16
  # Begin Rumfile
19
17
  stdout.write "#!/usr/bin/env ruby\n"
20
18
  stdout.write "rum :\"#{image.family}\" do\n"
19
+ stdout.write parse_stages "Dockerfile" if File.exist? "Dockerfile"
20
+ stdout.write "end\n"
21
+ end
21
22
 
22
- # Put stages
23
- if File.exists? "Dockerfile"
24
- stages = File.read("Dockerfile").scan(/^FROM .*? AS (.*?)$/).flatten
25
- stages.reverse.zip(stages.reverse[1..-1]).reverse.each do |stage, dep|
26
- if dep.nil?
27
- stdout.write " stage :\"#{stage}\"\n"
28
- else
29
- stdout.write " stage :\"#{stage}\" => :\"#{dep}\"\n"
30
- end
31
- end unless stages.empty?
23
+ private
24
+
25
+ ##
26
+ # Get Docker image from input (taken from <tt>$stdin</tt> if nil)
27
+ def gets_image(input = nil, stdin = $stdin, stderr = $stderr)
28
+ if input.nil?
29
+ default = File.split(Dir.pwd).last
30
+ stderr.write "Docker image name [#{default}]: "
31
+ input = stdin.gets.chomp
32
32
  end
33
+ Docker::Image.parse(input.empty? ? default : input)
34
+ end
33
35
 
34
- # Fin
35
- stdout.write "end\n"
36
+ ##
37
+ # Parse stages from Dockerfile
38
+ def parse_stages(dockerfile)
39
+ stages = File.read(dockerfile).scan(/^FROM .*? AS (.*?)$/).flatten
40
+ deps = [nil] + stages[0..-2]
41
+ lines = stages.zip(deps).map do |stage, dep|
42
+ dep.nil? ? %{ stage :"#{stage}"\n} : %{ stage :"#{stage}" => :"#{dep}"\n}
43
+ end.join
36
44
  end
37
45
  end
38
46
  end
@@ -96,44 +96,16 @@ module Rum
96
96
  end
97
97
 
98
98
  # Build stage and save digest in iidfile
99
- file iidfile => iiddeps do
100
- build = Docker::Build.new(options: build_options, &block)
101
- build.with_defaults(iidfile: iidfile, tag: image, target: name)
102
- sh build.to_s
103
- end
99
+ stage_file iidfile, iiddeps, tag: image, target: name, &block
104
100
 
105
101
  # Shortcut to build stage by name
106
- desc "Build `#{name}` stage"
107
- task name => iidfile
102
+ stage_task name, iidfile
108
103
 
109
104
  # Shell into stage
110
- desc "Shell into `#{name}` stage"
111
- task :"#{name}:shell", [:shell] => iidfile do |t,args|
112
- digest = File.read(iidfile)
113
- shell = args.any? ? args.to_a.join(" ") : "/bin/sh"
114
- run = Docker::Run.new(options: run_options)
115
- .entrypoint(shell)
116
- .interactive(true)
117
- .rm(true)
118
- .tty(true)
119
- .image(digest)
120
- sh run.to_s
121
- end
105
+ stage_shell name, iidfile
122
106
 
123
107
  # Clean stage
124
- desc "Remove any temporary images and products from `#{name}` stage"
125
- task :"#{name}:clean" do
126
- if File.exists? iidfile
127
- sh "docker", "image", "rm", "--force", File.read(iidfile)
128
- rm iidfile
129
- end
130
- end
131
-
132
- # Add stage to general clean
133
- task :clean => :"#{name}:clean"
134
-
135
- # Ensure subsequent stages are cleaned before this one
136
- deps.each{|dep| task :"#{dep}:clean" => :"#{name}:clean" }
108
+ stage_clean name, iidfile, deps
137
109
  end
138
110
 
139
111
  ##
@@ -158,19 +130,9 @@ module Rum
158
130
  deps << path
159
131
  end
160
132
 
161
- desc "Build `#{name}`"
162
- file name => deps do
163
- digest = File.read(iidfile)
164
- run = Docker::Run.new(options: run_options, image: digest, cmd: ["cat", name], &block)
165
- run.with_defaults(rm: true)
166
- sh "#{run} > #{name}"
167
- end
133
+ artifact_file name, deps, iidfile, &block
168
134
 
169
- desc "Remove any generated files"
170
- task :clobber do
171
- rm name if File.exists?(name)
172
- rm_r path if Dir.exists?(path) && path != "."
173
- end
135
+ artifact_clobber name, path
174
136
  end
175
137
 
176
138
  ##
@@ -230,8 +192,96 @@ module Rum
230
192
  sh "docker", "image", "rm", "--force", File.read(name) if File.file?(name)
231
193
  rm_r name
232
194
  end
233
- rm_r root if Dir.exists?(root)
195
+ rm_r root if Dir.exist?(root)
196
+ end
197
+ end
198
+
199
+ ##
200
+ # Install file task for stage and save digest in iidfile
201
+ def stage_file(iidfile, iiddeps, tag:, target:, &block)
202
+ file iidfile => iiddeps do
203
+ build = Docker::Build.new(options: build_options, &block)
204
+ build.with_defaults(iidfile: iidfile, tag: tag, target: target)
205
+ sh build.to_s
206
+ end
207
+ end
208
+
209
+ ##
210
+ # Install alias task for building stage
211
+ def stage_task(name, iidfile)
212
+ desc "Build `#{name}` stage"
213
+ task name => iidfile
214
+ end
215
+
216
+ ##
217
+ # Install shell task for shelling into stage
218
+ def stage_shell(name, iidfile)
219
+ desc "Shell into `#{name}` stage"
220
+ task task_name(shell: name), [:shell] => iidfile do |t,args|
221
+ digest = File.read(iidfile)
222
+ shell = args.any? ? args.to_a.join(" ") : "/bin/sh"
223
+ run = Docker::Run.new(options: run_options)
224
+ .entrypoint(shell)
225
+ .interactive(true)
226
+ .rm(true)
227
+ .tty(true)
228
+ .image(digest)
229
+ sh run.to_s
230
+ end
231
+ end
232
+
233
+ ##
234
+ # Install clean tasks for cleaning up stage image and iidfile
235
+ def stage_clean(name, iidfile, deps)
236
+ # Clean stage image
237
+ desc "Remove any temporary images and products from `#{name}` stage"
238
+ task task_name(clean: name) do
239
+ if File.exist? iidfile
240
+ sh "docker", "image", "rm", "--force", File.read(iidfile)
241
+ rm iidfile
242
+ end
234
243
  end
244
+
245
+ # Add stage to general clean
246
+ task :clean => task_name(clean: name)
247
+
248
+ # Ensure subsequent stages are cleaned before this one
249
+ deps.each{|dep| task task_name(clean: dep) => task_name(clean: name) }
250
+ end
251
+
252
+ ##
253
+ # Install file task for artifact
254
+ def artifact_file(name, deps, iidfile, &block)
255
+ desc "Build `#{name}`"
256
+ file name => deps do
257
+ digest = File.read(iidfile)
258
+ run = Docker::Run.new(options: run_options, image: digest, cmd: ["cat", name], &block)
259
+ run.with_defaults(rm: true)
260
+ sh "#{run} > #{name}"
261
+ end
262
+ end
263
+
264
+ ##
265
+ # Install clobber tasks for cleaning up generated files
266
+ def artifact_clobber(name, path)
267
+ desc "Remove any generated files"
268
+ task :clobber do
269
+ rm name if File.exist?(name)
270
+ rm_r path if Dir.exist?(path) && path != "."
271
+ end
272
+ end
273
+
274
+ ##
275
+ # Get name of support task
276
+ def task_name(verb_stage)
277
+ case ENV["RUM_TASK_NAME"]&.upcase
278
+ when "STAGE_FIRST"
279
+ verb_stage.first.reverse
280
+ when "VERB_FIRST"
281
+ verb_stage.first
282
+ else
283
+ verb_stage.first.reverse
284
+ end.join(":").to_sym
235
285
  end
236
286
  end
237
287
  end
@@ -1,5 +1,5 @@
1
1
  module Rum
2
2
  ##
3
3
  # Rum Runner gem version.
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.6"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rumrunner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Mancevice
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-24 00:00:00.000000000 Z
11
+ date: 2019-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake