2013-11-26 21:48:51 +00:00
|
|
|
require 'set'
|
|
|
|
|
|
|
|
module VagrantPlugins
|
2014-03-26 23:32:31 +00:00
|
|
|
module DockerProvisioner
|
2013-11-26 21:48:51 +00:00
|
|
|
class Config < Vagrant.plugin("2", :config)
|
2014-02-03 21:23:26 +00:00
|
|
|
attr_reader :images
|
2017-06-23 23:32:36 +00:00
|
|
|
attr_accessor :post_install_provisioner
|
2015-11-20 01:45:16 +00:00
|
|
|
|
2013-11-26 21:48:51 +00:00
|
|
|
def initialize
|
2015-11-20 01:45:16 +00:00
|
|
|
@images = Set.new
|
2017-06-23 23:32:36 +00:00
|
|
|
@post_install_provisioner = nil
|
2014-02-03 21:23:26 +00:00
|
|
|
|
|
|
|
@__build_images = []
|
|
|
|
@__containers = Hash.new { |h, k| h[k] = {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Accessor for internal state.
|
|
|
|
def build_images
|
|
|
|
@__build_images
|
|
|
|
end
|
|
|
|
|
|
|
|
# Accessor for the internal state.
|
|
|
|
def containers
|
|
|
|
@__containers
|
2013-11-26 21:48:51 +00:00
|
|
|
end
|
|
|
|
|
2014-01-14 17:22:55 +00:00
|
|
|
# Defines an image to build using `docker build` within the machine.
|
|
|
|
#
|
|
|
|
# @param [String] path Path to the Dockerfile to pass to
|
|
|
|
# `docker build`.
|
|
|
|
def build_image(path, **opts)
|
2014-02-03 21:23:26 +00:00
|
|
|
@__build_images << [path, opts]
|
2014-01-14 17:22:55 +00:00
|
|
|
end
|
|
|
|
|
2013-12-03 22:25:20 +00:00
|
|
|
def images=(images)
|
|
|
|
@images = Set.new(images)
|
|
|
|
end
|
|
|
|
|
2013-11-26 21:48:51 +00:00
|
|
|
def pull_images(*images)
|
|
|
|
@images += images.map(&:to_s)
|
|
|
|
end
|
|
|
|
|
2017-06-23 23:32:36 +00:00
|
|
|
def post_install_provision(name, **options, &block)
|
|
|
|
# Abort
|
|
|
|
raise DockerError, :wrong_provisioner if options[:type] == "docker"
|
|
|
|
|
|
|
|
proxy = VagrantPlugins::Kernel_V2::VMConfig.new
|
|
|
|
proxy.provision(name, **options, &block)
|
|
|
|
@post_install_provisioner = proxy.provisioners.first
|
|
|
|
end
|
|
|
|
|
2013-12-03 22:33:13 +00:00
|
|
|
def run(name, **options)
|
2014-02-03 21:23:26 +00:00
|
|
|
@__containers[name.to_s] = options.dup
|
|
|
|
end
|
2013-11-26 21:48:51 +00:00
|
|
|
|
2014-02-03 21:23:26 +00:00
|
|
|
def merge(other)
|
|
|
|
super.tap do |result|
|
|
|
|
result.pull_images(*(other.images + self.images))
|
|
|
|
|
|
|
|
build_images = @__build_images.dup
|
|
|
|
build_images += other.build_images
|
|
|
|
result.instance_variable_set(:@__build_images, build_images)
|
|
|
|
|
|
|
|
containers = {}
|
|
|
|
@__containers.each do |name, params|
|
|
|
|
containers[name] = params.dup
|
|
|
|
end
|
|
|
|
other.containers.each do |name, params|
|
|
|
|
containers[name] = @__containers[name].merge(params)
|
|
|
|
end
|
|
|
|
|
|
|
|
result.instance_variable_set(:@__containers, containers)
|
|
|
|
end
|
2013-11-26 21:48:51 +00:00
|
|
|
end
|
|
|
|
|
2013-12-03 22:18:30 +00:00
|
|
|
def finalize!
|
2014-02-03 21:23:26 +00:00
|
|
|
@__containers.each do |name, params|
|
|
|
|
params[:image] ||= name
|
2015-01-05 23:29:01 +00:00
|
|
|
params[:auto_assign_name] = true if !params.key?(:auto_assign_name)
|
|
|
|
params[:daemonize] = true if !params.key?(:daemonize)
|
2015-07-07 18:17:24 +00:00
|
|
|
params[:restart] = "always" if !params.key?(:restart)
|
2013-11-26 21:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|