diff --git a/chef-provisioning-docker.gemspec b/chef-provisioning-docker.gemspec index a84d217..679c1fb 100644 --- a/chef-provisioning-docker.gemspec +++ b/chef-provisioning-docker.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/opscode/chef-provisioning-docker' s.add_dependency 'chef' - s.add_dependency 'chef-provisioning', '~> 0.9' + s.add_dependency 'chef-provisioning', '~> 1.0' s.add_dependency 'docker-api' s.add_dependency 'sys-proctable' diff --git a/lib/chef/provisioning/docker_driver/docker_transport.rb b/lib/chef/provisioning/docker_driver/docker_transport.rb index c4a7efc..82eb7fa 100644 --- a/lib/chef/provisioning/docker_driver/docker_transport.rb +++ b/lib/chef/provisioning/docker_driver/docker_transport.rb @@ -63,8 +63,8 @@ def execute(command, options={}) args = ['docker', 'run', '--name', container_name] - if options[:env] - options[:env].each do |key, value| + if options[:env] + options[:env].each do |key, value| args << '-e' args << "#{key}=#{value}" end @@ -178,6 +178,10 @@ def upload_file(local_path, path) def make_url_available_to_remote(url) # The host is already open to the container. Just find out its address and return it! + # FIXME: make a proper fix + if /^chefzero:\/\/localhost/ =~ url + url.gsub!(/^chefzero/, 'http') + end uri = URI(url) host = Socket.getaddrinfo(uri.host, uri.scheme, nil, :STREAM)[0][3] Chef::Log.debug("Making URL available: #{host}") diff --git a/lib/chef/provisioning/docker_driver/driver.rb b/lib/chef/provisioning/docker_driver/driver.rb index b270638..b3be326 100644 --- a/lib/chef/provisioning/docker_driver/driver.rb +++ b/lib/chef/provisioning/docker_driver/driver.rb @@ -5,6 +5,7 @@ require 'chef/provisioning/docker_driver/docker_container_machine' require 'chef/provisioning/convergence_strategy/install_cached' require 'chef/provisioning/convergence_strategy/no_converge' +require 'chef/mash' require 'yaml' require 'docker/container' @@ -73,13 +74,14 @@ def self.connection_url(driver_url) def allocate_machine(action_handler, machine_spec, machine_options) container_name = machine_spec.name - machine_spec.location = { + machine_spec.reference = { 'driver_url' => driver_url, 'driver_version' => Chef::Provisioning::DockerDriver::VERSION, 'allocated_at' => Time.now.utc.to_s, 'host_node' => action_handler.host_node, 'container_name' => container_name, - 'image_id' => machine_options[:image_id] + 'image_id' => machine_options[:image_id], + 'docker_options' => machine_options[:docker_options] } end @@ -90,10 +92,13 @@ def ready_machine(action_handler, machine_spec, machine_options) end def build_container(machine_spec, machine_options) - docker_options = machine_options[:docker_options] base_image = docker_options[:base_image] + if !base_image + Chef::Log.debug("No base images specified in docker options.") + base_image = base_image_for(machine_spec) + end source_name = base_image[:name] source_repository = base_image[:repository] source_tag = base_image[:tag] @@ -119,7 +124,7 @@ def build_container(machine_spec, machine_options) Chef::Log.debug("Tagged image #{image}") elsif not image.info['RepoTags'].include? "#{target_repository}:#{target_tag}" # if `find_image(source_repository, source_tag)` returned result, assign target tag to it to be able - # find it in `start_machine`. + # find it in `start_machine`. image.tag('repo' => target_repository, 'tag' => target_tag) end @@ -127,9 +132,12 @@ def build_container(machine_spec, machine_options) end end - def allocate_image(action_handler, image_spec, image_options, machine_spec) + def allocate_image(action_handler, image_spec, image_options, machine_spec, machine_options) # Set machine options on the image to match our newly created image - image_spec.machine_options = { + image_spec.reference = { + 'driver_url' => driver_url, + 'driver_version' => Chef::Provisioning::DockerDriver::VERSION, + 'allocated_at' => Time.now.to_i, :docker_options => { :base_image => { :name => "chef_#{image_spec.name}", @@ -139,6 +147,8 @@ def allocate_image(action_handler, image_spec, image_options, machine_spec) :from_image => true } } + # Workaround for chef/chef-provisioning-docker#37 + machine_spec.attrs[:keep_image] = true end def ready_image(action_handler, image_spec, image_options) @@ -166,10 +176,11 @@ def destroy_machine(action_handler, machine_spec, machine_options) 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 - + if !machine_spec.attrs[:keep_image] && !machine_options[:keep_image] + Chef::Log.debug("Destroying image: chef:#{container_name}") + image = Docker::Image.get("chef:#{container_name}") + image.delete + end end def stop_machine(action_handler, node) @@ -231,8 +242,8 @@ def machine_for(machine_spec, machine_options, base_image_name) convergence_strategy, :command => docker_options[:command], :env => docker_options[:env], - :ports => [].push(docker_options[:ports]).flatten, - :volumes => [].push(docker_options[:volumes]).flatten.compact, + :ports => Array(docker_options[:ports]), + :volumes => Array(docker_options[:volumes]), :keep_stdin_open => docker_options[:keep_stdin_open] ) end @@ -244,6 +255,11 @@ def convergence_strategy_for(machine_spec, machine_options) end end + def base_image_for(machine_spec) + Chef::Log.debug("Looking for image #{machine_spec.from_image}") + image_spec = machine_spec.managed_entry_store.get!(:machine_image, machine_spec.from_image) + Mash.new(image_spec.reference)[:docker_options][:base_image] + end end end end