Sha256: 1b4466733349949a417e261eb1c09061ba238cff766be5bacdb11ce0a2185fcf

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

require 'fileutils'

require 'cide/docker'

module CIDE
  class Builder
    include CIDE::Docker

    attr_reader :config

    def initialize(config)
      @config = config
      @tmp_files = []
    end

    def build(tag: nil, pull: nil, ssh_key: nil)
      fail ArgumentError, 'tag missing' unless tag

      if config.use_ssh
        fail ArgumentError, 'ssh_key missing' unless ssh_key
        unless File.exist?(ssh_key)
          fail ArgumentError, "ssh_key #{ssh_key} not found"
        end
        create_tmp_file! TEMP_SSH_KEY, File.read(ssh_key)
      end
      create_tmp_file! DOCKERFILE, config.to_dockerfile

      build_options = ['--force-rm']
      build_options << '--pull' if pull
      build_options.push '-f', DOCKERFILE
      build_options.push '-t', tag
      build_options << '.'
      docker :build, *build_options
    ensure
      release_tmp_files!
    end

    protected

    def create_tmp_file!(destination, content)
      FileUtils.mkdir_p(File.dirname(destination))
      @tmp_files << destination
      File.write(destination, content)
      # Dockerfile ADD compares content and mtime, we don't want that
      File.utime(1_286_701_800, 1_286_701_800, destination)
    end

    def release_tmp_files!
      @tmp_files.each do |file|
        FileUtils.rm_f(file)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cide-0.9.1 lib/cide/builder.rb
cide-0.9.0 lib/cide/builder.rb
cide-0.8.1 lib/cide/builder.rb
cide-0.8.0 lib/cide/builder.rb