Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional compose driver #8576

Merged
merged 14 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/providers/docker/action/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def call(env)
dockerfile = machine.provider_config.dockerfile
dockerfile_path = File.join(build_dir, dockerfile)

args.push("--file=\"#{dockerfile_path}\"")
args.push("--file").push(dockerfile_path)
machine.ui.output(
I18n.t("docker_provider.building_named_dockerfile",
file: machine.provider_config.dockerfile))
Expand Down
24 changes: 24 additions & 0 deletions plugins/providers/docker/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ class Config < Vagrant.plugin("2", :config)
# @return [String]
attr_accessor :build_dir

# Use docker-compose to manage the lifecycle and environment for
# containers instead of using docker directly.
#
# @return [Boolean]
attr_accessor :compose

# Configuration Hash used for build the docker-compose composition
# file. This can be used for adding networks or volumes.
#
# @return [Hash]
attr_accessor :compose_configuration

# An optional file name of a Dockerfile to be used when building
# the image. This requires Docker >1.5.0.
#
Expand Down Expand Up @@ -138,6 +150,8 @@ def initialize
@build_args = []
@build_dir = UNSET_VALUE
@cmd = UNSET_VALUE
@compose = UNSET_VALUE
@compose_configuration = {}
@create_args = UNSET_VALUE
@dockerfile = UNSET_VALUE
@env = {}
Expand Down Expand Up @@ -201,6 +215,7 @@ def finalize!
@build_args = [] if @build_args == UNSET_VALUE
@build_dir = nil if @build_dir == UNSET_VALUE
@cmd = [] if @cmd == UNSET_VALUE
@compose = false if @compose == UNSET_VALUE
@create_args = [] if @create_args == UNSET_VALUE
@dockerfile = nil if @dockerfile == UNSET_VALUE
@env ||= {}
Expand Down Expand Up @@ -237,6 +252,11 @@ def finalize!
@vagrant_machine = @vagrant_machine.to_sym if @vagrant_machine

@expose.uniq!

if @compose_configuration.is_a?(Hash)
# Ensures configuration is using basic types
@compose_configuration = JSON.parse(@compose_configuration.to_json)
end
end

def validate(machine)
Expand All @@ -257,6 +277,10 @@ def validate(machine)
end
end

if !@compose_configuration.is_a?(Hash)
errors << I18n.t("docker_provider.errors.config.compose_configuration_hash")
end

if !@create_args.is_a?(Array)
errors << I18n.t("docker_provider.errors.config.create_args_array")
end
Expand Down
3 changes: 2 additions & 1 deletion plugins/providers/docker/driver.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "json"

require "log4r"

require_relative "./driver/compose"

module VagrantPlugins
module DockerProvider
class Driver
Expand Down
287 changes: 287 additions & 0 deletions plugins/providers/docker/driver/compose.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
require "json"
require "log4r"

module VagrantPlugins
module DockerProvider
class Driver
class Compose < Driver

# @return [Integer] Maximum number of seconds to wait for lock
LOCK_TIMEOUT = 60
# @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)
if !Vagrant::Util::Which.which("vagrant-compose")
raise Errors::DockerComposeNotInstalledError
end
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
@logger.debug("Docker compose driver initialize for machine `#{@machine.name}` (`#{@machine.id}`)")
@logger.debug("Data directory for composition file `#{@data_directory}`")
end

def build(dir, **opts, &block)
name = machine.name.to_s
@logger.debug("Applying build for `#{name}` using `#{dir}` directory.")
begin
update_composition do |composition|
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
end
rescue => error
@logger.error("Failed to apply build using `#{dir}` directory: #{error.class} - #{error}")
update_composition do |composition|
composition["services"].delete(name)
end
raise
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)
links = params.fetch(:links)
ports = Array(params[:ports])
volumes = Array(params[:volumes]).map do |v|
v = v.to_s
if v.include?(":") && (Vagrant::Util::Platform.windows? || Vagrant::Util::Platform.wsl?)
host, guest = v.split(":", 2)
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]+/, "")
v = [host, guest].join(":")
end
v
end
cmd = Array(params.fetch(:cmd))
env = Hash[*params.fetch(:env).flatten.map(&:to_s)]
expose = Array(params[:expose])
@logger.debug("Creating container `#{name}`")
begin
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)
services[name].merge!(
Hash[
params[:extra_args].map{ |k, v|
[k.to_s, v]
}
]
)
end
services[name].merge!(
"environment" => env,
"expose" => expose,
"ports" => ports,
"volumes" => volumes,
"links" => links,
"command" => cmd
)
services[name]["image"] = image if image
services[name]["hostname"] = params[:hostname] if params[:hostname]
services[name]["privileged"] = true if params[:privileged]
services[name]["pty"] = true if params[:pty]
end
rescue => error
@logger.error("Failed to create container `#{name}`: #{error.class} - #{error}")
update_composition do |composition|
composition["services"].delete(name)
end
raise
end
get_container_id(name)
end

def rm(cid)
if created?(cid)
destroy = false
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
end
end
end
end

def rmi(*_)
true
end

def created?(cid)
result = super
if !result
composition = get_composition
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
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, &block)
end
end

# Apply any changes made to the 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
if block
compose_execute(*execute_args, &block)
else
compose_execute(*execute_args)
end
end
end

# Update the composition and apply changes if requested
#
# @param [Boolean] apply Apply composition changes
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)
apply_composition!(*args)
end
end
end
end

# @return [Hash] current composition contents
def get_composition
composition = {"version" => COMPOSE_VERSION.dup}
if composition_path.exist?
composition = Vagrant::Util::DeepMerge.deep_merge(composition, YAML.load(composition_path.read))
end
composition = Vagrant::Util::DeepMerge.deep_merge(composition, machine.provider_config.compose_configuration.dup)
@logger.debug("Fetched composition with provider configuration applied: #{composition}")
composition
end

# Save the composition
#
# @param [Hash] composition New composition
def write_composition(composition)
@logger.debug("Saving composition to `#{composition_path}`: #{composition}")
tmp_file = Tempfile.new("vagrant-docker-compose")
tmp_file.write(composition.to_yaml)
tmp_file.close
synchronized do
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

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
end
end
end
end
8 changes: 8 additions & 0 deletions plugins/providers/docker/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class CommunicatorNonDocker < DockerError
error_key(:communicator_non_docker)
end

class ComposeLockTimeoutError < DockerError
error_key(:compose_lock_timeout)
end

class ContainerNotRunningError < DockerError
error_key(:not_running)
end
Expand All @@ -17,6 +21,10 @@ class ContainerNotCreatedError < DockerError
error_key(:not_created)
end

class DockerComposeNotInstalledError < DockerError
error_key(:docker_compose_not_installed)
end

class ExecuteError < DockerError
error_key(:execute_error)
end
Expand Down
Loading