dockerun 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -11
  3. data/Rakefile +0 -2
  4. data/Rakefile.bak +8 -0
  5. data/dockerspec.sample +104 -0
  6. data/{dockerun.gemspec → dockerun.gemspec.bak} +20 -22
  7. data/exe/dockerun +8 -115
  8. data/lib/dockerun/cli/command.rb +133 -0
  9. data/lib/dockerun/cli/command_factory.rb +313 -0
  10. data/lib/dockerun/cli/command_result.rb +67 -0
  11. data/lib/dockerun/cli/delete_container.rb +55 -0
  12. data/lib/dockerun/cli/delete_image.rb +99 -0
  13. data/lib/dockerun/cli/run.rb +39 -0
  14. data/lib/dockerun/cli/user_info.rb +39 -0
  15. data/lib/dockerun/cli_engine.rb +104 -0
  16. data/lib/dockerun/config.rb +40 -70
  17. data/lib/dockerun/context/rubygems.rb +142 -0
  18. data/lib/dockerun/dfile.rb +204 -0
  19. data/lib/dockerun/docker_cli.rb +124 -0
  20. data/lib/dockerun/dsl.rb +291 -0
  21. data/lib/dockerun/version.rb +1 -1
  22. data/lib/dockerun.rb +31 -24
  23. data/sig/dockerun.rbs +4 -0
  24. metadata +62 -73
  25. data/.release_history.yml +0 -56
  26. data/Dockerfile.dockerun +0 -38
  27. data/Gemfile +0 -15
  28. data/Gemfile.lock +0 -94
  29. data/bin/console +0 -15
  30. data/bin/setup +0 -8
  31. data/lib/dockerun/bundler_helper.rb +0 -25
  32. data/lib/dockerun/cli_prompt.rb +0 -18
  33. data/lib/dockerun/command/dockerun.rb +0 -30
  34. data/lib/dockerun/command/init.rb +0 -79
  35. data/lib/dockerun/command/remove_container.rb +0 -99
  36. data/lib/dockerun/command/reset_image.rb +0 -107
  37. data/lib/dockerun/command/run.rb +0 -221
  38. data/lib/dockerun/command/run_new_container.rb +0 -94
  39. data/lib/dockerun/command/run_new_image.rb +0 -89
  40. data/lib/dockerun/command/stop_container.rb +0 -90
  41. data/lib/dockerun/docker_command_factory_helper.rb +0 -19
  42. data/lib/dockerun/docker_container_helper.rb +0 -204
  43. data/lib/dockerun/docker_image_helper.rb +0 -381
  44. data/lib/dockerun/template/general_template_writer.rb +0 -14
  45. data/lib/dockerun/template/jruby_template_writer.rb +0 -24
  46. data/lib/dockerun/template/template_engine.rb +0 -27
  47. data/lib/dockerun/template/template_writer.rb +0 -102
  48. data/lib/dockerun/user_info.rb +0 -37
  49. data/template/Dockerfile_general.erb +0 -41
  50. data/template/setup_ruby_devenv.rb.erb +0 -22
@@ -1,204 +0,0 @@
1
-
2
- require_relative 'docker_command_factory_helper'
3
- require_relative 'bundler_helper'
4
-
5
- module Dockerun
6
- module CommandHelper
7
-
8
- #
9
- # Common functions for docker container management
10
- #
11
- module DockerContainerHelper
12
- include DockerCommandFactoryHelper
13
- include BundlerHelper
14
-
15
- class DockerContainerBuildFailed < StandardError; end
16
- class DockerContainerStartFailed < StandardError; end
17
- class DockerContainerStopFailed < StandardError; end
18
-
19
- def run_docker_container(image_name, container_name, mount_points = [], &block)
20
-
21
- raise DockerContainerBuildFailed, "block is required" if not block
22
- raise DockerContainerBuildFailed, "Image name is required" if is_empty?(image_name)
23
-
24
- Dockerun.udebug("Running image '#{image_name}', container '#{container_name}'")
25
-
26
- reuse = nil
27
-
28
- if is_empty?(container_name)
29
- Dockerun.udebug "Container name empty. Creating new container"
30
- container_name = block.call(:new_container_name)
31
- loop do
32
- st, _ = is_container_exist?(container_name)
33
- if st
34
- reuse = block.call(:container_name_exist, container_name)
35
- break if reuse
36
- container_name = block.call(:new_container_name)
37
- else
38
- break
39
- end
40
- end
41
-
42
- else
43
- reuse, _ = is_container_exist?(container_name)
44
- Dockerun.udebug "Container name not empty. Is container exist? : #{reuse}"
45
- #if st
46
- # reuse = true
47
- #else
48
- # # if not found shall drop into the next block's else clause
49
- #end
50
- end
51
-
52
- if reuse == true
53
-
54
- Dockerun.udebug "Find out of container '#{container_name}' is running..."
55
- #res = dcFact.find_running_container(container_name).run
56
- #if not res.failed? and res.is_out_stream_empty?
57
- st, _ = is_container_running?(container_name)
58
- if not st
59
- # not running
60
- Dockerun.udebug "Container '#{container_name}' does not seems to be running. Starting container."
61
- ress = dcFact.start_container(container_name).run
62
- raise DockerContainerStartFailed, "Failed to start container '#{container_name}'. Error was : #{ress.err_stream}" if ress.failed?
63
- end
64
-
65
- ucmd = cli.ask("Command to be run inside the container. Empty to attach to existing session : ", value: "/bin/bash")
66
- if is_empty?(ucmd)
67
- dcFact.attach_container(container_name).run
68
- else
69
- dcFact.run_command_in_running_container(container_name, ucmd, tty: true, interactive: true).run
70
- end
71
-
72
- else
73
-
74
- #@workspace_root = "/opt"
75
- #@shared_dirs = {}
76
-
77
- #mount = []
78
- #sharedInsideDocker = []
79
- #res = find_local_dev_gems
80
- #puts "Found #{res.length} local gems #{res}"
81
- #if not res.empty?
82
- #
83
- # transferMapping = block.call(:transfer_dev_gem_mapping?)
84
- # if transferMapping
85
- # res.each do |name, path|
86
- # tsrc = block.call(:workspace_root_inside_docker, @workspace_root, name, path)
87
- # inPath = File.join(tsrc, name)
88
- # mount << { path => inPath }
89
- # @shared_dirs[name] = inPath
90
- # end
91
- # end
92
-
93
- #end
94
-
95
- #mapProjectDir = block.call(:map_project_dir, @workspace_root)
96
- #if not_empty?(mapProjectDir)
97
- # mount << { Dir.getwd => mapProjectDir }
98
- #end
99
-
100
- #reqVolMap = block.call(:volume_mapping_required?)
101
- #if reqVolMap
102
-
103
- # loop do
104
- #
105
- # block.call(:already_mapped, mount)
106
-
107
- # src = block.call(:source_prompt)
108
- # dest = block.call(:destination_prompt, src)
109
- # mount << { src => dest }
110
- #
111
- # add_to_bundle = block.call(:add_to_bundle?, dest)
112
- # if add_to_bundle
113
- # @shared_dirs[File.basename(dest)] = dest
114
- # end
115
-
116
- # block.call(:add_mount_to_container, container_name, mount.last)
117
- # repeat = block.call(:add_more_volume_mapping?)
118
- # break if not repeat
119
-
120
- # end
121
-
122
- #end
123
-
124
-
125
- #insideDockerConfig = File.join(File.dirname(__FILE__),"..","..","template","setup_ruby_devenv.rb.erb")
126
- #if File.exist?(insideDockerConfig)
127
- #
128
- # @docker_init_file_path = File.join(Dir.getwd,"on_docker_config")
129
-
130
- # cont = File.read(insideDockerConfig)
131
- #
132
- # b = binding
133
-
134
- # res = ERB.new(cont)
135
- # out = res.result(b)
136
-
137
- # # fixed this name to be used inside Dockerfile
138
- # File.open(@docker_init_file_path, "w") do |f|
139
- # f.write out
140
- # end
141
-
142
- # block.call(:on_docker_init_file_path,@docker_init_file_path)
143
-
144
- #end
145
-
146
-
147
-
148
- dcFact.create_container_from_image(image_name, interactive: true, tty: true, container_name: container_name, mount: mount_points).run
149
-
150
- end
151
-
152
- container_name
153
-
154
- end
155
-
156
- private
157
- def is_container_exist?(name)
158
- if not_empty?(name)
159
- res = dcFact.find_from_all_container("^#{name}\\z").run
160
- raise DockerContainerBuildFailed, "Failed to find container. Error was : #{res.err_stream}" if res.failed?
161
-
162
- if res.is_out_stream_empty?
163
- # nothing found
164
- [false, ""]
165
- else
166
- [true, res.out_stream]
167
- end
168
- else
169
- [false, nil]
170
- end
171
- end
172
-
173
- def run_container(name)
174
-
175
- res = dcFact.start_container(name)
176
- raise DockerContainerStartFailed, "Failed to start docker container name '#{name}'. Error was : #{res.err_stream}" if res.failed?
177
-
178
- end
179
-
180
- def is_container_running?(name)
181
- if not_empty?(name)
182
- res = dcFact.find_running_container("^#{name}\\z").run
183
- raise DockerContainerBuildFailed, "Failed to find is running container. Error was : #{res.err_stream}" if res.failed?
184
-
185
- if res.is_out_stream_empty?
186
- # nothing found
187
- [false, ""]
188
- else
189
- [true, res.out_stream]
190
- end
191
- else
192
- [false, nil]
193
- end
194
- end
195
-
196
- def stop_container(name)
197
- res = dcFact.stop_container(name).run
198
- raise DockerContainerStopFailed, "Failed to stop docker container '#{name}'. Error was : #{res.err_stream}" if res.failed?
199
- end
200
-
201
-
202
- end
203
- end
204
- end
@@ -1,381 +0,0 @@
1
-
2
- require_relative 'cli_prompt'
3
- require_relative 'docker_command_factory_helper'
4
- require_relative 'bundler_helper'
5
- require_relative 'template/template_writer'
6
-
7
- module Dockerun
8
- module CommandHelper
9
-
10
- #
11
- # common functions for docker image management
12
- #
13
- module DockerImageHelper
14
- include CliHelper::CliPrompt
15
- include DockerCommandFactoryHelper
16
- include BundlerHelper
17
-
18
- class DockerfileNotExist < StandardError; end
19
- class DockerImageBuildFailed < StandardError; end
20
- class DockerImageDeleteFailed < StandardError; end
21
- class DockerImagePrebuiltConfigFailed < StandardError; end
22
-
23
- def load_dockerfile(root = Dir.getwd, dockerInitPath = nil, &block)
24
-
25
- #avail = ::Dockerun::Template::TemplateEngine.available_templates
26
- df = ::Dockerun::Template::TemplateEngine.available_templates
27
-
28
- #df = Dir.glob(File.join(root,"Dockerfile*"))
29
- if df.length == 0
30
- raise DockerfileNotExist, "Dockerfile not yet available. Please create one or run init first."
31
- end
32
-
33
- selectedDf = nil
34
- if df.length > 1
35
- selectedDf = cli.select("Please select one of the Dockerfile to proceed: ") do |m|
36
- df.each do |d|
37
- m.choice File.basename(d), d
38
- end
39
- end
40
- else
41
- selectedDf = df.first
42
- end
43
-
44
- tw = ::Dockerun::Template::TemplateWriter.new(selectedDf)
45
- tw.docker_init_file_path = dockerInitPath
46
- userFields = tw.user_configurables
47
- if block
48
- tw.user_configurables = block.call(:prompt_user_configurables, { template: selectedDf, userFields: userFields })
49
- end
50
- res = tw.compile
51
-
52
- loc = "."
53
- #loc = params[:location] if not_empty?(params[:location])
54
-
55
- loc = File.expand_path(loc)
56
- out = nil
57
- if File.directory?(loc)
58
- out = File.join(loc, "Dockerfile.dockerun")
59
- else
60
- out = File.join(File.dirname(loc), "Dockerfile.dockerun")
61
- end
62
-
63
- File.open(out, "w") do |f|
64
- f.write res
65
- end
66
-
67
-
68
- #selectedDf
69
- File.basename(out)
70
-
71
- end
72
-
73
- def build_image_if_not_exist(name, &block)
74
-
75
- raise DockerImageBuildFailed, "block is required" if not block
76
-
77
- mountPoints = []
78
- if is_empty?(name)
79
- reuse = false
80
- loop do
81
- name = block.call(:new_image_name)
82
- break if not is_image_existed?(name)
83
- reuse, name = block.call(:image_exist, name)
84
- break if reuse
85
- end
86
-
87
- if reuse
88
-
89
- else
90
-
91
- #@workspace_root = "/opt"
92
- #@shared_dirs = {}
93
-
94
- #mount = []
95
- #sharedInsideDocker = []
96
- #res = find_local_dev_gems
97
- #puts "Found #{res.length} local gems #{res}"
98
- #if not res.empty?
99
-
100
- # transferMapping = block.call(:transfer_dev_gem_mapping?)
101
- # if transferMapping
102
- # res.each do |name, path|
103
- # tsrc = block.call(:workspace_root_inside_docker, @workspace_root, name, path)
104
- # inPath = File.join(tsrc, name)
105
- # mount << { path => inPath }
106
- # @shared_dirs[name] = inPath
107
- # end
108
- # end
109
-
110
- #end
111
-
112
- #mapProjectDir = block.call(:map_project_dir, @workspace_root)
113
- #if not_empty?(mapProjectDir)
114
- # mount << { Dir.getwd => mapProjectDir }
115
- #end
116
-
117
- #reqVolMap = block.call(:volume_mapping_required?)
118
- #if reqVolMap
119
-
120
- # loop do
121
-
122
- # block.call(:already_mapped, mount)
123
-
124
- # src = block.call(:source_prompt)
125
- # dest = block.call(:destination_prompt, src)
126
- # mount << { src => dest }
127
-
128
- # add_to_bundle = block.call(:add_to_bundle?, dest)
129
- # if add_to_bundle
130
- # @shared_dirs[File.basename(dest)] = dest
131
- # end
132
-
133
- # block.call(:add_mount_to_container, container_name, mount.last)
134
- # repeat = block.call(:add_more_volume_mapping?)
135
- # break if not repeat
136
-
137
- # end
138
-
139
- #end
140
-
141
-
142
- #insideDockerConfig = File.join(File.dirname(__FILE__),"..","..","template","setup_ruby_devenv.rb.erb")
143
- #if File.exist?(insideDockerConfig)
144
-
145
- # @docker_init_file_path = File.join(Dir.getwd,"on_docker_config")
146
-
147
- # cont = File.read(insideDockerConfig)
148
-
149
- # b = binding
150
-
151
- # res = ERB.new(cont)
152
- # out = res.result(b)
153
-
154
- # # fixed this name to be used inside Dockerfile
155
- # File.open(@docker_init_file_path, "w") do |f|
156
- # f.write out
157
- # end
158
-
159
- # block.call(:on_docker_init_file_path,@docker_init_file_path)
160
-
161
- #end
162
-
163
- mountPoints, dockerinit = prompt_mount_points(&block)
164
-
165
-
166
- dockerfile = load_dockerfile(Dir.getwd, dockerinit, &block)
167
- build_docker_image(name, dockerfile: dockerfile)
168
-
169
- FileUtils.rm(dockerinit) if File.exist?(dockerinit)
170
-
171
- end
172
-
173
- else
174
-
175
- if not is_image_existed?(name)
176
-
177
- mountPoints, dockerinit = prompt_mount_points(&block)
178
-
179
- dockerfile = load_dockerfile(Dir.getwd, dockerinit, &block)
180
- build_docker_image(name, dockerfile: dockerfile)
181
-
182
- FileUtils.rm(dockerinit) if File.exist?(dockerinit)
183
- end
184
-
185
- end
186
-
187
- [name, mountPoints]
188
-
189
- end
190
-
191
- def prompt_mount_points(&block)
192
-
193
- raise DockerImagePrebuiltConfigFailed, "block is mandatory" if not block
194
-
195
- @workspace_root = "/opt"
196
- @shared_dirs = {}
197
-
198
- block.call(:prompt_mount_points_starting)
199
-
200
- mount = []
201
- res = find_local_dev_gems
202
- #puts "Found #{res.length} local gems #{res}"
203
- if not res.empty?
204
-
205
- transferMapping = block.call(:transfer_dev_gem_mapping?, res)
206
- if transferMapping
207
- res.each do |name, path|
208
- tsrc = block.call(:workspace_root_inside_docker, @workspace_root, name, path)
209
- mount << { path => tsrc }
210
- @shared_dirs[name] = tsrc
211
- end
212
- end
213
-
214
- end
215
-
216
- mapProjectDir = block.call(:map_project_dir, @workspace_root)
217
- if not_empty?(mapProjectDir)
218
- mount << { Dir.getwd => mapProjectDir }
219
- end
220
-
221
- reqVolMap = block.call(:volume_mapping_required?)
222
- if reqVolMap
223
-
224
- loop do
225
-
226
- block.call(:already_mapped, mount)
227
-
228
- src = block.call(:source_prompt, { control: [ "Type 's' to skip" ] })
229
- if src == "s"
230
- block.call(:volume_mapping_skipped)
231
- break
232
- end
233
-
234
- dest = block.call(:destination_prompt, src)
235
- mount << { src => dest }
236
-
237
- add_to_bundle = block.call(:add_to_bundle?, dest)
238
- if add_to_bundle
239
- @shared_dirs[File.basename(dest)] = dest
240
- end
241
-
242
- repeat = block.call(:add_more_volume_mapping?)
243
- break if not repeat
244
-
245
- end
246
-
247
- end
248
-
249
-
250
- #
251
- # Try to run once to install bundle config --local and create alias during image building
252
- #
253
- insideDockerConfig = File.join(File.dirname(__FILE__),"..","..","template","setup_ruby_devenv.rb.erb")
254
- if File.exist?(insideDockerConfig)
255
-
256
- @docker_init_file_path = File.join(Dir.getwd,"on_docker_config")
257
-
258
- cont = File.read(insideDockerConfig)
259
-
260
- b = binding
261
-
262
- res = ERB.new(cont)
263
- out = res.result(b)
264
-
265
- # fixed this name to be used inside Dockerfile
266
- File.open(@docker_init_file_path, "w") do |f|
267
- f.write out
268
- end
269
-
270
- block.call(:on_docker_init_file_path,@docker_init_file_path)
271
-
272
- end
273
-
274
- [mount, @docker_init_file_path]
275
-
276
- end
277
-
278
- #def new_image(config, selectedDf)
279
-
280
- # if not_empty?(selectedDf)
281
- # imageName = cli.ask("Please provide an image name : ", required: true)
282
- # res = dcFact.find_image(imageName).run
283
- # if not res[:result].failed? and not_empty?(res[:outStream])
284
- # reuse = cli.yes?("Image named '#{imageName}' already exist. Use the existing image? ")
285
- # if reuse
286
- # config.image_name = imageName
287
- # else
288
- # keep = cli.no?("Remove existing image and create a new image? ")
289
- # if not keep
290
- # res = dcFact.delete_image(imageName).run
291
- # if not res[:result].failed?
292
- # puts "Existing image with name '#{imageName}' removed."
293
- # res = dcFact.build_image(imageName, dockerfile: selectedDf).run
294
- # if not res[:result].failed?
295
- # config.image_name = imageName
296
- # else
297
- # raise DockerImageBuildFailed, "Error while building image '#{imageName}'. Error stream : #{res[:errStream].join(" ")}"
298
- # end
299
-
300
- # else
301
- # raise DockerImageDeleteFailed, "Error while deleting image '#{imageName}'. Error stream : #{res[:errStream].join(" ")}"
302
- # end
303
-
304
- # else
305
- # # user select not to use existing and keep the existing image...
306
- # # nothing can be done then right?
307
- # raise Dockerun::Error, "Alreay has existing image with name '#{imageName}' but not reusing the image. System cannot proceed. Please either delete or reuse the image"
308
- # end
309
- # end # if reuse or not
310
-
311
- # else
312
-
313
- # # no existing image
314
- # res = dcFact.build_image(imageName, dockerfile: selectedDf).run
315
- # if res[:result].failed?
316
- # raise DockerImageBuildFailed, "Error building image '#{imageName}' from dockerfile '#{selectedDf}'. Error was : #{res[:errStream].join(" ")}"
317
- # end
318
-
319
- # config.image_name = imageName
320
-
321
- # end # if image found or not
322
-
323
- # else
324
- # # no Dockerfile found
325
- # raise DockerfileNotExist, "No Dockerfile given. Please make sure there is one or run init first"
326
- # end # not_empy? selectedDf
327
-
328
-
329
- #end
330
-
331
- #def build_image(imageName, selectedDf)
332
-
333
- # raise DockerImageBuildFailed, "Image name cannot be empty" if is_empty?(imageName)
334
-
335
- # res = dcFact.find_image(imageName).run
336
- # if not res[:result].failed? and is_empty?(res[:outStream])
337
- # # image does not exist
338
- # res = dcFact.build_image(imageName, dockerfile: selectedDf).run
339
- # if res[:result].failed?
340
- # raise DockerImageBuildFailed, "Building image '#{config.image_name}' failed. Error stream : #{res[:errStream].join(" ")}"
341
- # end
342
- # end
343
-
344
- #end
345
-
346
- private
347
- def is_image_existed?(name)
348
- if is_empty?(name)
349
- false
350
- else
351
- res = dcFact.find_image(name).run
352
- if not res.failed? and res.is_out_stream_empty?
353
- false
354
- else
355
- true
356
- end
357
- end
358
- end
359
-
360
- def delete_docker_image(name)
361
- if not_empty?(name)
362
- res = dcFact.delete_image(name).run
363
- not res.failed?
364
- else
365
- true
366
- end
367
- end
368
-
369
- def build_docker_image(name, opts = { })
370
- raise DockerImageBuildFailed, "Given name to build docker image is empty" if is_empty?(name)
371
-
372
- res = dcFact.build_image(name, opts).run
373
- if res.failed?
374
- raise DockerImageBuildFailed, "Building image '#{name}' failed. Error stream : #{res.err_stream}"
375
- end
376
-
377
- end
378
-
379
- end
380
- end
381
- end
@@ -1,14 +0,0 @@
1
-
2
- require_relative 'template_writer'
3
-
4
- module Dockerun
5
- module Template
6
- class GeneratlTemplateWriter < TemplateWriter
7
-
8
- def initialize
9
- super
10
- end
11
-
12
- end
13
- end
14
- end
@@ -1,24 +0,0 @@
1
-
2
- require_relative 'template_writer'
3
-
4
- module Dockerun
5
- module Template
6
- class JrubyTemplateWriter < TemplateWriter
7
-
8
- def initialize
9
- super
10
- end
11
-
12
- alias_method :find_template, :find_template_super
13
- def find_template
14
- case @template
15
- when "jruby-9.4.0-jdk11"
16
- @image = "jruby:9.4.0-jdk11"
17
- end
18
-
19
- find_template_super
20
- end
21
-
22
- end
23
- end
24
- end
@@ -1,27 +0,0 @@
1
-
2
-
3
- module Dockerun
4
- module Template
5
- class TemplateEngine
6
-
7
- def self.template_root
8
- File.join(File.dirname(__FILE__),"..","..","..","template")
9
- end
10
-
11
- def self.available_templates
12
- avail = Dir.glob(File.join(template_root,"Dockerfile_*.erb"))
13
-
14
- #userDir = File.join(Dir.home,".dockerun","template")
15
- #if File.exist?(userDir)
16
- # avail += Dir.glob(userDir,"*.erb")
17
- #end
18
-
19
- avail.map! { |f|
20
- name = File.basename(f,File.extname(f))
21
- name.gsub("Dockerfile_","")
22
- }
23
- end
24
-
25
- end
26
- end
27
- end