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

Commit

Permalink
Merge pull request #47 from marc-/master
Browse files Browse the repository at this point in the history
Added support for chef-provisioning 1.0
  • Loading branch information
randomcamel committed Jun 8, 2015
2 parents 98969e4 + 058969c commit 8d30040
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion chef-provisioning-docker.gemspec
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
7 changes: 4 additions & 3 deletions lib/chef/provisioning/docker_driver/docker_transport.rb
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 @@ -179,11 +179,12 @@ 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!
uri = URI(url)
uri.scheme = 'http' if 'chefzero' == uri.scheme && uri.host == 'localhost'
host = Socket.getaddrinfo(uri.host, uri.scheme, nil, :STREAM)[0][3]
Chef::Log.debug("Making URL available: #{host}")

if host == '127.0.0.1' || host == '::1'
result = execute('ip route ls', :read_only => true)
result = execute('ip route list', :read_only => true)

Chef::Log.debug("IP route: #{result.stdout}")

Expand Down
45 changes: 33 additions & 12 deletions lib/chef/provisioning/docker_driver/driver.rb
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 @@ -105,7 +110,8 @@ def build_container(machine_spec, machine_options)
target_repository = 'chef'
target_tag = machine_spec.name

image = find_image(target_repository, target_tag)
# check if target image exists, if not try to look up for source image.
image = find_image(target_repository, target_tag) || find_image(source_repository, source_tag)

# kick off image creation
if image == nil
Expand All @@ -116,15 +122,22 @@ def build_container(machine_spec, machine_options)
Chef::Log.debug("Allocated #{image}")
image.tag('repo' => 'chef', 'tag' => target_tag)
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`.
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 @@ -134,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 @@ -161,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 @@ -226,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 @@ -239,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 8d30040

Please sign in to comment.