Merge pull request #5924 from mitchellh/sethvargo/docker_check_args

Save docker args to check if running containers have changed in the Vagrantfile
This commit is contained in:
Mitchell Hashimoto 2015-07-10 11:52:20 -06:00
commit f6bf8b4789
2 changed files with 65 additions and 15 deletions

View File

@ -59,7 +59,15 @@ module VagrantPlugins
id = "$(cat #{config[:cidfile]})" id = "$(cat #{config[:cidfile]})"
if container_exists?(id) if container_exists?(id)
if container_args_changed?(config)
@machine.ui.info(I18n.t("vagrant.docker_restarting_container",
name: config[:name],
))
stop_container(id)
create_container(config)
else
start_container(id) start_container(id)
end
else else
create_container(config) create_container(config)
end end
@ -75,27 +83,41 @@ module VagrantPlugins
end end
end end
def stop_container(id)
@machine.communicate.sudo %[
docker stop #{id}
docker rm #{id}
]
end
def container_running?(id) def container_running?(id)
lookup_container(id) lookup_container(id)
end end
def container_args_changed?(config)
path = container_data_path(config)
return true if !path.exist?
args = container_run_args(config)
sha = Digest::SHA1.hexdigest(args)
return true if path.read.chomp != sha
return false
end
def create_container(config) def create_container(config)
name = config[:name] args = container_run_args(config)
# If the name is the automatically assigned name, then
# replace the "/" with "-" because "/" is not a valid
# character for a docker container name.
name = name.gsub("/", "-") if name == config[:original_name]
args = "--cidfile=#{config[:cidfile]} "
args << "-d " if config[:daemonize]
args << "--name #{name} " if name && config[:auto_assign_name]
args << "--restart=#{config[:restart]}" if config[:restart]
args << " #{config[:args]}" if config[:args]
@machine.communicate.sudo %[ @machine.communicate.sudo %[
rm -f #{config[:cidfile]} rm -f #{config[:cidfile]}
docker run #{args} #{config[:image]} #{config[:cmd]} docker run #{args}
] ]
name = container_name(config)
sha = Digest::SHA1.hexdigest(args)
container_data_path(config).open("w+") do |f|
f.write(sha)
end
end end
def lookup_container(id, list_all = false) def lookup_container(id, list_all = false)
@ -110,6 +132,33 @@ module VagrantPlugins
end end
end end
def container_name(config)
name = config[:name]
# If the name is the automatically assigned name, then
# replace the "/" with "-" because "/" is not a valid
# character for a docker container name.
name = name.gsub("/", "-") if name == config[:original_name]
name
end
def container_run_args(config)
name = container_name(config)
args = "--cidfile=#{config[:cidfile]} "
args << "-d " if config[:daemonize]
args << "--name #{name} " if name && config[:auto_assign_name]
args << "--restart=#{config[:restart]}" if config[:restart]
args << " #{config[:args]}" if config[:args]
"#{args} #{config[:image]} #{config[:cmd]}".strip
end
def container_data_path(config)
name = container_name(config)
@machine.data_dir.join("docker-#{name}")
end
protected protected
# This handles outputting the communication data back to the UI # This handles outputting the communication data back to the UI
@ -125,7 +174,6 @@ module VagrantPlugins
@machine.ui.info(data.chomp, options) @machine.ui.info(data.chomp, options)
end end
end end
end end
end end
end end

View File

@ -141,7 +141,9 @@ en:
Building Docker images... Building Docker images...
docker_running: |- docker_running: |-
-- Container: %{name} -- Container: %{name}
docker_starting_containers: docker_restarting_container: |-
-- Detected changes to container '%{name}', restarting...
docker_starting_containers: |-
Starting Docker containers... Starting Docker containers...
inserted_key: |- inserted_key: |-
Key inserted! Disconnecting and reconnecting using new SSH key... Key inserted! Disconnecting and reconnecting using new SSH key...