Skip to content

Commit

Permalink
Merge #156
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Jun 24, 2014
2 parents 0162495 + 4d1c28c commit 357d22c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
17 changes: 14 additions & 3 deletions providers/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ def load_current_resource
@current_resource = Chef::Resource::DockerContainer.new(new_resource)
wait_until_ready!
docker_containers.each do |ps|
next unless container_image_matches?(ps['image'])
next unless container_command_matches_if_exists?(ps['command'])
next unless container_name_matches_if_exists?(ps['names'])
next unless container_matches?(ps)
Chef::Log.debug('Matched docker container: ' + ps['line'].squeeze(' '))
@current_resource.container_name(ps['names'])
@current_resource.created(ps['created'])
Expand Down Expand Up @@ -132,6 +130,15 @@ def commit
docker_cmd!("commit #{commit_args} #{current_resource.id} #{commit_end_args}")
end

def container_matches?(ps)
return true if container_id_matches?(ps['id'])
return true if container_name_matches?(ps['names'])
return false unless container_image_matches?(ps['image'])
return false unless container_command_matches_if_exists?(ps['command'])
return false unless container_name_matches_if_exists?(ps['names'])
true
end

def container_command_matches_if_exists?(command)
return true if new_resource.command.nil?
# try the exact command but also the command with the ' and " stripped out, since docker will
Expand All @@ -148,6 +155,10 @@ def container_image_matches?(image)
image.include?(new_resource.image)
end

def container_name_matches?(names)
new_resource.container_name && new_resource.container_name == names
end

def container_name_matches_if_exists?(names)
return false if new_resource.container_name && new_resource.container_name != names
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
assert container_running?('bflad/testcontainerd')
service('testcontainerd').must_be_running
end

it "has a named busybox-container running sleep 8888" do
cmd = container_info("busybox-container").first['Config']['Cmd']
assert cmd.grep(/8888/).count > 0
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'json'

module Helpers
module DockerTest
require 'chef/mixin/shell_out'
Expand Down Expand Up @@ -29,5 +31,9 @@ def image_exists?(image)
di.stdout.include?(image)
end

def container_info(name)
JSON.parse(shell_out("docker inspect #{name}").stdout)
end

end
end
16 changes: 16 additions & 0 deletions test/cookbooks/docker_test/recipes/container_lwrp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@
detach true
port '9999:9999'
end

docker_container "busybox-container" do
image "busybox"
container_name "busybox-container"
command "sleep 7777"
detach true
init_type false
end

docker_container "busybox-container" do
image "busybox"
container_name "busybox-container"
command "sleep 8888"
init_type false
action :redeploy
end

0 comments on commit 357d22c

Please sign in to comment.