Support optional detach and pass blocks through to execution.

This commit is contained in:
Chris Roberts 2017-05-12 14:58:49 -07:00
parent 6096bb299b
commit 36ecd40c52
3 changed files with 29 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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: |-