Class: Kamal::Commands::Traefik

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

Constant Summary collapse

DEFAULT_IMAGE =
"traefik:v2.10"
CONTAINER_PORT =
80
DEFAULT_ARGS =
{
  "log.level" => "DEBUG"
}
DEFAULT_LABELS =
{
  # These ensure we serve a 502 rather than a 404 if no containers are available
  "traefik.http.routers.catchall.entryPoints" => "http",
  "traefik.http.routers.catchall.rule" => "PathPrefix(`/`)",
  "traefik.http.routers.catchall.service" => "unavailable",
  "traefik.http.routers.catchall.priority" => 1,
  "traefik.http.services.unavailable.loadbalancer.server.port" => "0"
}

Constants inherited from Base

Base::DOCKER_HEALTH_STATUS_FORMAT

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#container_id_for, #initialize, #make_directory, #make_directory_for, #remove_directory, #run_over_ssh

Constructor Details

This class inherits a constructor from Kamal::Commands::Base

Instance Method Details

#envObject



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

def env
  Kamal::Configuration::Env.from_config \
    config: config.traefik.fetch("env", {}),
    secrets_file: File.join(config.host_env_directory, "traefik", "traefik.env")
end

#follow_logs(host:, grep: nil) ⇒ Object



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

def follow_logs(host:, grep: nil)
  run_over_ssh pipe(
    docker(:logs, "traefik", "--timestamps", "--tail", "10", "--follow", "2>&1"),
    (%(grep "#{grep}") if grep)
  ).join(" "), host: host
end

#infoObject



45
46
47
# File 'lib/kamal/commands/traefik.rb', line 45

def info
  docker :ps, "--filter", "name=^traefik$"
end

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



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

def logs(since: nil, lines: nil, grep: nil)
  pipe \
    docker(:logs, "traefik", (" --since #{since}" if since), (" --tail #{lines}" if lines), "--timestamps", "2>&1"),
    ("grep '#{grep}'" if grep)
end

#make_env_directoryObject



80
81
82
# File 'lib/kamal/commands/traefik.rb', line 80

def make_env_directory
  make_directory(env.secrets_directory)
end

#portObject



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

def port
  "#{host_port}:#{CONTAINER_PORT}"
end

#remove_containerObject



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

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

#remove_env_fileObject



84
85
86
# File 'lib/kamal/commands/traefik.rb', line 84

def remove_env_file
  [ :rm, "-f", env.secrets_file ]
end

#remove_imageObject



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

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

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kamal/commands/traefik.rb', line 18

def run
  docker :run, "--name traefik",
    "--detach",
    "--restart", "unless-stopped",
    *publish_args,
    "--volume", "/var/run/docker.sock:/var/run/docker.sock",
    *env_args,
    *config.logging_args,
    *label_args,
    *docker_options_args,
    image,
    "--providers.docker",
    *cmd_option_args
end

#startObject



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

def start
  docker :container, :start, "traefik"
end

#start_or_runObject



41
42
43
# File 'lib/kamal/commands/traefik.rb', line 41

def start_or_run
  any start, run
end

#stopObject



37
38
39
# File 'lib/kamal/commands/traefik.rb', line 37

def stop
  docker :container, :stop, "traefik"
end