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



71
72
73
# File 'lib/dash/commands/loadbalancer.rb', line 71

def cache_purge(service, path_prefix: nil)
  docker :exec, container_name, "dash-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.



67
68
69
# File 'lib/dash/commands/loadbalancer.rb', line 67

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

#config_digestObject



75
76
77
# File 'lib/dash/commands/loadbalancer.rb', line 75

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

#container_id(only_running: false) ⇒ Object



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

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

#container_nameObject



161
162
163
# File 'lib/dash/commands/loadbalancer.rb', line 161

def container_name
  loadbalancer_config.container_name
end

#copy_legacy_config_volumeObject

A dedicated load balancer host went through the 4.0 rename with nothing adopting its kamal-loadbalancer-config volume: the per-host proxies got Dash::Cli::Proxy::LegacyRename, the load balancer got a fresh empty volume and lost its routing table, dynamic domains and ACME cache. On a shared proxy host the volume is the proxy's own and LegacyRename already copied it, so this is a no-op there.



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

def copy_legacy_config_volume
  copy_legacy_volume(legacy: legacy_config_volume_name, volume: config_volume_name, image: loadbalancer_config.run.image)
end

#deploy(targets: []) ⇒ Object



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

def deploy(targets: [])
  docker :exec, container_name, "dash-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.



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

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

#ensure_apps_config_directoryObject



141
142
143
# File 'lib/dash/commands/loadbalancer.rb', line 141

def ensure_apps_config_directory
  make_directory config.proxy_boot.apps_directory
end

#ensure_directoryObject



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

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.



133
134
135
# File 'lib/dash/commands/loadbalancer.rb', line 133

def ensure_proxy_directory
  make_directory loadbalancer_config.run.host_directory
end

#ensure_services_directoryObject



145
146
147
# File 'lib/dash/commands/loadbalancer.rb', line 145

def ensure_services_directory
  make_directory loadbalancer_config.services_directory
end

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



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

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



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

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

#list(json: false) ⇒ Object



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

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

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



93
94
95
96
97
# File 'lib/dash/commands/loadbalancer.rb', line 93

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



153
154
155
# File 'lib/dash/commands/loadbalancer.rb', line 153

def read_run_config_record
  read_file loadbalancer_config.run_config_file
end

#read_service_ownerObject



149
150
151
# File 'lib/dash/commands/loadbalancer.rb', line 149

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 dash-proxy title, and pruning by the loadbalancer title would leave the container behind for run to collide with. Both the current and the pre-rename title are pruned, so a container created before the rename is not left behind to collide either.



111
112
113
114
115
# File 'lib/dash/commands/loadbalancer.rb', line 111

def remove_container
  combine \
    prune_containers_titled(image_title),
    prune_containers_titled(legacy_image_title)
end

#remove_directoryObject



157
158
159
# File 'lib/dash/commands/loadbalancer.rb', line 157

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 dash-proxy image whichever host it sits on. Both titles are matched so a host still carrying a pre-rename image is cleaned up too; Docker ANDs multiple --filter label= values, so that is two commands.



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

def remove_image
  combine \
    prune_images_titled(Dash::Configuration::Proxy::IMAGE_TITLE),
    prune_images_titled(Dash::Configuration::Proxy::LEGACY_IMAGE_TITLE)
end

#remove_proxy_secrets_fileObject



137
138
139
# File 'lib/dash/commands/loadbalancer.rb', line 137

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", "dash",
    "--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



87
88
89
90
91
# File 'lib/dash/commands/loadbalancer.rb', line 87

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