lib/dockly/build_cache/docker.rb in dockly-1.8.0 vs lib/dockly/build_cache/docker.rb in dockly-1.9.0

- old
+ new

@@ -1,12 +1,8 @@ class Dockly::BuildCache::Docker < Dockly::BuildCache::Base attr_accessor :image - def wait_time - 300 # max 5 minutes - end - def execute! ensure_present! :image super image end @@ -27,36 +23,53 @@ def push_cache(version) ensure_present! :output_dir if cache = pull_from_s3(version) debug "inserting to #{output_directory}" - path = File.expand_path(cache.path) - path_parent = File.dirname(path) - tar_flags = keep_old_files ? '-xkf' : 'xf' - container = ::Docker::Container.create( - 'Image' => image.id, - 'Cmd' => ['/bin/bash', '-c', [ - "mkdir -p #{File.dirname(output_directory)}", - '&&', - "tar #{tar_flags} #{File.join('/', 'host', path)} -C #{File.dirname(output_directory)}" - ].join(' ') - ], - 'Volumes' => { - File.join('/', 'host', path_parent) => { path_parent => 'rw' } - } - ) - container.start('Binds' => ["#{path_parent}:#{File.join('/', 'host', path_parent)}"]) - result = container.wait['StatusCode'] - raise "Got bad status code when copying build cache: #{result}" unless result.zero? - self.image = container.commit + if safe_push_cache + push_cache_safe(cache) + else + push_cache_with_volumes(cache) + end debug "inserted cache into #{output_directory}" cache.close else info "could not find #{s3_object(version)}" end end + def push_cache_safe(cache) + container = image.run("mkdir -p #{File.dirname(output_directory)}") + image_with_dir = container.tap(&:wait).commit + self.image = image_with_dir.insert_local( + 'localPath' => cache.path, + 'outputPath' => File.dirname(output_directory) + ) + end + + def push_cache_with_volumes(cache) + path = File.expand_path(cache.path) + path_parent = File.dirname(path) + tar_flags = keep_old_files ? '-xkf' : 'xf' + container = ::Docker::Container.create( + 'Image' => image.id, + 'Cmd' => ['/bin/bash', '-c', [ + "mkdir -p #{File.dirname(output_directory)}", + '&&', + "tar #{tar_flags} #{File.join('/', 'host', path)} -C #{File.dirname(output_directory)}" + ].join(' ') + ], + 'Volumes' => { + File.join('/', 'host', path_parent) => { path_parent => 'rw' } + } + ) + container.start('Binds' => ["#{path_parent}:#{File.join('/', 'host', path_parent)}"]) + result = container.wait['StatusCode'] + raise "Got bad status code when copying build cache: #{result}" unless result.zero? + self.image = container.commit + end + def copy_output_dir(container) ensure_present! :output_dir file_path = File.join(tmp_dir,s3_object(hash_output)) FileUtils.mkdir_p(File.dirname(file_path)) file = File.open(file_path, 'w+b') @@ -90,10 +103,10 @@ def run_command(command) debug "running command `#{command}` on image #{image.id}" container = image.run(["/bin/bash", "-c", "cd #{command_directory} && #{command}"]) debug "command running in container #{container.id}" - status = container.wait(wait_time)['StatusCode'] + status = container.wait(docker.timeout)['StatusCode'] resp = container.streaming_logs(stdout: true, stderr: true) debug "`#{command}` returned the following output:" debug resp.strip debug "`#{command}` exited with status #{status}, resulting container id: #{container.id}" [status, resp.strip, container]