Compare commits
2 Commits
v2.5.1
...
kamal-prox
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
015c5a6f90 | ||
|
|
6568cef868 |
@@ -1,6 +1,6 @@
|
|||||||
# Kamal: Deploy web apps anywhere
|
# Kamal: Deploy web apps anywhere
|
||||||
|
|
||||||
From bare metal to cloud VMs, deploy web apps anywhere with zero downtime. Kamal has the dynamic reverse-proxy Traefik hold requests while a new app container is started and the old one is stopped. Works seamlessly across multiple hosts, using SSHKit to execute commands. Originally built for Rails apps, Kamal will work with any type of web app that can be containerized with Docker.
|
From bare metal to cloud VMs, deploy web apps anywhere with zero downtime. Kamal uses a [custom proxy](https://github.com/basecamp/kamal-proxy) for zero-downtime deployments. Works seamlessly across multiple hosts, using SSHKit to execute commands. Originally built for Rails apps, Kamal will work with any type of web app that can be containerized with Docker.
|
||||||
|
|
||||||
➡️ See [kamal-deploy.org](https://kamal-deploy.org) for documentation on [installation](https://kamal-deploy.org/docs/installation), [configuration](https://kamal-deploy.org/docs/configuration), and [commands](https://kamal-deploy.org/docs/commands).
|
➡️ See [kamal-deploy.org](https://kamal-deploy.org) for documentation on [installation](https://kamal-deploy.org/docs/installation), [configuration](https://kamal-deploy.org/docs/configuration), and [commands](https://kamal-deploy.org/docs/commands).
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
module Kamal::Cli
|
module Kamal::Cli
|
||||||
class LockError < StandardError; end
|
class LockError < StandardError; end
|
||||||
class HookError < StandardError; end
|
class HookError < StandardError; end
|
||||||
|
class BootError < StandardError; end
|
||||||
end
|
end
|
||||||
|
|
||||||
# SSHKit uses instance eval, so we need a global const for ergonomics
|
# SSHKit uses instance eval, so we need a global const for ergonomics
|
||||||
|
|||||||
@@ -9,16 +9,16 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
# Assets are prepared in a separate step to ensure they are on all hosts before booting
|
# Assets are prepared in a separate step to ensure they are on all hosts before booting
|
||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
KAMAL.roles_on(host).each do |role|
|
KAMAL.roles_on(host).each do |role|
|
||||||
Kamal::Cli::App::PrepareAssets.new(host, role, self).run
|
PrepareAssets.new(host, role, self).run
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Primary hosts and roles are returned first, so they can open the barrier
|
# Primary hosts and roles are returned first, so they can open the barrier
|
||||||
barrier = Kamal::Cli::Healthcheck::Barrier.new if KAMAL.roles.many?
|
barrier = Barrier.new if KAMAL.roles.many?
|
||||||
|
|
||||||
on(KAMAL.hosts, **KAMAL.boot_strategy) do |host|
|
on(KAMAL.hosts, **KAMAL.boot_strategy) do |host|
|
||||||
KAMAL.roles_on(host).each do |role|
|
KAMAL.roles_on(host).each do |role|
|
||||||
Kamal::Cli::App::Boot.new(host, role, self, version, barrier).run
|
Boot.new(host, role, self, version, barrier).run
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -38,8 +38,17 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
roles = KAMAL.roles_on(host)
|
roles = KAMAL.roles_on(host)
|
||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
|
app = KAMAL.app(role: role, host: host)
|
||||||
execute *KAMAL.auditor.record("Started app version #{KAMAL.config.version}"), verbosity: :debug
|
execute *KAMAL.auditor.record("Started app version #{KAMAL.config.version}"), verbosity: :debug
|
||||||
execute *KAMAL.app(role: role, host: host).start, raise_on_non_zero_exit: false
|
execute *app.start, raise_on_non_zero_exit: false
|
||||||
|
|
||||||
|
if role.running_proxy?
|
||||||
|
version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
|
||||||
|
endpoint = capture_with_info(*app.container_endpoint(version: version)).strip
|
||||||
|
raise Kamal::Cli::BootError, "Failed to get endpoint for #{role} on #{host}, did the container boot?" if endpoint.empty?
|
||||||
|
|
||||||
|
execute *KAMAL.proxy.deploy(role.container_prefix, target: endpoint)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -52,8 +61,19 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
roles = KAMAL.roles_on(host)
|
roles = KAMAL.roles_on(host)
|
||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
|
app = KAMAL.app(role: role, host: host)
|
||||||
|
version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
|
||||||
|
|
||||||
execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug
|
execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug
|
||||||
execute *KAMAL.app(role: role, host: host).stop, raise_on_non_zero_exit: false
|
|
||||||
|
if role.running_proxy?
|
||||||
|
endpoint = capture_with_info(*app.container_endpoint(version: version)).strip
|
||||||
|
if endpoint.present?
|
||||||
|
execute *KAMAL.proxy.remove(role.container_prefix, target: endpoint), raise_on_non_zero_exit: false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
execute *app.stop, raise_on_non_zero_exit: false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
class Kamal::Cli::Healthcheck::Barrier
|
class Kamal::Cli::App::Barrier
|
||||||
def initialize
|
def initialize
|
||||||
@ivar = Concurrent::IVar.new
|
@ivar = Concurrent::IVar.new
|
||||||
end
|
end
|
||||||
@@ -13,7 +13,7 @@ class Kamal::Cli::Healthcheck::Barrier
|
|||||||
|
|
||||||
def wait
|
def wait
|
||||||
unless opened?
|
unless opened?
|
||||||
raise Kamal::Cli::Healthcheck::Error.new("Halted at barrier")
|
raise Kamal::Cli::BootError.new("Halted at barrier")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
class Kamal::Cli::App::Boot
|
class Kamal::Cli::App::Boot
|
||||||
attr_reader :host, :role, :version, :barrier, :sshkit
|
attr_reader :host, :role, :version, :barrier, :sshkit
|
||||||
delegate :execute, :capture_with_info, :capture_with_pretty_json, :info, :error, to: :sshkit
|
delegate :execute, :capture_with_info, :capture_with_pretty_json, :info, :error, to: :sshkit
|
||||||
delegate :uses_cord?, :assets?, :running_traefik?, to: :role
|
delegate :assets?, :running_proxy?, to: :role
|
||||||
|
|
||||||
def initialize(host, role, sshkit, version, barrier)
|
def initialize(host, role, sshkit, version, barrier)
|
||||||
@host = host
|
@host = host
|
||||||
@@ -45,11 +45,13 @@ class Kamal::Cli::App::Boot
|
|||||||
|
|
||||||
def start_new_version
|
def start_new_version
|
||||||
audit "Booted app version #{version}"
|
audit "Booted app version #{version}"
|
||||||
|
|
||||||
execute *app.tie_cord(role.cord_host_file) if uses_cord?
|
|
||||||
hostname = "#{host.to_s[0...51].gsub(/\.+$/, '')}-#{SecureRandom.hex(6)}"
|
hostname = "#{host.to_s[0...51].gsub(/\.+$/, '')}-#{SecureRandom.hex(6)}"
|
||||||
execute *app.run(hostname: hostname)
|
execute *app.run(hostname: hostname)
|
||||||
Kamal::Cli::Healthcheck::Poller.wait_for_healthy(pause_after_ready: true) { capture_with_info(*app.status(version: version)) }
|
if running_proxy?
|
||||||
|
endpoint = capture_with_info(*app.container_endpoint(version: version)).strip
|
||||||
|
raise Kamal::Cli::BootError, "Failed to get endpoint for #{role} on #{host}, did the container boot?" if endpoint.empty?
|
||||||
|
execute *KAMAL.proxy.deploy(role.container_prefix, target: endpoint)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop_new_version
|
def stop_new_version
|
||||||
@@ -57,16 +59,7 @@ class Kamal::Cli::App::Boot
|
|||||||
end
|
end
|
||||||
|
|
||||||
def stop_old_version(version)
|
def stop_old_version(version)
|
||||||
if uses_cord?
|
|
||||||
cord = capture_with_info(*app.cord(version: version), raise_on_non_zero_exit: false).strip
|
|
||||||
if cord.present?
|
|
||||||
execute *app.cut_cord(cord)
|
|
||||||
Kamal::Cli::Healthcheck::Poller.wait_for_unhealthy(pause_after_ready: true) { capture_with_info(*app.status(version: version)) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
execute *app.stop(version: version), raise_on_non_zero_exit: false
|
execute *app.stop(version: version), raise_on_non_zero_exit: false
|
||||||
|
|
||||||
execute *app.clean_up_assets if assets?
|
execute *app.clean_up_assets if assets?
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -80,7 +73,7 @@ class Kamal::Cli::App::Boot
|
|||||||
info "Waiting for the first healthy #{KAMAL.primary_role} container before booting #{role} on #{host}..."
|
info "Waiting for the first healthy #{KAMAL.primary_role} container before booting #{role} on #{host}..."
|
||||||
barrier.wait
|
barrier.wait
|
||||||
info "First #{KAMAL.primary_role} container is healthy, booting #{role} on #{host}..."
|
info "First #{KAMAL.primary_role} container is healthy, booting #{role} on #{host}..."
|
||||||
rescue Kamal::Cli::Healthcheck::Error
|
rescue Kamal::Cli::BootError
|
||||||
info "First #{KAMAL.primary_role} container is unhealthy, not booting #{role} on #{host}"
|
info "First #{KAMAL.primary_role} container is unhealthy, not booting #{role} on #{host}"
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
@@ -89,7 +82,6 @@ class Kamal::Cli::App::Boot
|
|||||||
if barrier.close
|
if barrier.close
|
||||||
info "First #{KAMAL.primary_role} container is unhealthy on #{host}, not booting other roles"
|
info "First #{KAMAL.primary_role} container is unhealthy on #{host}, not booting other roles"
|
||||||
error capture_with_info(*app.logs(version: version))
|
error capture_with_info(*app.logs(version: version))
|
||||||
error capture_with_info(*app.container_health_log(version: version))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -13,11 +13,6 @@ class Kamal::Cli::Env < Kamal::Cli::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.traefik.make_env_directory
|
|
||||||
upload! KAMAL.traefik.env.secrets_io, KAMAL.traefik.env.secrets_file, mode: 400
|
|
||||||
end
|
|
||||||
|
|
||||||
on(KAMAL.accessory_hosts) do
|
on(KAMAL.accessory_hosts) do
|
||||||
KAMAL.accessories_on(host).each do |accessory|
|
KAMAL.accessories_on(host).each do |accessory|
|
||||||
accessory_config = KAMAL.config.accessory(accessory)
|
accessory_config = KAMAL.config.accessory(accessory)
|
||||||
@@ -39,10 +34,6 @@ class Kamal::Cli::Env < Kamal::Cli::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.traefik.remove_env_file
|
|
||||||
end
|
|
||||||
|
|
||||||
on(KAMAL.accessory_hosts) do
|
on(KAMAL.accessory_hosts) do
|
||||||
KAMAL.accessories_on(host).each do |accessory|
|
KAMAL.accessories_on(host).each do |accessory|
|
||||||
accessory_config = KAMAL.config.accessory(accessory)
|
accessory_config = KAMAL.config.accessory(accessory)
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
class Kamal::Cli::Healthcheck::Error < StandardError
|
|
||||||
end
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
module Kamal::Cli::Healthcheck::Poller
|
|
||||||
extend self
|
|
||||||
|
|
||||||
TRAEFIK_UPDATE_DELAY = 5
|
|
||||||
|
|
||||||
|
|
||||||
def wait_for_healthy(pause_after_ready: false, &block)
|
|
||||||
attempt = 1
|
|
||||||
max_attempts = KAMAL.config.healthcheck["max_attempts"]
|
|
||||||
|
|
||||||
begin
|
|
||||||
case status = block.call
|
|
||||||
when "healthy"
|
|
||||||
sleep TRAEFIK_UPDATE_DELAY if pause_after_ready
|
|
||||||
when "running" # No health check configured
|
|
||||||
sleep KAMAL.config.readiness_delay if pause_after_ready
|
|
||||||
else
|
|
||||||
raise Kamal::Cli::Healthcheck::Error, "container not ready (#{status})"
|
|
||||||
end
|
|
||||||
rescue Kamal::Cli::Healthcheck::Error => e
|
|
||||||
if attempt <= max_attempts
|
|
||||||
info "#{e.message}, retrying in #{attempt}s (attempt #{attempt}/#{max_attempts})..."
|
|
||||||
sleep attempt
|
|
||||||
attempt += 1
|
|
||||||
retry
|
|
||||||
else
|
|
||||||
raise
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
info "Container is healthy!"
|
|
||||||
end
|
|
||||||
|
|
||||||
def wait_for_unhealthy(pause_after_ready: false, &block)
|
|
||||||
attempt = 1
|
|
||||||
max_attempts = KAMAL.config.healthcheck["max_attempts"]
|
|
||||||
|
|
||||||
begin
|
|
||||||
case status = block.call
|
|
||||||
when "unhealthy"
|
|
||||||
sleep TRAEFIK_UPDATE_DELAY if pause_after_ready
|
|
||||||
else
|
|
||||||
raise Kamal::Cli::Healthcheck::Error, "container not unhealthy (#{status})"
|
|
||||||
end
|
|
||||||
rescue Kamal::Cli::Healthcheck::Error => e
|
|
||||||
if attempt <= max_attempts
|
|
||||||
info "#{e.message}, retrying in #{attempt}s (attempt #{attempt}/#{max_attempts})..."
|
|
||||||
sleep attempt
|
|
||||||
attempt += 1
|
|
||||||
retry
|
|
||||||
else
|
|
||||||
raise
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
info "Container is unhealthy!"
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def info(message)
|
|
||||||
SSHKit.config.output.info(message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -38,8 +38,8 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
with_lock do
|
with_lock do
|
||||||
run_hook "pre-deploy"
|
run_hook "pre-deploy"
|
||||||
|
|
||||||
say "Ensure Traefik is running...", :magenta
|
say "Ensure proxy is running...", :magenta
|
||||||
invoke "kamal:cli:traefik:boot", [], invoke_options
|
invoke "kamal:cli:proxy:boot", [], invoke_options
|
||||||
|
|
||||||
say "Detect stale containers...", :magenta
|
say "Detect stale containers...", :magenta
|
||||||
invoke "kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true)
|
invoke "kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true)
|
||||||
@@ -54,7 +54,7 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
run_hook "post-deploy", runtime: runtime.round
|
run_hook "post-deploy", runtime: runtime.round
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "redeploy", "Deploy app to servers without bootstrapping servers, starting Traefik, pruning, and registry login"
|
desc "redeploy", "Deploy app to servers without bootstrapping servers, starting proxy, pruning, and registry login"
|
||||||
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip image build and push"
|
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip image build and push"
|
||||||
def redeploy
|
def redeploy
|
||||||
runtime = print_runtime do
|
runtime = print_runtime do
|
||||||
@@ -107,7 +107,7 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "details", "Show details about all containers"
|
desc "details", "Show details about all containers"
|
||||||
def details
|
def details
|
||||||
invoke "kamal:cli:traefik:details"
|
invoke "kamal:cli:proxy:details"
|
||||||
invoke "kamal:cli:app:details"
|
invoke "kamal:cli:app:details"
|
||||||
invoke "kamal:cli:accessory:details", [ "all" ]
|
invoke "kamal:cli:accessory:details", [ "all" ]
|
||||||
end
|
end
|
||||||
@@ -189,12 +189,12 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "remove", "Remove Traefik, app, accessories, and registry session from servers"
|
desc "remove", "Remove proxy, app, accessories, and registry session from servers"
|
||||||
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
|
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
|
||||||
def remove
|
def remove
|
||||||
confirming "This will remove all containers and images. Are you sure?" do
|
confirming "This will remove all containers and images. Are you sure?" do
|
||||||
with_lock do
|
with_lock do
|
||||||
invoke "kamal:cli:traefik:remove", [], options.without(:confirmed)
|
invoke "kamal:cli:proxy:remove", [], options.without(:confirmed)
|
||||||
invoke "kamal:cli:app:remove", [], options.without(:confirmed)
|
invoke "kamal:cli:app:remove", [], options.without(:confirmed)
|
||||||
invoke "kamal:cli:accessory:remove", [ "all" ], options
|
invoke "kamal:cli:accessory:remove", [ "all" ], options
|
||||||
invoke "kamal:cli:registry:logout", [], options.without(:confirmed)
|
invoke "kamal:cli:registry:logout", [], options.without(:confirmed)
|
||||||
@@ -231,8 +231,8 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
desc "server", "Bootstrap servers with curl and Docker"
|
desc "server", "Bootstrap servers with curl and Docker"
|
||||||
subcommand "server", Kamal::Cli::Server
|
subcommand "server", Kamal::Cli::Server
|
||||||
|
|
||||||
desc "traefik", "Manage Traefik load balancer"
|
desc "proxy", "Manage load balancer proxy"
|
||||||
subcommand "traefik", Kamal::Cli::Traefik
|
subcommand "proxy", Kamal::Cli::Proxy
|
||||||
|
|
||||||
private
|
private
|
||||||
def container_available?(version)
|
def container_available?(version)
|
||||||
|
|||||||
164
lib/kamal/cli/proxy.rb
Normal file
164
lib/kamal/cli/proxy.rb
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
class Kamal::Cli::Proxy < Kamal::Cli::Base
|
||||||
|
desc "boot", "Boot proxy on servers"
|
||||||
|
def boot
|
||||||
|
with_lock do
|
||||||
|
on(KAMAL.proxy_hosts) do
|
||||||
|
execute *KAMAL.registry.login
|
||||||
|
execute *KAMAL.proxy.start_or_run
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "reboot", "Reboot proxy on servers (stop container, remove container, start new container)"
|
||||||
|
option :rolling, type: :boolean, default: false, desc: "Reboot proxy on hosts in sequence, rather than in parallel"
|
||||||
|
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
|
||||||
|
def reboot
|
||||||
|
confirming "This will cause a brief outage on each host. Are you sure?" do
|
||||||
|
with_lock do
|
||||||
|
host_groups = options[:rolling] ? KAMAL.proxy_hosts : [ KAMAL.proxy_hosts ]
|
||||||
|
host_groups.each do |hosts|
|
||||||
|
host_list = Array(hosts).join(",")
|
||||||
|
run_hook "pre-proxy-reboot", hosts: host_list
|
||||||
|
on(hosts) do
|
||||||
|
execute *KAMAL.auditor.record("Rebooted proxy"), verbosity: :debug
|
||||||
|
execute *KAMAL.registry.login
|
||||||
|
execute *KAMAL.proxy.stop, raise_on_non_zero_exit: false
|
||||||
|
execute *KAMAL.proxy.remove_container
|
||||||
|
execute *KAMAL.proxy.run
|
||||||
|
end
|
||||||
|
run_hook "post-proxy-reboot", hosts: host_list
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "start", "Start existing proxy container on servers"
|
||||||
|
def start
|
||||||
|
with_lock do
|
||||||
|
on(KAMAL.proxy_hosts) do
|
||||||
|
execute *KAMAL.auditor.record("Started proxy"), verbosity: :debug
|
||||||
|
execute *KAMAL.proxy.start
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "stop", "Stop existing proxy container on servers"
|
||||||
|
def stop
|
||||||
|
with_lock do
|
||||||
|
on(KAMAL.proxy_hosts) do
|
||||||
|
execute *KAMAL.auditor.record("Stopped proxy"), verbosity: :debug
|
||||||
|
execute *KAMAL.proxy.stop, raise_on_non_zero_exit: false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "restart", "Restart existing proxy container on servers"
|
||||||
|
def restart
|
||||||
|
with_lock do
|
||||||
|
stop
|
||||||
|
start
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "update", "Update from Traefik to kamal-proxy, for when moving from Kamal v1 to Kamal v2"
|
||||||
|
option :rolling, type: :boolean, default: false, desc: "Reboot proxy on hosts in sequence, rather than in parallel"
|
||||||
|
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
|
||||||
|
def update
|
||||||
|
confirming "This will cause a brief outage on each host. Are you sure?" do
|
||||||
|
with_lock do
|
||||||
|
host_groups = options[:rolling] ? KAMAL.proxy_hosts : [ KAMAL.proxy_hosts ]
|
||||||
|
host_groups.each do |hosts|
|
||||||
|
host_list = Array(hosts).join(",")
|
||||||
|
run_hook "pre-proxy-reboot", hosts: host_list
|
||||||
|
on(hosts) do
|
||||||
|
info "Updating proxy from Traefik to kamal-proxy on #{host}..."
|
||||||
|
execute *KAMAL.auditor.record("Updated proxy from Traefik to kamal-proxy"), verbosity: :debug
|
||||||
|
execute *KAMAL.registry.login
|
||||||
|
|
||||||
|
info "Stopping and removing Traefik on #{host}..."
|
||||||
|
execute *KAMAL.proxy.stop(name: "traefik"), raise_on_non_zero_exit: false
|
||||||
|
execute *KAMAL.proxy.remove_container(filter: "label=org.opencontainers.image.title=traefik")
|
||||||
|
execute *KAMAL.proxy.remove_image(filter: "label=org.opencontainers.image.title=traefik")
|
||||||
|
|
||||||
|
info "Stopping and removing kamal-proxy on #{host}, if running..."
|
||||||
|
execute *KAMAL.proxy.stop, raise_on_non_zero_exit: false
|
||||||
|
execute *KAMAL.proxy.remove_container
|
||||||
|
|
||||||
|
info "Starting kamal-proxy on #{host}..."
|
||||||
|
execute *KAMAL.proxy.run
|
||||||
|
|
||||||
|
KAMAL.roles_on(host).select(&:running_proxy?).each do |role|
|
||||||
|
app = KAMAL.app(role: role, host: host)
|
||||||
|
|
||||||
|
version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
|
||||||
|
endpoint = capture_with_info(*app.container_endpoint(version: version)).strip
|
||||||
|
raise Kamal::Cli::BootError, "Failed to get endpoint for #{role} on #{host}, is the app container running?" if endpoint.empty?
|
||||||
|
|
||||||
|
info "Deploying #{endpoint} for role `#{role}` on #{host}..."
|
||||||
|
execute *KAMAL.proxy.deploy(role.container_prefix, target: endpoint)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
run_hook "post-proxy-reboot", hosts: host_list
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "details", "Show details about proxy container from servers"
|
||||||
|
def details
|
||||||
|
on(KAMAL.proxy_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.proxy.info), type: "Proxy" }
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "logs", "Show log lines from proxy on servers"
|
||||||
|
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
||||||
|
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
||||||
|
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
|
||||||
|
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
|
||||||
|
def logs
|
||||||
|
grep = options[:grep]
|
||||||
|
|
||||||
|
if options[:follow]
|
||||||
|
run_locally do
|
||||||
|
info "Following logs on #{KAMAL.primary_host}..."
|
||||||
|
info KAMAL.proxy.follow_logs(host: KAMAL.primary_host, grep: grep)
|
||||||
|
exec KAMAL.proxy.follow_logs(host: KAMAL.primary_host, grep: grep)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
since = options[:since]
|
||||||
|
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
|
||||||
|
|
||||||
|
on(KAMAL.proxy_hosts) do |host|
|
||||||
|
puts_by_host host, capture(*KAMAL.proxy.logs(since: since, lines: lines, grep: grep)), type: "Proxy"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "remove", "Remove proxy container and image from servers"
|
||||||
|
def remove
|
||||||
|
with_lock do
|
||||||
|
stop
|
||||||
|
remove_container
|
||||||
|
remove_image
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "remove_container", "Remove proxy container from servers", hide: true
|
||||||
|
def remove_container
|
||||||
|
with_lock do
|
||||||
|
on(KAMAL.proxy_hosts) do
|
||||||
|
execute *KAMAL.auditor.record("Removed proxy container"), verbosity: :debug
|
||||||
|
execute *KAMAL.proxy.remove_container
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "remove_image", "Remove proxy image from servers", hide: true
|
||||||
|
def remove_image
|
||||||
|
with_lock do
|
||||||
|
on(KAMAL.proxy_hosts) do
|
||||||
|
execute *KAMAL.auditor.record("Removed proxy image"), verbosity: :debug
|
||||||
|
execute *KAMAL.proxy.remove_image
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -28,7 +28,6 @@ class Kamal::Cli::Prune < Kamal::Cli::Base
|
|||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
execute *KAMAL.auditor.record("Pruned containers"), verbosity: :debug
|
execute *KAMAL.auditor.record("Pruned containers"), verbosity: :debug
|
||||||
execute *KAMAL.prune.app_containers(retain: retain)
|
execute *KAMAL.prune.app_containers(retain: retain)
|
||||||
execute *KAMAL.prune.healthcheck_containers
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -63,17 +63,6 @@ registry:
|
|||||||
# directories:
|
# directories:
|
||||||
# - data:/data
|
# - data:/data
|
||||||
|
|
||||||
# Configure custom arguments for Traefik. Be sure to reboot traefik when you modify it.
|
|
||||||
# traefik:
|
|
||||||
# args:
|
|
||||||
# accesslog: true
|
|
||||||
# accesslog.format: json
|
|
||||||
|
|
||||||
# Configure a custom healthcheck (default is /up on port 3000)
|
|
||||||
# healthcheck:
|
|
||||||
# path: /healthz
|
|
||||||
# port: 4000
|
|
||||||
|
|
||||||
# Bridge fingerprinted assets, like JS and CSS, between versions to avoid
|
# Bridge fingerprinted assets, like JS and CSS, between versions to avoid
|
||||||
# hitting 404 on in-flight requests. Combines all files from new and old
|
# hitting 404 on in-flight requests. Combines all files from new and old
|
||||||
# version inside the asset_path.
|
# version inside the asset_path.
|
||||||
|
|||||||
3
lib/kamal/cli/templates/sample_hooks/post-proxy-reboot.sample
Executable file
3
lib/kamal/cli/templates/sample_hooks/post-proxy-reboot.sample
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
echo "Rebooted proxy on $KAMAL_HOSTS"
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
echo "Rebooted Traefik on $KAMAL_HOSTS"
|
|
||||||
3
lib/kamal/cli/templates/sample_hooks/pre-proxy-reboot.sample
Executable file
3
lib/kamal/cli/templates/sample_hooks/pre-proxy-reboot.sample
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
echo "Rebooting proxy on $KAMAL_HOSTS..."
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
echo "Rebooting Traefik on $KAMAL_HOSTS..."
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
class Kamal::Cli::Traefik < Kamal::Cli::Base
|
|
||||||
desc "boot", "Boot Traefik on servers"
|
|
||||||
def boot
|
|
||||||
with_lock do
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.registry.login
|
|
||||||
execute *KAMAL.traefik.start_or_run
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "reboot", "Reboot Traefik on servers (stop container, remove container, start new container)"
|
|
||||||
option :rolling, type: :boolean, default: false, desc: "Reboot traefik on hosts in sequence, rather than in parallel"
|
|
||||||
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
|
|
||||||
def reboot
|
|
||||||
confirming "This will cause a brief outage on each host. Are you sure?" do
|
|
||||||
with_lock do
|
|
||||||
host_groups = options[:rolling] ? KAMAL.traefik_hosts : [ KAMAL.traefik_hosts ]
|
|
||||||
host_groups.each do |hosts|
|
|
||||||
host_list = Array(hosts).join(",")
|
|
||||||
run_hook "pre-traefik-reboot", hosts: host_list
|
|
||||||
on(hosts) do
|
|
||||||
execute *KAMAL.auditor.record("Rebooted traefik"), verbosity: :debug
|
|
||||||
execute *KAMAL.registry.login
|
|
||||||
execute *KAMAL.traefik.stop, raise_on_non_zero_exit: false
|
|
||||||
execute *KAMAL.traefik.remove_container
|
|
||||||
execute *KAMAL.traefik.run
|
|
||||||
end
|
|
||||||
run_hook "post-traefik-reboot", hosts: host_list
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "start", "Start existing Traefik container on servers"
|
|
||||||
def start
|
|
||||||
with_lock do
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.auditor.record("Started traefik"), verbosity: :debug
|
|
||||||
execute *KAMAL.traefik.start
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "stop", "Stop existing Traefik container on servers"
|
|
||||||
def stop
|
|
||||||
with_lock do
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.auditor.record("Stopped traefik"), verbosity: :debug
|
|
||||||
execute *KAMAL.traefik.stop, raise_on_non_zero_exit: false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "restart", "Restart existing Traefik container on servers"
|
|
||||||
def restart
|
|
||||||
with_lock do
|
|
||||||
stop
|
|
||||||
start
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "details", "Show details about Traefik container from servers"
|
|
||||||
def details
|
|
||||||
on(KAMAL.traefik_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.traefik.info), type: "Traefik" }
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "logs", "Show log lines from Traefik on servers"
|
|
||||||
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
|
||||||
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
|
||||||
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
|
|
||||||
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
|
|
||||||
def logs
|
|
||||||
grep = options[:grep]
|
|
||||||
|
|
||||||
if options[:follow]
|
|
||||||
run_locally do
|
|
||||||
info "Following logs on #{KAMAL.primary_host}..."
|
|
||||||
info KAMAL.traefik.follow_logs(host: KAMAL.primary_host, grep: grep)
|
|
||||||
exec KAMAL.traefik.follow_logs(host: KAMAL.primary_host, grep: grep)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
since = options[:since]
|
|
||||||
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
|
|
||||||
|
|
||||||
on(KAMAL.traefik_hosts) do |host|
|
|
||||||
puts_by_host host, capture(*KAMAL.traefik.logs(since: since, lines: lines, grep: grep)), type: "Traefik"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "remove", "Remove Traefik container and image from servers"
|
|
||||||
def remove
|
|
||||||
with_lock do
|
|
||||||
stop
|
|
||||||
remove_container
|
|
||||||
remove_image
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "remove_container", "Remove Traefik container from servers", hide: true
|
|
||||||
def remove_container
|
|
||||||
with_lock do
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.auditor.record("Removed traefik container"), verbosity: :debug
|
|
||||||
execute *KAMAL.traefik.remove_container
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "remove_image", "Remove Traefik image from servers", hide: true
|
|
||||||
def remove_image
|
|
||||||
with_lock do
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.auditor.record("Removed traefik image"), verbosity: :debug
|
|
||||||
execute *KAMAL.traefik.remove_image
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -3,7 +3,7 @@ require "active_support/core_ext/module/delegation"
|
|||||||
|
|
||||||
class Kamal::Commander
|
class Kamal::Commander
|
||||||
attr_accessor :verbosity, :holding_lock, :connected
|
attr_accessor :verbosity, :holding_lock, :connected
|
||||||
delegate :hosts, :roles, :primary_host, :primary_role, :roles_on, :traefik_hosts, :accessory_hosts, to: :specifics
|
delegate :hosts, :roles, :primary_host, :primary_role, :roles_on, :proxy_hosts, :accessory_hosts, to: :specifics
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
self.verbosity = :info
|
self.verbosity = :info
|
||||||
@@ -85,10 +85,6 @@ class Kamal::Commander
|
|||||||
@docker ||= Kamal::Commands::Docker.new(config)
|
@docker ||= Kamal::Commands::Docker.new(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
def healthcheck
|
|
||||||
@healthcheck ||= Kamal::Commands::Healthcheck.new(config)
|
|
||||||
end
|
|
||||||
|
|
||||||
def hook
|
def hook
|
||||||
@hook ||= Kamal::Commands::Hook.new(config)
|
@hook ||= Kamal::Commands::Hook.new(config)
|
||||||
end
|
end
|
||||||
@@ -109,8 +105,8 @@ class Kamal::Commander
|
|||||||
@server ||= Kamal::Commands::Server.new(config)
|
@server ||= Kamal::Commands::Server.new(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
def traefik
|
def proxy
|
||||||
@traefik ||= Kamal::Commands::Traefik.new(config)
|
@proxy ||= Kamal::Commands::Proxy.new(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ class Kamal::Commander::Specifics
|
|||||||
roles.select { |role| role.hosts.include?(host.to_s) }
|
roles.select { |role| role.hosts.include?(host.to_s) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def traefik_hosts
|
def proxy_hosts
|
||||||
config.traefik_hosts & specified_hosts
|
config.proxy_hosts & specified_hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
def accessory_hosts
|
def accessory_hosts
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class Kamal::Commands::App < Kamal::Commands::Base
|
class Kamal::Commands::App < Kamal::Commands::Base
|
||||||
include Assets, Containers, Cord, Execution, Images, Logging
|
include Assets, Containers, Execution, Images, Logging
|
||||||
|
|
||||||
ACTIVE_DOCKER_STATUSES = [ :running, :restarting ]
|
ACTIVE_DOCKER_STATUSES = [ :running, :restarting ]
|
||||||
|
|
||||||
@@ -20,7 +20,6 @@ class Kamal::Commands::App < Kamal::Commands::Base
|
|||||||
"-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
|
"-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
|
||||||
"-e", "KAMAL_VERSION=\"#{config.version}\"",
|
"-e", "KAMAL_VERSION=\"#{config.version}\"",
|
||||||
*role.env_args(host),
|
*role.env_args(host),
|
||||||
*role.health_check_args,
|
|
||||||
*role.logging_args,
|
*role.logging_args,
|
||||||
*config.volume_args,
|
*config.volume_args,
|
||||||
*role.asset_volume_args,
|
*role.asset_volume_args,
|
||||||
@@ -57,6 +56,10 @@ class Kamal::Commands::App < Kamal::Commands::Base
|
|||||||
container_id_for(container_name: container_name(version), only_running: only_running)
|
container_id_for(container_name: container_name(version), only_running: only_running)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def container_name(version = nil)
|
||||||
|
[ role.container_prefix, version || config.version ].compact.join("-")
|
||||||
|
end
|
||||||
|
|
||||||
def current_running_version
|
def current_running_version
|
||||||
pipe \
|
pipe \
|
||||||
current_running_container(format: "--format '{{.Names}}'"),
|
current_running_container(format: "--format '{{.Names}}'"),
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ module Kamal::Commands::App::Containers
|
|||||||
docker :container, :prune, "--force", *filter_args
|
docker :container, :prune, "--force", *filter_args
|
||||||
end
|
end
|
||||||
|
|
||||||
def container_health_log(version:)
|
def container_endpoint(version:)
|
||||||
pipe \
|
pipe \
|
||||||
container_id_for(container_name: container_name(version)),
|
container_id_for(container_name: container_name(version)),
|
||||||
xargs(docker(:inspect, "--format", DOCKER_HEALTH_LOG_FORMAT))
|
xargs(docker(:inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'")),
|
||||||
|
[ :sed, "-e", "'s/\\/tcp$//'" ]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
module Kamal::Commands::App::Cord
|
|
||||||
def cord(version:)
|
|
||||||
pipe \
|
|
||||||
docker(:inspect, "-f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}'", container_name(version)),
|
|
||||||
[ :awk, "'$2 == \"#{role.cord_volume.container_path}\" {print $1}'" ]
|
|
||||||
end
|
|
||||||
|
|
||||||
def tie_cord(cord)
|
|
||||||
create_empty_file(cord)
|
|
||||||
end
|
|
||||||
|
|
||||||
def cut_cord(cord)
|
|
||||||
remove_directory(cord)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def create_empty_file(file)
|
|
||||||
chain \
|
|
||||||
make_directory_for(file),
|
|
||||||
[ :touch, file ]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
79
lib/kamal/commands/proxy.rb
Normal file
79
lib/kamal/commands/proxy.rb
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
class Kamal::Commands::Proxy < Kamal::Commands::Base
|
||||||
|
delegate :argumentize, :optionize, to: Kamal::Utils
|
||||||
|
delegate :container_name, to: :proxy_config
|
||||||
|
|
||||||
|
attr_reader :proxy_config
|
||||||
|
|
||||||
|
def initialize(config)
|
||||||
|
super
|
||||||
|
@proxy_config = config.proxy
|
||||||
|
end
|
||||||
|
|
||||||
|
def run
|
||||||
|
docker :run,
|
||||||
|
"--name", container_name,
|
||||||
|
"--detach",
|
||||||
|
"--restart", "unless-stopped",
|
||||||
|
*proxy_config.publish_args,
|
||||||
|
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
|
||||||
|
"--volume", "#{container_name}:/root/.config/kamal-proxy",
|
||||||
|
*config.logging_args,
|
||||||
|
*proxy_config.docker_options_args,
|
||||||
|
proxy_config.image
|
||||||
|
end
|
||||||
|
|
||||||
|
def start
|
||||||
|
docker :container, :start, container_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop(name: container_name)
|
||||||
|
docker :container, :stop, name
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_or_run
|
||||||
|
combine start, run, by: "||"
|
||||||
|
end
|
||||||
|
|
||||||
|
def deploy(service, target:)
|
||||||
|
optionize({ target: target })
|
||||||
|
docker :exec, container_name, "kamal-proxy", :deploy, service, *optionize({ target: target }), *proxy_config.deploy_command_args
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove(service, target:)
|
||||||
|
docker :exec, container_name, "kamal-proxy", :remove, service, *optionize({ target: target })
|
||||||
|
end
|
||||||
|
|
||||||
|
def info
|
||||||
|
docker :ps, "--filter", "name=^#{container_name}$"
|
||||||
|
end
|
||||||
|
|
||||||
|
def logs(since: nil, lines: nil, grep: nil)
|
||||||
|
pipe \
|
||||||
|
docker(:logs, container_name, (" --since #{since}" if since), (" --tail #{lines}" if lines), "--timestamps", "2>&1"),
|
||||||
|
("grep '#{grep}'" if grep)
|
||||||
|
end
|
||||||
|
|
||||||
|
def follow_logs(host:, grep: nil)
|
||||||
|
run_over_ssh pipe(
|
||||||
|
docker(:logs, container_name, "--timestamps", "--tail", "10", "--follow", "2>&1"),
|
||||||
|
(%(grep "#{grep}") if grep)
|
||||||
|
).join(" "), host: host
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_container(filter: container_filter)
|
||||||
|
docker :container, :prune, "--force", "--filter", filter
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_image(filter: image_filter)
|
||||||
|
docker :image, :prune, "--all", "--force", "--filter", filter
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def container_filter
|
||||||
|
"label=org.opencontainers.image.title=kamal-proxy"
|
||||||
|
end
|
||||||
|
|
||||||
|
def image_filter
|
||||||
|
"label=org.opencontainers.image.title=kamal-proxy"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -20,10 +20,6 @@ class Kamal::Commands::Prune < Kamal::Commands::Base
|
|||||||
"while read container_id; do docker rm $container_id; done"
|
"while read container_id; do docker rm $container_id; done"
|
||||||
end
|
end
|
||||||
|
|
||||||
def healthcheck_containers
|
|
||||||
docker :container, :prune, "--force", *healthcheck_service_filter
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def stopped_containers_filters
|
def stopped_containers_filters
|
||||||
[ "created", "exited", "dead" ].flat_map { |status| [ "--filter", "status=#{status}" ] }
|
[ "created", "exited", "dead" ].flat_map { |status| [ "--filter", "status=#{status}" ] }
|
||||||
@@ -39,8 +35,4 @@ class Kamal::Commands::Prune < Kamal::Commands::Base
|
|||||||
def service_filter
|
def service_filter
|
||||||
[ "--filter", "label=service=#{config.service}" ]
|
[ "--filter", "label=service=#{config.service}" ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def healthcheck_service_filter
|
|
||||||
[ "--filter", "label=service=#{config.healthcheck_service}" ]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,124 +0,0 @@
|
|||||||
class Kamal::Commands::Traefik < Kamal::Commands::Base
|
|
||||||
delegate :argumentize, :optionize, to: Kamal::Utils
|
|
||||||
|
|
||||||
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
def start
|
|
||||||
docker :container, :start, "traefik"
|
|
||||||
end
|
|
||||||
|
|
||||||
def stop
|
|
||||||
docker :container, :stop, "traefik"
|
|
||||||
end
|
|
||||||
|
|
||||||
def start_or_run
|
|
||||||
any start, run
|
|
||||||
end
|
|
||||||
|
|
||||||
def info
|
|
||||||
docker :ps, "--filter", "name=^traefik$"
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
def remove_container
|
|
||||||
docker :container, :prune, "--force", "--filter", "label=org.opencontainers.image.title=Traefik"
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_image
|
|
||||||
docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik"
|
|
||||||
end
|
|
||||||
|
|
||||||
def port
|
|
||||||
"#{host_port}:#{CONTAINER_PORT}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def env
|
|
||||||
Kamal::Configuration::Env.from_config \
|
|
||||||
config: config.traefik.fetch("env", {}),
|
|
||||||
secrets_file: File.join(config.host_env_directory, "traefik", "traefik.env")
|
|
||||||
end
|
|
||||||
|
|
||||||
def make_env_directory
|
|
||||||
make_directory(env.secrets_directory)
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_env_file
|
|
||||||
[ :rm, "-f", env.secrets_file ]
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def publish_args
|
|
||||||
argumentize "--publish", port unless config.traefik["publish"] == false
|
|
||||||
end
|
|
||||||
|
|
||||||
def label_args
|
|
||||||
argumentize "--label", labels
|
|
||||||
end
|
|
||||||
|
|
||||||
def env_args
|
|
||||||
env.args
|
|
||||||
end
|
|
||||||
|
|
||||||
def labels
|
|
||||||
DEFAULT_LABELS.merge(config.traefik["labels"] || {})
|
|
||||||
end
|
|
||||||
|
|
||||||
def image
|
|
||||||
config.traefik.fetch("image") { DEFAULT_IMAGE }
|
|
||||||
end
|
|
||||||
|
|
||||||
def docker_options_args
|
|
||||||
optionize(config.traefik["options"] || {})
|
|
||||||
end
|
|
||||||
|
|
||||||
def cmd_option_args
|
|
||||||
if args = config.traefik["args"]
|
|
||||||
optionize DEFAULT_ARGS.merge(args), with: "="
|
|
||||||
else
|
|
||||||
optionize DEFAULT_ARGS, with: "="
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def host_port
|
|
||||||
config.traefik["host_port"] || CONTAINER_PORT
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -6,7 +6,7 @@ require "erb"
|
|||||||
require "net/ssh/proxy/jump"
|
require "net/ssh/proxy/jump"
|
||||||
|
|
||||||
class Kamal::Configuration
|
class Kamal::Configuration
|
||||||
delegate :service, :image, :servers, :labels, :registry, :stop_wait_time, :hooks_path, :logging, to: :raw_config, allow_nil: true
|
delegate :service, :image, :port, :servers, :labels, :registry, :stop_wait_time, :hooks_path, :logging, to: :raw_config, allow_nil: true
|
||||||
delegate :argumentize, :optionize, to: Kamal::Utils
|
delegate :argumentize, :optionize, to: Kamal::Utils
|
||||||
|
|
||||||
attr_reader :destination, :raw_config
|
attr_reader :destination, :raw_config
|
||||||
@@ -107,16 +107,16 @@ class Kamal::Configuration
|
|||||||
raw_config.allow_empty_roles
|
raw_config.allow_empty_roles
|
||||||
end
|
end
|
||||||
|
|
||||||
def traefik_roles
|
def proxy_roles
|
||||||
roles.select(&:running_traefik?)
|
roles.select(&:running_proxy?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def traefik_role_names
|
def proxy_role_names
|
||||||
traefik_roles.flat_map(&:name)
|
proxy_roles.flat_map(&:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def traefik_hosts
|
def proxy_hosts
|
||||||
traefik_roles.flat_map(&:hosts).uniq
|
proxy_roles.flat_map(&:hosts).uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
def repository
|
def repository
|
||||||
@@ -174,8 +174,8 @@ class Kamal::Configuration
|
|||||||
Kamal::Configuration::Builder.new(config: self)
|
Kamal::Configuration::Builder.new(config: self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def traefik
|
def proxy
|
||||||
raw_config.traefik || {}
|
Kamal::Configuration::Proxy.new(config: self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def ssh
|
def ssh
|
||||||
@@ -187,14 +187,6 @@ class Kamal::Configuration
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def healthcheck
|
|
||||||
{ "path" => "/up", "port" => 3000, "max_attempts" => 7, "cord" => "/tmp/kamal-cord", "log_lines" => 50 }.merge(raw_config.healthcheck || {})
|
|
||||||
end
|
|
||||||
|
|
||||||
def healthcheck_service
|
|
||||||
[ "healthcheck", service, destination ].compact.join("-")
|
|
||||||
end
|
|
||||||
|
|
||||||
def readiness_delay
|
def readiness_delay
|
||||||
raw_config.readiness_delay || 7
|
raw_config.readiness_delay || 7
|
||||||
end
|
end
|
||||||
@@ -264,8 +256,7 @@ class Kamal::Configuration
|
|||||||
sshkit: sshkit.to_h,
|
sshkit: sshkit.to_h,
|
||||||
builder: builder.to_h,
|
builder: builder.to_h,
|
||||||
accessories: raw_config.accessories,
|
accessories: raw_config.accessories,
|
||||||
logging: logging_args,
|
logging: logging_args
|
||||||
healthcheck: healthcheck
|
|
||||||
}.compact
|
}.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
50
lib/kamal/configuration/proxy.rb
Normal file
50
lib/kamal/configuration/proxy.rb
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
class Kamal::Configuration::Proxy
|
||||||
|
DEFAULT_HTTP_PORT = 80
|
||||||
|
DEFAULT_HTTPS_PORT = 443
|
||||||
|
DEFAULT_IMAGE = "basecamp/kamal-proxy:latest"
|
||||||
|
|
||||||
|
delegate :argumentize, :optionize, to: Kamal::Utils
|
||||||
|
|
||||||
|
def initialize(config:)
|
||||||
|
@options = config.raw_config.proxy || {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def image
|
||||||
|
options.fetch("image", DEFAULT_IMAGE)
|
||||||
|
end
|
||||||
|
|
||||||
|
def debug?
|
||||||
|
!!options[:debug]
|
||||||
|
end
|
||||||
|
|
||||||
|
def http_port
|
||||||
|
options.fetch(:http_port, DEFAULT_HTTP_PORT)
|
||||||
|
end
|
||||||
|
|
||||||
|
def https_port
|
||||||
|
options.fetch(:http_port, DEFAULT_HTTPS_PORT)
|
||||||
|
end
|
||||||
|
|
||||||
|
def container_name
|
||||||
|
"kamal-proxy"
|
||||||
|
end
|
||||||
|
|
||||||
|
def docker_options_args
|
||||||
|
optionize(options.fetch("options", {}))
|
||||||
|
end
|
||||||
|
|
||||||
|
def publish_args
|
||||||
|
argumentize "--publish", [ *("#{http_port}:#{DEFAULT_HTTP_PORT}" if http_port), *("#{https_port}:#{DEFAULT_HTTPS_PORT}" if https_port) ]
|
||||||
|
end
|
||||||
|
|
||||||
|
def deploy_options
|
||||||
|
options.fetch(:deploy, {})
|
||||||
|
end
|
||||||
|
|
||||||
|
def deploy_command_args
|
||||||
|
optionize deploy_options
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
attr_accessor :options
|
||||||
|
end
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
class Kamal::Configuration::Role
|
class Kamal::Configuration::Role
|
||||||
CORD_FILE = "cord"
|
|
||||||
delegate :argumentize, :optionize, to: Kamal::Utils
|
delegate :argumentize, :optionize, to: Kamal::Utils
|
||||||
|
|
||||||
attr_accessor :name
|
attr_accessor :name
|
||||||
@@ -35,7 +34,7 @@ class Kamal::Configuration::Role
|
|||||||
end
|
end
|
||||||
|
|
||||||
def labels
|
def labels
|
||||||
default_labels.merge(traefik_labels).merge(custom_labels)
|
default_labels.merge(custom_labels)
|
||||||
end
|
end
|
||||||
|
|
||||||
def label_args
|
def label_args
|
||||||
@@ -69,37 +68,11 @@ class Kamal::Configuration::Role
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def health_check_args(cord: true)
|
def running_proxy?
|
||||||
if health_check_cmd.present?
|
if specializations["proxy"].nil?
|
||||||
if cord && uses_cord?
|
|
||||||
optionize({ "health-cmd" => health_check_cmd_with_cord, "health-interval" => health_check_interval })
|
|
||||||
.concat(cord_volume.docker_args)
|
|
||||||
else
|
|
||||||
optionize({ "health-cmd" => health_check_cmd, "health-interval" => health_check_interval })
|
|
||||||
end
|
|
||||||
else
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def health_check_cmd
|
|
||||||
health_check_options["cmd"] || http_health_check(port: health_check_options["port"], path: health_check_options["path"])
|
|
||||||
end
|
|
||||||
|
|
||||||
def health_check_cmd_with_cord
|
|
||||||
"(#{health_check_cmd}) && (stat #{cord_container_file} > /dev/null || exit 1)"
|
|
||||||
end
|
|
||||||
|
|
||||||
def health_check_interval
|
|
||||||
health_check_options["interval"] || "1s"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def running_traefik?
|
|
||||||
if specializations["traefik"].nil?
|
|
||||||
primary?
|
primary?
|
||||||
else
|
else
|
||||||
specializations["traefik"]
|
specializations["proxy"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -108,35 +81,6 @@ class Kamal::Configuration::Role
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def uses_cord?
|
|
||||||
running_traefik? && cord_volume && health_check_cmd.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
def cord_host_directory
|
|
||||||
File.join config.run_directory_as_docker_volume, "cords", [ container_prefix, config.run_id ].join("-")
|
|
||||||
end
|
|
||||||
|
|
||||||
def cord_volume
|
|
||||||
if (cord = health_check_options["cord"])
|
|
||||||
@cord_volume ||= Kamal::Configuration::Volume.new \
|
|
||||||
host_path: File.join(config.run_directory, "cords", [ container_prefix, config.run_id ].join("-")),
|
|
||||||
container_path: cord
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def cord_host_file
|
|
||||||
File.join cord_volume.host_path, CORD_FILE
|
|
||||||
end
|
|
||||||
|
|
||||||
def cord_container_directory
|
|
||||||
health_check_options.fetch("cord", nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
def cord_container_file
|
|
||||||
File.join cord_volume.container_path, CORD_FILE
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def container_name(version = nil)
|
def container_name(version = nil)
|
||||||
[ container_prefix, version || config.version ].compact.join("-")
|
[ container_prefix, version || config.version ].compact.join("-")
|
||||||
end
|
end
|
||||||
@@ -151,7 +95,7 @@ class Kamal::Configuration::Role
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assets?
|
def assets?
|
||||||
asset_path.present? && running_traefik?
|
asset_path.present? && running_proxy?
|
||||||
end
|
end
|
||||||
|
|
||||||
def asset_volume(version = nil)
|
def asset_volume(version = nil)
|
||||||
@@ -202,27 +146,6 @@ class Kamal::Configuration::Role
|
|||||||
{ "service" => config.service, "role" => name, "destination" => config.destination }
|
{ "service" => config.service, "role" => name, "destination" => config.destination }
|
||||||
end
|
end
|
||||||
|
|
||||||
def traefik_labels
|
|
||||||
if running_traefik?
|
|
||||||
{
|
|
||||||
# Setting a service property ensures that the generated service name will be consistent between versions
|
|
||||||
"traefik.http.services.#{traefik_service}.loadbalancer.server.scheme" => "http",
|
|
||||||
|
|
||||||
"traefik.http.routers.#{traefik_service}.rule" => "PathPrefix(`/`)",
|
|
||||||
"traefik.http.routers.#{traefik_service}.priority" => "2",
|
|
||||||
"traefik.http.middlewares.#{traefik_service}-retry.retry.attempts" => "5",
|
|
||||||
"traefik.http.middlewares.#{traefik_service}-retry.retry.initialinterval" => "500ms",
|
|
||||||
"traefik.http.routers.#{traefik_service}.middlewares" => "#{traefik_service}-retry@docker"
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def traefik_service
|
|
||||||
container_prefix
|
|
||||||
end
|
|
||||||
|
|
||||||
def custom_labels
|
def custom_labels
|
||||||
Hash.new.tap do |labels|
|
Hash.new.tap do |labels|
|
||||||
labels.merge!(config.labels) if config.labels.present?
|
labels.merge!(config.labels) if config.labels.present?
|
||||||
@@ -248,16 +171,4 @@ class Kamal::Configuration::Role
|
|||||||
config: config.env,
|
config: config.env,
|
||||||
secrets_file: File.join(config.host_env_directory, "roles", "#{container_prefix}.env")
|
secrets_file: File.join(config.host_env_directory, "roles", "#{container_prefix}.env")
|
||||||
end
|
end
|
||||||
|
|
||||||
def http_health_check(port:, path:)
|
|
||||||
"curl -f #{URI.join("http://localhost:#{port}", path)} || exit 1" if path.present? || port.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
def health_check_options
|
|
||||||
@health_check_options ||= begin
|
|
||||||
options = specializations["healthcheck"] || {}
|
|
||||||
options = config.healthcheck.merge(options) if running_traefik?
|
|
||||||
options
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,28 +11,20 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "boot will rename if same version is already running" do
|
test "boot will rename if same version is already running" do
|
||||||
Object.any_instance.stubs(:sleep)
|
|
||||||
run_command("details") # Preheat Kamal const
|
run_command("details") # Preheat Kamal const
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
||||||
.returns("12345678") # running version
|
.returns("12345678") # running version
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("running") # health check
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
||||||
.returns("123") # old version
|
.returns("123") # old version
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :inspect, "-f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}'", "app-web-123", "|", :awk, "'$2 == \"/tmp/kamal-cord\" {print $1}'", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
.returns("cordfile") # old version
|
.returns("172.1.0.2:80")
|
||||||
|
.at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("unhealthy") # old version unhealthy
|
|
||||||
|
|
||||||
run_command("boot").tap do |output|
|
run_command("boot").tap do |output|
|
||||||
assert_match /Renaming container .* to .* as already deployed on 1.1.1.1/, output # Rename
|
assert_match /Renaming container .* to .* as already deployed on 1.1.1.1/, output # Rename
|
||||||
@@ -65,22 +57,16 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "boot with assets" do
|
test "boot with assets" do
|
||||||
Object.any_instance.stubs(:sleep)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info) .with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
|
||||||
.returns("12345678") # running version
|
.returns("12345678") # running version
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("running") # health check
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
||||||
.returns("123").twice # old version
|
.returns("123").twice # old version
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :inspect, "-f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}'", "app-web-123", "|", :awk, "'$2 == \"/tmp/kamal-cord\" {print $1}'", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
.returns("") # old version
|
.returns("172.1.0.2:80").at_least_once
|
||||||
|
|
||||||
run_command("boot", config: :with_assets).tap do |output|
|
run_command("boot", config: :with_assets).tap do |output|
|
||||||
assert_match "docker tag dhh/app:latest dhh/app:latest", output
|
assert_match "docker tag dhh/app:latest dhh/app:latest", output
|
||||||
@@ -93,23 +79,17 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "boot with host tags" do
|
test "boot with host tags" do
|
||||||
Object.any_instance.stubs(:sleep)
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
||||||
.returns("12345678") # running version
|
.returns("12345678") # running version
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("running") # health check
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
||||||
.returns("123") # old version
|
.returns("123") # old version
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :inspect, "-f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}'", "app-web-123", "|", :awk, "'$2 == \"/tmp/kamal-cord\" {print $1}'", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
.returns("") # old version
|
.returns("172.1.0.2:80").at_least_once
|
||||||
|
|
||||||
run_command("boot", config: :with_env_tags).tap do |output|
|
run_command("boot", config: :with_env_tags).tap do |output|
|
||||||
assert_match "docker tag dhh/app:latest dhh/app:latest", output
|
assert_match "docker tag dhh/app:latest dhh/app:latest", output
|
||||||
@@ -119,21 +99,11 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "boot with web barrier opened" do
|
test "boot with web barrier opened" do
|
||||||
Object.any_instance.stubs(:sleep)
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("")
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("123") # old version
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
.returns("running").at_least_once # web health check passing
|
.returns("172.1.0.2:80").at_least_once
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("unhealthy").at_least_once # web health check failing
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-workers-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("running").at_least_once # workers health check
|
|
||||||
|
|
||||||
run_command("boot", config: :with_roles, host: nil).tap do |output|
|
run_command("boot", config: :with_roles, host: nil).tap do |output|
|
||||||
assert_match "Waiting for the first healthy web container before booting workers on 1.1.1.3...", output
|
assert_match "Waiting for the first healthy web container before booting workers on 1.1.1.3...", output
|
||||||
@@ -146,22 +116,35 @@ class CliAppTest < CliTestCase
|
|||||||
test "boot with web barrier closed" do
|
test "boot with web barrier closed" do
|
||||||
Thread.report_on_exception = false
|
Thread.report_on_exception = false
|
||||||
|
|
||||||
Object.any_instance.stubs(:sleep)
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("")
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("123") # old version
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", raise_on_non_zero_exit: false)
|
||||||
.returns("unhealthy").at_least_once # web health check failing
|
.returns("abcdef123456")
|
||||||
|
.twice # web container id
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-workers-latest$", "--quiet", raise_on_non_zero_exit: false)
|
||||||
|
.returns("abcdef123456")
|
||||||
|
.twice # worker container id
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info).with { |*args| args[0..1] == [ :sh, "-c" ] }.returns("123").at_least_once
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
|
.returns("172.1.0.2:80")
|
||||||
|
.at_least_once
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).returns("")
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, :exec, "kamal-proxy", "kamal-proxy", :deploy, "app-web", "--target", "\"172.1.0.2:80\"").raises(SSHKit::Command::Failed, "Deploy failed").at_least_once
|
||||||
|
|
||||||
stderred do
|
stderred do
|
||||||
run_command("boot", config: :with_roles, host: nil, allow_execute_error: true).tap do |output|
|
run_command("boot", config: :with_roles, host: nil, allowed_error_message: "Deploy failed").tap do |output|
|
||||||
assert_match "Waiting for the first healthy web container before booting workers on 1.1.1.3...", output
|
assert_match "Waiting for the first healthy web container before booting workers on 1.1.1.3...", output
|
||||||
assert_match "Waiting for the first healthy web container before booting workers on 1.1.1.4...", output
|
assert_match "Waiting for the first healthy web container before booting workers on 1.1.1.4...", output
|
||||||
assert_match "First web container is unhealthy, not booting workers on 1.1.1.3", output
|
assert_match "First web container is unhealthy, not booting workers on 1.1.1.3", output
|
||||||
assert_match "First web container is unhealthy, not booting workers on 1.1.1.4", output
|
assert_match "First web container is unhealthy, not booting workers on 1.1.1.4", output
|
||||||
assert_match "Running docker container ls --all --filter name=^app-web-latest$ --quiet | xargs docker stop on 1.1.1.1", output
|
|
||||||
assert_match "Running docker container ls --all --filter name=^app-web-latest$ --quiet | xargs docker stop on 1.1.1.2", output
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
@@ -169,6 +152,17 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "start" do
|
test "start" do
|
||||||
|
# SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("")
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
|
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
||||||
|
.returns("123") # current version
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
|
.returns("172.1.0.2:80")
|
||||||
|
.at_least_once
|
||||||
|
|
||||||
run_command("start").tap do |output|
|
run_command("start").tap do |output|
|
||||||
assert_match "docker start app-web-999", output
|
assert_match "docker start app-web-999", output
|
||||||
end
|
end
|
||||||
@@ -333,25 +327,15 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def run_command(*command, config: :with_accessories, host: "1.1.1.1", allow_execute_error: false)
|
def run_command(*command, config: :with_accessories, host: "1.1.1.1", allowed_error_message: nil)
|
||||||
stdouted do
|
stdouted do
|
||||||
Kamal::Cli::App.start([ *command, "-c", "test/fixtures/deploy_#{config}.yml", *([ "--hosts", host ] if host) ])
|
Kamal::Cli::App.start([ *command, "-c", "test/fixtures/deploy_#{config}.yml", *([ "--hosts", host ] if host) ])
|
||||||
rescue SSHKit::Runner::ExecuteError => e
|
rescue SSHKit::Runner::ExecuteError => e
|
||||||
raise e unless allow_execute_error
|
raise e unless allowed_error_message && e.message.include?(allowed_error_message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def stub_running
|
def stub_running
|
||||||
Object.any_instance.stubs(:sleep)
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("123") # old version
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("123") # old version
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-latest$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("running") # health check
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("unhealthy") # health check
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,14 +4,11 @@ class CliEnvTest < CliTestCase
|
|||||||
test "push" do
|
test "push" do
|
||||||
run_command("push").tap do |output|
|
run_command("push").tap do |output|
|
||||||
assert_match "Running /usr/bin/env mkdir -p .kamal/env/roles on 1.1.1.1", output
|
assert_match "Running /usr/bin/env mkdir -p .kamal/env/roles on 1.1.1.1", output
|
||||||
assert_match "Running /usr/bin/env mkdir -p .kamal/env/traefik on 1.1.1.1", output
|
|
||||||
assert_match "Running /usr/bin/env mkdir -p .kamal/env/accessories on 1.1.1.1", output
|
assert_match "Running /usr/bin/env mkdir -p .kamal/env/accessories on 1.1.1.1", output
|
||||||
assert_match "Running /usr/bin/env mkdir -p .kamal/env/roles on 1.1.1.1", output
|
assert_match "Running /usr/bin/env mkdir -p .kamal/env/roles on 1.1.1.1", output
|
||||||
assert_match "Running /usr/bin/env mkdir -p .kamal/env/traefik on 1.1.1.2", output
|
|
||||||
assert_match "Running /usr/bin/env mkdir -p .kamal/env/accessories on 1.1.1.1", output
|
assert_match "Running /usr/bin/env mkdir -p .kamal/env/accessories on 1.1.1.1", output
|
||||||
assert_match ".kamal/env/roles/app-web.env", output
|
assert_match ".kamal/env/roles/app-web.env", output
|
||||||
assert_match ".kamal/env/roles/app-workers.env", output
|
assert_match ".kamal/env/roles/app-workers.env", output
|
||||||
assert_match ".kamal/env/traefik/traefik.env", output
|
|
||||||
assert_match ".kamal/env/accessories/app-redis.env", output
|
assert_match ".kamal/env/accessories/app-redis.env", output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -22,8 +19,6 @@ class CliEnvTest < CliTestCase
|
|||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/roles/app-web.env on 1.1.1.2", output
|
assert_match "Running /usr/bin/env rm -f .kamal/env/roles/app-web.env on 1.1.1.2", output
|
||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/roles/app-workers.env on 1.1.1.3", output
|
assert_match "Running /usr/bin/env rm -f .kamal/env/roles/app-workers.env on 1.1.1.3", output
|
||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/roles/app-workers.env on 1.1.1.4", output
|
assert_match "Running /usr/bin/env rm -f .kamal/env/roles/app-workers.env on 1.1.1.4", output
|
||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/traefik/traefik.env on 1.1.1.1", output
|
|
||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/traefik/traefik.env on 1.1.1.2", output
|
|
||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/accessories/app-redis.env on 1.1.1.1", output
|
assert_match "Running /usr/bin/env rm -f .kamal/env/accessories/app-redis.env on 1.1.1.1", output
|
||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/accessories/app-redis.env on 1.1.1.2", output
|
assert_match "Running /usr/bin/env rm -f .kamal/env/accessories/app-redis.env on 1.1.1.2", output
|
||||||
assert_match "Running /usr/bin/env rm -f .kamal/env/accessories/app-mysql.env on 1.1.1.3", output
|
assert_match "Running /usr/bin/env rm -f .kamal/env/accessories/app-mysql.env on 1.1.1.3", output
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class CliMainTest < CliTestCase
|
|||||||
# deploy
|
# deploy
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:pull", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:pull", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
||||||
@@ -36,7 +36,7 @@ class CliMainTest < CliTestCase
|
|||||||
assert_match /Acquiring the deploy lock/, output
|
assert_match /Acquiring the deploy lock/, output
|
||||||
assert_match /Log into image registry/, output
|
assert_match /Log into image registry/, output
|
||||||
assert_match /Pull app image/, output
|
assert_match /Pull app image/, output
|
||||||
assert_match /Ensure Traefik is running/, output
|
assert_match /Ensure proxy is running/, output
|
||||||
assert_match /Detect stale containers/, output
|
assert_match /Detect stale containers/, output
|
||||||
assert_match /Prune old containers and images/, output
|
assert_match /Prune old containers and images/, output
|
||||||
assert_match /Releasing the deploy lock/, output
|
assert_match /Releasing the deploy lock/, output
|
||||||
@@ -48,7 +48,7 @@ class CliMainTest < CliTestCase
|
|||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
||||||
@@ -61,7 +61,7 @@ class CliMainTest < CliTestCase
|
|||||||
assert_match /Log into image registry/, output
|
assert_match /Log into image registry/, output
|
||||||
assert_match /Build and push app image/, output
|
assert_match /Build and push app image/, output
|
||||||
assert_hook_ran "pre-deploy", output, **hook_variables
|
assert_hook_ran "pre-deploy", output, **hook_variables
|
||||||
assert_match /Ensure Traefik is running/, output
|
assert_match /Ensure proxy is running/, output
|
||||||
assert_match /Detect stale containers/, output
|
assert_match /Detect stale containers/, output
|
||||||
assert_match /Prune old containers and images/, output
|
assert_match /Prune old containers and images/, output
|
||||||
assert_hook_ran "post-deploy", output, **hook_variables, runtime: true
|
assert_hook_ran "post-deploy", output, **hook_variables, runtime: true
|
||||||
@@ -73,7 +73,7 @@ class CliMainTest < CliTestCase
|
|||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:pull", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:pull", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
||||||
@@ -82,7 +82,7 @@ class CliMainTest < CliTestCase
|
|||||||
assert_match /Acquiring the deploy lock/, output
|
assert_match /Acquiring the deploy lock/, output
|
||||||
assert_match /Log into image registry/, output
|
assert_match /Log into image registry/, output
|
||||||
assert_match /Pull app image/, output
|
assert_match /Pull app image/, output
|
||||||
assert_match /Ensure Traefik is running/, output
|
assert_match /Ensure proxy is running/, output
|
||||||
assert_match /Detect stale containers/, output
|
assert_match /Detect stale containers/, output
|
||||||
assert_match /Prune old containers and images/, output
|
assert_match /Prune old containers and images/, output
|
||||||
assert_match /Releasing the deploy lock/, output
|
assert_match /Releasing the deploy lock/, output
|
||||||
@@ -169,7 +169,7 @@ class CliMainTest < CliTestCase
|
|||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
||||||
@@ -179,27 +179,12 @@ class CliMainTest < CliTestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "deploy without healthcheck if primary host doesn't have traefik" do
|
|
||||||
invoke_options = { "config_file" => "test/fixtures/deploy_workers_only.yml", "version" => "999", "skip_hooks" => false }
|
|
||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:healthcheck:perform", [], invoke_options).never
|
|
||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:boot", [], invoke_options)
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
|
||||||
|
|
||||||
run_command("deploy", config_file: "deploy_workers_only")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "deploy with missing secrets" do
|
test "deploy with missing secrets" do
|
||||||
invoke_options = { "config_file" => "test/fixtures/deploy_with_secrets.yml", "version" => "999", "skip_hooks" => false }
|
invoke_options = { "config_file" => "test/fixtures/deploy_with_secrets.yml", "version" => "999", "skip_hooks" => false }
|
||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
|
||||||
@@ -251,7 +236,6 @@ class CliMainTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "rollback good version" do
|
test "rollback good version" do
|
||||||
Object.any_instance.stubs(:sleep)
|
|
||||||
[ "web", "workers" ].each do |role|
|
[ "web", "workers" ].each do |role|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-#{role}-123$", "--quiet", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-#{role}-123$", "--quiet", raise_on_non_zero_exit: false)
|
||||||
@@ -262,18 +246,11 @@ class CliMainTest < CliTestCase
|
|||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=#{role} --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=#{role} --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-#{role}-}; done", raise_on_non_zero_exit: false)
|
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=#{role} --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=#{role} --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-#{role}-}; done", raise_on_non_zero_exit: false)
|
||||||
.returns("version-to-rollback\n").at_least_once
|
.returns("version-to-rollback\n").at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-#{role}-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("running").at_least_once # health check
|
|
||||||
end
|
end
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :inspect, "-f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}'", "app-web-version-to-rollback", "|", :awk, "'$2 == \"/tmp/kamal-cord\" {print $1}'", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
.returns("corddirectory").at_least_once # health check
|
.returns("172.1.0.2:80").at_least_once
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-version-to-rollback$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("unhealthy").at_least_once # health check
|
|
||||||
|
|
||||||
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
|
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
|
||||||
hook_variables = { version: 123, service_version: "app@123", hosts: "1.1.1.1,1.1.1.2,1.1.1.3,1.1.1.4", command: "rollback" }
|
hook_variables = { version: 123, service_version: "app@123", hosts: "1.1.1.1,1.1.1.2,1.1.1.3,1.1.1.4", command: "rollback" }
|
||||||
@@ -290,8 +267,6 @@ class CliMainTest < CliTestCase
|
|||||||
test "rollback without old version" do
|
test "rollback without old version" do
|
||||||
Kamal::Cli::Main.any_instance.stubs(:container_available?).returns(true)
|
Kamal::Cli::Main.any_instance.stubs(:container_available?).returns(true)
|
||||||
|
|
||||||
Kamal::Cli::Healthcheck::Poller.stubs(:sleep)
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", raise_on_non_zero_exit: false)
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", raise_on_non_zero_exit: false)
|
||||||
.returns("").at_least_once
|
.returns("").at_least_once
|
||||||
@@ -299,8 +274,8 @@ class CliMainTest < CliTestCase
|
|||||||
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
.with(:sh, "-c", "'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting'", "|", :head, "-1", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)
|
||||||
.returns("").at_least_once
|
.returns("").at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
.returns("running").at_least_once # health check
|
.returns("127.1.0.4:80").at_least_once
|
||||||
|
|
||||||
run_command("rollback", "123").tap do |output|
|
run_command("rollback", "123").tap do |output|
|
||||||
assert_match "docker run --detach --restart unless-stopped --name app-web-123", output
|
assert_match "docker run --detach --restart unless-stopped --name app-web-123", output
|
||||||
@@ -309,7 +284,7 @@ class CliMainTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "details" do
|
test "details" do
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:details")
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:details")
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:details")
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:details")
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:details", [ "all" ])
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:details", [ "all" ])
|
||||||
|
|
||||||
@@ -478,9 +453,9 @@ class CliMainTest < CliTestCase
|
|||||||
|
|
||||||
test "remove with confirmation" do
|
test "remove with confirmation" do
|
||||||
run_command("remove", "-y", config_file: "deploy_with_accessories").tap do |output|
|
run_command("remove", "-y", config_file: "deploy_with_accessories").tap do |output|
|
||||||
assert_match /docker container stop traefik/, output
|
assert_match /docker container stop kamal-proxy/, output
|
||||||
assert_match /docker container prune --force --filter label=org.opencontainers.image.title=Traefik/, output
|
assert_match /docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy/, output
|
||||||
assert_match /docker image prune --all --force --filter label=org.opencontainers.image.title=Traefik/, output
|
assert_match /docker image prune --all --force --filter label=org.opencontainers.image.title=kamal-proxy/, output
|
||||||
|
|
||||||
assert_match /docker ps --quiet --filter label=service=app | xargs docker stop/, output
|
assert_match /docker ps --quiet --filter label=service=app | xargs docker stop/, output
|
||||||
assert_match /docker container prune --force --filter label=service=app/, output
|
assert_match /docker container prune --force --filter label=service=app/, output
|
||||||
|
|||||||
116
test/cli/proxy_test.rb
Normal file
116
test/cli/proxy_test.rb
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
require_relative "cli_test_case"
|
||||||
|
|
||||||
|
class CliProxyTest < CliTestCase
|
||||||
|
test "boot" do
|
||||||
|
run_command("boot").tap do |output|
|
||||||
|
assert_match "docker login", output
|
||||||
|
assert_match "docker run --name kamal-proxy --detach --restart unless-stopped --publish 80:80 --publish 443:443 --volume /var/run/docker.sock:/var/run/docker.sock --volume kamal-proxy:/root/.config/kamal-proxy --log-opt max-size=\"10m\" #{Kamal::Configuration::Proxy::DEFAULT_IMAGE}", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "reboot" do
|
||||||
|
Kamal::Commands::Registry.any_instance.expects(:login).twice
|
||||||
|
|
||||||
|
run_command("reboot", "-y").tap do |output|
|
||||||
|
assert_match "docker container stop kamal-proxy", output
|
||||||
|
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy", output
|
||||||
|
assert_match "docker run --name kamal-proxy --detach --restart unless-stopped --publish 80:80 --publish 443:443 --volume /var/run/docker.sock:/var/run/docker.sock --volume kamal-proxy:/root/.config/kamal-proxy --log-opt max-size=\"10m\" #{Kamal::Configuration::Proxy::DEFAULT_IMAGE}", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "reboot --rolling" do
|
||||||
|
run_command("reboot", "--rolling", "-y").tap do |output|
|
||||||
|
assert_match "Running docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy on 1.1.1.1", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "start" do
|
||||||
|
run_command("start").tap do |output|
|
||||||
|
assert_match "docker container start kamal-proxy", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "stop" do
|
||||||
|
run_command("stop").tap do |output|
|
||||||
|
assert_match "docker container stop kamal-proxy", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "restart" do
|
||||||
|
Kamal::Cli::Proxy.any_instance.expects(:stop)
|
||||||
|
Kamal::Cli::Proxy.any_instance.expects(:start)
|
||||||
|
|
||||||
|
run_command("restart")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "details" do
|
||||||
|
run_command("details").tap do |output|
|
||||||
|
assert_match "docker ps --filter name=^kamal-proxy$", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "logs" do
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||||
|
.with(:docker, :logs, "kamal-proxy", " --tail 100", "--timestamps", "2>&1")
|
||||||
|
.returns("Log entry")
|
||||||
|
|
||||||
|
run_command("logs").tap do |output|
|
||||||
|
assert_match "Proxy Host: 1.1.1.1", output
|
||||||
|
assert_match "Log entry", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "logs with follow" do
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
|
||||||
|
.with("ssh -t root@1.1.1.1 -p 22 'docker logs kamal-proxy --timestamps --tail 10 --follow 2>&1'")
|
||||||
|
|
||||||
|
assert_match "docker logs kamal-proxy --timestamps --tail 10 --follow", run_command("logs", "--follow")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "remove" do
|
||||||
|
Kamal::Cli::Proxy.any_instance.expects(:stop)
|
||||||
|
Kamal::Cli::Proxy.any_instance.expects(:remove_container)
|
||||||
|
Kamal::Cli::Proxy.any_instance.expects(:remove_image)
|
||||||
|
|
||||||
|
run_command("remove")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "remove_container" do
|
||||||
|
run_command("remove_container").tap do |output|
|
||||||
|
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "remove_image" do
|
||||||
|
run_command("remove_image").tap do |output|
|
||||||
|
assert_match "docker image prune --all --force --filter label=org.opencontainers.image.title=kamal-proxy", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update" do
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
|
.with(:docker, :container, :ls, "--all", "--filter", "name=^app-web-123$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'", "|", :sed, "-e", "'s/\\/tcp$//'")
|
||||||
|
.returns("172.1.0.2:80")
|
||||||
|
.at_least_once
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
|
||||||
|
.with { |*args| args[0..1] == [ :sh, "-c" ] }
|
||||||
|
.returns("123")
|
||||||
|
.at_least_once
|
||||||
|
|
||||||
|
run_command("update", "-y").tap do |output|
|
||||||
|
assert_match "docker container stop traefik", output
|
||||||
|
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=traefik", output
|
||||||
|
assert_match "docker image prune --all --force --filter label=org.opencontainers.image.title=traefik", output
|
||||||
|
assert_match "docker container stop kamal-proxy", output
|
||||||
|
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy", output
|
||||||
|
assert_match "docker run --name kamal-proxy --detach --restart unless-stopped --publish 80:80 --publish 443:443 --volume /var/run/docker.sock:/var/run/docker.sock --volume kamal-proxy:/root/.config/kamal-proxy --log-opt max-size=\"10m\" #{Kamal::Configuration::Proxy::DEFAULT_IMAGE}", output
|
||||||
|
assert_match "docker exec kamal-proxy kamal-proxy deploy app-web --target \"172.1.0.2:80\"", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def run_command(*command)
|
||||||
|
stdouted { Kamal::Cli::Proxy.start([ *command, "-c", "test/fixtures/deploy_with_accessories.yml" ]) }
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -18,12 +18,10 @@ class CliPruneTest < CliTestCase
|
|||||||
test "containers" do
|
test "containers" do
|
||||||
run_command("containers").tap do |output|
|
run_command("containers").tap do |output|
|
||||||
assert_match /docker ps -q -a --filter label=service=app --filter status=created --filter status=exited --filter status=dead | tail -n +6 | while read container_id; do docker rm $container_id; done on 1.1.1.\d/, output
|
assert_match /docker ps -q -a --filter label=service=app --filter status=created --filter status=exited --filter status=dead | tail -n +6 | while read container_id; do docker rm $container_id; done on 1.1.1.\d/, output
|
||||||
assert_match /docker container prune --force --filter label=service=healthcheck-app on 1.1.1.\d/, output
|
|
||||||
end
|
end
|
||||||
|
|
||||||
run_command("containers", "--retain", "10").tap do |output|
|
run_command("containers", "--retain", "10").tap do |output|
|
||||||
assert_match /docker ps -q -a --filter label=service=app --filter status=created --filter status=exited --filter status=dead | tail -n +11 | while read container_id; do docker rm $container_id; done on 1.1.1.\d/, output
|
assert_match /docker ps -q -a --filter label=service=app --filter status=created --filter status=exited --filter status=dead | tail -n +11 | while read container_id; do docker rm $container_id; done on 1.1.1.\d/, output
|
||||||
assert_match /docker container prune --force --filter label=service=healthcheck-app on 1.1.1.\d/, output
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_raises(RuntimeError, "retain must be at least 1") do
|
assert_raises(RuntimeError, "retain must be at least 1") do
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
require_relative "cli_test_case"
|
|
||||||
|
|
||||||
class CliTraefikTest < CliTestCase
|
|
||||||
test "boot" do
|
|
||||||
run_command("boot").tap do |output|
|
|
||||||
assert_match "docker login", output
|
|
||||||
assert_match "docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{Kamal::Commands::Traefik::DEFAULT_IMAGE} --providers.docker --log.level=\"DEBUG\"", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "reboot" do
|
|
||||||
Kamal::Commands::Registry.any_instance.expects(:login).twice
|
|
||||||
|
|
||||||
run_command("reboot", "-y").tap do |output|
|
|
||||||
assert_match "docker container stop traefik", output
|
|
||||||
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=Traefik", output
|
|
||||||
assert_match "docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{Kamal::Commands::Traefik::DEFAULT_IMAGE} --providers.docker --log.level=\"DEBUG\"", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "reboot --rolling" do
|
|
||||||
Object.any_instance.stubs(:sleep)
|
|
||||||
|
|
||||||
run_command("reboot", "--rolling", "-y").tap do |output|
|
|
||||||
assert_match "Running docker container prune --force --filter label=org.opencontainers.image.title=Traefik on 1.1.1.1", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "start" do
|
|
||||||
run_command("start").tap do |output|
|
|
||||||
assert_match "docker container start traefik", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "stop" do
|
|
||||||
run_command("stop").tap do |output|
|
|
||||||
assert_match "docker container stop traefik", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "restart" do
|
|
||||||
Kamal::Cli::Traefik.any_instance.expects(:stop)
|
|
||||||
Kamal::Cli::Traefik.any_instance.expects(:start)
|
|
||||||
|
|
||||||
run_command("restart")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "details" do
|
|
||||||
run_command("details").tap do |output|
|
|
||||||
assert_match "docker ps --filter name=^traefik$", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "logs" do
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
|
||||||
.with(:docker, :logs, "traefik", " --tail 100", "--timestamps", "2>&1")
|
|
||||||
.returns("Log entry")
|
|
||||||
|
|
||||||
run_command("logs").tap do |output|
|
|
||||||
assert_match "Traefik Host: 1.1.1.1", output
|
|
||||||
assert_match "Log entry", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "logs with follow" do
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
|
|
||||||
.with("ssh -t root@1.1.1.1 -p 22 'docker logs traefik --timestamps --tail 10 --follow 2>&1'")
|
|
||||||
|
|
||||||
assert_match "docker logs traefik --timestamps --tail 10 --follow", run_command("logs", "--follow")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "remove" do
|
|
||||||
Kamal::Cli::Traefik.any_instance.expects(:stop)
|
|
||||||
Kamal::Cli::Traefik.any_instance.expects(:remove_container)
|
|
||||||
Kamal::Cli::Traefik.any_instance.expects(:remove_image)
|
|
||||||
|
|
||||||
run_command("remove")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "remove_container" do
|
|
||||||
run_command("remove_container").tap do |output|
|
|
||||||
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=Traefik", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "remove_image" do
|
|
||||||
run_command("remove_image").tap do |output|
|
|
||||||
assert_match "docker image prune --all --force --filter label=org.opencontainers.image.title=Traefik", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def run_command(*command)
|
|
||||||
stdouted { Kamal::Cli::Traefik.start([ *command, "-c", "test/fixtures/deploy_with_accessories.yml" ]) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -136,18 +136,18 @@ class CommanderTest < ActiveSupport::TestCase
|
|||||||
assert_equal [ "1.1.1.3", "1.1.1.4", "1.1.1.1", "1.1.1.2" ], @kamal.hosts
|
assert_equal [ "1.1.1.3", "1.1.1.4", "1.1.1.1", "1.1.1.2" ], @kamal.hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
test "traefik hosts should observe filtered roles" do
|
test "proxy hosts should observe filtered roles" do
|
||||||
configure_with(:deploy_with_aliases)
|
configure_with(:deploy_with_aliases)
|
||||||
|
|
||||||
@kamal.specific_roles = [ "web_tokyo" ]
|
@kamal.specific_roles = [ "web_tokyo" ]
|
||||||
assert_equal [ "1.1.1.3", "1.1.1.4" ], @kamal.traefik_hosts
|
assert_equal [ "1.1.1.3", "1.1.1.4" ], @kamal.proxy_hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
test "traefik hosts should observe filtered hosts" do
|
test "proxy hosts should observe filtered hosts" do
|
||||||
configure_with(:deploy_with_aliases)
|
configure_with(:deploy_with_aliases)
|
||||||
|
|
||||||
@kamal.specific_hosts = [ "1.1.1.4" ]
|
@kamal.specific_hosts = [ "1.1.1.4" ]
|
||||||
assert_equal [ "1.1.1.4" ], @kamal.traefik_hosts
|
assert_equal [ "1.1.1.4" ], @kamal.proxy_hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
test "run" do
|
test "run" do
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(curl -f http://localhost:3000/up || exit 1) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999",
|
||||||
new_command.run.join(" ")
|
new_command.run.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "run with hostname" do
|
test "run with hostname" do
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 --hostname myhost -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(curl -f http://localhost:3000/up || exit 1) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
"docker run --detach --restart unless-stopped --name app-web-999 --hostname myhost -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999",
|
||||||
new_command.run(hostname: "myhost").join(" ")
|
new_command.run(hostname: "myhost").join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -28,31 +28,7 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
@config[:volumes] = [ "/local/path:/container/path" ]
|
@config[:volumes] = [ "/local/path:/container/path" ]
|
||||||
|
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(curl -f http://localhost:3000/up || exit 1) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-opt max-size=\"10m\" --volume /local/path:/container/path --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --log-opt max-size=\"10m\" --volume /local/path:/container/path --label service=\"app\" --label role=\"web\" --label destination dhh/app:999",
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with custom healthcheck path" do
|
|
||||||
@config[:healthcheck] = { "path" => "/healthz" }
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(curl -f http://localhost:3000/healthz || exit 1) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with custom healthcheck command" do
|
|
||||||
@config[:healthcheck] = { "cmd" => "/bin/up" }
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(/bin/up) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with role-specific healthcheck options" do
|
|
||||||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "healthcheck" => { "cmd" => "/bin/healthy" } } }
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(/bin/healthy) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
|
||||||
new_command.run.join(" ")
|
new_command.run.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -67,7 +43,7 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } }
|
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } }
|
||||||
|
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(curl -f http://localhost:3000/up || exit 1) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999",
|
||||||
new_command.run.join(" ")
|
new_command.run.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -76,7 +52,7 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "logging" => { "driver" => "local", "options" => { "max-size" => "100m" } } } }
|
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "logging" => { "driver" => "local", "options" => { "max-size" => "100m" } } } }
|
||||||
|
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --health-cmd \"(curl -f http://localhost:3000/up || exit 1) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999",
|
||||||
new_command.run.join(" ")
|
new_command.run.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -85,7 +61,7 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
@config[:env]["tags"] = { "tag1" => { "ENV1" => "value1" } }
|
@config[:env]["tags"] = { "tag1" => { "ENV1" => "value1" } }
|
||||||
|
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --env ENV1=\"value1\" --health-cmd \"(curl -f http://localhost:3000/up || exit 1) && (stat /tmp/kamal-cord/cord > /dev/null || exit 1)\" --health-interval \"1s\" --volume $(pwd)/.kamal/cords/app-web-12345678901234567890123456789012:/tmp/kamal-cord --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination --label traefik.http.services.app-web.loadbalancer.server.scheme=\"http\" --label traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.app-web.priority=\"2\" --label traefik.http.middlewares.app-web-retry.retry.attempts=\"5\" --label traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\" --label traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\" dhh/app:999",
|
"docker run --detach --restart unless-stopped --name app-web-999 -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-web.env --env ENV1=\"value1\" --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999",
|
||||||
new_command.run.join(" ")
|
new_command.run.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -398,20 +374,6 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
assert_equal "rm -f .kamal/env/roles/app-web.env", new_command.remove_env_file.join(" ")
|
assert_equal "rm -f .kamal/env/roles/app-web.env", new_command.remove_env_file.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "cord" do
|
|
||||||
assert_equal "docker inspect -f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}' app-web-123 | awk '$2 == \"/tmp/kamal-cord\" {print $1}'", new_command.cord(version: 123).join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "tie cord" do
|
|
||||||
assert_equal "mkdir -p . ; touch cordfile", new_command.tie_cord("cordfile").join(" ")
|
|
||||||
assert_equal "mkdir -p corddir ; touch corddir/cordfile", new_command.tie_cord("corddir/cordfile").join(" ")
|
|
||||||
assert_equal "mkdir -p /corddir ; touch /corddir/cordfile", new_command.tie_cord("/corddir/cordfile").join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "cut cord" do
|
|
||||||
assert_equal "rm -r corddir", new_command.cut_cord("corddir").join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "extract assets" do
|
test "extract assets" do
|
||||||
assert_equal [
|
assert_equal [
|
||||||
:mkdir, "-p", ".kamal/assets/extracted/app-web-999", "&&",
|
:mkdir, "-p", ".kamal/assets/extracted/app-web-999", "&&",
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ class CommandsHookTest < ActiveSupport::TestCase
|
|||||||
freeze_time
|
freeze_time
|
||||||
|
|
||||||
@config = {
|
@config = {
|
||||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ]
|
||||||
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@performer = `whoami`.strip
|
@performer = `whoami`.strip
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ require "test_helper"
|
|||||||
class CommandsLockTest < ActiveSupport::TestCase
|
class CommandsLockTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
@config = {
|
@config = {
|
||||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ]
|
||||||
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
126
test/commands/proxy_test.rb
Normal file
126
test/commands/proxy_test.rb
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class CommandsProxyTest < ActiveSupport::TestCase
|
||||||
|
setup do
|
||||||
|
@config = {
|
||||||
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
ENV["EXAMPLE_API_KEY"] = "456"
|
||||||
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
ENV.delete("EXAMPLE_API_KEY")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "run" do
|
||||||
|
assert_equal \
|
||||||
|
"docker run --name kamal-proxy --detach --restart unless-stopped --publish 80:80 --publish 443:443 --volume /var/run/docker.sock:/var/run/docker.sock --volume kamal-proxy:/root/.config/kamal-proxy --log-opt max-size=\"10m\" #{Kamal::Configuration::Proxy::DEFAULT_IMAGE}",
|
||||||
|
new_command.run.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "run with ports configured" do
|
||||||
|
assert_equal \
|
||||||
|
"docker run --name kamal-proxy --detach --restart unless-stopped --publish 80:80 --publish 443:443 --volume /var/run/docker.sock:/var/run/docker.sock --volume kamal-proxy:/root/.config/kamal-proxy --log-opt max-size=\"10m\" #{Kamal::Configuration::Proxy::DEFAULT_IMAGE}",
|
||||||
|
new_command.run.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "run without configuration" do
|
||||||
|
@config.delete(:proxy)
|
||||||
|
|
||||||
|
assert_equal \
|
||||||
|
"docker run --name kamal-proxy --detach --restart unless-stopped --publish 80:80 --publish 443:443 --volume /var/run/docker.sock:/var/run/docker.sock --volume kamal-proxy:/root/.config/kamal-proxy --log-opt max-size=\"10m\" #{Kamal::Configuration::Proxy::DEFAULT_IMAGE}",
|
||||||
|
new_command.run.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "run with logging config" do
|
||||||
|
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } }
|
||||||
|
|
||||||
|
assert_equal \
|
||||||
|
"docker run --name kamal-proxy --detach --restart unless-stopped --publish 80:80 --publish 443:443 --volume /var/run/docker.sock:/var/run/docker.sock --volume kamal-proxy:/root/.config/kamal-proxy --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" #{Kamal::Configuration::Proxy::DEFAULT_IMAGE}",
|
||||||
|
new_command.run.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy start" do
|
||||||
|
assert_equal \
|
||||||
|
"docker container start kamal-proxy",
|
||||||
|
new_command.start.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy stop" do
|
||||||
|
assert_equal \
|
||||||
|
"docker container stop kamal-proxy",
|
||||||
|
new_command.stop.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy info" do
|
||||||
|
assert_equal \
|
||||||
|
"docker ps --filter name=^kamal-proxy$",
|
||||||
|
new_command.info.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs kamal-proxy --timestamps 2>&1",
|
||||||
|
new_command.logs.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs since 2h" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs kamal-proxy --since 2h --timestamps 2>&1",
|
||||||
|
new_command.logs(since: "2h").join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs last 10 lines" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs kamal-proxy --tail 10 --timestamps 2>&1",
|
||||||
|
new_command.logs(lines: 10).join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs with grep hello!" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs kamal-proxy --timestamps 2>&1 | grep 'hello!'",
|
||||||
|
new_command.logs(grep: "hello!").join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy remove container" do
|
||||||
|
assert_equal \
|
||||||
|
"docker container prune --force --filter label=org.opencontainers.image.title=kamal-proxy",
|
||||||
|
new_command.remove_container.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy remove image" do
|
||||||
|
assert_equal \
|
||||||
|
"docker image prune --all --force --filter label=org.opencontainers.image.title=kamal-proxy",
|
||||||
|
new_command.remove_image.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy follow logs" do
|
||||||
|
assert_equal \
|
||||||
|
"ssh -t root@1.1.1.1 -p 22 'docker logs kamal-proxy --timestamps --tail 10 --follow 2>&1'",
|
||||||
|
new_command.follow_logs(host: @config[:servers].first)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy follow logs with grep hello!" do
|
||||||
|
assert_equal \
|
||||||
|
"ssh -t root@1.1.1.1 -p 22 'docker logs kamal-proxy --timestamps --tail 10 --follow 2>&1 | grep \"hello!\"'",
|
||||||
|
new_command.follow_logs(host: @config[:servers].first, grep: "hello!")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "deploy" do
|
||||||
|
assert_equal \
|
||||||
|
"docker exec kamal-proxy kamal-proxy deploy service --target \"172.1.0.2:80\"",
|
||||||
|
new_command.deploy("service", target: "172.1.0.2:80").join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "remove" do
|
||||||
|
assert_equal \
|
||||||
|
"docker exec kamal-proxy kamal-proxy remove service --target \"172.1.0.2:80\"",
|
||||||
|
new_command.remove("service", target: "172.1.0.2:80").join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def new_command
|
||||||
|
Kamal::Commands::Proxy.new(Kamal::Configuration.new(@config, version: "123"))
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -3,8 +3,7 @@ require "test_helper"
|
|||||||
class CommandsPruneTest < ActiveSupport::TestCase
|
class CommandsPruneTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
@config = {
|
@config = {
|
||||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ]
|
||||||
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -30,12 +29,6 @@ class CommandsPruneTest < ActiveSupport::TestCase
|
|||||||
new_command.app_containers(retain: 3).join(" ")
|
new_command.app_containers(retain: 3).join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "healthcheck containers" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container prune --force --filter label=service=healthcheck-app",
|
|
||||||
new_command.healthcheck_containers.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def new_command
|
def new_command
|
||||||
Kamal::Commands::Prune.new(Kamal::Configuration.new(@config, version: "123"))
|
Kamal::Commands::Prune.new(Kamal::Configuration.new(@config, version: "123"))
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ require "test_helper"
|
|||||||
class CommandsServerTest < ActiveSupport::TestCase
|
class CommandsServerTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
@config = {
|
@config = {
|
||||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ]
|
||||||
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,198 +0,0 @@
|
|||||||
require "test_helper"
|
|
||||||
|
|
||||||
class CommandsTraefikTest < ActiveSupport::TestCase
|
|
||||||
setup do
|
|
||||||
@image = "traefik:test"
|
|
||||||
|
|
||||||
@config = {
|
|
||||||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
|
||||||
traefik: { "image" => @image, "args" => { "accesslog.format" => "json", "api.insecure" => true, "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
|
||||||
}
|
|
||||||
|
|
||||||
ENV["EXAMPLE_API_KEY"] = "456"
|
|
||||||
end
|
|
||||||
|
|
||||||
teardown do
|
|
||||||
ENV.delete("EXAMPLE_API_KEY")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run" do
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
|
|
||||||
@config[:traefik]["host_port"] = "8080"
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 8080:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
|
|
||||||
@config[:traefik]["publish"] = false
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with ports configured" do
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
|
|
||||||
@config[:traefik]["options"] = { "publish" => %w[9000:9000 9001:9001] }
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" --publish \"9000:9000\" --publish \"9001:9001\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with volumes configured" do
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
|
|
||||||
@config[:traefik]["options"] = { "volume" => %w[./letsencrypt/acme.json:/letsencrypt/acme.json] }
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" --volume \"./letsencrypt/acme.json:/letsencrypt/acme.json\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with several options configured" do
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
|
|
||||||
@config[:traefik]["options"] = { "volume" => %w[./letsencrypt/acme.json:/letsencrypt/acme.json], "publish" => %w[8080:8080], "memory" => "512m" }
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" --volume \"./letsencrypt/acme.json:/letsencrypt/acme.json\" --publish \"8080:8080\" --memory \"512m\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with labels configured" do
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
|
|
||||||
@config[:traefik]["labels"] = { "traefik.http.routers.dashboard.service" => "api@internal", "traefik.http.routers.dashboard.middlewares" => "auth" }
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" --label traefik.http.routers.dashboard.service=\"api@internal\" --label traefik.http.routers.dashboard.middlewares=\"auth\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with env configured" do
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
|
|
||||||
@config[:traefik]["env"] = { "secret" => %w[EXAMPLE_API_KEY] }
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run without configuration" do
|
|
||||||
@config.delete(:traefik)
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{Kamal::Commands::Traefik::DEFAULT_IMAGE} --providers.docker --log.level=\"DEBUG\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with logging config" do
|
|
||||||
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } }
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"DEBUG\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with default args overriden" do
|
|
||||||
@config[:traefik]["args"]["log.level"] = "ERROR"
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --env-file .kamal/env/traefik/traefik.env --log-opt max-size=\"10m\" --label traefik.http.routers.catchall.entryPoints=\"http\" --label traefik.http.routers.catchall.rule=\"PathPrefix(\\`/\\`)\" --label traefik.http.routers.catchall.service=\"unavailable\" --label traefik.http.routers.catchall.priority=\"1\" --label traefik.http.services.unavailable.loadbalancer.server.port=\"0\" #{@image} --providers.docker --log.level=\"ERROR\" --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik start" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container start traefik",
|
|
||||||
new_command.start.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik stop" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container stop traefik",
|
|
||||||
new_command.stop.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik info" do
|
|
||||||
assert_equal \
|
|
||||||
"docker ps --filter name=^traefik$",
|
|
||||||
new_command.info.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik logs" do
|
|
||||||
assert_equal \
|
|
||||||
"docker logs traefik --timestamps 2>&1",
|
|
||||||
new_command.logs.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik logs since 2h" do
|
|
||||||
assert_equal \
|
|
||||||
"docker logs traefik --since 2h --timestamps 2>&1",
|
|
||||||
new_command.logs(since: "2h").join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik logs last 10 lines" do
|
|
||||||
assert_equal \
|
|
||||||
"docker logs traefik --tail 10 --timestamps 2>&1",
|
|
||||||
new_command.logs(lines: 10).join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik logs with grep hello!" do
|
|
||||||
assert_equal \
|
|
||||||
"docker logs traefik --timestamps 2>&1 | grep 'hello!'",
|
|
||||||
new_command.logs(grep: "hello!").join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik remove container" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container prune --force --filter label=org.opencontainers.image.title=Traefik",
|
|
||||||
new_command.remove_container.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik remove image" do
|
|
||||||
assert_equal \
|
|
||||||
"docker image prune --all --force --filter label=org.opencontainers.image.title=Traefik",
|
|
||||||
new_command.remove_image.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik follow logs" do
|
|
||||||
assert_equal \
|
|
||||||
"ssh -t root@1.1.1.1 -p 22 'docker logs traefik --timestamps --tail 10 --follow 2>&1'",
|
|
||||||
new_command.follow_logs(host: @config[:servers].first)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "traefik follow logs with grep hello!" do
|
|
||||||
assert_equal \
|
|
||||||
"ssh -t root@1.1.1.1 -p 22 'docker logs traefik --timestamps --tail 10 --follow 2>&1 | grep \"hello!\"'",
|
|
||||||
new_command.follow_logs(host: @config[:servers].first, grep: "hello!")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "secrets io" do
|
|
||||||
@config[:traefik]["env"] = { "secret" => %w[EXAMPLE_API_KEY] }
|
|
||||||
|
|
||||||
assert_equal "EXAMPLE_API_KEY=456\n", new_command.env.secrets_io.string
|
|
||||||
end
|
|
||||||
|
|
||||||
test "make_env_directory" do
|
|
||||||
assert_equal "mkdir -p .kamal/env/traefik", new_command.make_env_directory.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "remove_env_file" do
|
|
||||||
assert_equal "rm -f .kamal/env/traefik/traefik.env", new_command.remove_env_file.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def new_command
|
|
||||||
Kamal::Commands::Traefik.new(Kamal::Configuration.new(@config, version: "123"))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -42,7 +42,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "special label args for web" do
|
test "special label args for web" do
|
||||||
assert_equal [ "--label", "service=\"app\"", "--label", "role=\"web\"", "--label", "destination", "--label", "traefik.http.services.app-web.loadbalancer.server.scheme=\"http\"", "--label", "traefik.http.routers.app-web.rule=\"PathPrefix(\\`/\\`)\"", "--label", "traefik.http.routers.app-web.priority=\"2\"", "--label", "traefik.http.middlewares.app-web-retry.retry.attempts=\"5\"", "--label", "traefik.http.middlewares.app-web-retry.retry.initialinterval=\"500ms\"", "--label", "traefik.http.routers.app-web.middlewares=\"app-web-retry@docker\"" ], @config.role(:web).label_args
|
assert_equal [ "--label", "service=\"app\"", "--label", "role=\"web\"", "--label", "destination" ], @config.role(:web).label_args
|
||||||
end
|
end
|
||||||
|
|
||||||
test "custom labels" do
|
test "custom labels" do
|
||||||
@@ -56,19 +56,6 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
assert_equal "70", @config_with_roles.role(:workers).labels["my.custom.label"]
|
assert_equal "70", @config_with_roles.role(:workers).labels["my.custom.label"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "overwriting default traefik label" do
|
|
||||||
@deploy[:labels] = { "traefik.http.routers.app-web.rule" => "\"Host(\\`example.com\\`) || (Host(\\`example.org\\`) && Path(\\`/traefik\\`))\"" }
|
|
||||||
assert_equal "\"Host(\\`example.com\\`) || (Host(\\`example.org\\`) && Path(\\`/traefik\\`))\"", @config.role(:web).labels["traefik.http.routers.app-web.rule"]
|
|
||||||
end
|
|
||||||
|
|
||||||
test "default traefik label on non-web role" do
|
|
||||||
config = Kamal::Configuration.new(@deploy_with_roles.tap { |c|
|
|
||||||
c[:servers]["beta"] = { "traefik" => "true", "hosts" => [ "1.1.1.5" ] }
|
|
||||||
})
|
|
||||||
|
|
||||||
assert_equal [ "--label", "service=\"app\"", "--label", "role=\"beta\"", "--label", "destination", "--label", "traefik.http.services.app-beta.loadbalancer.server.scheme=\"http\"", "--label", "traefik.http.routers.app-beta.rule=\"PathPrefix(\\`/\\`)\"", "--label", "traefik.http.routers.app-beta.priority=\"2\"", "--label", "traefik.http.middlewares.app-beta-retry.retry.attempts=\"5\"", "--label", "traefik.http.middlewares.app-beta-retry.retry.initialinterval=\"500ms\"", "--label", "traefik.http.routers.app-beta.middlewares=\"app-beta-retry@docker\"" ], config.role(:beta).label_args
|
|
||||||
end
|
|
||||||
|
|
||||||
test "env overwritten by role" do
|
test "env overwritten by role" do
|
||||||
assert_equal "redis://a/b", @config_with_roles.role(:workers).env("1.1.1.3").clear["REDIS_URL"]
|
assert_equal "redis://a/b", @config_with_roles.role(:workers).env("1.1.1.3").clear["REDIS_URL"]
|
||||||
|
|
||||||
@@ -201,26 +188,6 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
assert_equal ".kamal/env/roles/app-workers.env", @config_with_roles.role(:workers).env("1.1.1.3").secrets_file
|
assert_equal ".kamal/env/roles/app-workers.env", @config_with_roles.role(:workers).env("1.1.1.3").secrets_file
|
||||||
end
|
end
|
||||||
|
|
||||||
test "uses cord" do
|
|
||||||
assert @config_with_roles.role(:web).uses_cord?
|
|
||||||
assert_not @config_with_roles.role(:workers).uses_cord?
|
|
||||||
end
|
|
||||||
|
|
||||||
test "cord host file" do
|
|
||||||
assert_match %r{.kamal/cords/app-web-[0-9a-f]{32}/cord}, @config_with_roles.role(:web).cord_host_file
|
|
||||||
end
|
|
||||||
|
|
||||||
test "cord volume" do
|
|
||||||
assert_equal "/tmp/kamal-cord", @config_with_roles.role(:web).cord_volume.container_path
|
|
||||||
assert_match %r{.kamal/cords/app-web-[0-9a-f]{32}}, @config_with_roles.role(:web).cord_volume.host_path
|
|
||||||
assert_equal "--volume", @config_with_roles.role(:web).cord_volume.docker_args[0]
|
|
||||||
assert_match %r{\$\(pwd\)/.kamal/cords/app-web-[0-9a-f]{32}:/tmp/kamal-cord}, @config_with_roles.role(:web).cord_volume.docker_args[1]
|
|
||||||
end
|
|
||||||
|
|
||||||
test "cord container file" do
|
|
||||||
assert_equal "/tmp/kamal-cord/cord", @config_with_roles.role(:web).cord_container_file
|
|
||||||
end
|
|
||||||
|
|
||||||
test "asset path and volume args" do
|
test "asset path and volume args" do
|
||||||
ENV["VERSION"] = "12345"
|
ENV["VERSION"] = "12345"
|
||||||
assert_nil @config_with_roles.role(:web).asset_volume_args
|
assert_nil @config_with_roles.role(:web).asset_volume_args
|
||||||
|
|||||||
@@ -74,22 +74,22 @@ class ConfigurationTest < ActiveSupport::TestCase
|
|||||||
assert_equal "1.1.1.1", @config_with_roles.primary_host
|
assert_equal "1.1.1.1", @config_with_roles.primary_host
|
||||||
end
|
end
|
||||||
|
|
||||||
test "traefik hosts" do
|
test "proxy hosts" do
|
||||||
assert_equal [ "1.1.1.1", "1.1.1.2" ], @config_with_roles.traefik_hosts
|
assert_equal [ "1.1.1.1", "1.1.1.2" ], @config_with_roles.proxy_hosts
|
||||||
|
|
||||||
@deploy_with_roles[:servers]["workers"]["traefik"] = true
|
@deploy_with_roles[:servers]["workers"]["proxy"] = true
|
||||||
config = Kamal::Configuration.new(@deploy_with_roles)
|
config = Kamal::Configuration.new(@deploy_with_roles)
|
||||||
|
|
||||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], config.traefik_hosts
|
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], config.proxy_hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
test "filtered traefik hosts" do
|
test "filtered proxy hosts" do
|
||||||
assert_equal [ "1.1.1.1", "1.1.1.2" ], @config_with_roles.traefik_hosts
|
assert_equal [ "1.1.1.1", "1.1.1.2" ], @config_with_roles.proxy_hosts
|
||||||
|
|
||||||
@deploy_with_roles[:servers]["workers"]["traefik"] = true
|
@deploy_with_roles[:servers]["workers"]["proxy"] = true
|
||||||
config = Kamal::Configuration.new(@deploy_with_roles)
|
config = Kamal::Configuration.new(@deploy_with_roles)
|
||||||
|
|
||||||
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], config.traefik_hosts
|
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], config.proxy_hosts
|
||||||
end
|
end
|
||||||
|
|
||||||
test "version no git repo" do
|
test "version no git repo" do
|
||||||
@@ -154,10 +154,6 @@ class ConfigurationTest < ActiveSupport::TestCase
|
|||||||
assert_equal "app-missing", @config.service_with_version
|
assert_equal "app-missing", @config.service_with_version
|
||||||
end
|
end
|
||||||
|
|
||||||
test "healthcheck service" do
|
|
||||||
assert_equal "healthcheck-app", @config.healthcheck_service
|
|
||||||
end
|
|
||||||
|
|
||||||
test "valid config" do
|
test "valid config" do
|
||||||
assert @config.valid?
|
assert @config.valid?
|
||||||
assert @config_with_roles.valid?
|
assert @config_with_roles.valid?
|
||||||
@@ -271,8 +267,7 @@ class ConfigurationTest < ActiveSupport::TestCase
|
|||||||
sshkit: {},
|
sshkit: {},
|
||||||
volume_args: [ "--volume", "/local/path:/container/path" ],
|
volume_args: [ "--volume", "/local/path:/container/path" ],
|
||||||
builder: {},
|
builder: {},
|
||||||
logging: [ "--log-opt", "max-size=\"10m\"" ],
|
logging: [ "--log-opt", "max-size=\"10m\"" ] }
|
||||||
healthcheck: { "path"=>"/up", "port"=>3000, "max_attempts" => 7, "cord" => "/tmp/kamal-cord", "log_lines" => 50 } }
|
|
||||||
|
|
||||||
assert_equal expected_config, @config.to_h
|
assert_equal expected_config, @config.to_h
|
||||||
end
|
end
|
||||||
@@ -330,7 +325,7 @@ class ConfigurationTest < ActiveSupport::TestCase
|
|||||||
assert_equal "alternate_web", config.primary_role.name
|
assert_equal "alternate_web", config.primary_role.name
|
||||||
assert_equal "1.1.1.4", config.primary_host
|
assert_equal "1.1.1.4", config.primary_host
|
||||||
assert config.role(:alternate_web).primary?
|
assert config.role(:alternate_web).primary?
|
||||||
assert config.role(:alternate_web).running_traefik?
|
assert config.role(:alternate_web).running_proxy?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "primary role missing" do
|
test "primary role missing" do
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ service: app
|
|||||||
image: dhh/app
|
image: dhh/app
|
||||||
servers:
|
servers:
|
||||||
web_chicago:
|
web_chicago:
|
||||||
traefik: enabled
|
proxy: enabled
|
||||||
hosts:
|
hosts:
|
||||||
- 1.1.1.1
|
- 1.1.1.1
|
||||||
- 1.1.1.2
|
- 1.1.1.2
|
||||||
web_tokyo:
|
web_tokyo:
|
||||||
traefik: enabled
|
proxy: enabled
|
||||||
hosts:
|
hosts:
|
||||||
- 1.1.1.3
|
- 1.1.1.3
|
||||||
- 1.1.1.4
|
- 1.1.1.4
|
||||||
|
|||||||
2
test/fixtures/deploy_with_aliases.yml
vendored
2
test/fixtures/deploy_with_aliases.yml
vendored
@@ -10,7 +10,7 @@ tokyo_hosts: &tokyo_hosts
|
|||||||
web_common: &web_common
|
web_common: &web_common
|
||||||
env:
|
env:
|
||||||
ROLE: "web"
|
ROLE: "web"
|
||||||
traefik: true
|
proxy: true
|
||||||
|
|
||||||
# actual config
|
# actual config
|
||||||
service: app
|
service: app
|
||||||
|
|||||||
2
test/fixtures/deploy_workers_only.yml
vendored
2
test/fixtures/deploy_workers_only.yml
vendored
@@ -2,7 +2,7 @@ service: app
|
|||||||
image: dhh/app
|
image: dhh/app
|
||||||
servers:
|
servers:
|
||||||
workers:
|
workers:
|
||||||
traefik: false
|
proxy: false
|
||||||
hosts:
|
hosts:
|
||||||
- 1.1.1.1
|
- 1.1.1.1
|
||||||
- 1.1.1.2
|
- 1.1.1.2
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
require_relative "integration_test"
|
require_relative "integration_test"
|
||||||
|
|
||||||
class AccessoryTest < IntegrationTest
|
class IntegrationAccessoryTest < IntegrationTest
|
||||||
test "boot, stop, start, restart, logs, remove" do
|
test "boot, stop, start, restart, logs, remove" do
|
||||||
kamal :envify
|
kamal :envify
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
require_relative "integration_test"
|
require_relative "integration_test"
|
||||||
|
|
||||||
class AppTest < IntegrationTest
|
class IntegrationAppTest < IntegrationTest
|
||||||
test "stop, start, boot, logs, images, containers, exec, remove" do
|
test "stop, start, boot, logs, images, containers, exec, remove" do
|
||||||
kamal :envify
|
kamal :envify
|
||||||
|
|
||||||
|
kamal :setup
|
||||||
|
|
||||||
kamal :deploy
|
kamal :deploy
|
||||||
|
|
||||||
assert_app_is_up
|
assert_app_is_up
|
||||||
|
|
||||||
kamal :app, :stop
|
kamal :app, :stop
|
||||||
|
|
||||||
assert_app_is_down
|
assert_app_is_down response_code: "504"
|
||||||
|
|
||||||
kamal :app, :start
|
kamal :app, :start
|
||||||
|
|
||||||
@@ -24,7 +26,7 @@ class AppTest < IntegrationTest
|
|||||||
logs = kamal :app, :logs, capture: true
|
logs = kamal :app, :logs, capture: true
|
||||||
assert_match /App Host: vm1/, logs
|
assert_match /App Host: vm1/, logs
|
||||||
assert_match /App Host: vm2/, logs
|
assert_match /App Host: vm2/, logs
|
||||||
assert_match /GET \/ HTTP\/1.1/, logs
|
assert_match /GET \/up HTTP\/1.1/, logs
|
||||||
|
|
||||||
images = kamal :app, :images, capture: true
|
images = kamal :app, :images, capture: true
|
||||||
assert_match /App Host: vm1/, images
|
assert_match /App Host: vm1/, images
|
||||||
@@ -50,6 +52,6 @@ class AppTest < IntegrationTest
|
|||||||
|
|
||||||
kamal :app, :remove
|
kamal :app, :remove
|
||||||
|
|
||||||
assert_app_is_down
|
assert_app_is_down response_code: "504"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -29,6 +29,5 @@ class BrokenDeployTest < IntegrationTest
|
|||||||
assert_match /First web container is unhealthy on vm[12], not booting other roles/, output
|
assert_match /First web container is unhealthy on vm[12], not booting other roles/, output
|
||||||
assert_match "First web container is unhealthy, not booting workers on vm3", output
|
assert_match "First web container is unhealthy, not booting workers on vm3", output
|
||||||
assert_match "nginx: [emerg] unexpected end of file, expecting \";\" or \"}\" in /etc/nginx/conf.d/default.conf:2", output
|
assert_match "nginx: [emerg] unexpected end of file, expecting \";\" or \"}\" in /etc/nginx/conf.d/default.conf:2", output
|
||||||
assert_match 'ERROR {"Status":"unhealthy","FailingStreak":0,"Log":[]}', output
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
3
test/integration/docker/deployer/app/.kamal/hooks/post-proxy-reboot
Executable file
3
test/integration/docker/deployer/app/.kamal/hooks/post-proxy-reboot
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
echo "Rebooted proxy on ${KAMAL_HOSTS}"
|
||||||
|
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/post-proxy-reboot
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
echo "Rebooted Traefik on ${KAMAL_HOSTS}"
|
|
||||||
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/post-traefik-reboot
|
|
||||||
3
test/integration/docker/deployer/app/.kamal/hooks/pre-proxy-reboot
Executable file
3
test/integration/docker/deployer/app/.kamal/hooks/pre-proxy-reboot
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
echo "Rebooting proxy on ${KAMAL_HOSTS}..."
|
||||||
|
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-proxy-reboot
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
echo "Rebooting Traefik on ${KAMAL_HOSTS}..."
|
|
||||||
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-traefik-reboot
|
|
||||||
@@ -6,4 +6,5 @@ ARG COMMIT_SHA
|
|||||||
RUN echo $COMMIT_SHA > /usr/share/nginx/html/version
|
RUN echo $COMMIT_SHA > /usr/share/nginx/html/version
|
||||||
RUN mkdir -p /usr/share/nginx/html/versions && echo "version" > /usr/share/nginx/html/versions/$COMMIT_SHA
|
RUN mkdir -p /usr/share/nginx/html/versions && echo "version" > /usr/share/nginx/html/versions/$COMMIT_SHA
|
||||||
RUN mkdir -p /usr/share/nginx/html/versions && echo "hidden" > /usr/share/nginx/html/versions/.hidden
|
RUN mkdir -p /usr/share/nginx/html/versions && echo "hidden" > /usr/share/nginx/html/versions/.hidden
|
||||||
|
RUN echo "Up!" > /usr/share/nginx/html/up
|
||||||
|
|
||||||
|
|||||||
@@ -26,14 +26,11 @@ builder:
|
|||||||
multiarch: false
|
multiarch: false
|
||||||
args:
|
args:
|
||||||
COMMIT_SHA: <%= `git rev-parse HEAD` %>
|
COMMIT_SHA: <%= `git rev-parse HEAD` %>
|
||||||
healthcheck:
|
proxy:
|
||||||
cmd: wget -qO- http://localhost > /dev/null || exit 1
|
image: registry:4443/basecamp/kamal-proxy:latest
|
||||||
max_attempts: 3
|
http_port: 80
|
||||||
traefik:
|
https_port: 443
|
||||||
args:
|
debug: true
|
||||||
accesslog: true
|
|
||||||
accesslog.format: json
|
|
||||||
image: registry:4443/traefik:v2.10
|
|
||||||
accessories:
|
accessories:
|
||||||
busybox:
|
busybox:
|
||||||
service: custom-busybox
|
service: custom-busybox
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
echo "Rebooted Traefik on ${KAMAL_HOSTS}"
|
|
||||||
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/post-traefik-reboot
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
echo "Rebooting Traefik on ${KAMAL_HOSTS}..."
|
|
||||||
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-traefik-reboot
|
|
||||||
@@ -6,4 +6,4 @@ ARG COMMIT_SHA
|
|||||||
RUN echo $COMMIT_SHA > /usr/share/nginx/html/version
|
RUN echo $COMMIT_SHA > /usr/share/nginx/html/version
|
||||||
RUN mkdir -p /usr/share/nginx/html/versions && echo "version" > /usr/share/nginx/html/versions/$COMMIT_SHA
|
RUN mkdir -p /usr/share/nginx/html/versions && echo "version" > /usr/share/nginx/html/versions/$COMMIT_SHA
|
||||||
RUN mkdir -p /usr/share/nginx/html/versions && echo "hidden" > /usr/share/nginx/html/versions/.hidden
|
RUN mkdir -p /usr/share/nginx/html/versions && echo "hidden" > /usr/share/nginx/html/versions/.hidden
|
||||||
|
RUN echo "Up!" > /usr/share/nginx/html/up
|
||||||
|
|||||||
@@ -20,14 +20,8 @@ builder:
|
|||||||
multiarch: false
|
multiarch: false
|
||||||
args:
|
args:
|
||||||
COMMIT_SHA: <%= `git rev-parse HEAD` %>
|
COMMIT_SHA: <%= `git rev-parse HEAD` %>
|
||||||
healthcheck:
|
proxy:
|
||||||
cmd: wget -qO- http://localhost > /dev/null || exit 1
|
image: registry:4443/basecamp/kamal-proxy:latest
|
||||||
max_attempts: 3
|
|
||||||
traefik:
|
|
||||||
args:
|
|
||||||
accesslog: true
|
|
||||||
accesslog.format: json
|
|
||||||
image: registry:4443/traefik:v2.10
|
|
||||||
accessories:
|
accessories:
|
||||||
busybox:
|
busybox:
|
||||||
service: custom-busybox
|
service: custom-busybox
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ push_image_to_registry_4443() {
|
|||||||
|
|
||||||
install_kamal
|
install_kamal
|
||||||
push_image_to_registry_4443 nginx 1-alpine-slim
|
push_image_to_registry_4443 nginx 1-alpine-slim
|
||||||
push_image_to_registry_4443 traefik v2.10
|
push_image_to_registry_4443 basecamp/kamal-proxy latest
|
||||||
push_image_to_registry_4443 busybox 1.36.0
|
push_image_to_registry_4443 busybox 1.36.0
|
||||||
|
|
||||||
# .ssh is on a shared volume that persists between runs. Clean it up as the
|
# .ssh is on a shared volume that persists between runs. Clean it up as the
|
||||||
|
|||||||
@@ -44,10 +44,10 @@ class IntegrationTest < ActiveSupport::TestCase
|
|||||||
deployer_exec(:kamal, *commands, **options)
|
deployer_exec(:kamal, *commands, **options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_app_is_down
|
def assert_app_is_down(response_code: "503")
|
||||||
response = app_response
|
response = app_response
|
||||||
debug_response_code(response, "502")
|
debug_response_code(response, response_code)
|
||||||
assert_equal "502", response.code
|
assert_equal response_code, response.code
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_app_is_up(version: nil)
|
def assert_app_is_up(version: nil)
|
||||||
@@ -101,8 +101,8 @@ class IntegrationTest < ActiveSupport::TestCase
|
|||||||
def assert_200(response)
|
def assert_200(response)
|
||||||
code = response.code
|
code = response.code
|
||||||
if code != "200"
|
if code != "200"
|
||||||
puts "Got response code #{code}, here are the traefik logs:"
|
puts "Got response code #{code}, here are the proxy logs:"
|
||||||
kamal :traefik, :logs
|
kamal :proxy, :logs
|
||||||
puts "And here are the load balancer logs"
|
puts "And here are the load balancer logs"
|
||||||
docker_compose :logs, :load_balancer
|
docker_compose :logs, :load_balancer
|
||||||
puts "Tried to get the response code again and got #{app_response.code}"
|
puts "Tried to get the response code again and got #{app_response.code}"
|
||||||
@@ -129,10 +129,10 @@ class IntegrationTest < ActiveSupport::TestCase
|
|||||||
def debug_response_code(app_response, expected_code)
|
def debug_response_code(app_response, expected_code)
|
||||||
code = app_response.code
|
code = app_response.code
|
||||||
if code != expected_code
|
if code != expected_code
|
||||||
puts "Got response code #{code}, here are the traefik logs:"
|
puts "Got response code #{code}, here are the proxy logs:"
|
||||||
kamal :traefik, :logs
|
kamal :proxy, :logs, raise_on_error: false
|
||||||
puts "And here are the load balancer logs"
|
puts "And here are the load balancer logs"
|
||||||
docker_compose :logs, :load_balancer
|
docker_compose :logs, :load_balancer, raise_on_error: false
|
||||||
puts "Tried to get the response code again and got #{app_response.code}"
|
puts "Tried to get the response code again and got #{app_response.code}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
require_relative "integration_test"
|
require_relative "integration_test"
|
||||||
|
|
||||||
class LockTest < IntegrationTest
|
class IntegrationLockTest < IntegrationTest
|
||||||
test "acquire, release, status" do
|
test "acquire, release, status" do
|
||||||
kamal :envify
|
kamal :envify
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
require_relative "integration_test"
|
require_relative "integration_test"
|
||||||
|
|
||||||
class MainTest < IntegrationTest
|
class IntegrationMainTest < IntegrationTest
|
||||||
test "envify, deploy, redeploy, rollback, details and audit" do
|
test "envify, deploy, redeploy, rollback, details and audit" do
|
||||||
|
kamal :server, :bootstrap
|
||||||
kamal :envify
|
kamal :envify
|
||||||
assert_env_files
|
assert_env_files
|
||||||
remove_local_env_file
|
remove_local_env_file
|
||||||
|
|
||||||
first_version = latest_app_version
|
first_version = latest_app_version
|
||||||
|
|
||||||
assert_app_is_down
|
assert_app_is_down response_code: "502"
|
||||||
|
|
||||||
kamal :deploy
|
kamal :deploy
|
||||||
assert_app_is_up version: first_version
|
assert_app_is_up version: first_version
|
||||||
@@ -28,11 +29,11 @@ class MainTest < IntegrationTest
|
|||||||
assert_app_is_up version: first_version
|
assert_app_is_up version: first_version
|
||||||
|
|
||||||
details = kamal :details, capture: true
|
details = kamal :details, capture: true
|
||||||
assert_match /Traefik Host: vm1/, details
|
assert_match /Proxy Host: vm1/, details
|
||||||
assert_match /Traefik Host: vm2/, details
|
assert_match /Proxy Host: vm2/, details
|
||||||
assert_match /App Host: vm1/, details
|
assert_match /App Host: vm1/, details
|
||||||
assert_match /App Host: vm2/, details
|
assert_match /App Host: vm2/, details
|
||||||
assert_match /traefik:v2.10/, details
|
assert_match /basecamp\/kamal-proxy:latest/, details
|
||||||
assert_match /registry:4443\/app:#{first_version}/, details
|
assert_match /registry:4443\/app:#{first_version}/, details
|
||||||
|
|
||||||
audit = kamal :audit, capture: true
|
audit = kamal :audit, capture: true
|
||||||
@@ -45,11 +46,12 @@ class MainTest < IntegrationTest
|
|||||||
test "app with roles" do
|
test "app with roles" do
|
||||||
@app = "app_with_roles"
|
@app = "app_with_roles"
|
||||||
|
|
||||||
|
kamal :server, :bootstrap
|
||||||
kamal :envify
|
kamal :envify
|
||||||
|
|
||||||
version = latest_app_version
|
version = latest_app_version
|
||||||
|
|
||||||
assert_app_is_down
|
assert_app_is_down response_code: "502"
|
||||||
|
|
||||||
kamal :deploy
|
kamal :deploy
|
||||||
|
|
||||||
@@ -79,7 +81,6 @@ class MainTest < IntegrationTest
|
|||||||
assert_equal({ user: "root", port: 22, keepalive: true, keepalive_interval: 30, log_level: :fatal }, config[:ssh_options])
|
assert_equal({ user: "root", port: 22, keepalive: true, keepalive_interval: 30, log_level: :fatal }, config[:ssh_options])
|
||||||
assert_equal({ "multiarch" => false, "args" => { "COMMIT_SHA" => version } }, config[:builder])
|
assert_equal({ "multiarch" => false, "args" => { "COMMIT_SHA" => version } }, config[:builder])
|
||||||
assert_equal [ "--log-opt", "max-size=\"10m\"" ], config[:logging]
|
assert_equal [ "--log-opt", "max-size=\"10m\"" ], config[:logging]
|
||||||
assert_equal({ "path" => "/up", "port" => 3000, "max_attempts" => 3, "cord"=>"/tmp/kamal-cord", "log_lines" => 50, "cmd"=>"wget -qO- http://localhost > /dev/null || exit 1" }, config[:healthcheck])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "setup and remove" do
|
test "setup and remove" do
|
||||||
|
|||||||
66
test/integration/proxy_test.rb
Normal file
66
test/integration/proxy_test.rb
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
require_relative "integration_test"
|
||||||
|
|
||||||
|
class IntegrationProxyTest < IntegrationTest
|
||||||
|
test "boot, reboot, stop, start, restart, logs, remove" do
|
||||||
|
kamal :server, :bootstrap
|
||||||
|
kamal :envify
|
||||||
|
|
||||||
|
kamal :proxy, :boot
|
||||||
|
assert_proxy_running
|
||||||
|
|
||||||
|
output = kamal :proxy, :reboot, "-y", "--verbose", capture: true
|
||||||
|
assert_proxy_running
|
||||||
|
assert_hooks_ran "pre-proxy-reboot", "post-proxy-reboot"
|
||||||
|
assert_match /Rebooting proxy on vm1,vm2.../, output
|
||||||
|
assert_match /Rebooted proxy on vm1,vm2/, output
|
||||||
|
|
||||||
|
output = kamal :proxy, :reboot, "--rolling", "-y", "--verbose", capture: true
|
||||||
|
assert_proxy_running
|
||||||
|
assert_hooks_ran "pre-proxy-reboot", "post-proxy-reboot"
|
||||||
|
assert_match /Rebooting proxy on vm1.../, output
|
||||||
|
assert_match /Rebooted proxy on vm1/, output
|
||||||
|
assert_match /Rebooting proxy on vm2.../, output
|
||||||
|
assert_match /Rebooted proxy on vm2/, output
|
||||||
|
|
||||||
|
kamal :proxy, :boot
|
||||||
|
assert_proxy_running
|
||||||
|
|
||||||
|
# Check booting when booted doesn't raise an error
|
||||||
|
kamal :proxy, :stop
|
||||||
|
assert_proxy_not_running
|
||||||
|
|
||||||
|
# Check booting when stopped works
|
||||||
|
kamal :proxy, :boot
|
||||||
|
assert_proxy_running
|
||||||
|
|
||||||
|
kamal :proxy, :stop
|
||||||
|
assert_proxy_not_running
|
||||||
|
|
||||||
|
kamal :proxy, :start
|
||||||
|
assert_proxy_running
|
||||||
|
|
||||||
|
kamal :proxy, :restart
|
||||||
|
assert_proxy_running
|
||||||
|
|
||||||
|
logs = kamal :proxy, :logs, capture: true
|
||||||
|
assert_match %r{"level":"INFO","msg":"Server started","http":80,"https":443}, logs
|
||||||
|
|
||||||
|
kamal :proxy, :remove
|
||||||
|
assert_proxy_not_running
|
||||||
|
|
||||||
|
kamal :env, :delete
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def assert_proxy_running
|
||||||
|
assert_match %r{registry:4443/basecamp/kamal-proxy:latest "kamal-proxy run"}, proxy_details
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_proxy_not_running
|
||||||
|
assert_no_match %r{registry:4443/basecamp/kamal-proxy:latest "kamal-proxy run"}, proxy_details
|
||||||
|
end
|
||||||
|
|
||||||
|
def proxy_details
|
||||||
|
kamal :proxy, :details, capture: true
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
require_relative "integration_test"
|
|
||||||
|
|
||||||
class TraefikTest < IntegrationTest
|
|
||||||
test "boot, reboot, stop, start, restart, logs, remove" do
|
|
||||||
kamal :envify
|
|
||||||
|
|
||||||
kamal :traefik, :boot
|
|
||||||
assert_traefik_running
|
|
||||||
|
|
||||||
output = kamal :traefik, :reboot, "-y", "--verbose", capture: true
|
|
||||||
assert_traefik_running
|
|
||||||
assert_hooks_ran "pre-traefik-reboot", "post-traefik-reboot"
|
|
||||||
assert_match /Rebooting Traefik on vm1,vm2.../, output
|
|
||||||
assert_match /Rebooted Traefik on vm1,vm2/, output
|
|
||||||
|
|
||||||
output = kamal :traefik, :reboot, "--rolling", "-y", "--verbose", capture: true
|
|
||||||
assert_traefik_running
|
|
||||||
assert_hooks_ran "pre-traefik-reboot", "post-traefik-reboot"
|
|
||||||
assert_match /Rebooting Traefik on vm1.../, output
|
|
||||||
assert_match /Rebooted Traefik on vm1/, output
|
|
||||||
assert_match /Rebooting Traefik on vm2.../, output
|
|
||||||
assert_match /Rebooted Traefik on vm2/, output
|
|
||||||
|
|
||||||
kamal :traefik, :boot
|
|
||||||
assert_traefik_running
|
|
||||||
|
|
||||||
# Check booting when booted doesn't raise an error
|
|
||||||
kamal :traefik, :stop
|
|
||||||
assert_traefik_not_running
|
|
||||||
|
|
||||||
# Check booting when stopped works
|
|
||||||
kamal :traefik, :boot
|
|
||||||
assert_traefik_running
|
|
||||||
|
|
||||||
kamal :traefik, :stop
|
|
||||||
assert_traefik_not_running
|
|
||||||
|
|
||||||
kamal :traefik, :start
|
|
||||||
assert_traefik_running
|
|
||||||
|
|
||||||
kamal :traefik, :restart
|
|
||||||
assert_traefik_running
|
|
||||||
|
|
||||||
logs = kamal :traefik, :logs, capture: true
|
|
||||||
assert_match /Traefik version [\d.]+ built on/, logs
|
|
||||||
|
|
||||||
kamal :traefik, :remove
|
|
||||||
assert_traefik_not_running
|
|
||||||
|
|
||||||
kamal :env, :delete
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def assert_traefik_running
|
|
||||||
assert_match /traefik:v2.10 "\/entrypoint.sh/, traefik_details
|
|
||||||
end
|
|
||||||
|
|
||||||
def assert_traefik_not_running
|
|
||||||
assert_no_match /traefik:v2.10 "\/entrypoint.sh/, traefik_details
|
|
||||||
end
|
|
||||||
|
|
||||||
def traefik_details
|
|
||||||
kamal :traefik, :details, capture: true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Reference in New Issue
Block a user