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 # @param [Vagrant::Machine] machine Machine instance for this driver
def initialize(machine) def initialize(machine)
if !Vagrant::Util::Which.which("vagrant-compose")
raise Errors::DockerComposeNotInstalledError
end
super() super()
@machine = machine @machine = machine
@data_directory = Pathname.new(machine.env.local_data_path). @data_directory = Pathname.new(machine.env.local_data_path).
@ -88,7 +91,10 @@ module VagrantPlugins
expose = Array(params[:expose]) expose = Array(params[:expose])
@logger.debug("Creating container `#{name}`") @logger.debug("Creating container `#{name}`")
begin 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 = composition["services"] ||= {}
services[name] ||= {} services[name] ||= {}
if params[:extra_args].is_a?(Hash) if params[:extra_args].is_a?(Hash)
@ -174,17 +180,26 @@ module VagrantPlugins
end end
# Execute a `docker-compose` command # Execute a `docker-compose` command
def compose_execute(*cmd, **opts) def compose_execute(*cmd, **opts, &block)
synchronized do synchronized do
execute("docker-compose", "-f", composition_path.to_s, 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
end end
# Apply any changes made to the composition # 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 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
end end
@ -198,7 +213,7 @@ module VagrantPlugins
result = yield composition result = yield composition
write_composition(composition) write_composition(composition)
if args.include?(:apply) || (args.include?(:conditional) && result) if args.include?(:apply) || (args.include?(:conditional) && result)
apply_composition! apply_composition!(*args)
end end
end end
end end

View File

@ -21,6 +21,10 @@ module VagrantPlugins
error_key(:not_created) error_key(:not_created)
end end
class DockerComposeNotInstalledError < DockerError
error_key(:docker_compose_not_installed)
end
class ExecuteError < DockerError class ExecuteError < DockerError
error_key(:execute_error) error_key(:execute_error)
end end

View File

@ -122,6 +122,10 @@ en:
to become available. Please try to run your command again. If you to become available. Please try to run your command again. If you
continue to experience this error it may be resolved by disabling continue to experience this error it may be resolved by disabling
parallel execution. 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: |- not_created: |-
The container hasn't been created yet. The container hasn't been created yet.
not_running: |- not_running: |-