2017-05-10 01:54:15 +00:00
|
|
|
require "json"
|
|
|
|
require "log4r"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module DockerProvider
|
|
|
|
class Driver
|
|
|
|
class Compose < Driver
|
|
|
|
|
2017-05-11 17:13:31 +00:00
|
|
|
# @return [Integer] Maximum number of seconds to wait for lock
|
|
|
|
LOCK_TIMEOUT = 60
|
2017-05-10 01:54:15 +00:00
|
|
|
# @return [String] Compose file format version
|
|
|
|
COMPOSE_VERSION = "2".freeze
|
|
|
|
|
|
|
|
# @return [Pathname] data directory to store composition
|
|
|
|
attr_reader :data_directory
|
|
|
|
# @return [Vagrant::Machine]
|
|
|
|
attr_reader :machine
|
|
|
|
|
|
|
|
# Create a new driver instance
|
|
|
|
#
|
|
|
|
# @param [Vagrant::Machine] machine Machine instance for this driver
|
|
|
|
def initialize(machine)
|
2017-06-08 18:09:41 +00:00
|
|
|
if !Vagrant::Util::Which.which("docker-compose")
|
2017-05-12 21:58:49 +00:00
|
|
|
raise Errors::DockerComposeNotInstalledError
|
|
|
|
end
|
2017-05-10 01:54:15 +00:00
|
|
|
super()
|
|
|
|
@machine = machine
|
|
|
|
@data_directory = Pathname.new(machine.env.local_data_path).
|
|
|
|
join("docker-compose")
|
|
|
|
@data_directory.mkpath
|
|
|
|
@logger = Log4r::Logger.new("vagrant::docker::driver::compose")
|
|
|
|
@compose_lock = Mutex.new
|
2017-05-11 17:13:31 +00:00
|
|
|
@logger.debug("Docker compose driver initialize for machine `#{@machine.name}` (`#{@machine.id}`)")
|
|
|
|
@logger.debug("Data directory for composition file `#{@data_directory}`")
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def build(dir, **opts, &block)
|
2017-05-11 21:01:42 +00:00
|
|
|
name = machine.name.to_s
|
|
|
|
@logger.debug("Applying build for `#{name}` using `#{dir}` directory.")
|
2017-05-11 17:13:31 +00:00
|
|
|
begin
|
2017-05-12 13:46:31 +00:00
|
|
|
update_composition do |composition|
|
2017-05-11 21:01:42 +00:00
|
|
|
services = composition["services"] ||= {}
|
|
|
|
services[name] ||= {}
|
|
|
|
services[name]["build"] = {"context" => dir}
|
|
|
|
# Extract custom dockerfile location if set
|
|
|
|
if opts[:extra_args] && opts[:extra_args].include?("--file")
|
|
|
|
services[name]["build"]["dockerfile"] = opts[:extra_args][opts[:extra_args].index("--file") + 1]
|
|
|
|
end
|
|
|
|
# Extract any build args that can be found
|
|
|
|
case opts[:build_args]
|
|
|
|
when Array
|
|
|
|
if opts[:build_args].include?("--build-arg")
|
|
|
|
idx = 0
|
|
|
|
build_args = {}
|
|
|
|
while(idx < opts[:build_args].size)
|
|
|
|
arg_value = opts[:build_args][idx]
|
|
|
|
idx += 1
|
|
|
|
if arg_value.start_with?("--build-arg")
|
|
|
|
if !arg_value.include?("=")
|
|
|
|
arg_value = opts[:build_args][idx]
|
|
|
|
idx += 1
|
|
|
|
end
|
|
|
|
key, val = arg_value.to_s.split("=", 2).to_s.split("=")
|
|
|
|
build_args[key] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when Hash
|
|
|
|
services[name]["build"]["args"] = opts[:build_args]
|
|
|
|
end
|
2017-05-11 17:13:31 +00:00
|
|
|
end
|
|
|
|
rescue => error
|
|
|
|
@logger.error("Failed to apply build using `#{dir}` directory: #{error.class} - #{error}")
|
|
|
|
update_composition do |composition|
|
2017-05-11 21:01:42 +00:00
|
|
|
composition["services"].delete(name)
|
2017-05-11 17:13:31 +00:00
|
|
|
end
|
|
|
|
raise
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(params, **opts, &block)
|
|
|
|
# NOTE: Use the direct machine name as we don't
|
|
|
|
# need to worry about uniqueness with compose
|
|
|
|
name = machine.name.to_s
|
|
|
|
image = params.fetch(:image)
|
2017-07-31 21:23:50 +00:00
|
|
|
links = Array(params.fetch(:links, [])).map do |link|
|
|
|
|
case link
|
|
|
|
when Array
|
|
|
|
link
|
|
|
|
else
|
|
|
|
link.to_s.split(":")
|
|
|
|
end
|
|
|
|
end
|
2017-05-10 01:54:15 +00:00
|
|
|
ports = Array(params[:ports])
|
2017-05-12 22:11:10 +00:00
|
|
|
volumes = Array(params[:volumes]).map do |v|
|
|
|
|
v = v.to_s
|
2017-07-31 21:56:51 +00:00
|
|
|
host, guest = v.split(":", 2)
|
2017-05-12 22:11:10 +00:00
|
|
|
if v.include?(":") && (Vagrant::Util::Platform.windows? || Vagrant::Util::Platform.wsl?)
|
|
|
|
host = Vagrant::Util::Platform.windows_path(host)
|
|
|
|
# NOTE: Docker does not support UNC style paths (which also
|
|
|
|
# means that there's no long path support). Hopefully this
|
|
|
|
# will be fixed someday and the gsub below can be removed.
|
|
|
|
host.gsub!(/^[^A-Za-z]+/, "")
|
|
|
|
end
|
2017-07-31 21:56:51 +00:00
|
|
|
host = @machine.env.cwd.join(host).to_s
|
|
|
|
"#{host}:#{guest}"
|
2017-05-12 22:11:10 +00:00
|
|
|
end
|
2017-05-10 01:54:15 +00:00
|
|
|
cmd = Array(params.fetch(:cmd))
|
2017-05-12 00:28:04 +00:00
|
|
|
env = Hash[*params.fetch(:env).flatten.map(&:to_s)]
|
2017-05-10 01:54:15 +00:00
|
|
|
expose = Array(params[:expose])
|
2017-05-11 17:13:31 +00:00
|
|
|
@logger.debug("Creating container `#{name}`")
|
2017-05-10 01:54:15 +00:00
|
|
|
begin
|
2017-05-12 21:58:49 +00:00
|
|
|
update_args = [:apply]
|
|
|
|
update_args.push(:detach) if params[:detach]
|
|
|
|
update_args << block
|
|
|
|
update_composition(*update_args) do |composition|
|
2017-05-10 01:54:15 +00:00
|
|
|
services = composition["services"] ||= {}
|
2017-05-11 21:01:42 +00:00
|
|
|
services[name] ||= {}
|
2017-05-12 00:28:04 +00:00
|
|
|
if params[:extra_args].is_a?(Hash)
|
|
|
|
services[name].merge!(
|
|
|
|
Hash[
|
|
|
|
params[:extra_args].map{ |k, v|
|
|
|
|
[k.to_s, v]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2017-05-11 21:25:57 +00:00
|
|
|
services[name].merge!(
|
2017-05-10 01:54:15 +00:00
|
|
|
"environment" => env,
|
|
|
|
"expose" => expose,
|
|
|
|
"ports" => ports,
|
|
|
|
"volumes" => volumes,
|
|
|
|
"links" => links,
|
|
|
|
"command" => cmd
|
2017-05-11 21:01:42 +00:00
|
|
|
)
|
2017-05-12 13:46:31 +00:00
|
|
|
services[name]["image"] = image if image
|
2017-05-12 00:28:04 +00:00
|
|
|
services[name]["hostname"] = params[:hostname] if params[:hostname]
|
|
|
|
services[name]["privileged"] = true if params[:privileged]
|
|
|
|
services[name]["pty"] = true if params[:pty]
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
2017-05-11 17:13:31 +00:00
|
|
|
rescue => error
|
|
|
|
@logger.error("Failed to create container `#{name}`: #{error.class} - #{error}")
|
|
|
|
update_composition do |composition|
|
2017-05-10 01:54:15 +00:00
|
|
|
composition["services"].delete(name)
|
|
|
|
end
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
get_container_id(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm(cid)
|
|
|
|
if created?(cid)
|
|
|
|
destroy = false
|
2017-05-11 21:01:42 +00:00
|
|
|
synchronized do
|
|
|
|
compose_execute("rm", "-f", machine.name.to_s)
|
|
|
|
update_composition do |composition|
|
|
|
|
if composition["services"] && composition["services"].key?(machine.name.to_s)
|
|
|
|
@logger.info("Removing container `#{machine.name}`")
|
|
|
|
if composition["services"].size > 1
|
|
|
|
composition["services"].delete(machine.name.to_s)
|
|
|
|
else
|
|
|
|
destroy = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if destroy
|
|
|
|
@logger.info("No containers remain. Destroying full environment.")
|
|
|
|
compose_execute("down", "--volumes", "--rmi", "local")
|
|
|
|
@logger.info("Deleting composition path `#{composition_path}`")
|
|
|
|
composition_path.delete
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-11 21:01:42 +00:00
|
|
|
def rmi(*_)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-05-10 01:54:15 +00:00
|
|
|
def created?(cid)
|
|
|
|
result = super
|
|
|
|
if !result
|
2017-05-11 17:13:31 +00:00
|
|
|
composition = get_composition
|
2017-05-10 01:54:15 +00:00
|
|
|
if composition["services"] && composition["services"].has_key?(machine.name.to_s)
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Lookup the ID for the container with the given name
|
|
|
|
#
|
|
|
|
# @param [String] name Name of container
|
|
|
|
# @return [String] Container ID
|
|
|
|
def get_container_id(name)
|
|
|
|
compose_execute("ps", "-q", name).chomp
|
|
|
|
end
|
|
|
|
|
|
|
|
# Execute a `docker-compose` command
|
2017-05-12 21:58:49 +00:00
|
|
|
def compose_execute(*cmd, **opts, &block)
|
2017-05-11 17:13:31 +00:00
|
|
|
synchronized do
|
2017-05-10 01:54:15 +00:00
|
|
|
execute("docker-compose", "-f", composition_path.to_s,
|
2017-05-12 21:58:49 +00:00
|
|
|
"-p", machine.env.cwd.basename.to_s, *cmd, **opts, &block)
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Apply any changes made to the composition
|
2017-05-12 21:58:49 +00:00
|
|
|
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
|
2017-05-10 01:54:15 +00:00
|
|
|
machine.env.lock("compose", retry: true) do
|
2017-05-12 21:58:49 +00:00
|
|
|
if block
|
|
|
|
compose_execute(*execute_args, &block)
|
|
|
|
else
|
|
|
|
compose_execute(*execute_args)
|
|
|
|
end
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update the composition and apply changes if requested
|
|
|
|
#
|
|
|
|
# @param [Boolean] apply Apply composition changes
|
2017-05-11 17:13:31 +00:00
|
|
|
def update_composition(*args)
|
|
|
|
synchronized do
|
|
|
|
machine.env.lock("compose", retry: true) do
|
|
|
|
composition = get_composition
|
|
|
|
result = yield composition
|
|
|
|
write_composition(composition)
|
|
|
|
if args.include?(:apply) || (args.include?(:conditional) && result)
|
2017-05-12 21:58:49 +00:00
|
|
|
apply_composition!(*args)
|
2017-05-11 17:13:31 +00:00
|
|
|
end
|
|
|
|
end
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Hash] current composition contents
|
2017-05-11 17:13:31 +00:00
|
|
|
def get_composition
|
2017-05-10 01:54:15 +00:00
|
|
|
composition = {"version" => COMPOSE_VERSION.dup}
|
|
|
|
if composition_path.exist?
|
2017-05-12 00:28:04 +00:00
|
|
|
composition = Vagrant::Util::DeepMerge.deep_merge(composition, YAML.load(composition_path.read))
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
2017-05-12 00:28:04 +00:00
|
|
|
composition = Vagrant::Util::DeepMerge.deep_merge(composition, machine.provider_config.compose_configuration.dup)
|
2017-05-11 17:13:31 +00:00
|
|
|
@logger.debug("Fetched composition with provider configuration applied: #{composition}")
|
2017-05-10 01:54:15 +00:00
|
|
|
composition
|
|
|
|
end
|
|
|
|
|
|
|
|
# Save the composition
|
|
|
|
#
|
|
|
|
# @param [Hash] composition New composition
|
|
|
|
def write_composition(composition)
|
2017-05-11 17:13:31 +00:00
|
|
|
@logger.debug("Saving composition to `#{composition_path}`: #{composition}")
|
2017-05-10 01:54:15 +00:00
|
|
|
tmp_file = Tempfile.new("vagrant-docker-compose")
|
|
|
|
tmp_file.write(composition.to_yaml)
|
|
|
|
tmp_file.close
|
2017-05-11 17:13:31 +00:00
|
|
|
synchronized do
|
2017-05-10 01:54:15 +00:00
|
|
|
FileUtils.mv(tmp_file.path, composition_path.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Pathname] path to the docker-compose.yml file
|
|
|
|
def composition_path
|
|
|
|
data_directory.join("docker-compose.yml")
|
|
|
|
end
|
2017-05-11 17:13:31 +00:00
|
|
|
|
|
|
|
def synchronized
|
|
|
|
if !@compose_lock.owned?
|
|
|
|
timeout = LOCK_TIMEOUT.to_f
|
|
|
|
until @compose_lock.owned?
|
|
|
|
if @compose_lock.try_lock
|
|
|
|
if timeout > 0
|
|
|
|
timeout -= sleep(1)
|
|
|
|
else
|
|
|
|
raise Errors::ComposeLockTimeoutError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
got_lock = true
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
result = yield
|
|
|
|
ensure
|
|
|
|
@compose_lock.unlock if got_lock
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
2017-05-10 01:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|