Class: PicsolveDockerBuilder::Builder::Builder

Inherits:
Object
  • Object
show all
Includes:
PicsolveDockerBuilder::Base
Defined in:
lib/picsolve_docker_builder/builder/builder.rb

Overview

This Builder supports the building process of an docker image

Instance Method Summary collapse

Methods included from PicsolveDockerBuilder::Base

#base_dir, #config, #config_file, #config_path, #config_paths, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initialize(opts = {}) ⇒ Builder

Returns a new instance of Builder.



11
12
13
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 11

def initialize(opts = {})
  @opts = opts
end

Instance Method Details

#addsObject



32
33
34
35
36
37
38
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 32

def adds
  files = @opts[:files].select(&:add_to_image?)
  files.map! do |f|
    "ADD #{f.path} #{f.destination}"
  end
  files.join("\n")
end

#base_imageObject



19
20
21
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 19

def base_image
  @opts[:base_image]
end

#buildObject

Overwrite docker_build and filter build assets



81
82
83
84
85
86
87
88
89
90
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 81

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

#build_tarObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 61

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

#commandObject



40
41
42
43
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 40

def command
  return '' if @opts[:command].nil?
  "CMD [\"#{@opts[:command].join('", "')}\"]"
end

#dockerfileObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 92

def dockerfile
  <<EOS
FROM #{base_image}
MAINTAINER #{maintainer}

#{hook(:early)}

#{hook(:before_adds)}

#{adds}

#{hook(:after_adds)}

#{ports}

# call run script
#{command}

#{hook(:late)}
EOS
end

#hook(name) ⇒ Object



45
46
47
48
49
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 45

def hook(name)
  v = @opts[:hooks][name]
  v = [v] unless v.is_a?(Array)
  v.join("\n") + "\n"
end

#maintainerObject



15
16
17
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 15

def maintainer
  @opts[:maintainer]
end

#portsObject



23
24
25
26
27
28
29
30
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 23

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

#required_attrsObject



51
52
53
54
55
56
57
58
59
# File 'lib/picsolve_docker_builder/builder/builder.rb', line 51

def required_attrs
  [
    :base_image,
    :maintainer,
    :ports,
    :command,
    :files
  ]
end