nutkins 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bfa69ab3c9cd1fa876a151921a9e04f1af3de8d
4
- data.tar.gz: 52b50703467b3f9f010733a7681f9739653002c9
3
+ metadata.gz: 69265ab31556f8e5e183ff64ce97bd771a7126ea
4
+ data.tar.gz: 4ed7e202dc0dc4a1ccc7338b4869f2ea2d314c64
5
5
  SHA512:
6
- metadata.gz: a6ff593c34f989c41d76c1f45395ed35b2a44dd289ae1e1fbe9ced7909467315c15a8a3896b712874d5ab4895748c3ab3d46187a8ec6048c0ef82e341c8dcbca
7
- data.tar.gz: 258db6118fb8c67204521773771497b1e6f51a21731feb41ab6a82a1274fb36b43a426f6dce404bd261f29e3dd522d75b86e052d1341fcd36218e1ea751882b5
6
+ metadata.gz: bb313cc3b14afc954f49518aafd2712fe2db1bde0fb03b3c12317ce6544c019f089bf867b469d65c01bf19f5667473e6e6ee4974603006a17f75a330164b1bf5
7
+ data.tar.gz: 48654d38201f36f9659781cccc7b358b8417ddd5fe0647926f60cc6fecd5a5675e59cbeb2924234c7732a0f80a6d9487b221a252bba05ff79fef60dbebde0eb7
@@ -15,16 +15,8 @@ module Nutkins::DockerBuilder
15
15
  Docker.run 'pull', base, stdout: true
16
16
  end
17
17
 
18
- cont_id = Nutkins::Docker.container_id_for_tag base, running: true
19
- if cont_id
20
- puts "found existing container #{cont_id}"
21
- Nutkins::Docker.kill_and_remove_container cont_id
22
- puts "killed and removed existing container"
23
- end
24
-
25
18
  # the base image to start rebuilding from
26
19
  parent_img_id = base
27
- cont_id = nil
28
20
  pwd = Dir.pwd
29
21
  begin
30
22
  Dir.chdir cfg["directory"]
@@ -34,87 +26,94 @@ module Nutkins::DockerBuilder
34
26
  build_commands.each do |build_cmd|
35
27
  cmd = /^\w+/.match(build_cmd).to_s.downcase
36
28
  cmd_args = build_cmd[(cmd.length + 1)..-1].strip
37
-
38
- docker_args = []
39
- # the commit_msg is used to look up cache entries, it can be
40
- # modified if the command uses dynamic data, e.g. to add checksums
41
- commit_msg = nil
29
+ # docker run is always used and forms the basis of the cache key
30
+ run_args = nil
31
+ env_args = nil
32
+ add_files = nil
33
+ add_files_dest = nil
42
34
 
43
35
  case cmd
44
36
  when "run"
45
37
  cmd_args.gsub! /\n+/, ' '
46
- docker_args = ['exec', '%CONT_ID%', cfg['shell'], '-c', cmd_args]
47
- commit_msg = cmd + ' ' + cmd_args
38
+ run_args = cmd_args
48
39
  when "add"
49
- *srcs, dest = cmd_args.split ' '
50
- srcs = srcs.map { |src| Dir.glob src }.flatten
51
-
52
- docker_args = srcs.map { |src| ['cp', src, '%CONT_ID%' + ':' + dest] }
53
- # ensure checksum of each file is embedded into commit_msg
40
+ *add_files, add_files_dest = cmd_args.split ' '
41
+ add_files = add_files.map { |src| Dir.glob src }.flatten
42
+ # ensure checksum of each file is embedded into run command
54
43
  # if any file changes the cache is dirtied
55
- commit_msg = 'add ' + srcs.map do |src|
44
+ run_args = '#(nop) add ' + add_files.map do |src|
56
45
  src + ':' + Digest::MD5.file(src).to_s
57
- end.push(dest).join(' ')
46
+ end.push(add_files_dest).join(' ')
58
47
  when "cmd", "entrypoint", "env", "expose", "label", "onbuild", "user", "volume", "workdir"
59
- commit_msg = commit_change = build_cmd
48
+ run_args = "#(nop) #{build_cmd}"
49
+ env_args = build_cmd
60
50
  else
61
51
  raise "unsupported command: #{cmd}"
62
52
  # TODO add metadata flags
63
53
  end
64
54
 
65
- if (commit_change or docker_args) and commit_msg
66
- commit_msg = "#{parent_img_id} -> #{commit_msg}"
67
-
55
+ if run_args
56
+ run_shell_cmd = [ cfg['shell'], '-c', run_args ]
68
57
  unless cache_is_dirty
69
58
  # searches the commit messages of all images for the one matching the expected
70
59
  # cache entry for the given content
71
- all_images = Nutkins::Docker.run_get_stdout('images', '-aq').split("\n")
72
- images_meta = JSON.parse(Nutkins::Docker.run_get_stdout('inspect', *all_images))
73
- cache_entry = images_meta.find do |image_meta|
74
- if image_meta['Comment'] == commit_msg
75
- parent_img_id = Nutkins::Docker.get_short_commit(image_meta['Id'])
76
- true
77
- end
78
- end
60
+ cache_img_id = find_cached_img_id run_shell_cmd
79
61
 
80
- if cache_entry
81
- puts "cached: #{commit_msg}"
62
+ if cache_img_id
63
+ puts "cached: #{run_args}"
64
+ parent_img_id = cache_img_id
82
65
  next
83
66
  else
84
- puts "starting build container from commit #{parent_img_id}"
85
- Nutkins::Docker.run 'run', '-d', parent_img_id, 'sleep', '3600'
86
- cont_id = Nutkins::Docker.container_id_for_tag parent_img_id, running: true
87
- puts "started build container #{cont_id}"
67
+ puts "not in cache, starting from #{parent_img_id}"
88
68
  cache_is_dirty = true
89
69
  end
90
70
  end
91
71
 
92
- puts "#{cmd}: #{cmd_args}"
72
+ if run_args
73
+ puts "run #{run_args}"
74
+ unless Nutkins::Docker.run 'run', parent_img_id, *run_shell_cmd, stdout: true
75
+ raise "run failed: #{run_args}"
76
+ end
77
+
78
+ cont_id = `docker ps -aq`.lines.first.strip
79
+ begin
80
+ if add_files
81
+ add_files.each do |src|
82
+ if not Nutkins::Docker.run 'cp', src, "#{cont_id}:#{add_files_dest}"
83
+ raise "could not copy #{src} to #{cont_id}:#{add_files_dest}"
84
+ end
85
+ end
86
+ end
93
87
 
94
- unless docker_args.empty?
95
- # docker can be an array of one set of args, or an array of arrays of args
96
- docker_args = [ docker_args ] unless docker_args[0].kind_of? Array
97
- docker_args.each do |one_docker_args|
98
- run_args = one_docker_args.map { |arg| arg.gsub '%CONT_ID%', cont_id }
99
- puts "run #{run_args.join ' '}"
100
- unless Nutkins::Docker.run *run_args, stdout: true
101
- raise "build failed: #{one_docker_args.join ' '}"
88
+ commit_args = env_args ? ['-c', env_args] : []
89
+ parent_img_id = Nutkins::Docker.run_get_stdout 'commit', *commit_args, cont_id
90
+ raise "could not commit docker image" if parent_img_id.nil?
91
+ parent_img_id = Nutkins::Docker.get_short_commit parent_img_id
92
+ ensure
93
+ if not Nutkins::Docker.run 'rm', cont_id
94
+ puts "could not remove build container #{cont_id}"
102
95
  end
103
96
  end
104
97
  end
105
-
106
- commit_args = commit_change ? ['-c', commit_change] : []
107
- parent_img_id = Nutkins::Docker.run_get_stdout 'commit', '-m', commit_msg, *commit_args, cont_id
108
- raise "could not commit docker image" if parent_img_id.nil?
109
- parent_img_id = Nutkins::Docker.get_short_commit parent_img_id
110
98
  else
111
99
  puts "TODO: support cmd #{build_cmd}"
112
100
  end
113
101
  end
114
102
  ensure
115
103
  Dir.chdir pwd
116
- Nutkins::Docker.kill_and_remove_container cont_id if cont_id
117
- puts "killed and removed build container"
118
104
  end
105
+
106
+ Nutkins::Docker.run 'tag', parent_img_id, cfg['tag']
107
+ end
108
+
109
+ def self.find_cached_img_id command
110
+ all_images = Nutkins::Docker.run_get_stdout('images', '-aq').split("\n")
111
+ images_meta = JSON.parse(Nutkins::Docker.run_get_stdout('inspect', *all_images))
112
+ images_meta.each do |image_meta|
113
+ if image_meta.dig('ContainerConfig', 'Cmd') == command
114
+ return Nutkins::Docker.get_short_commit(image_meta['Id'])
115
+ end
116
+ end
117
+ nil
119
118
  end
120
119
  end
@@ -1,3 +1,3 @@
1
1
  module Nutkins
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/nutkins.rb CHANGED
@@ -57,11 +57,17 @@ module Nutkins
57
57
  end
58
58
 
59
59
  image_id = Docker.image_id_for_tag tag
60
- if prev_image_id and image_id != prev_image_id
61
- puts "deleting previous image #{prev_image_id}"
62
- Docker.run "rmi", prev_image_id
60
+ if prev_image_id
61
+ if image_id != prev_image_id
62
+ puts "deleting previous image #{prev_image_id}"
63
+ Docker.run "rmi", prev_image_id
64
+ else
65
+ puts "image is identical to cached version"
66
+ end
67
+ elsif image_id
68
+ puts "created new image #{image_id}"
63
69
  else
64
- puts "image is identical to cached version"
70
+ puts "no image exists for image... what went wrong?"
65
71
  end
66
72
  end
67
73
 
@@ -295,7 +301,7 @@ module Nutkins
295
301
  img_cfg_path = File.join directory, IMG_CONFIG_FILE_NAME
296
302
  img_cfg = File.exists?(img_cfg_path) ? YAML.load_file(img_cfg_path) : {}
297
303
  img_cfg['image'] ||= path if path != '.'
298
- img_cfg['shell'] ||= 'sh'
304
+ img_cfg['shell'] ||= '/bin/sh'
299
305
  img_cfg['directory'] = directory
300
306
  img_cfg["version"] ||= @config.version if @config.version
301
307
  img_cfg['version'] = img_cfg['version'].to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nutkins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pike
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-25 00:00:00.000000000 Z
11
+ date: 2016-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler