2014-04-18 22:53:12 +00:00
|
|
|
require "log4r"
|
|
|
|
|
2014-10-23 22:02:46 +00:00
|
|
|
require "vagrant/util/ansi_escape_code_remover"
|
|
|
|
|
2014-04-18 22:53:12 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module DockerProvider
|
|
|
|
module Action
|
|
|
|
class Build
|
2014-10-23 22:02:46 +00:00
|
|
|
include Vagrant::Util::ANSIEscapeCodeRemover
|
|
|
|
|
2014-04-18 22:53:12 +00:00
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
@logger = Log4r::Logger.new("vagrant::docker::build")
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
machine = env[:machine]
|
|
|
|
build_dir = env[:build_dir]
|
|
|
|
build_dir ||= machine.provider_config.build_dir
|
2016-10-16 21:51:33 +00:00
|
|
|
git_repo = env[:git_repo]
|
|
|
|
git_repo ||= machine.provider_config.git_repo
|
|
|
|
|
2014-04-18 22:53:12 +00:00
|
|
|
# If we're not building a container, then just skip this step
|
2016-10-16 21:51:33 +00:00
|
|
|
return @app.call(env) if (!build_dir && !git_repo)
|
2014-04-18 22:53:12 +00:00
|
|
|
|
|
|
|
# Try to read the image ID from the cache file if we've
|
|
|
|
# already built it.
|
|
|
|
image_file = machine.data_dir.join("docker_build_image")
|
|
|
|
image = nil
|
|
|
|
if image_file.file?
|
|
|
|
image = image_file.read.chomp
|
|
|
|
end
|
|
|
|
|
2014-04-20 15:24:43 +00:00
|
|
|
# Verify the image exists if we have one
|
|
|
|
if image && !machine.provider.driver.image?(image)
|
|
|
|
machine.ui.output(I18n.t("docker_provider.build_image_invalid"))
|
|
|
|
image = nil
|
|
|
|
end
|
|
|
|
|
2014-04-18 22:53:12 +00:00
|
|
|
# If we have no image or we're rebuilding, we rebuild
|
|
|
|
if !image || env[:build_rebuild]
|
|
|
|
# Build it
|
2015-03-14 00:30:42 +00:00
|
|
|
args = machine.provider_config.build_args.clone
|
2015-07-08 22:07:57 +00:00
|
|
|
if machine.provider_config.dockerfile
|
2015-03-14 00:30:42 +00:00
|
|
|
dockerfile = machine.provider_config.dockerfile
|
|
|
|
dockerfile_path = File.join(build_dir, dockerfile)
|
|
|
|
|
2017-05-11 21:00:53 +00:00
|
|
|
args.push("--file").push(dockerfile_path)
|
2015-03-14 00:30:42 +00:00
|
|
|
machine.ui.output(
|
|
|
|
I18n.t("docker_provider.building_named_dockerfile",
|
|
|
|
file: machine.provider_config.dockerfile))
|
|
|
|
else
|
|
|
|
machine.ui.output(I18n.t("docker_provider.building"))
|
|
|
|
end
|
|
|
|
|
2014-05-08 01:41:24 +00:00
|
|
|
image = machine.provider.driver.build(
|
2016-10-16 21:51:33 +00:00
|
|
|
build_dir || git_repo,
|
2015-03-14 00:30:42 +00:00
|
|
|
extra_args: args) do |type, data|
|
2014-10-23 22:02:46 +00:00
|
|
|
data = remove_ansi_escape_codes(data.chomp).chomp
|
|
|
|
env[:ui].detail(data) if data != ""
|
|
|
|
end
|
|
|
|
|
|
|
|
# Output the final image
|
|
|
|
machine.ui.detail("\nImage: #{image}")
|
2014-04-18 22:53:12 +00:00
|
|
|
|
|
|
|
# Store the image ID
|
|
|
|
image_file.open("w") do |f|
|
|
|
|
f.binmode
|
|
|
|
f.write("#{image}\n")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
machine.ui.output(I18n.t("docker_provider.already_built"))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the image for creation
|
|
|
|
env[:create_image] = image
|
|
|
|
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|