Class: Conjure::Service::Image
- Inherits:
-
Object
- Object
- Conjure::Service::Image
- Defined in:
- lib/conjure/service/docker_host.rb
Instance Attribute Summary collapse
-
#host_volumes ⇒ Object
readonly
Returns the value of attribute host_volumes.
Instance Method Summary collapse
- #base_image_name ⇒ Object
- #build ⇒ Object
- #command(command, options = {}, &block) ⇒ Object
- #destroy_instances ⇒ Object
- #dockerfile ⇒ Object
- #expected_image_name ⇒ Object
- #files_hash(files_array) ⇒ Object
- #host_volume_options(host_volumes) ⇒ Object
- #image_fingerprint ⇒ Object
- #image_installed? ⇒ Boolean
-
#initialize(host, options) ⇒ Image
constructor
A new instance of Image.
- #installed_image_name ⇒ Object
- #port_options(ports) ⇒ Object
- #raise_build_errors(build_output) ⇒ Object
- #run(command = "") ⇒ Object
- #running_container ⇒ Object
- #shell_command(command) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(host, options) ⇒ Image
Returns a new instance of Image.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/conjure/service/docker_host.rb', line 88 def initialize(host, ) @host = host @label = [:label] @base_image = [:base_image] @ports = [:ports].to_a @volumes = [:volumes].to_a @host_volumes = [:host_volumes] @setup_commands = [:setup_commands].to_a @daemon_command = [:daemon_command] @environment = [:environment] @files = [:files] end |
Instance Attribute Details
#host_volumes ⇒ Object (readonly)
Returns the value of attribute host_volumes.
86 87 88 |
# File 'lib/conjure/service/docker_host.rb', line 86 def host_volumes @host_volumes end |
Instance Method Details
#base_image_name ⇒ Object
154 155 156 |
# File 'lib/conjure/service/docker_host.rb', line 154 def base_image_name @base_image.respond_to?(:installed_image_name) ? @base_image.installed_image_name : @base_image end |
#build ⇒ Object
158 159 160 161 162 163 |
# File 'lib/conjure/service/docker_host.rb', line 158 def build destroy_instances Conjure.log "[docker] Building #{@label} image" raise_build_errors(@host.command "build -t #{expected_image_name} -", stdin: dockerfile) @host.containers.destroy_all_stopped end |
#command(command, options = {}, &block) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/conjure/service/docker_host.rb', line 165 def command(command, = {}, &block) destroy_instances = [:files] ? ["-v /files:/files"] : [] += (@host_volumes) << "-i" if [:stream_stdin] @host.command "run #{.join ' '} #{installed_image_name} #{shell_command command}", :stream_stdin => [:stream_stdin], :files => files_hash([:files]), &block end |
#destroy_instances ⇒ Object
198 199 200 |
# File 'lib/conjure/service/docker_host.rb', line 198 def destroy_instances @host.containers.destroy_all :image_name => @label end |
#dockerfile ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/conjure/service/docker_host.rb', line 145 def dockerfile lines = ["FROM #{base_image_name}"] lines += @environment.map{|k, v| "ENV #{k} #{v}"} if @environment lines += @setup_commands.map{|c| "RUN #{c}"} lines << "VOLUME #{@volumes.inspect}" if @volumes.to_a.any? lines << "ENTRYPOINT #{@daemon_command}" if @daemon_command lines.join "\n" end |
#expected_image_name ⇒ Object
105 106 107 |
# File 'lib/conjure/service/docker_host.rb', line 105 def expected_image_name "#{@label}:#{image_fingerprint}" end |
#files_hash(files_array) ⇒ Object
184 185 186 187 188 |
# File 'lib/conjure/service/docker_host.rb', line 184 def files_hash(files_array) files_array.to_a.inject({}) do |hash, local_file| hash.merge local_file => "/files/#{File.basename local_file}" end end |
#host_volume_options(host_volumes) ⇒ Object
173 174 175 176 177 178 |
# File 'lib/conjure/service/docker_host.rb', line 173 def (host_volumes) host_volumes.to_a.map do |host_path, container_path| @host.ensure_host_directory host_path "-v=#{host_path}:#{container_path}:rw" end end |
#image_fingerprint ⇒ Object
101 102 103 |
# File 'lib/conjure/service/docker_host.rb', line 101 def image_fingerprint Digest::SHA1.hexdigest(dockerfile)[0..11] end |
#image_installed? ⇒ Boolean
114 115 116 |
# File 'lib/conjure/service/docker_host.rb', line 114 def image_installed? @host.command("history #{expected_image_name}") rescue false end |
#installed_image_name ⇒ Object
109 110 111 112 |
# File 'lib/conjure/service/docker_host.rb', line 109 def installed_image_name build unless image_installed? expected_image_name end |
#port_options(ports) ⇒ Object
180 181 182 |
# File 'lib/conjure/service/docker_host.rb', line 180 def (ports) ports.to_a.map {|port| "-p=#{port}:#{port}" } end |
#raise_build_errors(build_output) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/conjure/service/docker_host.rb', line 134 def raise_build_errors(build_output) match = build_output.match(/Error build: The command \[([^\]]*)\] returned a non-zero code:/) if match failed_command = match[1] last_section = build_output.split("--->").last last_section.gsub!(/Running in [0-9a-f]+/, "") last_section.gsub!(/Error build: The command.*/m, "") raise "Docker: build step '#{failed_command}' failed: #{last_section.strip}" end end |
#run(command = "") ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/conjure/service/docker_host.rb', line 118 def run(command = "") unless running_container Conjure.log "[docker] Starting #{@label}" = (@host_volumes) += (@ports) command = shell_command command if command != "" container_id = @host.command("run #{.join ' '} -d #{installed_image_name} #{command}").strip if(!running_container) output = @host.command "logs #{container_id}" raise "Docker: #{@label} daemon exited with: #{output}" end end Conjure.log "[docker] #{@label} is running at #{running_container.ip_address}" running_container end |
#running_container ⇒ Object
194 195 196 |
# File 'lib/conjure/service/docker_host.rb', line 194 def running_container @runnning_container ||= @host.containers.find(:image_name => expected_image_name) end |
#shell_command(command) ⇒ Object
190 191 192 |
# File 'lib/conjure/service/docker_host.rb', line 190 def shell_command(command) "bash -c '#{@host.shell_escape command}'" end |
#stop ⇒ Object
202 203 204 |
# File 'lib/conjure/service/docker_host.rb', line 202 def stop destroy_instances end |