Class: Kamal::Cli::Proxy

Inherits:
Base
  • Object
show all
Defined in:
lib/kamal/cli/proxy.rb

Constant Summary

Constants inherited from Base

Base::AUTOMATIC_DEPLOY_LOCK_MESSAGE, Base::VERBOSITY

Instance Method Summary collapse

Methods inherited from Base

dynamic_command_class, exit_on_failure?, #initialize

Constructor Details

This class inherits a constructor from Kamal::Cli::Base

Instance Method Details

#bootObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kamal/cli/proxy.rb', line 3

def boot
  modify(lock: true) do
    on(KAMAL.hosts) do |host|
      execute *KAMAL.docker.create_network
    rescue SSHKit::Command::Failed => e
      raise unless e.message.include?("already exists")
    end

    # Skip proxy on loadbalancer host - the loadbalancer will handle it
    proxy_hosts = KAMAL.proxy_hosts
    if KAMAL.config.proxy.loadbalancer_on_proxy_host?
      proxy_hosts = proxy_hosts - [ KAMAL.config.proxy.effective_loadbalancer ]
    end

    on(proxy_hosts) do |host|
      execute *KAMAL.registry.

      version = capture_with_info(*KAMAL.proxy(host).version).strip.presence

      if version && Kamal::Utils.older_version?(version, Kamal::Configuration::Proxy::Run::MINIMUM_VERSION)
        raise "kamal-proxy version #{version} is too old, run `kamal proxy reboot` in order to update to at least #{Kamal::Configuration::Proxy::Run::MINIMUM_VERSION}"
      end
      execute *KAMAL.proxy(host).ensure_apps_config_directory
      execute *KAMAL.proxy(host).start_or_run
    end

    if KAMAL.config.proxy.load_balancing?
      on(KAMAL.config.proxy.effective_loadbalancer) do |host|
        info "Starting loadbalancer on #{host}..."
        execute *KAMAL.registry.
        execute *KAMAL.loadbalancer.ensure_apps_config_directory
        execute *KAMAL.loadbalancer.start_or_run
      end
    end
  end
end

#boot_config(subcommand) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/kamal/cli/proxy.rb', line 52

def boot_config(subcommand)
  say "The proxy boot_config command is deprecated - set the config in the deploy YAML at proxy/run instead", :yellow
  proxy_boot_config = KAMAL.config.proxy_boot

  case subcommand
  when "set"
    boot_options = [
      *(proxy_boot_config.publish_args(options[:http_port], options[:https_port], options[:publish_host_ip]) if options[:publish]),
      *(proxy_boot_config.logging_args(options[:log_max_size])),
      *("--expose=#{options[:metrics_port]}" if options[:metrics_port]),
      *options[:docker_options].map { |option| "--#{option}" }
    ]

    image = [
      options[:registry].presence,
      options[:repository].presence || proxy_boot_config.repository_name,
      proxy_boot_config.image_name
    ].compact.join("/")

    image_version = options[:image_version]

    run_command_options = { debug: options[:debug] || nil, "metrics-port": options[:metrics_port] }.compact
    run_command = "kamal-proxy run #{Kamal::Utils.optionize(run_command_options).join(" ")}" if run_command_options.any?

    on(KAMAL.proxy_hosts) do |host|
      proxy = KAMAL.proxy(host)
      execute(*proxy.ensure_proxy_directory)
      if boot_options != proxy_boot_config.default_boot_options
        upload! StringIO.new(boot_options.join(" ")), proxy_boot_config.options_file
      else
        execute *proxy.reset_boot_options, raise_on_non_zero_exit: false
      end

      if image != proxy_boot_config.image_default
        upload! StringIO.new(image), proxy_boot_config.image_file
      else
        execute *proxy.reset_image, raise_on_non_zero_exit: false
      end

      if image_version
        upload! StringIO.new(image_version), proxy_boot_config.image_version_file
      else
        execute *proxy.reset_image_version, raise_on_non_zero_exit: false
      end

      if run_command
        upload! StringIO.new(run_command), proxy_boot_config.run_command_file
      else
        execute *proxy.reset_run_command, raise_on_non_zero_exit: false
      end
    end
  when "get"

    on(KAMAL.proxy_hosts) do |host|
      puts "Host #{host}: #{capture_with_info(*KAMAL.proxy(host).boot_config)}"
    end
  when "reset"
    on(KAMAL.proxy_hosts) do |host|
      proxy = KAMAL.proxy(host)
      execute *proxy.reset_boot_options, raise_on_non_zero_exit: false
      execute *proxy.reset_image, raise_on_non_zero_exit: false
      execute *proxy.reset_image_version, raise_on_non_zero_exit: false
      execute *proxy.reset_run_command, raise_on_non_zero_exit: false
    end
  else
    raise ArgumentError, "Unknown boot_config subcommand #{subcommand}"
  end
end

#detailsObject



246
247
248
249
250
251
252
253
254
255
# File 'lib/kamal/cli/proxy.rb', line 246

def details
  quiet = options[:quiet]
  on(KAMAL.proxy_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.proxy(host).info), type: "Proxy", quiet: quiet }

  if KAMAL.config.proxy.load_balancing?
    on(KAMAL.config.proxy.effective_loadbalancer) do |host|
      puts_by_host host, capture_with_info(*KAMAL.loadbalancer.info), type: "Loadbalancer"
    end
  end
end

#loadbalancer(status) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/kamal/cli/proxy.rb', line 298

def loadbalancer(status)
  case status
  when "info"
    if KAMAL.config.proxy.load_balancing?
      on(KAMAL.config.proxy.effective_loadbalancer) do |host|
        puts "Loadbalancer status on #{host}:"
        puts capture_with_info(*KAMAL.loadbalancer.info)
      end
    else
      puts "Load balancing is not configured"
    end
  when "start"
    if KAMAL.config.proxy.load_balancing?
      on(KAMAL.config.proxy.effective_loadbalancer) do |host|
        execute *KAMAL.registry.
        execute *KAMAL.loadbalancer.start_or_run
      end
    else
      puts "Load balancing is not configured"
    end
  when "stop"
    if KAMAL.config.proxy.load_balancing?
      on(KAMAL.config.proxy.effective_loadbalancer) do |host|
        execute *KAMAL.loadbalancer.stop, raise_on_non_zero_exit: false
      end
    else
      puts "Load balancing is not configured"
    end
  when "logs"
    if KAMAL.config.proxy.load_balancing?
      on(KAMAL.config.proxy.effective_loadbalancer) do |host|
        puts_by_host host, capture(*KAMAL.loadbalancer.logs(timestamps: true)), type: "Loadbalancer"
      end
    else
      puts "Load balancing is not configured"
    end
  when "deploy"
    if KAMAL.config.proxy.load_balancing?
      targets = []
      KAMAL.config.roles.each do |role|
        next unless role.running_proxy?

        role.hosts.each do |host|
          targets << host
        end
      end

      on(KAMAL.config.proxy.effective_loadbalancer) do |host|
        info "Deploying to loadbalancer on #{host} with targets: #{targets.join(', ')}"
        execute *KAMAL.loadbalancer.deploy(targets: targets)
      end
    else
      puts "Load balancing is not configured"
    end
  else
    puts "Unknown loadbalancer subcommand: #{status}. Available: info, start, stop, logs, deploy"
  end
end

#logsObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/kamal/cli/proxy.rb', line 263

def logs
  grep = options[:grep]
  timestamps = !options[:skip_timestamps]

  if options[:follow]
    run_locally do
      proxy = KAMAL.proxy(KAMAL.primary_host)
      info "Following logs on #{KAMAL.primary_host}..."
      info proxy.follow_logs(host: KAMAL.primary_host, timestamps: timestamps, grep: grep)
      exec proxy.follow_logs(host: KAMAL.primary_host, timestamps: timestamps, grep: grep)
    end
  else
    since = options[:since]
    lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set

    on(KAMAL.proxy_hosts) do |host|
      puts_by_host host, capture(*KAMAL.proxy(host).logs(timestamps: timestamps, since: since, lines: lines, grep: grep)), type: "Proxy"
    end
  end
end

#rebootObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/kamal/cli/proxy.rb', line 124

def reboot
  confirming "This will cause a brief outage on each host. Are you sure?" do
    modify(lock: true) do
      # Skip proxy on loadbalancer host - it will be handled by loadbalancer reboot
      proxy_hosts = KAMAL.proxy_hosts
      if KAMAL.config.proxy.loadbalancer_on_proxy_host?
        proxy_hosts = proxy_hosts - [ KAMAL.config.proxy.effective_loadbalancer ]
      end

      host_groups = options[:rolling] ? proxy_hosts : [ proxy_hosts ]
      host_groups.each do |hosts|
        next if Array(hosts).empty?

        host_list = Array(hosts).join(",")
        run_hook "pre-proxy-reboot", hosts: host_list
        on(hosts) do |host|
          proxy = KAMAL.proxy(host)
          execute *KAMAL.auditor.record("Rebooted proxy"), verbosity: :debug
          execute *KAMAL.registry.

          info "Stopping and removing kamal-proxy on #{host}, if running..."
          execute *proxy.stop, raise_on_non_zero_exit: false
          execute *proxy.remove_container
          execute *proxy.ensure_apps_config_directory

          execute *proxy.run
        end
        run_hook "post-proxy-reboot", hosts: host_list
      end

      if KAMAL.config.proxy.load_balancing?
        lb_host = KAMAL.config.proxy.effective_loadbalancer
        run_hook "pre-loadbalancer-reboot", hosts: lb_host

        on(lb_host) do |host|
          execute *KAMAL.auditor.record("Rebooted loadbalancer"), verbosity: :debug
          execute *KAMAL.registry.

          info "Stopping and removing #{KAMAL.loadbalancer.container_name} on #{host}, if running..."
          execute *KAMAL.loadbalancer.stop, raise_on_non_zero_exit: false
          execute *KAMAL.loadbalancer.remove_container
          execute *KAMAL.loadbalancer.ensure_apps_config_directory

          execute *KAMAL.loadbalancer.run
        end

        run_hook "post-loadbalancer-reboot", hosts: lb_host
      end
    end
  end
end

#removeObject



286
287
288
289
290
291
292
293
294
295
# File 'lib/kamal/cli/proxy.rb', line 286

def remove
  modify(lock: true) do
    if removal_allowed?(options[:force])
      stop
      remove_container
      remove_image
      remove_proxy_directory
    end
  end
end

#remove_containerObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/kamal/cli/proxy.rb', line 358

def remove_container
  modify(lock: true) do
    on(KAMAL.proxy_hosts) do
      execute *KAMAL.auditor.record("Removed proxy container"), verbosity: :debug
      execute *KAMAL.proxy(host).remove_container
    end

    if KAMAL.config.proxy.load_balancing?
      on(KAMAL.config.proxy.effective_loadbalancer) do
        execute *KAMAL.auditor.record("Removed loadbalancer container"), verbosity: :debug
        execute *KAMAL.loadbalancer.remove_container
      end
    end
  end
end

#remove_imageObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/kamal/cli/proxy.rb', line 375

def remove_image
  modify(lock: true) do
    on(KAMAL.proxy_hosts) do
      execute *KAMAL.auditor.record("Removed proxy image"), verbosity: :debug
      execute *KAMAL.proxy(host).remove_image
    end

    if KAMAL.config.proxy.load_balancing?
      on(KAMAL.config.proxy.effective_loadbalancer) do
        execute *KAMAL.auditor.record("Removed loadbalancer image"), verbosity: :debug
        execute *KAMAL.loadbalancer.remove_image
      end
    end
  end
end

#remove_proxy_directoryObject



392
393
394
395
396
397
398
# File 'lib/kamal/cli/proxy.rb', line 392

def remove_proxy_directory
  modify(lock: true) do
    on(KAMAL.proxy_hosts) do
      execute *KAMAL.proxy(host).remove_proxy_directory, raise_on_non_zero_exit: false
    end
  end
end

#restartObject



238
239
240
241
242
243
# File 'lib/kamal/cli/proxy.rb', line 238

def restart
  modify(lock: true) do
    stop
    start
  end
end

#startObject



218
219
220
221
222
223
224
225
# File 'lib/kamal/cli/proxy.rb', line 218

def start
  modify(lock: true) do
    on(KAMAL.proxy_hosts) do |host|
      execute *KAMAL.auditor.record("Started proxy"), verbosity: :debug
      execute *KAMAL.proxy(host).start
    end
  end
end

#stopObject



228
229
230
231
232
233
234
235
# File 'lib/kamal/cli/proxy.rb', line 228

def stop
  modify(lock: true) do
    on(KAMAL.proxy_hosts) do |host|
      execute *KAMAL.auditor.record("Stopped proxy"), verbosity: :debug
      execute *KAMAL.proxy(host).stop, raise_on_non_zero_exit: false
    end
  end
end

#upgradeObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/kamal/cli/proxy.rb', line 179

def upgrade
  invoke_options = { "version" => KAMAL.config.latest_tag }.merge(options)

  confirming "This will cause a brief outage on each host. Are you sure?" do
    host_groups = options[:rolling] ? KAMAL.hosts : [ KAMAL.hosts ]
    host_groups.each do |hosts|
      host_list = Array(hosts).join(",")
      say "Upgrading proxy on #{host_list}...", :magenta
      run_hook "pre-proxy-reboot", hosts: host_list
      on(hosts) do |host|
        proxy = KAMAL.proxy(host)
        execute *KAMAL.auditor.record("Rebooted proxy"), verbosity: :debug
        execute *KAMAL.registry.

        info "Stopping and removing Traefik on #{host}, if running..."
        execute *proxy.cleanup_traefik

        info "Stopping and removing kamal-proxy on #{host}, if running..."
        execute *proxy.stop, raise_on_non_zero_exit: false
        execute *proxy.remove_container
        execute *proxy.remove_image
      end

      KAMAL.with_specific_hosts(hosts) do
        invoke "kamal:cli:proxy:boot", [], invoke_options
        reset_invocation(Kamal::Cli::Proxy)
        invoke "kamal:cli:app:boot", [], invoke_options
        reset_invocation(Kamal::Cli::App)
        invoke "kamal:cli:prune:all", [], invoke_options
        reset_invocation(Kamal::Cli::Prune)
      end

      run_hook "post-proxy-reboot", hosts: host_list
      say "Upgraded proxy on #{host_list}", :magenta
    end
  end
end