Class: ChefMetalDocker::DockerDriver
- Inherits:
-
ChefMetal::Driver
- Object
- ChefMetal::Driver
- ChefMetalDocker::DockerDriver
- Includes:
- Chef::Mixin::ShellOut
- Defined in:
- lib/chef_metal_docker/docker_driver.rb
Overview
Provisions machines using Docker
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
- .canonicalize_url(driver_url, config) ⇒ Object
-
.connection_url(driver_url) ⇒ Object
Parse the url from a driver URL, try to clean it up Returns a proper URL from the driver_url string.
-
.from_url(driver_url, config) ⇒ Object
URL scheme: docker:<path> canonical URL calls realpath on <path>.
Instance Method Summary collapse
- #allocate_image(action_handler, image_spec, image_options, machine_spec) ⇒ Object
- #allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #build_container(machine_spec, machine_options) ⇒ Object
-
#connect_to_machine(machine_spec, machine_options) ⇒ Object
Connect to machine without acquiring it.
- #convergence_strategy_for(machine_spec, machine_options) ⇒ Object
- #destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #driver_url ⇒ Object
- #find_image(repository, tag) ⇒ Object
- #image_named(image_name) ⇒ Object
-
#initialize(driver_url, config) ⇒ DockerDriver
constructor
A new instance of DockerDriver.
- #machine_for(machine_spec, machine_options, base_image_name) ⇒ Object
- #ready_image(action_handler, image_spec, image_options) ⇒ Object
- #ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #start_machine(action_handler, machine_spec, machine_options, base_image_name) ⇒ Object
- #stop_machine(action_handler, node) ⇒ Object
Constructor Details
#initialize(driver_url, config) ⇒ DockerDriver
Returns a new instance of DockerDriver.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 29 def initialize(driver_url, config) super url = DockerDriver.connection_url(driver_url) if url # Export this as it's expected # to be set for command-line utilities ENV['DOCKER_HOST'] = url Chef::Log.debug("Setting Docker URL to #{url}") Docker.url = url end @connection = Docker.connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
20 21 22 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 20 def connection @connection end |
Class Method Details
.canonicalize_url(driver_url, config) ⇒ Object
44 45 46 47 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 44 def self.canonicalize_url(driver_url, config) url = DockerDriver.connection_url(driver_url) [ "docker:#{url}", config ] end |
.connection_url(driver_url) ⇒ Object
Parse the url from a driver URL, try to clean it up Returns a proper URL from the driver_url string. Examples include:
docker:/var/run/docker.sock => unix:///var/run/docker.sock
docker:192.168.0.1:1234 => tcp://192.168.0.1:1234
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 53 def self.connection_url(driver_url) scheme, url = driver_url.split(':', 2) if url && url.size > 0 # Clean up the URL with the protocol if needed (within reason) case url when /^\d+\.\d+\.\d+\.\d+:\d+$/ "tcp://#{url}" when /^\// "unix://#{url}" when /^(tcp|unix)/ url else "tcp://#{url}" end end end |
.from_url(driver_url, config) ⇒ Object
URL scheme: docker:<path> canonical URL calls realpath on <path>
25 26 27 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 25 def self.from_url(driver_url, config) DockerDriver.new(driver_url, config) end |
Instance Method Details
#allocate_image(action_handler, image_spec, image_options, machine_spec) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 124 def allocate_image(action_handler, image_spec, , machine_spec) # Set machine options on the image to match our newly created image image_spec. = { :docker_options => { :base_image => { :name => "chef_#{image_spec.name}", :repository => 'chef', :tag => image_spec.name }, :from_image => true } } end |
#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 72 def allocate_machine(action_handler, machine_spec, ) container_name = machine_spec.name machine_spec.location = { 'driver_url' => driver_url, 'driver_version' => ChefMetalDocker::VERSION, 'allocated_at' => Time.now.utc.to_s, 'host_node' => action_handler.host_node, 'container_name' => container_name, 'image_id' => [:image_id] } end |
#build_container(machine_spec, machine_options) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 91 def build_container(machine_spec, ) = [:docker_options] base_image = [:base_image] source_name = base_image[:name] source_repository = base_image[:repository] source_tag = base_image[:tag] # Don't do this if we're loading from an image if [:from_image] "#{source_repository}:#{source_tag}" else target_repository = 'chef' target_tag = machine_spec.name image = find_image(target_repository, target_tag) # kick off image creation if image == nil Chef::Log.debug("No matching images for #{target_repository}:#{target_tag}, creating!") image = Docker::Image.create('fromImage' => source_name, 'repo' => source_repository , 'tag' => source_tag) Chef::Log.debug("Allocated #{image}") image.tag('repo' => 'chef', 'tag' => target_tag) Chef::Log.debug("Tagged image #{image}") end "#{target_repository}:#{target_tag}" end end |
#connect_to_machine(machine_spec, machine_options) ⇒ Object
Connect to machine without acquiring it
143 144 145 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 143 def connect_to_machine(machine_spec, ) Chef::Log.debug('Connect to machine!') end |
#convergence_strategy_for(machine_spec, machine_options) ⇒ Object
232 233 234 235 236 237 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 232 def convergence_strategy_for(machine_spec, ) @unix_convergence_strategy ||= begin ChefMetal::ConvergenceStrategy::InstallCached. new([:convergence_options], config) end end |
#destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 147 def destroy_machine(action_handler, machine_spec, ) container_name = machine_spec.location['container_name'] Chef::Log.debug("Destroying container: #{container_name}") container = Docker::Container.get(container_name, @connection) begin Chef::Log.debug("Stopping #{container_name}") container.stop rescue Excon::Errors::NotModified # this is okay Chef::Log.debug('Already stopped!') end Chef::Log.debug("Removing #{container_name}") container.delete Chef::Log.debug("Destroying image: chef:#{container_name}") image = Docker::Image.get("chef:#{container_name}") image.delete end |
#driver_url ⇒ Object
185 186 187 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 185 def driver_url "docker:#{Docker.url}" end |
#find_image(repository, tag) ⇒ Object
179 180 181 182 183 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 179 def find_image(repository, tag) Docker::Image.all.select { |i| i.info['RepoTags'].include? "#{repository}:#{tag}" }.first end |
#image_named(image_name) ⇒ Object
173 174 175 176 177 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 173 def image_named(image_name) Docker::Image.all.select { |i| i.info['RepoTags'].include? image_name }.first end |
#machine_for(machine_spec, machine_options, base_image_name) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 206 def machine_for(machine_spec, , base_image_name) Chef::Log.debug('machine_for...') = [:docker_options] transport = DockerTransport.new(machine_spec.location['container_name'], base_image_name, nil, Docker.connection) convergence_strategy = if [:from_image] ChefMetal::ConvergenceStrategy::NoConverge.new({}, config) else convergence_strategy_for(machine_spec, ) end ChefMetalDocker::DockerContainerMachine.new( machine_spec, transport, convergence_strategy, :command => [:command], :ports => [].push([:ports]).flatten, :keep_stdin_open => [:keep_stdin_open] ) end |
#ready_image(action_handler, image_spec, image_options) ⇒ Object
138 139 140 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 138 def ready_image(action_handler, image_spec, ) Chef::Log.debug('READY IMAGE!') end |
#ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
85 86 87 88 89 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 85 def ready_machine(action_handler, machine_spec, ) base_image_name = build_container(machine_spec, ) start_machine(action_handler, machine_spec, , base_image_name) machine_for(machine_spec, , base_image_name) end |
#start_machine(action_handler, machine_spec, machine_options, base_image_name) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 189 def start_machine(action_handler, machine_spec, , base_image_name) # Spin up a docker instance if needed, otherwise use the existing one container_name = machine_spec.location['container_name'] begin Docker::Container.get(container_name, @connection) rescue Docker::Error::NotFoundError = [:docker_options] Chef::Log.debug("Start machine for container #{container_name} using base image #{base_image_name} with options #{docker_options.inspect}") image = image_named(base_image_name) container = Docker::Container.create('Image' => image.id, 'name' => container_name) Chef::Log.debug("Container id: #{container.id}") machine_spec.location['container_id'] = container.id end end |
#stop_machine(action_handler, node) ⇒ Object
169 170 171 |
# File 'lib/chef_metal_docker/docker_driver.rb', line 169 def stop_machine(action_handler, node) Chef::Log.debug("Stop machine: #{node.inspect}") end |