diff --git a/providers/container.rb b/providers/container.rb index 7cdf2fb00e..a20de09fec 100644 --- a/providers/container.rb +++ b/providers/container.rb @@ -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']) @@ -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 @@ -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