Skip to content

Commit 8425e72

Browse files
committed
support custom proxy image
- Allow users to specify custom proxy image repository instead of using default basecamp/kamal-proxy - Help users in regions where Docker Hub access is restricted (e.g. China) to pull proxy image from cloud vendor repositories - Move proxy related configurations from configuration to proxy
1 parent 9cf8da6 commit 8425e72

File tree

9 files changed

+78
-84
lines changed

9 files changed

+78
-84
lines changed

lib/kamal/cli/proxy.rb

+11-9
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,34 @@ def boot
1313

1414
version = capture_with_info(*KAMAL.proxy.version).strip.presence
1515

16-
if version && Kamal::Utils.older_version?(version, Kamal::Configuration::PROXY_MINIMUM_VERSION)
17-
raise "kamal-proxy version #{version} is too old, run `kamal proxy reboot` in order to update to at least #{Kamal::Configuration::PROXY_MINIMUM_VERSION}"
16+
if version && Kamal::Utils.older_version?(version, Kamal::Configuration::Proxy::MINIMUM_VERSION)
17+
raise "kamal-proxy version #{version} is too old, run `kamal proxy reboot` in order to update to at least #{Kamal::Configuration::Proxy::MINIMUM_VERSION}"
1818
end
1919
execute *KAMAL.proxy.start_or_run
2020
end
2121
end
2222
end
2323

2424
desc "boot_config <set|get|reset>", "Manage kamal-proxy boot configuration"
25+
option :image, type: :string, default: Kamal::Configuration::Proxy::DEFAULT_IMAGE, desc: "Name of the image"
2526
option :publish, type: :boolean, default: true, desc: "Publish the proxy ports on the host"
26-
option :http_port, type: :numeric, default: Kamal::Configuration::PROXY_HTTP_PORT, desc: "HTTP port to publish on the host"
27-
option :https_port, type: :numeric, default: Kamal::Configuration::PROXY_HTTPS_PORT, desc: "HTTPS port to publish on the host"
28-
option :log_max_size, type: :string, default: Kamal::Configuration::PROXY_LOG_MAX_SIZE, desc: "Max size of proxy logs"
27+
option :http_port, type: :numeric, default: Kamal::Configuration::Proxy::HTTP_PORT, desc: "HTTP port to publish on the host"
28+
option :https_port, type: :numeric, default: Kamal::Configuration::Proxy::HTTPS_PORT, desc: "HTTPS port to publish on the host"
29+
option :log_max_size, type: :string, default: Kamal::Configuration::Proxy::LOG_MAX_SIZE, desc: "Max size of proxy logs"
2930
option :docker_options, type: :array, default: [], desc: "Docker options to pass to the proxy container", banner: "option=value option2=value2"
3031
def boot_config(subcommand)
3132
case subcommand
3233
when "set"
3334
boot_options = [
34-
*(KAMAL.config.proxy_publish_args(options[:http_port], options[:https_port]) if options[:publish]),
35-
*(KAMAL.config.proxy_logging_args(options[:log_max_size])),
36-
*options[:docker_options].map { |option| "--#{option}" }
35+
*(KAMAL.config.proxy.publish_args(options[:http_port], options[:https_port]) if options[:publish]),
36+
*(KAMAL.config.proxy.logging_args(options[:log_max_size])),
37+
*options[:docker_options].map { |option| "--#{option}" },
38+
options[:image]
3739
]
3840

3941
on(KAMAL.proxy_hosts) do |host|
4042
execute(*KAMAL.proxy.ensure_proxy_directory)
41-
upload! StringIO.new(boot_options.join(" ")), KAMAL.config.proxy_options_file
43+
upload! StringIO.new(boot_options.join(" ")), KAMAL.config.proxy.options_file
4244
end
4345
when "get"
4446
on(KAMAL.proxy_hosts) do |host|

lib/kamal/commands/app/proxy.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
module Kamal::Commands::App::Proxy
2-
delegate :proxy_container_name, to: :config
3-
42
def deploy(target:)
53
proxy_exec :deploy, role.container_prefix, *role.proxy.deploy_command_args(target: target)
64
end
@@ -11,6 +9,6 @@ def remove
119

1210
private
1311
def proxy_exec(*command)
14-
docker :exec, proxy_container_name, "kamal-proxy", *command
12+
docker :exec, config.proxy.container_name, "kamal-proxy", *command
1513
end
1614
end

lib/kamal/commands/proxy.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ def run
88
"--detach",
99
"--restart", "unless-stopped",
1010
"--volume", "kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy",
11-
"\$\(#{get_boot_options.join(" ")}\)",
12-
config.proxy_image
11+
"\$\(#{get_boot_options.join(" ")}\)"
1312
end
1413

1514
def start
@@ -65,23 +64,23 @@ def cleanup_traefik
6564
end
6665

6766
def ensure_proxy_directory
68-
make_directory config.proxy_directory
67+
make_directory config.proxy.directory
6968
end
7069

7170
def remove_proxy_directory
72-
remove_directory config.proxy_directory
71+
remove_directory config.proxy.directory
7372
end
7473

7574
def get_boot_options
76-
combine [ :cat, config.proxy_options_file ], [ :echo, "\"#{config.proxy_options_default.join(" ")}\"" ], by: "||"
75+
combine [ :cat, config.proxy.options_file ], [ :echo, "\"#{config.proxy.options_default.join(" ")}\"" ], by: "||"
7776
end
7877

7978
def reset_boot_options
80-
remove_file config.proxy_options_file
79+
remove_file config.proxy.options_file
8180
end
8281

8382
private
8483
def container_name
85-
config.proxy_container_name
84+
config.proxy.container_name
8685
end
8786
end

lib/kamal/configuration.rb

-42
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ class Kamal::Configuration
1414

1515
include Validation
1616

17-
PROXY_MINIMUM_VERSION = "v0.8.2"
18-
PROXY_HTTP_PORT = 80
19-
PROXY_HTTPS_PORT = 443
20-
PROXY_LOG_MAX_SIZE = "10m"
21-
2217
class << self
2318
def create_from(config_file:, destination: nil, version: nil)
2419
ENV["KAMAL_DESTINATION"] = destination
@@ -82,7 +77,6 @@ def initialize(raw_config, destination: nil, version: nil, validate: true)
8277
ensure_unique_hosts_for_ssl_roles
8378
end
8479

85-
8680
def version=(version)
8781
@declared_version = version
8882
end
@@ -106,7 +100,6 @@ def minimum_version
106100
raw_config.minimum_version
107101
end
108102

109-
110103
def roles
111104
servers.roles
112105
end
@@ -119,7 +112,6 @@ def accessory(name)
119112
accessories.detect { |a| a.name == name.to_s }
120113
end
121114

122-
123115
def all_hosts
124116
(roles + accessories).flat_map(&:hosts).uniq
125117
end
@@ -180,7 +172,6 @@ def retain_containers
180172
raw_config.retain_containers || 5
181173
end
182174

183-
184175
def volume_args
185176
if raw_config.volumes.present?
186177
argumentize "--volume", raw_config.volumes
@@ -193,7 +184,6 @@ def logging_args
193184
logging.args
194185
end
195186

196-
197187
def readiness_delay
198188
raw_config.readiness_delay || 7
199189
end
@@ -206,7 +196,6 @@ def drain_timeout
206196
raw_config.drain_timeout || 30
207197
end
208198

209-
210199
def run_directory
211200
".kamal"
212201
end
@@ -227,7 +216,6 @@ def assets_directory
227216
File.join app_directory, "assets"
228217
end
229218

230-
231219
def hooks_path
232220
raw_config.hooks_path || ".kamal/hooks"
233221
end
@@ -236,7 +224,6 @@ def asset_path
236224
raw_config.asset_path
237225
end
238226

239-
240227
def env_tags
241228
@env_tags ||= if (tags = raw_config.env["tags"])
242229
tags.collect { |name, config| Env::Tag.new(name, config: config, secrets: secrets) }
@@ -249,35 +236,6 @@ def env_tag(name)
249236
env_tags.detect { |t| t.name == name.to_s }
250237
end
251238

252-
def proxy_publish_args(http_port, https_port)
253-
argumentize "--publish", [ "#{http_port}:#{PROXY_HTTP_PORT}", "#{https_port}:#{PROXY_HTTPS_PORT}" ]
254-
end
255-
256-
def proxy_logging_args(max_size)
257-
argumentize "--log-opt", "max-size=#{max_size}" if max_size.present?
258-
end
259-
260-
def proxy_options_default
261-
[ *proxy_publish_args(PROXY_HTTP_PORT, PROXY_HTTPS_PORT), *proxy_logging_args(PROXY_LOG_MAX_SIZE) ]
262-
end
263-
264-
def proxy_image
265-
"basecamp/kamal-proxy:#{PROXY_MINIMUM_VERSION}"
266-
end
267-
268-
def proxy_container_name
269-
"kamal-proxy"
270-
end
271-
272-
def proxy_directory
273-
File.join run_directory, "proxy"
274-
end
275-
276-
def proxy_options_file
277-
File.join proxy_directory, "options"
278-
end
279-
280-
281239
def to_h
282240
{
283241
roles: role_names,

lib/kamal/configuration/docs/proxy.yml

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# It is disabled by default on all other roles but can be enabled by setting
1717
# `proxy: true` or providing a proxy configuration.
1818
proxy:
19-
2019
# Hosts
2120
#
2221
# The hosts that will be used to serve the app. The proxy will only route requests

lib/kamal/configuration/proxy.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
class Kamal::Configuration::Proxy
22
include Kamal::Configuration::Validation
33

4+
HTTP_PORT = 80
5+
HTTPS_PORT = 443
6+
LOG_MAX_SIZE = "10m"
7+
MINIMUM_VERSION = "v0.8.2"
48
DEFAULT_LOG_REQUEST_HEADERS = [ "Cache-Control", "Last-Modified", "User-Agent" ]
5-
CONTAINER_NAME = "kamal-proxy"
9+
DEFAULT_CONTAINER_NAME = "kamal-proxy"
10+
DEFAULT_IMAGE = "basecamp/kamal-proxy:#{MINIMUM_VERSION}"
611

712
delegate :argumentize, :optionize, to: Kamal::Utils
813

@@ -14,6 +19,10 @@ def initialize(config:, proxy_config:, context: "proxy")
1419
validate! @proxy_config, with: Kamal::Configuration::Validator::Proxy, context: context
1520
end
1621

22+
def container_name
23+
DEFAULT_CONTAINER_NAME
24+
end
25+
1726
def app_port
1827
proxy_config.fetch("app_port", 80)
1928
end
@@ -47,6 +56,26 @@ def deploy_options
4756
}.compact
4857
end
4958

59+
def directory
60+
File.join config.run_directory, "proxy"
61+
end
62+
63+
def options_file
64+
File.join directory, "options"
65+
end
66+
67+
def publish_args(http_port, https_port)
68+
argumentize "--publish", [ "#{http_port}:#{HTTP_PORT}", "#{https_port}:#{HTTPS_PORT}" ]
69+
end
70+
71+
def logging_args(max_size)
72+
argumentize "--log-opt", "max-size=#{max_size}" if max_size.present?
73+
end
74+
75+
def options_default
76+
[ *publish_args(HTTP_PORT, HTTPS_PORT), *logging_args(LOG_MAX_SIZE), DEFAULT_IMAGE ]
77+
end
78+
5079
def deploy_command_args(target:)
5180
optionize ({ target: "#{target}:#{app_port}" }).merge(deploy_options), with: "="
5281
end

0 commit comments

Comments
 (0)