Skip to content

Commit 9a2895d

Browse files
committed
Allow kamal-proxy run command options to be set
Allow --metrics_port and --debug options to be set via the boot config. --metrics_port support will come in kamal-proxy v0.8.8, so this option doesn't work right now. This will be updated before the next Kamal release though and we can add integration tests for the metrics at that point.
1 parent eb915f8 commit 9a2895d

File tree

6 files changed

+74
-12
lines changed

6 files changed

+74
-12
lines changed

lib/kamal/cli/proxy.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def boot
3131
option :registry, type: :string, default: nil, desc: "Registry to use for the proxy image"
3232
option :repository, type: :string, default: nil, desc: "Repository for the proxy image"
3333
option :image_version, type: :string, default: nil, desc: "Version of the proxy to run"
34+
option :metrics_port, type: :numeric, default: nil, desc: "Port to report prometheus metrics on"
35+
option :debug, type: :boolean, default: false, desc: "Whether to run the proxy in debug mode"
3436
option :docker_options, type: :array, default: [], desc: "Docker options to pass to the proxy container", banner: "option=value option2=value2"
3537
def boot_config(subcommand)
3638
case subcommand
@@ -49,6 +51,9 @@ def boot_config(subcommand)
4951

5052
image_version = options[:image_version]
5153

54+
run_command_options = { debug: options[:debug] || nil, "metrics-port": options[:metrics_port] }.compact
55+
run_command = "kamal-proxy run #{Kamal::Utils.optionize(run_command_options).join(" ")}" if run_command_options.any?
56+
5257
on(KAMAL.proxy_hosts) do |host|
5358
execute(*KAMAL.proxy.ensure_proxy_directory)
5459
if boot_options != KAMAL.config.proxy_default_boot_options
@@ -68,6 +73,12 @@ def boot_config(subcommand)
6873
else
6974
execute *KAMAL.proxy.reset_image_version, raise_on_non_zero_exit: false
7075
end
76+
77+
if run_command
78+
upload! StringIO.new(run_command), KAMAL.config.proxy_run_command_file
79+
else
80+
execute *KAMAL.proxy.reset_run_command, raise_on_non_zero_exit: false
81+
end
7182
end
7283
when "get"
7384

@@ -79,6 +90,7 @@ def boot_config(subcommand)
7990
execute *KAMAL.proxy.reset_boot_options, raise_on_non_zero_exit: false
8091
execute *KAMAL.proxy.reset_image, raise_on_non_zero_exit: false
8192
execute *KAMAL.proxy.reset_image_version, raise_on_non_zero_exit: false
93+
execute *KAMAL.proxy.reset_run_command, raise_on_non_zero_exit: false
8294
end
8395
else
8496
raise ArgumentError, "Unknown boot_config subcommand #{subcommand}"

lib/kamal/commands/proxy.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def ensure_apps_config_directory
7070
end
7171

7272
def boot_config
73-
[ :echo, "#{substitute(read_boot_options)} #{substitute(read_image)}:#{substitute(read_image_version)}" ]
73+
[ :echo, "#{substitute(read_boot_options)} #{substitute(read_image)}:#{substitute(read_image_version)} #{substitute(read_run_command)}" ]
7474
end
7575

7676
def read_boot_options
@@ -85,6 +85,10 @@ def read_image_version
8585
read_file(config.proxy_image_version_file, default: Kamal::Configuration::PROXY_MINIMUM_VERSION)
8686
end
8787

88+
def read_run_command
89+
read_file(config.proxy_run_command_file)
90+
end
91+
8892
def reset_boot_options
8993
remove_file config.proxy_options_file
9094
end
@@ -97,6 +101,10 @@ def reset_image_version
97101
remove_file config.proxy_image_version_file
98102
end
99103

104+
def reset_run_command
105+
remove_file config.proxy_run_command_file
106+
end
107+
100108
private
101109
def container_name
102110
config.proxy_container_name

lib/kamal/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ def proxy_image_version_file
316316
File.join proxy_directory, "image_version"
317317
end
318318

319+
def proxy_run_command_file
320+
File.join proxy_directory, "run_command"
321+
end
322+
319323
def proxy_apps_directory
320324
File.join proxy_directory, "apps-config"
321325
end

test/cli/proxy_test.rb

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class CliProxyTest < CliTestCase
55
run_command("boot").tap do |output|
66
assert_match "docker login", output
77
assert_match "mkdir -p .kamal/proxy/apps-config", output
8-
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
8+
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") $(cat .kamal/proxy/run_command 2> /dev/null || echo \"\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
99
end
1010
end
1111

@@ -19,7 +19,7 @@ class CliProxyTest < CliTestCase
1919
exception = assert_raises do
2020
run_command("boot").tap do |output|
2121
assert_match "docker login", output
22-
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
22+
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") $(cat .kamal/proxy/run_command 2> /dev/null || echo \"\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
2323
end
2424
end
2525

@@ -37,7 +37,7 @@ class CliProxyTest < CliTestCase
3737

3838
run_command("boot").tap do |output|
3939
assert_match "docker login", output
40-
assert_match "docker container start kamal-proxy || echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
40+
assert_match "docker container start kamal-proxy || echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") $(cat .kamal/proxy/run_command 2> /dev/null || echo \"\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
4141
end
4242
ensure
4343
Thread.report_on_exception = false
@@ -58,13 +58,13 @@ class CliProxyTest < CliTestCase
5858
assert_match "docker container stop kamal-proxy on 1.1.1.1", output
5959
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy on 1.1.1.1", output
6060
assert_match "mkdir -p .kamal/proxy/apps-config on 1.1.1.1", output
61-
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy --volume $(pwd)/.kamal/proxy/apps-config:/home/kamal-proxy/.apps-config on 1.1.1.1", output
61+
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") $(cat .kamal/proxy/run_command 2> /dev/null || echo \"\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy --volume $(pwd)/.kamal/proxy/apps-config:/home/kamal-proxy/.apps-config on 1.1.1.1", output
6262
assert_match "docker exec kamal-proxy kamal-proxy deploy app-web --target=\"abcdefabcdef:80\" --deploy-timeout=\"6s\" --drain-timeout=\"30s\" --buffer-requests --buffer-responses --log-request-header=\"Cache-Control\" --log-request-header=\"Last-Modified\" --log-request-header=\"User-Agent\" on 1.1.1.1", output
6363

6464
assert_match "docker container stop kamal-proxy on 1.1.1.2", output
6565
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy on 1.1.1.2", output
6666
assert_match "mkdir -p .kamal/proxy/apps-config on 1.1.1.1", output
67-
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy --volume $(pwd)/.kamal/proxy/apps-config:/home/kamal-proxy/.apps-config on 1.1.1.2", output
67+
assert_match "echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") $(cat .kamal/proxy/run_command 2> /dev/null || echo \"\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy --volume $(pwd)/.kamal/proxy/apps-config:/home/kamal-proxy/.apps-config on 1.1.1.2", output
6868
assert_match "docker exec kamal-proxy kamal-proxy deploy app-web --target=\"abcdefabcdef:80\" --deploy-timeout=\"6s\" --drain-timeout=\"30s\" --buffer-requests --buffer-responses --log-request-header=\"Cache-Control\" --log-request-header=\"Last-Modified\" --log-request-header=\"User-Agent\" on 1.1.1.2", output
6969
end
7070
end
@@ -199,7 +199,7 @@ class CliProxyTest < CliTestCase
199199
assert_match "/usr/bin/env mkdir -p .kamal", output
200200
assert_match "docker network create kamal", output
201201
assert_match "docker login -u [REDACTED] -p [REDACTED]", output
202-
assert_match "docker container start kamal-proxy || echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
202+
assert_match "docker container start kamal-proxy || echo $(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") $(cat .kamal/proxy/run_command 2> /dev/null || echo \"\") | xargs docker run --name kamal-proxy --network kamal --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy", output
203203
assert_match "/usr/bin/env mkdir -p .kamal", output
204204
assert_match %r{docker rename app-web-latest app-web-latest_replaced_.*}, output
205205
assert_match "/usr/bin/env mkdir -p .kamal/apps/app/env/roles", output
@@ -243,7 +243,9 @@ class CliProxyTest < CliTestCase
243243
assert_match "Running /usr/bin/env mkdir -p .kamal/proxy on #{host}", output
244244
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
245245
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
246-
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output end
246+
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
247+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
248+
end
247249
end
248250
end
249251

@@ -254,6 +256,7 @@ class CliProxyTest < CliTestCase
254256
assert_match "Uploading \"--log-opt max-size=10m\" to .kamal/proxy/options on #{host}", output
255257
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
256258
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
259+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
257260
end
258261
end
259262
end
@@ -265,6 +268,7 @@ class CliProxyTest < CliTestCase
265268
assert_match "Uploading \"--publish 80:80 --publish 443:443 --log-opt max-size=100m\" to .kamal/proxy/options on #{host}", output
266269
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
267270
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
271+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
268272
end
269273
end
270274
end
@@ -276,6 +280,7 @@ class CliProxyTest < CliTestCase
276280
assert_match "Uploading \"--publish 80:80 --publish 443:443\" to .kamal/proxy/options on #{host}", output
277281
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
278282
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
283+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
279284
end
280285
end
281286
end
@@ -322,6 +327,7 @@ class CliProxyTest < CliTestCase
322327
assert_match "Uploading \"--publish 80:80 --publish 443:443 --log-opt max-size=10m --label=foo=bar --add_host=thishost:thathost\" to .kamal/proxy/options on #{host}", output
323328
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
324329
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
330+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
325331
end
326332
end
327333
end
@@ -333,6 +339,7 @@ class CliProxyTest < CliTestCase
333339
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
334340
assert_match "Uploading \"myreg/basecamp/kamal-proxy\" to .kamal/proxy/image on #{host}", output
335341
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
342+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
336343
end
337344
end
338345
end
@@ -344,6 +351,7 @@ class CliProxyTest < CliTestCase
344351
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
345352
assert_match "Uploading \"myrepo/kamal-proxy\" to .kamal/proxy/image on #{host}", output
346353
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
354+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
347355
end
348356
end
349357
end
@@ -355,23 +363,37 @@ class CliProxyTest < CliTestCase
355363
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
356364
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
357365
assert_match "Uploading \"0.9.9\" to .kamal/proxy/image_version on #{host}", output
366+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
367+
end
368+
end
369+
end
370+
371+
test "boot_config set run_command" do
372+
run_command("boot_config", "set", "--metrics_port", "9000", "--debug", "true").tap do |output|
373+
%w[ 1.1.1.1 1.1.1.2 ].each do |host|
374+
assert_match "Running /usr/bin/env mkdir -p .kamal/proxy on #{host}", output
375+
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
376+
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
377+
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
378+
assert_match "Uploading \"kamal-proxy run --debug --metrics-port \\\"9000\\\"\" to .kamal/proxy/run_command on #{host}", output
358379
end
359380
end
360381
end
361382

362383
test "boot_config set all" do
363-
run_command("boot_config", "set", "--docker_options", "label=foo=bar", "--registry", "myreg", "--repository", "myrepo", "--image_version", "0.9.9").tap do |output|
384+
run_command("boot_config", "set", "--docker_options", "label=foo=bar", "--registry", "myreg", "--repository", "myrepo", "--image_version", "0.9.9", "--metrics_port", "9000", "--debug", "true").tap do |output|
364385
%w[ 1.1.1.1 1.1.1.2 ].each do |host|
365386
assert_match "Uploading \"--publish 80:80 --publish 443:443 --log-opt max-size=10m --label=foo=bar\" to .kamal/proxy/options on #{host}", output
366387
assert_match "Uploading \"myreg/myrepo/kamal-proxy\" to .kamal/proxy/image on #{host}", output
367388
assert_match "Uploading \"0.9.9\" to .kamal/proxy/image_version on #{host}", output
389+
assert_match "Uploading \"kamal-proxy run --debug --metrics-port \\\"9000\\\"\" to .kamal/proxy/run_command on #{host}", output
368390
end
369391
end
370392
end
371393

372394
test "boot_config get" do
373395
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
374-
.with(:echo, "$(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"v0.8.7\")")
396+
.with(:echo, "$(cat .kamal/proxy/options 2> /dev/null || echo \"--publish 80:80 --publish 443:443 --log-opt max-size=10m\") $(cat .kamal/proxy/image 2> /dev/null || echo \"basecamp/kamal-proxy\"):$(cat .kamal/proxy/image_version 2> /dev/null || echo \"#{Kamal::Configuration::PROXY_MINIMUM_VERSION}\") $(cat .kamal/proxy/run_command 2> /dev/null || echo \"\")")
375397
.returns("--publish 80:80 --publish 8443:443 --label=foo=bar basecamp/kamal-proxy:v1.0.0")
376398
.twice
377399

0 commit comments

Comments
 (0)