Skip to content

Commit

Permalink
Better support for named containers
Browse files Browse the repository at this point in the history
This modifies container matching to look only at the container name for cases
in which a container_name is explicitly provided.
  • Loading branch information
Frank Macreery committed May 19, 2014
1 parent 57cbc97 commit 44ed75a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions providers/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ def load_current_resource
@current_resource = Chef::Resource::DockerContainer.new(new_resource)
wait_until_ready!
docker_containers.each do |ps|
unless container_id_matches?(ps['id'])
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'])
end
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 @@ -134,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 @@ -150,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

0 comments on commit 44ed75a

Please sign in to comment.