From c7a5592b96563fa9cd0a6d9555d5c7b313e5b6c2 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Wed, 4 Dec 2013 22:07:41 -0200 Subject: [PATCH 1/2] provisioners/docker: Ensure checks for whether the container exists / is running works accross multiple Docker versions [GH-2579] --- plugins/provisioners/docker/docker_client.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/plugins/provisioners/docker/docker_client.rb b/plugins/provisioners/docker/docker_client.rb index 535c6e9fc..b0a318bdf 100644 --- a/plugins/provisioners/docker/docker_client.rb +++ b/plugins/provisioners/docker/docker_client.rb @@ -44,15 +44,21 @@ module VagrantPlugins id = "$(cat #{config[:cidfile]})" - if container_exist?(id) + if container_exists?(id) start_container(id) else create_container(config) end end - def container_exist?(id) - @machine.communicate.test("sudo docker ps -a -q | grep -q #{id}") + def container_exists?(id) + @machine.communicate.tap do |comm| + # Docker < 0.7.0 stores container IDs using its short version while + # recent versions use the full container ID + # See https://github.com/dotcloud/docker/pull/2140 for more information + return comm.test("sudo docker ps -a -q | grep -wFq #{id}") || + comm.test("sudo docker ps -a -q -notrunc | grep -wFq #{id}") + end end def start_container(id) @@ -62,7 +68,13 @@ module VagrantPlugins end def container_running?(id) - @machine.communicate.test("sudo docker ps -q | grep #{id}") + @machine.communicate.tap do |comm| + # Docker < 0.7.0 stores container IDs using its short version while + # recent versions use the full container ID + # See https://github.com/dotcloud/docker/pull/2140 for more information + return comm.test("sudo docker ps -q | grep -wFq #{id}") || + comm.test("sudo docker ps -q -notrunc | grep -wFq #{id}") + end end def create_container(config) From 7e0b0a5a3c7df4e78184f11f5dabbd3c231e6371 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Wed, 4 Dec 2013 22:13:17 -0200 Subject: [PATCH 2/2] provisioners/docker: Extract container lookup method --- plugins/provisioners/docker/docker_client.rb | 28 ++++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/plugins/provisioners/docker/docker_client.rb b/plugins/provisioners/docker/docker_client.rb index b0a318bdf..7affc7c3a 100644 --- a/plugins/provisioners/docker/docker_client.rb +++ b/plugins/provisioners/docker/docker_client.rb @@ -52,13 +52,7 @@ module VagrantPlugins end def container_exists?(id) - @machine.communicate.tap do |comm| - # Docker < 0.7.0 stores container IDs using its short version while - # recent versions use the full container ID - # See https://github.com/dotcloud/docker/pull/2140 for more information - return comm.test("sudo docker ps -a -q | grep -wFq #{id}") || - comm.test("sudo docker ps -a -q -notrunc | grep -wFq #{id}") - end + lookup_container(id, true) end def start_container(id) @@ -68,13 +62,7 @@ module VagrantPlugins end def container_running?(id) - @machine.communicate.tap do |comm| - # Docker < 0.7.0 stores container IDs using its short version while - # recent versions use the full container ID - # See https://github.com/dotcloud/docker/pull/2140 for more information - return comm.test("sudo docker ps -q | grep -wFq #{id}") || - comm.test("sudo docker ps -q -notrunc | grep -wFq #{id}") - end + lookup_container(id) end def create_container(config) @@ -85,6 +73,18 @@ module VagrantPlugins docker run #{args} #{config[:image]} #{config[:cmd]} ] end + + def lookup_container(id, list_all = false) + docker_ps = "sudo docker ps -q" + docker_ps << " -a" if list_all + @machine.communicate.tap do |comm| + # Docker < 0.7.0 stores container IDs using its short version while + # recent versions use the full container ID + # See https://github.com/dotcloud/docker/pull/2140 for more information + return comm.test("#{docker_ps} | grep -wFq #{id}") || + comm.test("#{docker_ps} -notrunc | grep -wFq #{id}") + end + end end end end