Allow arbitrary docker options for traefik

This commit is contained in:
Stephen van Beek
2023-03-15 15:37:10 +00:00
parent 9843c5e1ce
commit 4c542930c5
4 changed files with 33 additions and 46 deletions

View File

@@ -8,8 +8,9 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
"--detach",
"--restart", "unless-stopped",
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
*published_ports,
*volumes,
"--publish", port,
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
*docker_option_args,
"traefik",
"--providers.docker",
"--log.level=DEBUG",
@@ -54,19 +55,11 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
end
private
def published_ports
if ports = config.raw_config.dig(:traefik, "options", "publish")
ports.collect { |value| "--publish #{value}:#{value}" }.compact
def docker_option_args
if args = config.raw_config.dig(:traefik, "options")
optionize args
else
["--publish #{port}"]
end
end
def volumes
if volumes = config.raw_config.dig(:traefik, "options", "volumes")
volumes.collect { |value| "--volume #{value}" }.compact
else
["--volume /var/run/docker.sock:/var/run/docker.sock"]
[]
end
end

View File

@@ -25,15 +25,21 @@ module Mrsk::Utils
# Returns a list of shell-dashed option arguments. If the value is true, it's treated like a value-less option.
def optionize(args, with: nil)
flattened_args = flatten_args args
options = if with
args.collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" }
flattened_args.collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" }
else
args.collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] }
flattened_args.collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] }
end
options.flatten.compact
end
# Flattens a one-to-many structure into an array of two-element arrays each containing a key-value pair
def flatten_args(args)
args.flat_map { |key, value| value.respond_to?('map') ? value.map { |entry| [key, entry] }: [[key, value]] }
end
# Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes
def redact(arg) # Used in execute_command to hide redact() args a user passes in
arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc