Class: Dash::Commands::Loadbalancer

Inherits:
Base
  • Object
show all
Includes:
Proxy::CertTransfer
Defined in:
lib/dash/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, #ensure_run_directory, #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/dash/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/dash/commands/loadbalancer.rb', line 6

def loadbalancer_config
  @loadbalancer_config
end

Instance Method Details

#cache_purge(service, path_prefix: nil) ⇒ Object



61
62
63
# File 'lib/dash/commands/loadbalancer.rb', line 61

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.



57
58
59
# File 'lib/dash/commands/loadbalancer.rb', line 57

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



65
66
67
# File 'lib/dash/commands/loadbalancer.rb', line 65

def config_digest
  docker :inspect, container_name, "--format", Dash::Commands::Proxy::CONFIG_DIGEST_FORMAT
end

#container_id(only_running: false) ⇒ Object



69
70
71
# File 'lib/dash/commands/loadbalancer.rb', line 69

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

#container_nameObject



143
144
145
# File 'lib/dash/commands/loadbalancer.rb', line 143

def container_name
  loadbalancer_config.container_name
end

#deploy(targets: []) ⇒ Object



40
41
42
43
# File 'lib/dash/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, *args) ⇒ Object

retry takes a host, or --all; the rest take no arguments.



46
47
48
# File 'lib/dash/commands/loadbalancer.rb', line 46

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

#ensure_apps_config_directoryObject



123
124
125
# File 'lib/dash/commands/loadbalancer.rb', line 123

def ensure_apps_config_directory
  make_directory config.proxy_boot.apps_directory
end

#ensure_directoryObject



109
110
111
# File 'lib/dash/commands/loadbalancer.rb', line 109

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 .dash/proxy directory the per-app proxy hosts use.



115
116
117
# File 'lib/dash/commands/loadbalancer.rb', line 115

def ensure_proxy_directory
  make_directory loadbalancer_config.run.host_directory
end

#ensure_services_directoryObject



127
128
129
# File 'lib/dash/commands/loadbalancer.rb', line 127

def ensure_services_directory
  make_directory loadbalancer_config.services_directory
end

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



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

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



73
74
75
# File 'lib/dash/commands/loadbalancer.rb', line 73

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

#list(json: false) ⇒ Object



50
51
52
# File 'lib/dash/commands/loadbalancer.rb', line 50

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



83
84
85
86
87
# File 'lib/dash/commands/loadbalancer.rb', line 83

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



135
136
137
# File 'lib/dash/commands/loadbalancer.rb', line 135

def read_run_config_record
  read_file loadbalancer_config.run_config_file
end

#read_service_ownerObject



131
132
133
# File 'lib/dash/commands/loadbalancer.rb', line 131

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.



99
100
101
# File 'lib/dash/commands/loadbalancer.rb', line 99

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

#remove_directoryObject



139
140
141
# File 'lib/dash/commands/loadbalancer.rb', line 139

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.



105
106
107
# File 'lib/dash/commands/loadbalancer.rb', line 105

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

#remove_proxy_secrets_fileObject



119
120
121
# File 'lib/dash/commands/loadbalancer.rb', line 119

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/dash/commands/loadbalancer.rb', line 13

def run
  docker \
    :run,
    "--name", container_name,
    "--network", "kamal",
    "--detach",
    "--restart", "unless-stopped",
    "--label", label,
    "--label", "#{Dash::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/dash/commands/loadbalancer.rb', line 28

def start
  docker :container, :start, container_name
end

#start_or_runObject



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

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

#stop(name: container_name) ⇒ Object



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

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

#versionObject



77
78
79
80
81
# File 'lib/dash/commands/loadbalancer.rb', line 77

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