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

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, #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

#container_nameObject



93
94
95
# File 'lib/kamal/commands/loadbalancer.rb', line 93

def container_name
  loadbalancer_config.container_name
end

#deploy(targets: []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kamal/commands/loadbalancer.rb', line 37

def deploy(targets: [])
  target_args = targets.map { |t| "#{t}:80" }

  hosts = loadbalancer_config.hosts

  options = []
  options << "--target=#{target_args.join(',')}"
  options << "--host=#{hosts.join(',')}"
  options << "--tls" if loadbalancer_config.ssl?

  docker :exec, container_name, "kamal-proxy", "deploy", loadbalancer_config.config.service, *options
end

#ensure_apps_config_directoryObject



85
86
87
# File 'lib/kamal/commands/loadbalancer.rb', line 85

def ensure_apps_config_directory
  make_directory config.proxy_boot.apps_directory
end

#ensure_directoryObject



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

def ensure_directory
  make_directory loadbalancer_config.directory
end

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



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

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



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

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

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



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

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

#remove_containerObject



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

def remove_container
  docker :container, :prune, "--force", "--filter", "label=org.opencontainers.image.title=kamal-loadbalancer"
end

#remove_directoryObject



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

def remove_directory
  super(loadbalancer_config.directory)
end

#remove_imageObject



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

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

#runObject



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

def run
  pipe \
    [ :echo, proxy_image ],
    xargs(docker(:run,
      "--name", container_name,
      "--network", "kamal",
      "--detach",
      "--restart", "unless-stopped",
      "--publish", "80:80",
      "--publish", "443:443",
      "--label", label,
      *volume_mounts))
end

#startObject



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

def start
  docker :container, :start, container_name
end

#start_or_runObject



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

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

#stop(name: container_name) ⇒ Object



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

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

#versionObject



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

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