Allow custom options per role

This commit is contained in:
David Heinemeier Hansson
2023-03-09 11:09:19 +01:00
parent 98a14f6173
commit d3f07d6313
5 changed files with 15 additions and 15 deletions

View File

@@ -10,9 +10,9 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
*role.env_args,
*config.volume_args,
*role.label_args,
*role.option_args,
config.absolute_image,
role.cmd,
*role.cmd_args
role.cmd
end
def start

View File

@@ -1,5 +1,5 @@
class Mrsk::Commands::Traefik < Mrsk::Commands::Base
delegate :argumentize_for_cmd, to: Mrsk::Utils
delegate :optionize, to: Mrsk::Utils
CONTAINER_PORT = 80
@@ -13,7 +13,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
"traefik",
"--providers.docker",
"--log.level=DEBUG",
*cmd_args
*cmd_option_args
end
def start
@@ -54,9 +54,9 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
end
private
def cmd_args
def cmd_option_args
if args = config.raw_config.dig(:traefik, "args")
argumentize_for_cmd args
optionize args
else
[]
end

View File

@@ -1,5 +1,5 @@
class Mrsk::Configuration::Role
delegate :argumentize, :argumentize_env_with_secrets, :argumentize_for_cmd, to: Mrsk::Utils
delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils
attr_accessor :name
@@ -35,9 +35,9 @@ class Mrsk::Configuration::Role
specializations["cmd"]
end
def cmd_args
if args = specializations["args"]
argumentize_for_cmd args
def option_args
if args = specializations["options"]
optionize args
else
[]
end

View File

@@ -23,8 +23,8 @@ module Mrsk::Utils
end
end
# Returns a list of shell-dashed arguments to be used to start a command.
def argumentize_for_cmd(args)
# Returns a list of shell-dashed option arguments.
def optionize(args)
args.collect { |(key, value)| [ "--#{key}", escape_shell_value(value) ] }.flatten
end

View File

@@ -34,12 +34,12 @@ class CommandsAppTest < ActiveSupport::TestCase
@app.run.join(" ")
end
test "run with cmd args" do
@config[:servers] = { "web" => [ "1.1.1.1" ], "jobs" => { "hosts" => [ "1.1.1.2" ], "cmd" => "bin/jobs", "args" => { "mount" => "somewhere" } } }
test "run with custom options" do
@config[:servers] = { "web" => [ "1.1.1.1" ], "jobs" => { "hosts" => [ "1.1.1.2" ], "cmd" => "bin/jobs", "options" => { "mount" => "somewhere" } } }
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config).tap { |c| c.version = "999" }
assert_equal \
"docker run --detach --restart unless-stopped --log-opt max-size=10m --name app-999 -e RAILS_MASTER_KEY=\"456\" --label service=\"app\" --label role=\"jobs\" dhh/app:999 bin/jobs --mount \"somewhere\"",
"docker run --detach --restart unless-stopped --log-opt max-size=10m --name app-999 -e RAILS_MASTER_KEY=\"456\" --label service=\"app\" --label role=\"jobs\" --mount \"somewhere\" dhh/app:999 bin/jobs",
@app.run(role: :jobs).join(" ")
end