Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Commit

Permalink
Added support for chef-provisioning 1.0
Browse files Browse the repository at this point in the history
Addresses
- #37
- #24

Signed-off-by: Maksim Chizhov <maksim.chizhov@gmail.com>
  • Loading branch information
marc- authored and Chris Doherty committed Jun 3, 2015
1 parent 17f4b12 commit 38fdc41
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion chef-provisioning-docker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
8 changes: 6 additions & 2 deletions lib/chef/provisioning/docker_driver/docker_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
Expand Down
40 changes: 28 additions & 12 deletions lib/chef/provisioning/docker_driver/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand All @@ -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]
Expand All @@ -119,17 +124,20 @@ 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

"#{target_repository}:#{target_tag}"
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}",
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 38fdc41

Please sign in to comment.