Class: Kamal::Commands::Proxy
- Inherits:
-
Base
- Object
- Base
- Kamal::Commands::Proxy
show all
- Defined in:
- lib/kamal/commands/proxy.rb
Constant Summary
collapse
- CONFIG_DIGEST_LABEL =
"org.kamal.proxy-config-digest"
Constants inherited
from Base
Base::DOCKER_HEALTH_STATUS_FORMAT, Base::NO_HEALTHCHECK
Instance Attribute Summary collapse
Attributes inherited from Base
#config
Instance Method Summary
collapse
-
#boot_config ⇒ Object
-
#cache_purge(service, path_prefix: nil) ⇒ Object
-
#cache_stats(count: false, json: false) ⇒ Object
-
#cleanup_traefik ⇒ Object
-
#config_digest ⇒ Object
-
#container_id(only_running: false) ⇒ Object
-
#disable_restart ⇒ Object
Cancel the restart policy before draining: drain makes the proxy exit on its own, which - unlike docker stop - an active restart policy would undo.
-
#domains(subcommand) ⇒ Object
-
#drain(timeout: nil) ⇒ Object
-
#ensure_apps_config_directory ⇒ Object
-
#ensure_proxy_directory ⇒ Object
-
#follow_logs(host:, timestamps: true, grep: nil, grep_options: nil) ⇒ Object
-
#holder_container_id ⇒ Object
-
#info ⇒ Object
-
#initialize(config, host:) ⇒ Proxy
constructor
-
#list(name: container_name, json: false) ⇒ Object
-
#loadbalancer ⇒ Object
-
#logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) ⇒ Object
-
#mount_destinations ⇒ Object
One mount destination per line - what the running container was actually booted with, as opposed to what the current configuration would mount.
-
#next_container_name ⇒ Object
-
#port_holder? ⇒ Boolean
Zero-downtime handoff commands (proxy/run port_holder mode).
-
#promote_next_container ⇒ Object
-
#pull ⇒ Object
-
#read_boot_options ⇒ Object
-
#read_image ⇒ Object
-
#read_image_version ⇒ Object
-
#read_run_command ⇒ Object
-
#remove_container ⇒ Object
-
#remove_image ⇒ Object
-
#remove_proxy_directory ⇒ Object
-
#remove_proxy_secrets_file ⇒ Object
Static path rather than proxy_run_config.secrets_path: the file must be removable precisely when the run config (or its secrets) is gone.
-
#remove_stopped_container(name: container_name) ⇒ Object
-
#reset_boot_options ⇒ Object
-
#reset_image ⇒ Object
-
#reset_image_version ⇒ Object
-
#reset_run_command ⇒ Object
-
#run(digest: nil, name: nil) ⇒ Object
-
#run_holder ⇒ Object
-
#start ⇒ Object
-
#start_holder_or_run ⇒ Object
-
#start_or_run(digest: nil) ⇒ Object
-
#stop(name: container_name, timeout: nil) ⇒ Object
-
#version ⇒ Object
-
#wait_for_exit(name: container_name) ⇒ Object
Methods inherited from Base
#container_id_for, #ensure_docker_installed, #make_directory, #make_directory_for, #read_file, #remove_directory, #remove_file, #run_over_ssh
Constructor Details
#initialize(config, host:) ⇒ Proxy
Returns a new instance of Proxy.
7
8
9
10
|
# File 'lib/kamal/commands/proxy.rb', line 7
def initialize(config, host:)
super(config)
@proxy_run_config = config.proxy_run(host)
end
|
Instance Attribute Details
#proxy_run_config ⇒ Object
Returns the value of attribute proxy_run_config.
3
4
5
|
# File 'lib/kamal/commands/proxy.rb', line 3
def proxy_run_config
@proxy_run_config
end
|
Instance Method Details
#boot_config ⇒ Object
190
191
192
|
# File 'lib/kamal/commands/proxy.rb', line 190
def boot_config
[ :echo, "#{substitute(read_boot_options)} #{substitute(read_image)}:#{substitute(read_image_version)} #{substitute(read_run_command)}" ]
end
|
#cache_purge(service, path_prefix: nil) ⇒ Object
76
77
78
|
# File 'lib/kamal/commands/proxy.rb', line 76
def cache_purge(service, path_prefix: nil)
docker :exec, container_name, "kamal-proxy", :cache, :purge, service, *optionize({ "path-prefix": path_prefix }.compact)
end
|
#cache_stats(count: false, json: false) ⇒ Object
72
73
74
|
# File 'lib/kamal/commands/proxy.rb', line 72
def cache_stats(count: false, json: false)
docker :exec, container_name, "kamal-proxy", :cache, :stats, *optionize({ count: count || nil, json: json || nil }.compact)
end
|
#cleanup_traefik ⇒ Object
163
164
165
166
167
168
169
170
|
# File 'lib/kamal/commands/proxy.rb', line 163
def cleanup_traefik
chain \
docker(:container, :stop, "traefik"),
combine(
docker(:container, :prune, "--force", "--filter", "label=org.opencontainers.image.title=Traefik"),
docker(:image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik")
)
end
|
#config_digest ⇒ Object
52
53
54
|
# File 'lib/kamal/commands/proxy.rb', line 52
def config_digest
docker :inspect, container_name, "--format", "'{{ index .Config.Labels \"#{CONFIG_DIGEST_LABEL}\" }}'"
end
|
#container_id(only_running: false) ⇒ Object
56
57
58
|
# File 'lib/kamal/commands/proxy.rb', line 56
def container_id(only_running: false)
container_id_for(container_name: container_name, only_running: only_running)
end
|
#disable_restart ⇒ Object
Cancel the restart policy before draining: drain makes the proxy exit on
its own, which - unlike docker stop - an active restart policy would undo.
118
119
120
|
# File 'lib/kamal/commands/proxy.rb', line 118
def disable_restart
docker :update, "--restart=no", container_name
end
|
#domains(subcommand) ⇒ Object
151
152
153
|
# File 'lib/kamal/commands/proxy.rb', line 151
def domains(subcommand)
docker :exec, container_name, "kamal-proxy", "domains", subcommand
end
|
#drain(timeout: nil) ⇒ Object
122
123
124
|
# File 'lib/kamal/commands/proxy.rb', line 122
def drain(timeout: nil)
docker :exec, container_name, "kamal-proxy", :drain, *("--drain-timeout=#{timeout}s" if timeout)
end
|
#ensure_apps_config_directory ⇒ Object
180
181
182
|
# File 'lib/kamal/commands/proxy.rb', line 180
def ensure_apps_config_directory
make_directory config.proxy_boot.apps_directory
end
|
#ensure_proxy_directory ⇒ Object
172
173
174
|
# File 'lib/kamal/commands/proxy.rb', line 172
def ensure_proxy_directory
make_directory config.proxy_boot.host_directory
end
|
#follow_logs(host:, timestamps: true, grep: nil, grep_options: nil) ⇒ Object
144
145
146
147
148
149
|
# File 'lib/kamal/commands/proxy.rb', line 144
def follow_logs(host:, timestamps: true, grep: nil, grep_options: nil)
run_over_ssh pipe(
docker(:logs, container_name, ("--timestamps" if timestamps), "--tail", "10", "--follow", "2>&1"),
(%(grep "#{grep}"#{" #{grep_options}" if grep_options}) if grep)
).join(" "), host: host
end
|
#holder_container_id ⇒ Object
112
113
114
|
# File 'lib/kamal/commands/proxy.rb', line 112
def holder_container_id
container_id_for(container_name: proxy_run_config.holder_container_name, only_running: true)
end
|
#info ⇒ Object
42
43
44
|
# File 'lib/kamal/commands/proxy.rb', line 42
def info
docker :ps, "--filter", "'name=^#{container_name}$'"
end
|
#list(name: container_name, json: false) ⇒ Object
68
69
70
|
# File 'lib/kamal/commands/proxy.rb', line 68
def list(name: container_name, json: false)
docker :exec, name, "kamal-proxy", :list, *("--json" if json)
end
|
#loadbalancer ⇒ Object
226
227
228
|
# File 'lib/kamal/commands/proxy.rb', line 226
def loadbalancer
@loadbalancer ||= Kamal::Commands::Loadbalancer.new(config, loadbalancer_config: KAMAL.loadbalancer_config)
end
|
#logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) ⇒ Object
138
139
140
141
142
|
# File 'lib/kamal/commands/proxy.rb', line 138
def logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil)
pipe \
docker(:logs, container_name, ("--since #{since}" if since), ("--tail #{lines}" if lines), ("--timestamps" if timestamps), "2>&1"),
("grep '#{grep}'#{" #{grep_options}" if grep_options}" if grep)
end
|
#mount_destinations ⇒ Object
One mount destination per line - what the running container was actually
booted with, as opposed to what the current configuration would mount.
82
83
84
|
# File 'lib/kamal/commands/proxy.rb', line 82
def mount_destinations
docker :inspect, container_name, "--format", "'{{range .Mounts}}{{println .Destination}}{{end}}'"
end
|
#next_container_name ⇒ Object
92
93
94
|
# File 'lib/kamal/commands/proxy.rb', line 92
def next_container_name
"#{container_name}-next"
end
|
#port_holder? ⇒ Boolean
Zero-downtime handoff commands (proxy/run port_holder mode)
88
89
90
|
# File 'lib/kamal/commands/proxy.rb', line 88
def port_holder?
proxy_run_config&.port_holder? || false
end
|
134
135
136
|
# File 'lib/kamal/commands/proxy.rb', line 134
def promote_next_container
docker :container, :rename, next_container_name, container_name
end
|
#pull ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/kamal/commands/proxy.rb', line 60
def pull
if proxy_run_config
docker :pull, proxy_run_config.image
else
docker :pull, "#{substitute(read_image)}:#{substitute(read_image_version)}"
end
end
|
#read_boot_options ⇒ Object
194
195
196
|
# File 'lib/kamal/commands/proxy.rb', line 194
def read_boot_options
read_file(config.proxy_boot.options_file, default: config.proxy_boot.default_boot_options.join(" "))
end
|
#read_image ⇒ Object
198
199
200
|
# File 'lib/kamal/commands/proxy.rb', line 198
def read_image
read_file(config.proxy_boot.image_file, default: config.proxy_boot.image_default)
end
|
#read_image_version ⇒ Object
#read_run_command ⇒ Object
206
207
208
|
# File 'lib/kamal/commands/proxy.rb', line 206
def read_run_command
read_file(config.proxy_boot.run_command_file)
end
|
#remove_container ⇒ Object
155
156
157
|
# File 'lib/kamal/commands/proxy.rb', line 155
def remove_container
docker :container, :prune, "--force", "--filter", "label=org.opencontainers.image.title=kamal-proxy"
end
|
#remove_image ⇒ Object
159
160
161
|
# File 'lib/kamal/commands/proxy.rb', line 159
def remove_image
docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=kamal-proxy"
end
|
#remove_proxy_directory ⇒ Object
176
177
178
|
# File 'lib/kamal/commands/proxy.rb', line 176
def remove_proxy_directory
remove_directory config.proxy_boot.host_directory
end
|
#remove_proxy_secrets_file ⇒ Object
Static path rather than proxy_run_config.secrets_path: the file must be
removable precisely when the run config (or its secrets) is gone.
#remove_stopped_container(name: container_name) ⇒ Object
130
131
132
|
# File 'lib/kamal/commands/proxy.rb', line 130
def remove_stopped_container(name: container_name)
docker :container, :rm, name
end
|
#reset_boot_options ⇒ Object
210
211
212
|
# File 'lib/kamal/commands/proxy.rb', line 210
def reset_boot_options
remove_file config.proxy_boot.options_file
end
|
#reset_image ⇒ Object
214
215
216
|
# File 'lib/kamal/commands/proxy.rb', line 214
def reset_image
remove_file config.proxy_boot.image_file
end
|
#reset_image_version ⇒ Object
218
219
220
|
# File 'lib/kamal/commands/proxy.rb', line 218
def reset_image_version
remove_file config.proxy_boot.image_version_file
end
|
#reset_run_command ⇒ Object
222
223
224
|
# File 'lib/kamal/commands/proxy.rb', line 222
def reset_run_command
remove_file config.proxy_boot.run_command_file
end
|
#run(digest: nil, name: nil) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/kamal/commands/proxy.rb', line 12
def run(digest: nil, name: nil)
if proxy_run_config
docker \
:run,
"--name", name || container_name,
*proxy_run_config.network_args,
"--detach",
"--restart", "unless-stopped",
"--volume", "kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy",
*config_digest_label_args(digest),
*proxy_run_config.docker_options_args,
*proxy_run_config.image,
*proxy_run_config.run_command
else
pipe boot_config, xargs(docker_run(digest: digest))
end
end
|
#run_holder ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/kamal/commands/proxy.rb', line 96
def run_holder
docker \
:run,
"--name", proxy_run_config.holder_container_name,
"--network", "kamal",
"--detach",
"--restart", "unless-stopped",
*proxy_run_config.holder_docker_args,
*proxy_run_config.image,
"kamal-proxy", "hold"
end
|
#start ⇒ Object
30
31
32
|
# File 'lib/kamal/commands/proxy.rb', line 30
def start
docker :container, :start, container_name
end
|
#start_holder_or_run ⇒ Object
108
109
110
|
# File 'lib/kamal/commands/proxy.rb', line 108
def start_holder_or_run
combine docker(:container, :start, proxy_run_config.holder_container_name), run_holder, by: "||"
end
|
#start_or_run(digest: nil) ⇒ Object
38
39
40
|
# File 'lib/kamal/commands/proxy.rb', line 38
def start_or_run(digest: nil)
combine start, run(digest: digest), by: "||"
end
|
#stop(name: container_name, timeout: nil) ⇒ Object
34
35
36
|
# File 'lib/kamal/commands/proxy.rb', line 34
def stop(name: container_name, timeout: nil)
docker :container, :stop, *("--time #{timeout}" if timeout), name
end
|
#version ⇒ Object
46
47
48
49
50
|
# File 'lib/kamal/commands/proxy.rb', line 46
def version
pipe \
docker(:inspect, container_name, "--format '{{.Config.Image}}'"),
[ :awk, "-F:", "'{print \$NF}'" ]
end
|
#wait_for_exit(name: container_name) ⇒ Object
126
127
128
|
# File 'lib/kamal/commands/proxy.rb', line 126
def wait_for_exit(name: container_name)
docker :wait, name
end
|