Class: Kamal::Commands::Loadbalancer

Inherits:
Base
  • Object
show all
Includes:
Proxy::CertTransfer
Defined in:
lib/kamal/commands/loadbalancer.rb

Constant Summary

Constants included from Proxy::CertTransfer

Proxy::CertTransfer::CERT_ARCHIVE_FILENAME, Proxy::CertTransfer::CERT_IMPORT_STAGING_FILENAME, Proxy::CertTransfer::CONTAINER_IMPORT_PATH

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

Methods included from Proxy::CertTransfer

#certs_archive_container_path, #certs_archive_host_path, #certs_import_host_path, #export_certs, #export_certs_offline, #import_certs, #remove_certs_archive, #remove_certs_import

Methods inherited from Base

#container_id_for, #ensure_docker_installed, #make_directory, #make_directory_for, #read_file, #remove_file, #run_over_ssh

Constructor Details

#initialize(config, loadbalancer_config: nil) ⇒ Loadbalancer

Returns a new instance of Loadbalancer.



8
9
10
11
# File 'lib/kamal/commands/loadbalancer.rb', line 8

def initialize(config, loadbalancer_config: nil)
  super(config)
  @loadbalancer_config = loadbalancer_config
end

Instance Attribute Details

#loadbalancer_configObject (readonly)

Returns the value of attribute loadbalancer_config.



6
7
8
# File 'lib/kamal/commands/loadbalancer.rb', line 6

def loadbalancer_config
  @loadbalancer_config
end

Instance Method Details

#cache_purge(service, path_prefix: nil) ⇒ Object



60
61
62
# File 'lib/kamal/commands/loadbalancer.rb', line 60

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

Cache policy is edge-only under load balancing (see the layering contract), so the cache admin surface lives here - registered under the bare service name, unlike the per-role services on the proxy hosts.



56
57
58
# File 'lib/kamal/commands/loadbalancer.rb', line 56

def cache_stats(count: false, json: false)
  docker :exec, container_name, "kamal-proxy", :cache, :stats, *optionize({ count: count || nil, json: json || nil }.compact)
end

#config_digestObject



64
65
66
# File 'lib/kamal/commands/loadbalancer.rb', line 64

def config_digest
  docker :inspect, container_name, "--format", "'{{ index .Config.Labels \"#{Kamal::Commands::Proxy::CONFIG_DIGEST_LABEL}\" }}'"
end

#container_id(only_running: false) ⇒ Object



68
69
70
# File 'lib/kamal/commands/loadbalancer.rb', line 68

def container_id(only_running: false)
  container_id_for(container_name: container_name, only_running: only_running)
end

#container_nameObject



142
143
144
# File 'lib/kamal/commands/loadbalancer.rb', line 142

def container_name
  loadbalancer_config.container_name
end

#deploy(targets: []) ⇒ Object



40
41
42
43
# File 'lib/kamal/commands/loadbalancer.rb', line 40

def deploy(targets: [])
  docker :exec, container_name, "kamal-proxy", "deploy", loadbalancer_config.config.service,
    *loadbalancer_config.deploy_command_args(targets: targets)
end

#domains(subcommand) ⇒ Object



45
46
47
# File 'lib/kamal/commands/loadbalancer.rb', line 45

def domains(subcommand)
  docker :exec, container_name, "kamal-proxy", "domains", subcommand
end

#ensure_apps_config_directoryObject



122
123
124
# File 'lib/kamal/commands/loadbalancer.rb', line 122

def ensure_apps_config_directory
  make_directory config.proxy_boot.apps_directory
end

#ensure_directoryObject



108
109
110
# File 'lib/kamal/commands/loadbalancer.rb', line 108

def ensure_directory
  make_directory loadbalancer_config.directory
end

#ensure_proxy_directoryObject

Where the proxy secrets env file lands (see Proxy::Run#secrets_path) - the same .kamal/proxy directory the per-app proxy hosts use.



114
115
116
# File 'lib/kamal/commands/loadbalancer.rb', line 114

def ensure_proxy_directory
  make_directory loadbalancer_config.run.host_directory
end

#ensure_services_directoryObject



126
127
128
# File 'lib/kamal/commands/loadbalancer.rb', line 126

def ensure_services_directory
  make_directory loadbalancer_config.services_directory
end

#follow_logs(host:, timestamps: true, grep: nil, grep_options: nil) ⇒ Object



88
89
90
91
92
93
# File 'lib/kamal/commands/loadbalancer.rb', line 88

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

#infoObject



72
73
74
# File 'lib/kamal/commands/loadbalancer.rb', line 72

def info
  docker :ps, "--filter", "'name=^#{container_name}$'"
end

#list(json: false) ⇒ Object



49
50
51
# File 'lib/kamal/commands/loadbalancer.rb', line 49

def list(json: false)
  docker :exec, container_name, "kamal-proxy", :list, *("--json" if json)
end

#logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) ⇒ Object



82
83
84
85
86
# File 'lib/kamal/commands/loadbalancer.rb', line 82

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

#read_run_config_recordObject



134
135
136
# File 'lib/kamal/commands/loadbalancer.rb', line 134

def read_run_config_record
  read_file loadbalancer_config.run_config_file
end

#read_service_ownerObject



130
131
132
# File 'lib/kamal/commands/loadbalancer.rb', line 130

def read_service_owner
  read_file loadbalancer_config.service_owner_file
end

#remove_containerObject

Prune by the label the container was actually created with - on a shared proxy host that is the kamal-proxy title, and pruning by the loadbalancer title would leave the container behind for run to collide with.



98
99
100
# File 'lib/kamal/commands/loadbalancer.rb', line 98

def remove_container
  docker :container, :prune, "--force", "--filter", "label=#{label}"
end

#remove_directoryObject



138
139
140
# File 'lib/kamal/commands/loadbalancer.rb', line 138

def remove_directory
  super(loadbalancer_config.directory)
end

#remove_imageObject

Image label filters match labels baked into the image, and the load balancer runs the kamal-proxy image whichever host it sits on.



104
105
106
# File 'lib/kamal/commands/loadbalancer.rb', line 104

def remove_image
  docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=kamal-proxy"
end

#remove_proxy_secrets_fileObject



118
119
120
# File 'lib/kamal/commands/loadbalancer.rb', line 118

def remove_proxy_secrets_file
  remove_file loadbalancer_config.run.secrets_path
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kamal/commands/loadbalancer.rb', line 13

def run
  docker \
    :run,
    "--name", container_name,
    "--network", "kamal",
    "--detach",
    "--restart", "unless-stopped",
    "--label", label,
    "--label", "#{Kamal::Commands::Proxy::CONFIG_DIGEST_LABEL}=#{loadbalancer_config.run_config_digest}",
    *config_volume,
    *run_args,
    *loadbalancer_config.run.image,
    *loadbalancer_config.run.run_command
end

#startObject



28
29
30
# File 'lib/kamal/commands/loadbalancer.rb', line 28

def start
  docker :container, :start, container_name
end

#start_or_runObject



36
37
38
# File 'lib/kamal/commands/loadbalancer.rb', line 36

def start_or_run
  combine start, run, by: "||"
end

#stop(name: container_name) ⇒ Object



32
33
34
# File 'lib/kamal/commands/loadbalancer.rb', line 32

def stop(name: container_name)
  docker :container, :stop, name
end

#versionObject



76
77
78
79
80
# File 'lib/kamal/commands/loadbalancer.rb', line 76

def version
  pipe \
    docker(:inspect, container_name, "--format '{{.Config.Image}}'"),
    [ :cut, "-d:", "-f2" ]
end