Class: Kamal::Commands::Proxy

Inherits:
Base
  • Object
show all
Defined in:
lib/kamal/commands/proxy.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_directory, #remove_file, #run_over_ssh

Constructor Details

#initialize(config, host:) ⇒ Proxy



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

def initialize(config, host:)
  super(config)
  @proxy_run_config = config.proxy_run(host)
end

Instance Attribute Details

#proxy_run_configObject (readonly)

Returns the value of attribute proxy_run_config.



3
4
5
# File 'lib/kamal/commands/proxy.rb', line 3

def proxy_run_config
  @proxy_run_config
end

Instance Method Details

#boot_configObject



91
92
93
# File 'lib/kamal/commands/proxy.rb', line 91

def boot_config
  [ :echo, "#{substitute(read_boot_options)} #{substitute(read_image)}:#{substitute(read_image_version)} #{substitute(read_run_command)}" ]
end

#cleanup_traefikObject



70
71
72
73
74
75
76
77
# File 'lib/kamal/commands/proxy.rb', line 70

def cleanup_traefik
  chain \
    docker(:container, :stop, "traefik"),
    combine(
      docker(:container, :prune, "--force", "--filter", "label=org.opencontainers.image.title=Traefik"),
      docker(:image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik")
    )
end

#ensure_apps_config_directoryObject



87
88
89
# File 'lib/kamal/commands/proxy.rb', line 87

def ensure_apps_config_directory
  make_directory config.proxy_boot.apps_directory
end

#ensure_proxy_directoryObject



79
80
81
# File 'lib/kamal/commands/proxy.rb', line 79

def ensure_proxy_directory
  make_directory config.proxy_boot.host_directory
end

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



55
56
57
58
59
60
# File 'lib/kamal/commands/proxy.rb', line 55

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



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

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

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



49
50
51
52
53
# File 'lib/kamal/commands/proxy.rb', line 49

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_boot_optionsObject



95
96
97
# File 'lib/kamal/commands/proxy.rb', line 95

def read_boot_options
  read_file(config.proxy_boot.options_file, default: config.proxy_boot.default_boot_options.join(" "))
end

#read_imageObject



99
100
101
# File 'lib/kamal/commands/proxy.rb', line 99

def read_image
  read_file(config.proxy_boot.image_file, default: config.proxy_boot.image_default)
end

#read_image_versionObject



103
104
105
# File 'lib/kamal/commands/proxy.rb', line 103

def read_image_version
  read_file(config.proxy_boot.image_version_file, default: Kamal::Configuration::Proxy::Run::MINIMUM_VERSION)
end

#read_run_commandObject



107
108
109
# File 'lib/kamal/commands/proxy.rb', line 107

def read_run_command
  read_file(config.proxy_boot.run_command_file)
end

#remove_containerObject



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

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

#remove_imageObject



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

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

#remove_proxy_directoryObject



83
84
85
# File 'lib/kamal/commands/proxy.rb', line 83

def remove_proxy_directory
  remove_directory config.proxy_boot.host_directory
end

#reset_boot_optionsObject



111
112
113
# File 'lib/kamal/commands/proxy.rb', line 111

def reset_boot_options
  remove_file config.proxy_boot.options_file
end

#reset_imageObject



115
116
117
# File 'lib/kamal/commands/proxy.rb', line 115

def reset_image
  remove_file config.proxy_boot.image_file
end

#reset_image_versionObject



119
120
121
# File 'lib/kamal/commands/proxy.rb', line 119

def reset_image_version
  remove_file config.proxy_boot.image_version_file
end

#reset_run_commandObject



123
124
125
# File 'lib/kamal/commands/proxy.rb', line 123

def reset_run_command
  remove_file config.proxy_boot.run_command_file
end

#runObject



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

def run
  if proxy_run_config
    docker \
      :run,
      "--name", container_name,
      "--network", "kamal",
      "--detach",
      "--restart", "unless-stopped",
      "--volume", "kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy",
      *proxy_run_config.docker_options_args,
      *proxy_run_config.image,
      *proxy_run_config.run_command
  else
    pipe boot_config, xargs(docker_run)
  end
end

#startObject



27
28
29
# File 'lib/kamal/commands/proxy.rb', line 27

def start
  docker :container, :start, container_name
end

#start_or_runObject



35
36
37
# File 'lib/kamal/commands/proxy.rb', line 35

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

#stop(name: container_name) ⇒ Object



31
32
33
# File 'lib/kamal/commands/proxy.rb', line 31

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

#versionObject



43
44
45
46
47
# File 'lib/kamal/commands/proxy.rb', line 43

def version
  pipe \
    docker(:inspect, container_name, "--format '{{.Config.Image}}'"),
    [ :awk, "-F:", "'{print \$NF}'" ]
end