require 'rubygems/package' require 'zlib' require 'picsolve_docker_builder/base' require 'picsolve_docker_builder/builder/file' module PicsolveDockerBuilder module Builder # This Builder supports the building process of an docker image class Builder include PicsolveDockerBuilder::Base def initialize(opts = {}) @opts = opts end def maintainer @opts[:maintainer] end def base_image @opts[:base_image] end def ports return if @opts[:ports].nil? ports = @opts[:ports] ports = [ports] unless ports.is_a?(Array) ports.map do |port| "EXPOSE #{port}" end.join('\n') end def adds files = @opts[:files].select(&:add_to_image?) files.map! do |f| "ADD #{f.path} #{f.destination}" end files.join("\n") end def command return '' if @opts[:command].nil? "CMD [\"#{@opts[:command].join('", "')}\"]" end def hook(name) v = @opts[:hooks][name] v = [v] unless v.is_a?(Array) v.join("\n") + "\n" end def required_attrs [ :base_image, :maintainer, :ports, :command, :files ] end def build_tar tarfile = StringIO.new('') Gem::Package::TarWriter.new(tarfile) do |tar| # add Dockerfile to tar dockerfile.each_line do |line| log.debug "docker_build Dockerfile: #{line.strip}" end tar.add_file 'Dockerfile', 0644 do |tf| tf.write dockerfile end # add other files to tar @opts[:files].each do |f| f.add_to_tar tar end end StringIO.new(tarfile.string) end # Overwrite docker_build and filter build assets def build log.info 'docker build starting...' Docker::Image.build_from_tar(build_tar) do |stream| s = JSON.parse(stream)['stream'] log.debug s.strip unless s.nil? end rescue NoMethodError => e log.fatal "docker build failed: #{e}" exit 1 end def dockerfile <