Class: Kamal::Commands::Loadbalancer

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

Constant Summary

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 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.



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

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.



4
5
6
# File 'lib/kamal/commands/loadbalancer.rb', line 4

def loadbalancer_config
  @loadbalancer_config
end

Instance Method Details

#cache_purge(service, path_prefix: nil) ⇒ Object



58
59
60
# File 'lib/kamal/commands/loadbalancer.rb', line 58

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.



54
55
56
# File 'lib/kamal/commands/loadbalancer.rb', line 54

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



62
63
64
# File 'lib/kamal/commands/loadbalancer.rb', line 62

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

#container_idObject



66
67
68
# File 'lib/kamal/commands/loadbalancer.rb', line 66

def container_id
  container_id_for(container_name: container_name)
end

#container_nameObject



140
141
142
# File 'lib/kamal/commands/loadbalancer.rb', line 140

def container_name
  loadbalancer_config.container_name
end

#deploy(targets: []) ⇒ Object



38
39
40
41
# File 'lib/kamal/commands/loadbalancer.rb', line 38

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



43
44
45
# File 'lib/kamal/commands/loadbalancer.rb', line 43

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

#ensure_apps_config_directoryObject



120
121
122
# File 'lib/kamal/commands/loadbalancer.rb', line 120

def ensure_apps_config_directory
  make_directory config.proxy_boot.apps_directory
end

#ensure_directoryObject



106
107
108
# File 'lib/kamal/commands/loadbalancer.rb', line 106

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.



112
113
114
# File 'lib/kamal/commands/loadbalancer.rb', line 112

def ensure_proxy_directory
  make_directory loadbalancer_config.run.host_directory
end

#ensure_services_directoryObject



124
125
126
# File 'lib/kamal/commands/loadbalancer.rb', line 124

def ensure_services_directory
  make_directory loadbalancer_config.services_directory
end

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



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

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



70
71
72
# File 'lib/kamal/commands/loadbalancer.rb', line 70

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

#listObject



47
48
49
# File 'lib/kamal/commands/loadbalancer.rb', line 47

def list
  docker :exec, container_name, "kamal-proxy", :list
end

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



80
81
82
83
84
# File 'lib/kamal/commands/loadbalancer.rb', line 80

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



132
133
134
# File 'lib/kamal/commands/loadbalancer.rb', line 132

def read_run_config_record
  read_file loadbalancer_config.run_config_file
end

#read_service_ownerObject



128
129
130
# File 'lib/kamal/commands/loadbalancer.rb', line 128

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.



96
97
98
# File 'lib/kamal/commands/loadbalancer.rb', line 96

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

#remove_directoryObject



136
137
138
# File 'lib/kamal/commands/loadbalancer.rb', line 136

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.



102
103
104
# File 'lib/kamal/commands/loadbalancer.rb', line 102

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

#remove_proxy_secrets_fileObject



116
117
118
# File 'lib/kamal/commands/loadbalancer.rb', line 116

def remove_proxy_secrets_file
  remove_file loadbalancer_config.run.secrets_path
end

#runObject



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

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



26
27
28
# File 'lib/kamal/commands/loadbalancer.rb', line 26

def start
  docker :container, :start, container_name
end

#start_or_runObject



34
35
36
# File 'lib/kamal/commands/loadbalancer.rb', line 34

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

#stop(name: container_name) ⇒ Object



30
31
32
# File 'lib/kamal/commands/loadbalancer.rb', line 30

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

#versionObject



74
75
76
77
78
# File 'lib/kamal/commands/loadbalancer.rb', line 74

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