feat: Add support for specifying custom Docker image tag in CLI options

This commit is contained in:
Glenn Y. Rolland 2024-11-18 21:49:02 +01:00
parent f55c06c05e
commit c0ae494c57
3 changed files with 42 additions and 16 deletions

View file

@ -40,6 +40,10 @@ module DocMachine::Build
config.enable_tty = true config.enable_tty = true
end end
opts.on("-i", "--image", "Use specific image:tag (default glenux/docmachine:latest)") do |image_tag|
config.image_tag = image_tag
end
commands << ->() : Nil do commands << ->() : Nil do
app = DocMachine::Build::Run.new(config) app = DocMachine::Build::Run.new(config)
app.prepare app.prepare

View file

@ -7,6 +7,7 @@ module DocMachine::Build
property port : Int32 = 5100 property port : Int32 = 5100
property enable_multiple : Bool = false property enable_multiple : Bool = false
property enable_cache : Bool = false property enable_cache : Bool = false
property image_tag : String = "glenux/docmachine:latest"
def initialize(@parent : DocMachine::Config) def initialize(@parent : DocMachine::Config)
end end

View file

@ -15,7 +15,6 @@ module DocMachine::Build
data = "#{@config.data_dir}:#{@config.port}" data = "#{@config.data_dir}:#{@config.port}"
@basehash = Digest::SHA256.hexdigest(data)[0..6] @basehash = Digest::SHA256.hexdigest(data)[0..6]
@docker_name = "docmachine-#{@basehash}" @docker_name = "docmachine-#{@basehash}"
@docker_image = "glenux/docmachine:latest"
@docker_opts = [] of String @docker_opts = [] of String
@process = nil @process = nil
end end
@ -25,7 +24,7 @@ module DocMachine::Build
# setup permissions # setup permissions
def prepare() def prepare()
Log.info { "basedir = #{@config.data_dir}" } Log.info { "basedir = #{@config.data_dir}" }
Log.info { "docker_image = #{@docker_image}" } Log.info { "docker_image = #{@config.image_tag}" }
Log.info { "action = #{@config.action}" } Log.info { "action = #{@config.action}" }
self._pull_image() self._pull_image()
@ -56,13 +55,13 @@ module DocMachine::Build
data_cache_file = data_cache_dir / "image.tar" data_cache_file = data_cache_dir / "image.tar"
Log.info { "Checking cache #{data_cache_file}..." } Log.info { "Checking cache #{data_cache_file}..." }
if ! File.exists? data_cache_file.to_s if ! File.exists? data_cache_file.to_s
Log.info { "Downloading #{@docker_image} image..." } Log.info { "Downloading #{@config.image_tag} image..." }
Process.run("docker", ["pull", @docker_image], output: STDOUT) Process.run("docker", ["pull", @config.image_tag], output: STDOUT)
Log.info { "Building cache for image (#{data_cache_dir})" } Log.info { "Building cache for image (#{data_cache_dir})" }
FileUtils.mkdir_p(data_cache_dir) FileUtils.mkdir_p(data_cache_dir)
status = Process.run( status = Process.run(
"docker", "docker",
["image", "save", @docker_image, "-o", data_cache_file.to_s], ["image", "save", @config.image_tag, "-o", data_cache_file.to_s],
output: STDOUT output: STDOUT
) )
if status.success? if status.success?
@ -77,7 +76,7 @@ module DocMachine::Build
end end
if @config.enable_cache if @config.enable_cache
Log.info { "Loading #{@docker_image} image from cache..." } Log.info { "Loading #{@config.image_tag} image from cache..." }
docker_image_loaded = false docker_image_loaded = false
status = Process.run( status = Process.run(
"docker", "docker",
@ -91,16 +90,34 @@ module DocMachine::Build
exit 1 exit 1
end end
else else
Log.info { "Loading #{@docker_image} image from local registry..." } Log.info { "Loading #{@config.image_tag} image from local registry..." }
# FIXME: check that local image exists # FIXME: check that local image exists
end end
end end
def start() def start()
uid = %x{id -u}.strip # start with default uid/gid
gid = %x{id -g}.strip ext_uid = %x{id -u}.strip.to_i
Log.info { "uid: #{uid}" } ext_gid = %x{id -g}.strip.to_i
Log.info { "cid: #{gid}" }
# ...but use subuid/subgid if available
File.each_line("/etc/subuid") do |line|
split = line.split(":")
next if split[0] != %x{id -u -n}
subuid = split[1].to_i
ext_uid += subuid - 1
end
File.each_line("/etc/subgid") do |line|
split = line.split(":")
next if split[0] != %x{id -g -n}
subgid = split[1].to_i
ext_gid += subgid - 1
end
Log.info { "ext uid: #{ext_uid}" }
Log.info { "ext cid: #{ext_gid}" }
docker_opts = [] of String docker_opts = [] of String
docker_opts << "run" docker_opts << "run"
@ -111,8 +128,9 @@ module DocMachine::Build
docker_opts.concat ["--name", @docker_name] docker_opts.concat ["--name", @docker_name]
docker_opts << "--rm" docker_opts << "--rm"
docker_opts << "--shm-size=1gb" docker_opts << "--shm-size=1gb"
docker_opts.concat ["-e", "EXT_UID=#{uid}"] # docker_opts << "--privileged"
docker_opts.concat ["-e", "EXT_GID=#{gid}"] docker_opts.concat ["-e", "EXT_UID=#{ext_uid}"]
docker_opts.concat ["-e", "EXT_GID=#{ext_gid}"]
docker_opts.concat ["-v", "#{@config.data_dir}/docs:/app/docs"] docker_opts.concat ["-v", "#{@config.data_dir}/docs:/app/docs"]
docker_opts.concat ["-v", "#{@config.data_dir}/slides:/app/slides"] docker_opts.concat ["-v", "#{@config.data_dir}/slides:/app/slides"]
docker_opts.concat ["-v", "#{@config.data_dir}/images:/app/images"] docker_opts.concat ["-v", "#{@config.data_dir}/images:/app/images"]
@ -166,11 +184,14 @@ module DocMachine::Build
Log.info { "Slides: no slides directory detected." } Log.info { "Slides: no slides directory detected." }
end end
docker_opts << @docker_image docker_opts << @config.image_tag
docker_opts << @config.action docker_opts << @config.action
Log.info { docker_opts.inspect.colorize(:yellow) } Log.info {
@process = Process.new("docker", docker_opts, output: STDOUT, error: STDERR) docker_str = ["docker"].concat(docker_opts).join(" ").colorize(:yellow)
"Docker: #{docker_str.to_s}"
}
@process = Process.new("docker", docker_opts, input: STDIN, output: STDOUT, error: STDERR)
end end
def wait() def wait()