provisioners/docker: configuration is mergeable
This commit is contained in:
parent
2cc56119b7
commit
d864187b1a
|
@ -3,14 +3,25 @@ require 'set'
|
|||
module VagrantPlugins
|
||||
module Docker
|
||||
class Config < Vagrant.plugin("2", :config)
|
||||
attr_reader :build_images, :images, :containers, :build_options
|
||||
attr_reader :images
|
||||
attr_accessor :version
|
||||
|
||||
def initialize
|
||||
@images = Set.new
|
||||
@containers = Hash.new
|
||||
@version = UNSET_VALUE
|
||||
@build_images = []
|
||||
|
||||
@__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
|
||||
end
|
||||
|
||||
# Defines an image to build using `docker build` within the machine.
|
||||
|
@ -18,7 +29,7 @@ module VagrantPlugins
|
|||
# @param [String] path Path to the Dockerfile to pass to
|
||||
# `docker build`.
|
||||
def build_image(path, **opts)
|
||||
@build_images << [path, opts]
|
||||
@__build_images << [path, opts]
|
||||
end
|
||||
|
||||
def images=(images)
|
||||
|
@ -30,22 +41,36 @@ module VagrantPlugins
|
|||
end
|
||||
|
||||
def run(name, **options)
|
||||
params = options.dup
|
||||
params[:image] ||= name
|
||||
params[:daemonize] = true if !params.has_key?(:daemonize)
|
||||
|
||||
# TODO: Validate provided parameters before assignment
|
||||
@containers[name.to_s] = params
|
||||
end
|
||||
|
||||
def finalize!
|
||||
@version = "latest" if @version == UNSET_VALUE
|
||||
@version = @version.to_sym
|
||||
@__containers[name.to_s] = options.dup
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
def finalize!
|
||||
@version = "latest" if @version == UNSET_VALUE
|
||||
@version = @version.to_sym
|
||||
|
||||
@__containers.each do |name, params|
|
||||
params[:image] ||= name
|
||||
params[:daemonize] = true if !params.has_key?(:daemonize)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,6 +31,56 @@ describe VagrantPlugins::Docker::Config do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#merge" do
|
||||
it "has all images to pull" do
|
||||
subject.pull_images("1")
|
||||
|
||||
other = described_class.new
|
||||
other.pull_images("2", "3")
|
||||
|
||||
result = subject.merge(other)
|
||||
expect(result.images.to_a.sort).to eq(
|
||||
["1", "2", "3"])
|
||||
end
|
||||
|
||||
it "has all the containers to run" do
|
||||
subject.run("foo", image: "bar", daemonize: false)
|
||||
subject.run("bar")
|
||||
|
||||
other = described_class.new
|
||||
other.run("foo", image: "foo")
|
||||
|
||||
result = subject.merge(other)
|
||||
result.finalize!
|
||||
|
||||
cs = result.containers
|
||||
expect(cs.length).to eq(2)
|
||||
expect(cs["foo"]).to eq({
|
||||
image: "foo",
|
||||
daemonize: false,
|
||||
})
|
||||
expect(cs["bar"]).to eq({
|
||||
image: "bar",
|
||||
daemonize: true,
|
||||
})
|
||||
end
|
||||
|
||||
it "has all the containers to build" do
|
||||
subject.build_image("foo")
|
||||
|
||||
other = described_class.new
|
||||
other.build_image("bar")
|
||||
|
||||
result = subject.merge(other)
|
||||
result.finalize!
|
||||
|
||||
images = result.build_images
|
||||
expect(images.length).to eq(2)
|
||||
expect(images[0]).to eq(["foo", {}])
|
||||
expect(images[1]).to eq(["bar", {}])
|
||||
end
|
||||
end
|
||||
|
||||
describe "#pull_images" do
|
||||
it "adds images to the list of images to build" do
|
||||
subject.pull_images("1")
|
||||
|
|
Loading…
Reference in New Issue