Skip to content

Commit 25626ed

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 8789a1b commit 25626ed

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
@@ -30,6 +30,8 @@ def boot
3030
option :registry, type: :string, default: nil, desc: "Registry to use for the proxy image"
3131
option :repository, type: :string, default: nil, desc: "Repository for the proxy image"
3232
option :image_version, type: :string, default: nil, desc: "Version of the proxy to run"
33+
option :metrics_port, type: :numeric, default: nil, desc: "Port to report prometheus metrics on"
34+
option :debug, type: :boolean, default: false, desc: "Whether to run the proxy in debug mode"
3335
option :docker_options, type: :array, default: [], desc: "Docker options to pass to the proxy container", banner: "option=value option2=value2"
3436
def boot_config(subcommand)
3537
case subcommand
@@ -48,6 +50,9 @@ def boot_config(subcommand)
4850

4951
image_version = options[:image_version]
5052

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

@@ -78,6 +89,7 @@ def boot_config(subcommand)
7889
execute *KAMAL.proxy.reset_boot_options, raise_on_non_zero_exit: false
7990
execute *KAMAL.proxy.reset_image, raise_on_non_zero_exit: false
8091
execute *KAMAL.proxy.reset_image_version, raise_on_non_zero_exit: false
92+
execute *KAMAL.proxy.reset_run_command, raise_on_non_zero_exit: false
8193
end
8294
else
8395
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
@@ -66,7 +66,7 @@ def remove_proxy_directory
6666
end
6767

6868
def boot_config
69-
[ :echo, "#{substitute(read_boot_options)} #{substitute(read_image)}:#{substitute(read_image_version)}" ]
69+
[ :echo, "#{substitute(read_boot_options)} #{substitute(read_image)}:#{substitute(read_image_version)} #{substitute(read_run_command)}" ]
7070
end
7171

7272
def read_boot_options
@@ -81,6 +81,10 @@ def read_image_version
8181
read_file(config.proxy_image_version_file, default: Kamal::Configuration::PROXY_MINIMUM_VERSION)
8282
end
8383

84+
def read_run_command
85+
read_file(config.proxy_run_command_file)
86+
end
87+
8488
def reset_boot_options
8589
remove_file config.proxy_options_file
8690
end
@@ -93,6 +97,10 @@ def reset_image_version
9397
remove_file config.proxy_image_version_file
9498
end
9599

100+
def reset_run_command
101+
remove_file config.proxy_run_command_file
102+
end
103+
96104
private
97105
def container_name
98106
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
@@ -4,7 +4,7 @@ class CliProxyTest < CliTestCase
44
test "boot" do
55
run_command("boot").tap do |output|
66
assert_match "docker login", output
7-
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
7+
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
88
end
99
end
1010

@@ -18,7 +18,7 @@ class CliProxyTest < CliTestCase
1818
exception = assert_raises do
1919
run_command("boot").tap do |output|
2020
assert_match "docker login", output
21-
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
21+
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
2222
end
2323
end
2424

@@ -36,7 +36,7 @@ class CliProxyTest < CliTestCase
3636

3737
run_command("boot").tap do |output|
3838
assert_match "docker login", output
39-
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
39+
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
4040
end
4141
ensure
4242
Thread.report_on_exception = false
@@ -56,12 +56,12 @@ class CliProxyTest < CliTestCase
5656
run_command("reboot", "-y").tap do |output|
5757
assert_match "docker container stop kamal-proxy on 1.1.1.1", output
5858
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy on 1.1.1.1", output
59-
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
59+
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
6060
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
6161

6262
assert_match "docker container stop kamal-proxy on 1.1.1.2", output
6363
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy on 1.1.1.2", output
64-
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
64+
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
6565
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
6666
end
6767
end
@@ -196,7 +196,7 @@ class CliProxyTest < CliTestCase
196196
assert_match "/usr/bin/env mkdir -p .kamal", output
197197
assert_match "docker network create kamal", output
198198
assert_match "docker login -u [REDACTED] -p [REDACTED]", output
199-
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
199+
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
200200
assert_match "/usr/bin/env mkdir -p .kamal", output
201201
assert_match %r{docker rename app-web-latest app-web-latest_replaced_.*}, output
202202
assert_match "/usr/bin/env mkdir -p .kamal/apps/app/env/roles", output
@@ -240,7 +240,9 @@ class CliProxyTest < CliTestCase
240240
assert_match "Running /usr/bin/env mkdir -p .kamal/proxy on #{host}", output
241241
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
242242
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
243-
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output end
243+
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
244+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
245+
end
244246
end
245247
end
246248

@@ -251,6 +253,7 @@ class CliProxyTest < CliTestCase
251253
assert_match "Uploading \"--log-opt max-size=10m\" to .kamal/proxy/options on #{host}", output
252254
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
253255
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
256+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
254257
end
255258
end
256259
end
@@ -262,6 +265,7 @@ class CliProxyTest < CliTestCase
262265
assert_match "Uploading \"--publish 80:80 --publish 443:443 --log-opt max-size=100m\" to .kamal/proxy/options on #{host}", output
263266
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
264267
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
268+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
265269
end
266270
end
267271
end
@@ -273,6 +277,7 @@ class CliProxyTest < CliTestCase
273277
assert_match "Uploading \"--publish 80:80 --publish 443:443\" to .kamal/proxy/options on #{host}", output
274278
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
275279
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
280+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
276281
end
277282
end
278283
end
@@ -319,6 +324,7 @@ class CliProxyTest < CliTestCase
319324
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
320325
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
321326
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
327+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
322328
end
323329
end
324330
end
@@ -330,6 +336,7 @@ class CliProxyTest < CliTestCase
330336
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
331337
assert_match "Uploading \"myreg/basecamp/kamal-proxy\" to .kamal/proxy/image on #{host}", output
332338
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
339+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
333340
end
334341
end
335342
end
@@ -341,6 +348,7 @@ class CliProxyTest < CliTestCase
341348
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
342349
assert_match "Uploading \"myrepo/kamal-proxy\" to .kamal/proxy/image on #{host}", output
343350
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
351+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
344352
end
345353
end
346354
end
@@ -352,23 +360,37 @@ class CliProxyTest < CliTestCase
352360
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
353361
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
354362
assert_match "Uploading \"0.9.9\" to .kamal/proxy/image_version on #{host}", output
363+
assert_match "Running /usr/bin/env rm .kamal/proxy/run_command on #{host}", output
364+
end
365+
end
366+
end
367+
368+
test "boot_config set run_command" do
369+
run_command("boot_config", "set", "--metrics_port", "9000", "--debug", "true").tap do |output|
370+
%w[ 1.1.1.1 1.1.1.2 ].each do |host|
371+
assert_match "Running /usr/bin/env mkdir -p .kamal/proxy on #{host}", output
372+
assert_match "Running /usr/bin/env rm .kamal/proxy/options on #{host}", output
373+
assert_match "Running /usr/bin/env rm .kamal/proxy/image on #{host}", output
374+
assert_match "Running /usr/bin/env rm .kamal/proxy/image_version on #{host}", output
375+
assert_match "Uploading \"kamal-proxy run --debug --metrics-port \\\"9000\\\"\" to .kamal/proxy/run_command on #{host}", output
355376
end
356377
end
357378
end
358379

359380
test "boot_config set all" do
360-
run_command("boot_config", "set", "--docker_options", "label=foo=bar", "--registry", "myreg", "--repository", "myrepo", "--image_version", "0.9.9").tap do |output|
381+
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|
361382
%w[ 1.1.1.1 1.1.1.2 ].each do |host|
362383
assert_match "Uploading \"--publish 80:80 --publish 443:443 --log-opt max-size=10m --label=foo=bar\" to .kamal/proxy/options on #{host}", output
363384
assert_match "Uploading \"myreg/myrepo/kamal-proxy\" to .kamal/proxy/image on #{host}", output
364385
assert_match "Uploading \"0.9.9\" to .kamal/proxy/image_version on #{host}", output
386+
assert_match "Uploading \"kamal-proxy run --debug --metrics-port \\\"9000\\\"\" to .kamal/proxy/run_command on #{host}", output
365387
end
366388
end
367389
end
368390

369391
test "boot_config get" do
370392
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
371-
.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\")")
393+
.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 \"\")")
372394
.returns("--publish 80:80 --publish 8443:443 --label=foo=bar basecamp/kamal-proxy:v1.0.0")
373395
.twice
374396

0 commit comments

Comments
 (0)