Class: Kamal::Cli::Proxy::LoadbalancerClaim

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

Overview

Ownership bookkeeping for a load balancer host that more than one kamal app may be deploying to. Kamal only ever sees one deploy.yml at a time, so the other apps are visible solely through the files they left on the host.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, sshkit) ⇒ LoadbalancerClaim

Returns a new instance of LoadbalancerClaim.



8
9
10
11
# File 'lib/kamal/cli/proxy/loadbalancer_claim.rb', line 8

def initialize(host, sshkit)
  @host = host
  @sshkit = sshkit
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/kamal/cli/proxy/loadbalancer_claim.rb', line 5

def host
  @host
end

#sshkitObject (readonly)

Returns the value of attribute sshkit.



5
6
7
# File 'lib/kamal/cli/proxy/loadbalancer_claim.rb', line 5

def sshkit
  @sshkit
end

Instance Method Details

#claim_run_config(replace: false) ⇒ Object

Every app sharing a load balancer boots the same container, so their proxy/run configurations have to agree. A mismatch owned by another app is an error; a mismatch owned by this app is ordinary drift, reported the same way the per-host proxy reports it and fixed by kamal proxy reboot.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kamal/cli/proxy/loadbalancer_claim.rb', line 33

def claim_run_config(replace: false)
  record = capture_with_info(*commands.read_run_config_record, raise_on_non_zero_exit: false).strip

  if record.present?
    *owner_parts, digest = record.split(" ")
    owner = owner_parts.join(" ")

    if digest != config.run_config_digest
      if owner != config.owner_token
        raise "The load balancer on #{host} was booted by #{repository_in(owner)} with a different proxy/run configuration. " \
              "Apps sharing a load balancer must agree on proxy/run - align the configurations or use a different load balancer host."
      elsif !replace
        warn "The load balancer on #{host} is running with a configuration that no longer matches the deploy config - " \
             "run `kamal proxy reboot` to apply the new configuration."
        return
      end
    end
  end

  execute *commands.ensure_directory
  upload! StringIO.new(config.run_config_record), config.run_config_file
end

#claim_serviceObject

The load balancer registers services under the bare service name, so a second app - or a second destination of this app - deploying the same name would silently take over its routes. Claim the name on first deploy and fail loudly rather than quietly stealing it.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kamal/cli/proxy/loadbalancer_claim.rb', line 17

def claim_service
  owner = capture_with_info(*commands.read_service_owner, raise_on_non_zero_exit: false).strip

  if owner.present? && owner != config.owner_token
    raise "Service '#{config.config.service}' is already registered on the load balancer at #{host} by #{repository_in(owner)}. " \
          "Deploying would take over its routes - rename this app's service or use a different load balancer."
  end

  execute *commands.ensure_services_directory
  upload! StringIO.new(config.owner_token), config.service_owner_file
end