From 36ecd40c529424bcfd4135a15a3e00cab64b0915 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 12 May 2017 14:58:49 -0700 Subject: [PATCH] Support optional detach and pass blocks through to execution. --- plugins/providers/docker/driver/compose.rb | 27 +++++++++++++++++----- plugins/providers/docker/errors.rb | 4 ++++ templates/locales/providers_docker.yml | 4 ++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/plugins/providers/docker/driver/compose.rb b/plugins/providers/docker/driver/compose.rb index a2d833a23..92e4e2fd8 100644 --- a/plugins/providers/docker/driver/compose.rb +++ b/plugins/providers/docker/driver/compose.rb @@ -20,6 +20,9 @@ module VagrantPlugins # # @param [Vagrant::Machine] machine Machine instance for this driver def initialize(machine) + if !Vagrant::Util::Which.which("vagrant-compose") + raise Errors::DockerComposeNotInstalledError + end super() @machine = machine @data_directory = Pathname.new(machine.env.local_data_path). @@ -88,7 +91,10 @@ module VagrantPlugins expose = Array(params[:expose]) @logger.debug("Creating container `#{name}`") begin - update_composition(:apply) do |composition| + update_args = [:apply] + update_args.push(:detach) if params[:detach] + update_args << block + update_composition(*update_args) do |composition| services = composition["services"] ||= {} services[name] ||= {} if params[:extra_args].is_a?(Hash) @@ -174,17 +180,26 @@ module VagrantPlugins end # Execute a `docker-compose` command - def compose_execute(*cmd, **opts) + def compose_execute(*cmd, **opts, &block) synchronized do execute("docker-compose", "-f", composition_path.to_s, - "-p", machine.env.cwd.basename.to_s, *cmd, **opts) + "-p", machine.env.cwd.basename.to_s, *cmd, **opts, &block) end end # Apply any changes made to the composition - def apply_composition! + def apply_composition!(*args) + block = args.detect{|arg| arg.is_a?(Proc) } + execute_args = ["up", "--remove-orphans"] + if args.include?(:detach) + execute_args << "-d" + end machine.env.lock("compose", retry: true) do - compose_execute("up", "-d", "--remove-orphans") + if block + compose_execute(*execute_args, &block) + else + compose_execute(*execute_args) + end end end @@ -198,7 +213,7 @@ module VagrantPlugins result = yield composition write_composition(composition) if args.include?(:apply) || (args.include?(:conditional) && result) - apply_composition! + apply_composition!(*args) end end end diff --git a/plugins/providers/docker/errors.rb b/plugins/providers/docker/errors.rb index 096f6fa5b..36e1b9859 100644 --- a/plugins/providers/docker/errors.rb +++ b/plugins/providers/docker/errors.rb @@ -21,6 +21,10 @@ module VagrantPlugins error_key(:not_created) end + class DockerComposeNotInstalledError < DockerError + error_key(:docker_compose_not_installed) + end + class ExecuteError < DockerError error_key(:execute_error) end diff --git a/templates/locales/providers_docker.yml b/templates/locales/providers_docker.yml index 50f9959ab..e21b90d81 100644 --- a/templates/locales/providers_docker.yml +++ b/templates/locales/providers_docker.yml @@ -122,6 +122,10 @@ en: to become available. Please try to run your command again. If you continue to experience this error it may be resolved by disabling parallel execution. + docker_compose_not_installed: |- + Vagrant has been instructed to use to use the Compose driver for the + Docker plugin but was unable to locate the `docker-compose` executable. + Ensure that `docker-compose` is installed and available on the PATH. not_created: |- The container hasn't been created yet. not_running: |-