Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
822590dcf6 | ||
|
|
10b8c826d8 | ||
|
|
187861fa60 | ||
|
|
5ff1203c80 | ||
|
|
0e73f02743 | ||
|
|
83d0078525 | ||
|
|
96ef0fbc4d | ||
|
|
b12654ccd0 | ||
|
|
64f5955444 | ||
|
|
d2a719998a | ||
|
|
6a7c90cf4d | ||
|
|
2c2d94c6d9 | ||
|
|
c62bd1dc31 | ||
|
|
a83df9e135 | ||
|
|
7b55f4734e | ||
|
|
1e296c4140 | ||
|
|
9700e2b3c4 | ||
|
|
706b82baa1 | ||
|
|
fa7e941648 | ||
|
|
78c0a0ba4b | ||
|
|
060e5d2027 | ||
|
|
8a4f7163bb | ||
|
|
ee758d951a | ||
|
|
bb2ca81d87 | ||
|
|
773ba3a5ab | ||
|
|
5be6fa3b4e | ||
|
|
07c5658396 | ||
|
|
0efb5ccfff | ||
|
|
990f1b4413 | ||
|
|
da9428f64d | ||
|
|
17dcaccb6a | ||
|
|
448349d0e5 | ||
|
|
b6dba57c7d | ||
|
|
0ea2a2c509 | ||
|
|
307750ff70 | ||
|
|
88947b6a7b | ||
|
|
f48c227768 | ||
|
|
f98380ef0c | ||
|
|
0bc27c10cc | ||
|
|
e58d2f67f2 | ||
|
|
938ac375a1 | ||
|
|
dc1f707a56 | ||
|
|
033f2a3401 | ||
|
|
7cac7e6fb0 | ||
|
|
fb58fc0ba6 | ||
|
|
12cad5458a | ||
|
|
f8b7f74543 | ||
|
|
489d6dbcbb | ||
|
|
6d062ce271 | ||
|
|
1e44cc2597 | ||
|
|
63c47eca4c | ||
|
|
947be0877f | ||
|
|
2f912367ac |
@@ -9,7 +9,7 @@ PATH
|
|||||||
dotenv (~> 2.8)
|
dotenv (~> 2.8)
|
||||||
ed25519 (~> 1.2)
|
ed25519 (~> 1.2)
|
||||||
net-ssh (~> 7.0)
|
net-ssh (~> 7.0)
|
||||||
sshkit (~> 1.21)
|
sshkit (>= 1.22.2, < 2.0)
|
||||||
thor (~> 1.2)
|
thor (~> 1.2)
|
||||||
zeitwerk (~> 2.5)
|
zeitwerk (~> 2.5)
|
||||||
|
|
||||||
@@ -75,6 +75,8 @@ GEM
|
|||||||
mutex_m (0.2.0)
|
mutex_m (0.2.0)
|
||||||
net-scp (4.0.0)
|
net-scp (4.0.0)
|
||||||
net-ssh (>= 2.6.5, < 8.0.0)
|
net-ssh (>= 2.6.5, < 8.0.0)
|
||||||
|
net-sftp (4.0.0)
|
||||||
|
net-ssh (>= 5.0.0, < 8.0.0)
|
||||||
net-ssh (7.2.1)
|
net-ssh (7.2.1)
|
||||||
nokogiri (1.16.0-arm64-darwin)
|
nokogiri (1.16.0-arm64-darwin)
|
||||||
racc (~> 1.4)
|
racc (~> 1.4)
|
||||||
@@ -151,9 +153,11 @@ GEM
|
|||||||
rubocop-rails
|
rubocop-rails
|
||||||
ruby-progressbar (1.13.0)
|
ruby-progressbar (1.13.0)
|
||||||
ruby2_keywords (0.0.5)
|
ruby2_keywords (0.0.5)
|
||||||
sshkit (1.21.7)
|
sshkit (1.22.2)
|
||||||
|
base64
|
||||||
mutex_m
|
mutex_m
|
||||||
net-scp (>= 1.1.2)
|
net-scp (>= 1.1.2)
|
||||||
|
net-sftp (>= 2.1.2)
|
||||||
net-ssh (>= 2.8.0)
|
net-ssh (>= 2.8.0)
|
||||||
stringio (3.1.0)
|
stringio (3.1.0)
|
||||||
thor (1.3.0)
|
thor (1.3.0)
|
||||||
|
|||||||
@@ -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 parachute 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).
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|||||||
spec.executables = %w[ kamal ]
|
spec.executables = %w[ kamal ]
|
||||||
|
|
||||||
spec.add_dependency "activesupport", ">= 7.0"
|
spec.add_dependency "activesupport", ">= 7.0"
|
||||||
spec.add_dependency "sshkit", "~> 1.21"
|
spec.add_dependency "sshkit", ">= 1.22.2", "< 2.0"
|
||||||
spec.add_dependency "net-ssh", "~> 7.0"
|
spec.add_dependency "net-ssh", "~> 7.0"
|
||||||
spec.add_dependency "thor", "~> 1.2"
|
spec.add_dependency "thor", "~> 1.2"
|
||||||
spec.add_dependency "dotenv", "~> 2.8"
|
spec.add_dependency "dotenv", "~> 2.8"
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class Kamal::Cli::Accessory < Kamal::Cli::Base
|
class Kamal::Cli::Accessory < Kamal::Cli::Base
|
||||||
desc "boot [NAME]", "Boot new accessory service on host (use NAME=all to boot all accessories)"
|
desc "boot [NAME]", "Boot new accessory service on host (use NAME=all to boot all accessories)"
|
||||||
def boot(name, login: true)
|
def boot(name, login: true)
|
||||||
mutating do
|
with_lock do
|
||||||
if name == "all"
|
if name == "all"
|
||||||
KAMAL.accessory_names.each { |accessory_name| boot(accessory_name) }
|
KAMAL.accessory_names.each { |accessory_name| boot(accessory_name) }
|
||||||
else
|
else
|
||||||
@@ -21,7 +21,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "upload [NAME]", "Upload accessory files to host", hide: true
|
desc "upload [NAME]", "Upload accessory files to host", hide: true
|
||||||
def upload(name)
|
def upload(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do |accessory, hosts|
|
with_accessory(name) do |accessory, hosts|
|
||||||
on(hosts) do
|
on(hosts) do
|
||||||
accessory.files.each do |(local, remote)|
|
accessory.files.each do |(local, remote)|
|
||||||
@@ -38,7 +38,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "directories [NAME]", "Create accessory directories on host", hide: true
|
desc "directories [NAME]", "Create accessory directories on host", hide: true
|
||||||
def directories(name)
|
def directories(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do |accessory, hosts|
|
with_accessory(name) do |accessory, hosts|
|
||||||
on(hosts) do
|
on(hosts) do
|
||||||
accessory.directories.keys.each do |host_path|
|
accessory.directories.keys.each do |host_path|
|
||||||
@@ -51,7 +51,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "reboot [NAME]", "Reboot existing accessory on host (stop container, remove container, start new container; use NAME=all to boot all accessories)"
|
desc "reboot [NAME]", "Reboot existing accessory on host (stop container, remove container, start new container; use NAME=all to boot all accessories)"
|
||||||
def reboot(name)
|
def reboot(name)
|
||||||
mutating do
|
with_lock do
|
||||||
if name == "all"
|
if name == "all"
|
||||||
KAMAL.accessory_names.each { |accessory_name| reboot(accessory_name) }
|
KAMAL.accessory_names.each { |accessory_name| reboot(accessory_name) }
|
||||||
else
|
else
|
||||||
@@ -70,7 +70,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "start [NAME]", "Start existing accessory container on host"
|
desc "start [NAME]", "Start existing accessory container on host"
|
||||||
def start(name)
|
def start(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do |accessory, hosts|
|
with_accessory(name) do |accessory, hosts|
|
||||||
on(hosts) do
|
on(hosts) do
|
||||||
execute *KAMAL.auditor.record("Started #{name} accessory"), verbosity: :debug
|
execute *KAMAL.auditor.record("Started #{name} accessory"), verbosity: :debug
|
||||||
@@ -82,7 +82,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "stop [NAME]", "Stop existing accessory container on host"
|
desc "stop [NAME]", "Stop existing accessory container on host"
|
||||||
def stop(name)
|
def stop(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do |accessory, hosts|
|
with_accessory(name) do |accessory, hosts|
|
||||||
on(hosts) do
|
on(hosts) do
|
||||||
execute *KAMAL.auditor.record("Stopped #{name} accessory"), verbosity: :debug
|
execute *KAMAL.auditor.record("Stopped #{name} accessory"), verbosity: :debug
|
||||||
@@ -94,7 +94,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "restart [NAME]", "Restart existing accessory container on host"
|
desc "restart [NAME]", "Restart existing accessory container on host"
|
||||||
def restart(name)
|
def restart(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do
|
with_accessory(name) do
|
||||||
stop(name)
|
stop(name)
|
||||||
start(name)
|
start(name)
|
||||||
@@ -174,17 +174,12 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
desc "remove [NAME]", "Remove accessory container, image and data directory from host (use NAME=all to remove all accessories)"
|
desc "remove [NAME]", "Remove accessory container, image and data directory from host (use NAME=all to remove all accessories)"
|
||||||
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(name)
|
def remove(name)
|
||||||
mutating do
|
|
||||||
if name == "all"
|
|
||||||
KAMAL.accessory_names.each { |accessory_name| remove(accessory_name) }
|
|
||||||
else
|
|
||||||
confirming "This will remove all containers, images and data directories for #{name}. Are you sure?" do
|
confirming "This will remove all containers, images and data directories for #{name}. Are you sure?" do
|
||||||
with_accessory(name) do
|
with_lock do
|
||||||
stop(name)
|
if name == "all"
|
||||||
remove_container(name)
|
KAMAL.accessory_names.each { |accessory_name| remove_accessory(accessory_name) }
|
||||||
remove_image(name)
|
else
|
||||||
remove_service_directory(name)
|
remove_accessory(name)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -192,7 +187,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "remove_container [NAME]", "Remove accessory container from host", hide: true
|
desc "remove_container [NAME]", "Remove accessory container from host", hide: true
|
||||||
def remove_container(name)
|
def remove_container(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do |accessory, hosts|
|
with_accessory(name) do |accessory, hosts|
|
||||||
on(hosts) do
|
on(hosts) do
|
||||||
execute *KAMAL.auditor.record("Remove #{name} accessory container"), verbosity: :debug
|
execute *KAMAL.auditor.record("Remove #{name} accessory container"), verbosity: :debug
|
||||||
@@ -204,7 +199,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "remove_image [NAME]", "Remove accessory image from host", hide: true
|
desc "remove_image [NAME]", "Remove accessory image from host", hide: true
|
||||||
def remove_image(name)
|
def remove_image(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do |accessory, hosts|
|
with_accessory(name) do |accessory, hosts|
|
||||||
on(hosts) do
|
on(hosts) do
|
||||||
execute *KAMAL.auditor.record("Removed #{name} accessory image"), verbosity: :debug
|
execute *KAMAL.auditor.record("Removed #{name} accessory image"), verbosity: :debug
|
||||||
@@ -216,7 +211,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host", hide: true
|
desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host", hide: true
|
||||||
def remove_service_directory(name)
|
def remove_service_directory(name)
|
||||||
mutating do
|
with_lock do
|
||||||
with_accessory(name) do |accessory, hosts|
|
with_accessory(name) do |accessory, hosts|
|
||||||
on(hosts) do
|
on(hosts) do
|
||||||
execute *accessory.remove_service_directory
|
execute *accessory.remove_service_directory
|
||||||
@@ -250,4 +245,13 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
|
|||||||
accessory.hosts
|
accessory.hosts
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def remove_accessory(name)
|
||||||
|
with_accessory(name) do
|
||||||
|
stop(name)
|
||||||
|
remove_container(name)
|
||||||
|
remove_image(name)
|
||||||
|
remove_service_directory(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
class Kamal::Cli::App < Kamal::Cli::Base
|
class Kamal::Cli::App < Kamal::Cli::Base
|
||||||
desc "boot", "Boot app on servers (or reboot app if already running)"
|
desc "boot", "Boot app on servers (or reboot app if already running)"
|
||||||
def boot
|
def boot
|
||||||
mutating do
|
with_lock do
|
||||||
hold_lock_on_error do
|
|
||||||
say "Get most recent version available as an image...", :magenta unless options[:version]
|
say "Get most recent version available as an image...", :magenta unless options[:version]
|
||||||
using_version(version_or_latest) do |version|
|
using_version(version_or_latest) do |version|
|
||||||
say "Start container with version #{version} using a #{KAMAL.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta
|
say "Start container with version #{version} using a #{KAMAL.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta
|
||||||
@@ -10,16 +9,20 @@ 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
|
||||||
|
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, version, self).run
|
Boot.new(host, role, self, version, barrier).run
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Tag once the app booted on all hosts
|
||||||
on(KAMAL.hosts) do |host|
|
on(KAMAL.hosts) do |host|
|
||||||
execute *KAMAL.auditor.record("Tagging #{KAMAL.config.absolute_image} as the latest image"), verbosity: :debug
|
execute *KAMAL.auditor.record("Tagging #{KAMAL.config.absolute_image} as the latest image"), verbosity: :debug
|
||||||
execute *KAMAL.app.tag_latest_image
|
execute *KAMAL.app.tag_latest_image
|
||||||
@@ -27,17 +30,25 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
desc "start", "Start existing app container on servers"
|
desc "start", "Start existing app container on servers"
|
||||||
def start
|
def start
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do |host|
|
on(KAMAL.hosts) do |host|
|
||||||
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).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
|
||||||
@@ -45,13 +56,24 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "stop", "Stop app container on servers"
|
desc "stop", "Stop app container on servers"
|
||||||
def stop
|
def stop
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do |host|
|
on(KAMAL.hosts) do |host|
|
||||||
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).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
|
||||||
@@ -64,12 +86,12 @@ 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|
|
||||||
puts_by_host host, capture_with_info(*KAMAL.app(role: role).info)
|
puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).info)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "exec [CMD]", "Execute a custom command on servers (use --help to show options)"
|
desc "exec [CMD]", "Execute a custom command on servers within the app container (use --help to show options)"
|
||||||
option :interactive, aliases: "-i", type: :boolean, default: false, desc: "Execute command over ssh for an interactive shell (use for console/bash)"
|
option :interactive, aliases: "-i", type: :boolean, default: false, desc: "Execute command over ssh for an interactive shell (use for console/bash)"
|
||||||
option :reuse, type: :boolean, default: false, desc: "Reuse currently running container instead of starting a new one"
|
option :reuse, type: :boolean, default: false, desc: "Reuse currently running container instead of starting a new one"
|
||||||
option :env, aliases: "-e", type: :hash, desc: "Set environment variables for the command"
|
option :env, aliases: "-e", type: :hash, desc: "Set environment variables for the command"
|
||||||
@@ -80,7 +102,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
say "Get current version of running container...", :magenta unless options[:version]
|
say "Get current version of running container...", :magenta unless options[:version]
|
||||||
using_version(options[:version] || current_running_version) do |version|
|
using_version(options[:version] || current_running_version) do |version|
|
||||||
say "Launching interactive command with version #{version} via SSH from existing container on #{KAMAL.primary_host}...", :magenta
|
say "Launching interactive command with version #{version} via SSH from existing container on #{KAMAL.primary_host}...", :magenta
|
||||||
run_locally { exec KAMAL.app(role: KAMAL.primary_role).execute_in_existing_container_over_ssh(cmd, host: KAMAL.primary_host, env: env) }
|
run_locally { exec KAMAL.app(role: KAMAL.primary_role, host: KAMAL.primary_host).execute_in_existing_container_over_ssh(cmd, env: env) }
|
||||||
end
|
end
|
||||||
|
|
||||||
when options[:interactive]
|
when options[:interactive]
|
||||||
@@ -88,7 +110,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
using_version(version_or_latest) do |version|
|
using_version(version_or_latest) do |version|
|
||||||
say "Launching interactive command with version #{version} via SSH from new container on #{KAMAL.primary_host}...", :magenta
|
say "Launching interactive command with version #{version} via SSH from new container on #{KAMAL.primary_host}...", :magenta
|
||||||
run_locally do
|
run_locally do
|
||||||
exec KAMAL.app(role: KAMAL.primary_role).execute_in_new_container_over_ssh(cmd, host: KAMAL.primary_host, env: env)
|
exec KAMAL.app(role: KAMAL.primary_role, host: KAMAL.primary_host).execute_in_new_container_over_ssh(cmd, env: env)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -102,7 +124,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}", role: role), verbosity: :debug
|
execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}", role: role), verbosity: :debug
|
||||||
puts_by_host host, capture_with_info(*KAMAL.app(role: role).execute_in_existing_container(cmd, env: env))
|
puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_existing_container(cmd, env: env))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -116,7 +138,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
||||||
puts_by_host host, capture_with_info(*KAMAL.app(role: role).execute_in_new_container(cmd, env: env))
|
puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_new_container(cmd, env: env))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -131,22 +153,21 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
desc "stale_containers", "Detect app stale containers"
|
desc "stale_containers", "Detect app stale containers"
|
||||||
option :stop, aliases: "-s", type: :boolean, default: false, desc: "Stop the stale containers found"
|
option :stop, aliases: "-s", type: :boolean, default: false, desc: "Stop the stale containers found"
|
||||||
def stale_containers
|
def stale_containers
|
||||||
mutating do
|
|
||||||
stop = options[:stop]
|
stop = options[:stop]
|
||||||
|
|
||||||
cli = self
|
with_lock_if_stopping do
|
||||||
|
|
||||||
on(KAMAL.hosts) do |host|
|
on(KAMAL.hosts) do |host|
|
||||||
roles = KAMAL.roles_on(host)
|
roles = KAMAL.roles_on(host)
|
||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
versions = capture_with_info(*KAMAL.app(role: role).list_versions, raise_on_non_zero_exit: false).split("\n")
|
app = KAMAL.app(role: role, host: host)
|
||||||
versions -= [ capture_with_info(*KAMAL.app(role: role).current_running_version, raise_on_non_zero_exit: false).strip ]
|
versions = capture_with_info(*app.list_versions, raise_on_non_zero_exit: false).split("\n")
|
||||||
|
versions -= [ capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip ]
|
||||||
|
|
||||||
versions.each do |version|
|
versions.each do |version|
|
||||||
if stop
|
if stop
|
||||||
puts_by_host host, "Stopping stale container for role #{role} with version #{version}"
|
puts_by_host host, "Stopping stale container for role #{role} with version #{version}"
|
||||||
execute *KAMAL.app(role: role).stop(version: version), raise_on_non_zero_exit: false
|
execute *app.stop(version: version), raise_on_non_zero_exit: false
|
||||||
else
|
else
|
||||||
puts_by_host host, "Detected stale container for role #{role} with version #{version} (use `kamal app stale_containers --stop` to stop)"
|
puts_by_host host, "Detected stale container for role #{role} with version #{version} (use `kamal app stale_containers --stop` to stop)"
|
||||||
end
|
end
|
||||||
@@ -180,8 +201,9 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
KAMAL.specific_roles ||= [ "web" ]
|
KAMAL.specific_roles ||= [ "web" ]
|
||||||
role = KAMAL.roles_on(KAMAL.primary_host).first
|
role = KAMAL.roles_on(KAMAL.primary_host).first
|
||||||
|
|
||||||
info KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep)
|
app = KAMAL.app(role: role, host: host)
|
||||||
exec KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep)
|
info app.follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep)
|
||||||
|
exec app.follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
|
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
|
||||||
@@ -191,7 +213,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
begin
|
begin
|
||||||
puts_by_host host, capture_with_info(*KAMAL.app(role: role).logs(since: since, lines: lines, grep: grep))
|
puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(since: since, lines: lines, grep: grep))
|
||||||
rescue SSHKit::Command::Failed
|
rescue SSHKit::Command::Failed
|
||||||
puts_by_host host, "Nothing found"
|
puts_by_host host, "Nothing found"
|
||||||
end
|
end
|
||||||
@@ -202,7 +224,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "remove", "Remove app containers and images from servers"
|
desc "remove", "Remove app containers and images from servers"
|
||||||
def remove
|
def remove
|
||||||
mutating do
|
with_lock do
|
||||||
stop
|
stop
|
||||||
remove_containers
|
remove_containers
|
||||||
remove_images
|
remove_images
|
||||||
@@ -211,13 +233,13 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "remove_container [VERSION]", "Remove app container with given version from servers", hide: true
|
desc "remove_container [VERSION]", "Remove app container with given version from servers", hide: true
|
||||||
def remove_container(version)
|
def remove_container(version)
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do |host|
|
on(KAMAL.hosts) do |host|
|
||||||
roles = KAMAL.roles_on(host)
|
roles = KAMAL.roles_on(host)
|
||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
execute *KAMAL.auditor.record("Removed app container with version #{version}", role: role), verbosity: :debug
|
execute *KAMAL.auditor.record("Removed app container with version #{version}", role: role), verbosity: :debug
|
||||||
execute *KAMAL.app(role: role).remove_container(version: version)
|
execute *KAMAL.app(role: role, host: host).remove_container(version: version)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -225,13 +247,13 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "remove_containers", "Remove all app containers from servers", hide: true
|
desc "remove_containers", "Remove all app containers from servers", hide: true
|
||||||
def remove_containers
|
def remove_containers
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do |host|
|
on(KAMAL.hosts) do |host|
|
||||||
roles = KAMAL.roles_on(host)
|
roles = KAMAL.roles_on(host)
|
||||||
|
|
||||||
roles.each do |role|
|
roles.each do |role|
|
||||||
execute *KAMAL.auditor.record("Removed all app containers", role: role), verbosity: :debug
|
execute *KAMAL.auditor.record("Removed all app containers", role: role), verbosity: :debug
|
||||||
execute *KAMAL.app(role: role).remove_containers
|
execute *KAMAL.app(role: role, host: host).remove_containers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -239,7 +261,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "remove_images", "Remove all app images from servers", hide: true
|
desc "remove_images", "Remove all app images from servers", hide: true
|
||||||
def remove_images
|
def remove_images
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
execute *KAMAL.auditor.record("Removed all app images"), verbosity: :debug
|
execute *KAMAL.auditor.record("Removed all app images"), verbosity: :debug
|
||||||
execute *KAMAL.app.remove_images
|
execute *KAMAL.app.remove_images
|
||||||
@@ -251,7 +273,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
def version
|
def version
|
||||||
on(KAMAL.hosts) do |host|
|
on(KAMAL.hosts) do |host|
|
||||||
role = KAMAL.roles_on(host).first
|
role = KAMAL.roles_on(host).first
|
||||||
puts_by_host host, capture_with_info(*KAMAL.app(role: role).current_running_version).strip
|
puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).current_running_version).strip
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -274,7 +296,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
version = nil
|
version = nil
|
||||||
on(host) do
|
on(host) do
|
||||||
role = KAMAL.roles_on(host).first
|
role = KAMAL.roles_on(host).first
|
||||||
version = capture_with_info(*KAMAL.app(role: role).current_running_version).strip
|
version = capture_with_info(*KAMAL.app(role: role, host: host).current_running_version).strip
|
||||||
end
|
end
|
||||||
version.presence
|
version.presence
|
||||||
end
|
end
|
||||||
@@ -282,4 +304,12 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
|||||||
def version_or_latest
|
def version_or_latest
|
||||||
options[:version] || KAMAL.config.latest_tag
|
options[:version] || KAMAL.config.latest_tag
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_lock_if_stopping
|
||||||
|
if options[:stop]
|
||||||
|
with_lock { yield }
|
||||||
|
else
|
||||||
|
yield
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
31
lib/kamal/cli/app/barrier.rb
Normal file
31
lib/kamal/cli/app/barrier.rb
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
class Kamal::Cli::App::Barrier
|
||||||
|
def initialize
|
||||||
|
@ivar = Concurrent::IVar.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
set(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def open
|
||||||
|
set(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def wait
|
||||||
|
unless opened?
|
||||||
|
raise Kamal::Cli::BootError.new("Halted at barrier")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def opened?
|
||||||
|
@ivar.value
|
||||||
|
end
|
||||||
|
|
||||||
|
def set(value)
|
||||||
|
@ivar.set(value)
|
||||||
|
true
|
||||||
|
rescue Concurrent::MultipleAssignmentError
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,19 +1,30 @@
|
|||||||
class Kamal::Cli::App::Boot
|
class Kamal::Cli::App::Boot
|
||||||
attr_reader :host, :role, :version, :sshkit
|
attr_reader :host, :role, :version, :barrier, :sshkit
|
||||||
delegate :execute, :capture_with_info, :info, to: :sshkit
|
delegate :execute, :capture_with_info, :capture_with_pretty_json, :info, :error, to: :sshkit
|
||||||
delegate :uses_cord?, :assets?, to: :role
|
delegate :assets?, :running_proxy?, to: :role
|
||||||
|
|
||||||
def initialize(host, role, version, sshkit)
|
def initialize(host, role, sshkit, version, barrier)
|
||||||
@host = host
|
@host = host
|
||||||
@role = role
|
@role = role
|
||||||
@version = version
|
@version = version
|
||||||
|
@barrier = barrier
|
||||||
@sshkit = sshkit
|
@sshkit = sshkit
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
old_version = old_version_renamed_if_clashing
|
old_version = old_version_renamed_if_clashing
|
||||||
|
|
||||||
|
wait_at_barrier if queuer?
|
||||||
|
|
||||||
|
begin
|
||||||
start_new_version
|
start_new_version
|
||||||
|
rescue => e
|
||||||
|
close_barrier if gatekeeper?
|
||||||
|
stop_new_version
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
|
||||||
|
release_barrier if gatekeeper?
|
||||||
|
|
||||||
if old_version
|
if old_version
|
||||||
stop_old_version(old_version)
|
stop_old_version(old_version)
|
||||||
@@ -21,18 +32,6 @@ class Kamal::Cli::App::Boot
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def app
|
|
||||||
@app ||= KAMAL.app(role: role)
|
|
||||||
end
|
|
||||||
|
|
||||||
def auditor
|
|
||||||
@auditor = KAMAL.auditor(role: role)
|
|
||||||
end
|
|
||||||
|
|
||||||
def audit(message)
|
|
||||||
execute *auditor.record(message), verbosity: :debug
|
|
||||||
end
|
|
||||||
|
|
||||||
def old_version_renamed_if_clashing
|
def old_version_renamed_if_clashing
|
||||||
if capture_with_info(*app.container_id_for_version(version), raise_on_non_zero_exit: false).present?
|
if capture_with_info(*app.container_id_for_version(version), raise_on_non_zero_exit: false).present?
|
||||||
renamed_version = "#{version}_replaced_#{SecureRandom.hex(8)}"
|
renamed_version = "#{version}_replaced_#{SecureRandom.hex(8)}"
|
||||||
@@ -46,22 +45,67 @@ 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)}"
|
||||||
execute *app.run(hostname: "#{host}-#{SecureRandom.hex(6)}")
|
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
|
||||||
|
|
||||||
|
def stop_new_version
|
||||||
|
execute *app.stop(version: version), raise_on_non_zero_exit: false
|
||||||
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
|
||||||
|
|
||||||
|
def release_barrier
|
||||||
|
if barrier.open
|
||||||
|
info "First #{KAMAL.primary_role} container is healthy on #{host}, booting other roles"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def wait_at_barrier
|
||||||
|
info "Waiting for the first healthy #{KAMAL.primary_role} container before booting #{role} on #{host}..."
|
||||||
|
barrier.wait
|
||||||
|
info "First #{KAMAL.primary_role} container is healthy, booting #{role} on #{host}..."
|
||||||
|
rescue Kamal::Cli::BootError
|
||||||
|
info "First #{KAMAL.primary_role} container is unhealthy, not booting #{role} on #{host}"
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
|
||||||
|
def close_barrier
|
||||||
|
if barrier.close
|
||||||
|
info "First #{KAMAL.primary_role} container is unhealthy on #{host}, not booting other roles"
|
||||||
|
error capture_with_info(*app.logs(version: version))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def barrier_role?
|
||||||
|
role == KAMAL.primary_role
|
||||||
|
end
|
||||||
|
|
||||||
|
def app
|
||||||
|
@app ||= KAMAL.app(role: role, host: host)
|
||||||
|
end
|
||||||
|
|
||||||
|
def auditor
|
||||||
|
@auditor = KAMAL.auditor(role: role)
|
||||||
|
end
|
||||||
|
|
||||||
|
def audit(message)
|
||||||
|
execute *auditor.record(message), verbosity: :debug
|
||||||
|
end
|
||||||
|
|
||||||
|
def gatekeeper?
|
||||||
|
barrier && barrier_role?
|
||||||
|
end
|
||||||
|
|
||||||
|
def queuer?
|
||||||
|
barrier && !barrier_role?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ class Kamal::Cli::App::PrepareAssets
|
|||||||
|
|
||||||
private
|
private
|
||||||
def app
|
def app
|
||||||
@app ||= KAMAL.app(role: role)
|
@app ||= KAMAL.app(role: role, host: host)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -79,11 +79,10 @@ module Kamal::Cli
|
|||||||
puts " Finished all in #{sprintf("%.1f seconds", runtime)}"
|
puts " Finished all in #{sprintf("%.1f seconds", runtime)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def mutating
|
def with_lock
|
||||||
return yield if KAMAL.holding_lock?
|
if KAMAL.holding_lock?
|
||||||
|
yield
|
||||||
run_hook "pre-connect"
|
else
|
||||||
|
|
||||||
ensure_run_and_locks_directory
|
ensure_run_and_locks_directory
|
||||||
|
|
||||||
acquire_lock
|
acquire_lock
|
||||||
@@ -91,17 +90,17 @@ module Kamal::Cli
|
|||||||
begin
|
begin
|
||||||
yield
|
yield
|
||||||
rescue
|
rescue
|
||||||
if KAMAL.hold_lock_on_error?
|
begin
|
||||||
error " \e[31mDeploy lock was not released\e[0m"
|
|
||||||
else
|
|
||||||
release_lock
|
release_lock
|
||||||
|
rescue => e
|
||||||
|
say "Error releasing the deploy lock: #{e.message}", :red
|
||||||
end
|
end
|
||||||
|
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
|
||||||
release_lock
|
release_lock
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def confirming(question)
|
def confirming(question)
|
||||||
return yield if options[:confirmed]
|
return yield if options[:confirmed]
|
||||||
@@ -141,16 +140,6 @@ module Kamal::Cli
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def hold_lock_on_error
|
|
||||||
if KAMAL.hold_lock_on_error?
|
|
||||||
yield
|
|
||||||
else
|
|
||||||
KAMAL.hold_lock_on_error = true
|
|
||||||
yield
|
|
||||||
KAMAL.hold_lock_on_error = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def run_hook(hook, **extra_details)
|
def run_hook(hook, **extra_details)
|
||||||
if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook)
|
if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook)
|
||||||
details = { hosts: KAMAL.hosts.join(","), command: command, subcommand: subcommand }
|
details = { hosts: KAMAL.hosts.join(","), command: command, subcommand: subcommand }
|
||||||
@@ -164,6 +153,15 @@ module Kamal::Cli
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def on(*args, &block)
|
||||||
|
if !KAMAL.connected?
|
||||||
|
run_hook "pre-connect"
|
||||||
|
KAMAL.connected = true
|
||||||
|
end
|
||||||
|
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
def command
|
def command
|
||||||
@kamal_command ||= begin
|
@kamal_command ||= begin
|
||||||
invocation_class, invocation_commands = *first_invocation
|
invocation_class, invocation_commands = *first_invocation
|
||||||
|
|||||||
@@ -5,35 +5,45 @@ class Kamal::Cli::Build < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "deliver", "Build app and push app image to registry then pull image on servers"
|
desc "deliver", "Build app and push app image to registry then pull image on servers"
|
||||||
def deliver
|
def deliver
|
||||||
mutating do
|
|
||||||
push
|
push
|
||||||
pull
|
pull
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
desc "push", "Build and push app image to registry"
|
desc "push", "Build and push app image to registry"
|
||||||
def push
|
def push
|
||||||
mutating do
|
|
||||||
cli = self
|
cli = self
|
||||||
|
|
||||||
verify_local_dependencies
|
verify_local_dependencies
|
||||||
run_hook "pre-build"
|
run_hook "pre-build"
|
||||||
|
|
||||||
if (uncommitted_changes = Kamal::Git.uncommitted_changes).present?
|
uncommitted_changes = Kamal::Git.uncommitted_changes
|
||||||
say "The following paths have uncommitted changes:\n #{uncommitted_changes}", :yellow
|
|
||||||
|
if KAMAL.config.builder.git_clone?
|
||||||
|
if uncommitted_changes.present?
|
||||||
|
say "Building from a local git clone, so ignoring these uncommitted changes:\n #{uncommitted_changes}", :yellow
|
||||||
end
|
end
|
||||||
|
|
||||||
|
prepare_clone
|
||||||
|
elsif uncommitted_changes.present?
|
||||||
|
say "Building with uncommitted changes:\n #{uncommitted_changes}", :yellow
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get the command here to ensure the Dir.chdir doesn't interfere with it
|
||||||
|
push = KAMAL.builder.push
|
||||||
|
|
||||||
run_locally do
|
run_locally do
|
||||||
begin
|
begin
|
||||||
KAMAL.with_verbosity(:debug) do
|
KAMAL.with_verbosity(:debug) do
|
||||||
execute *KAMAL.builder.push
|
Dir.chdir(KAMAL.config.builder.build_directory) { execute *push }
|
||||||
end
|
end
|
||||||
rescue SSHKit::Command::Failed => e
|
rescue SSHKit::Command::Failed => e
|
||||||
if e.message =~ /(no builder)|(no such file or directory)/
|
if e.message =~ /(no builder)|(no such file or directory)/
|
||||||
warn "Missing compatible builder, so creating a new one first"
|
warn "Missing compatible builder, so creating a new one first"
|
||||||
|
|
||||||
if cli.create
|
if cli.create
|
||||||
KAMAL.with_verbosity(:debug) { execute *KAMAL.builder.push }
|
KAMAL.with_verbosity(:debug) do
|
||||||
|
Dir.chdir(KAMAL.config.builder.build_directory) { execute *push }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise
|
raise
|
||||||
@@ -41,11 +51,9 @@ class Kamal::Cli::Build < Kamal::Cli::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
desc "pull", "Pull app image from registry onto servers"
|
desc "pull", "Pull app image from registry onto servers"
|
||||||
def pull
|
def pull
|
||||||
mutating do
|
|
||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
execute *KAMAL.auditor.record("Pulled image with version #{KAMAL.config.version}"), verbosity: :debug
|
execute *KAMAL.auditor.record("Pulled image with version #{KAMAL.config.version}"), verbosity: :debug
|
||||||
execute *KAMAL.builder.clean, raise_on_non_zero_exit: false
|
execute *KAMAL.builder.clean, raise_on_non_zero_exit: false
|
||||||
@@ -53,11 +61,9 @@ class Kamal::Cli::Build < Kamal::Cli::Base
|
|||||||
execute *KAMAL.builder.validate_image
|
execute *KAMAL.builder.validate_image
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
desc "create", "Create a build setup"
|
desc "create", "Create a build setup"
|
||||||
def create
|
def create
|
||||||
mutating do
|
|
||||||
if (remote_host = KAMAL.config.builder.remote_host)
|
if (remote_host = KAMAL.config.builder.remote_host)
|
||||||
connect_to_remote_host(remote_host)
|
connect_to_remote_host(remote_host)
|
||||||
end
|
end
|
||||||
@@ -76,17 +82,14 @@ class Kamal::Cli::Build < Kamal::Cli::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
desc "remove", "Remove build setup"
|
desc "remove", "Remove build setup"
|
||||||
def remove
|
def remove
|
||||||
mutating do
|
|
||||||
run_locally do
|
run_locally do
|
||||||
debug "Using builder: #{KAMAL.builder.name}"
|
debug "Using builder: #{KAMAL.builder.name}"
|
||||||
execute *KAMAL.builder.remove
|
execute *KAMAL.builder.remove
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
desc "details", "Show build setup"
|
desc "details", "Show build setup"
|
||||||
def details
|
def details
|
||||||
@@ -114,10 +117,32 @@ class Kamal::Cli::Build < Kamal::Cli::Base
|
|||||||
def connect_to_remote_host(remote_host)
|
def connect_to_remote_host(remote_host)
|
||||||
remote_uri = URI.parse(remote_host)
|
remote_uri = URI.parse(remote_host)
|
||||||
if remote_uri.scheme == "ssh"
|
if remote_uri.scheme == "ssh"
|
||||||
options = { user: remote_uri.user, port: remote_uri.port }.compact
|
host = SSHKit::Host.new(
|
||||||
on(remote_uri.host, options) do
|
hostname: remote_uri.host,
|
||||||
|
ssh_options: { user: remote_uri.user, port: remote_uri.port }.compact
|
||||||
|
)
|
||||||
|
on(host, options) do
|
||||||
execute "true"
|
execute "true"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prepare_clone
|
||||||
|
run_locally do
|
||||||
|
begin
|
||||||
|
info "Cloning repo into build directory `#{KAMAL.config.builder.build_directory}`..."
|
||||||
|
|
||||||
|
execute *KAMAL.builder.create_clone_directory
|
||||||
|
execute *KAMAL.builder.clone
|
||||||
|
rescue SSHKit::Command::Failed => e
|
||||||
|
if e.message =~ /already exists and is not an empty directory/
|
||||||
|
info "Resetting local clone as `#{KAMAL.config.builder.build_directory}` already exists..."
|
||||||
|
|
||||||
|
KAMAL.builder.clone_reset_steps.each { |step| execute *step }
|
||||||
|
else
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,21 +3,16 @@ require "tempfile"
|
|||||||
class Kamal::Cli::Env < Kamal::Cli::Base
|
class Kamal::Cli::Env < Kamal::Cli::Base
|
||||||
desc "push", "Push the env file to the remote hosts"
|
desc "push", "Push the env file to the remote hosts"
|
||||||
def push
|
def push
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
execute *KAMAL.auditor.record("Pushed env files"), verbosity: :debug
|
execute *KAMAL.auditor.record("Pushed env files"), verbosity: :debug
|
||||||
|
|
||||||
KAMAL.roles_on(host).each do |role|
|
KAMAL.roles_on(host).each do |role|
|
||||||
execute *KAMAL.app(role: role).make_env_directory
|
execute *KAMAL.app(role: role, host: host).make_env_directory
|
||||||
upload! role.env.secrets_io, role.env.secrets_file, mode: 400
|
upload! role.env(host).secrets_io, role.env(host).secrets_file, mode: 400
|
||||||
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)
|
||||||
@@ -30,19 +25,15 @@ class Kamal::Cli::Env < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "delete", "Delete the env file from the remote hosts"
|
desc "delete", "Delete the env file from the remote hosts"
|
||||||
def delete
|
def delete
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
execute *KAMAL.auditor.record("Deleted env files"), verbosity: :debug
|
execute *KAMAL.auditor.record("Deleted env files"), verbosity: :debug
|
||||||
|
|
||||||
KAMAL.roles_on(host).each do |role|
|
KAMAL.roles_on(host).each do |role|
|
||||||
execute *KAMAL.app(role: role).remove_env_file
|
execute *KAMAL.app(role: role, host: host).remove_env_file
|
||||||
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,21 +0,0 @@
|
|||||||
class Kamal::Cli::Healthcheck < Kamal::Cli::Base
|
|
||||||
default_command :perform
|
|
||||||
|
|
||||||
desc "perform", "Health check current app version"
|
|
||||||
def perform
|
|
||||||
raise "The primary host is not configured to run Traefik" unless KAMAL.config.role(KAMAL.config.primary_role).running_traefik?
|
|
||||||
on(KAMAL.primary_host) do
|
|
||||||
begin
|
|
||||||
execute *KAMAL.healthcheck.run
|
|
||||||
Poller.wait_for_healthy { capture_with_info(*KAMAL.healthcheck.status) }
|
|
||||||
rescue Poller::HealthcheckError => e
|
|
||||||
error capture_with_info(*KAMAL.healthcheck.logs)
|
|
||||||
error capture_with_pretty_json(*KAMAL.healthcheck.container_health_log)
|
|
||||||
raise
|
|
||||||
ensure
|
|
||||||
execute *KAMAL.healthcheck.stop, raise_on_non_zero_exit: false
|
|
||||||
execute *KAMAL.healthcheck.remove, raise_on_non_zero_exit: false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
module Kamal::Cli::Healthcheck::Poller
|
|
||||||
extend self
|
|
||||||
|
|
||||||
TRAEFIK_UPDATE_DELAY = 5
|
|
||||||
|
|
||||||
class HealthcheckError < StandardError; end
|
|
||||||
|
|
||||||
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 HealthcheckError, "container not ready (#{status})"
|
|
||||||
end
|
|
||||||
rescue HealthcheckError => 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 HealthcheckError, "container not unhealthy (#{status})"
|
|
||||||
end
|
|
||||||
rescue HealthcheckError => 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
|
|
||||||
@@ -3,14 +3,14 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
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 setup
|
def setup
|
||||||
print_runtime do
|
print_runtime do
|
||||||
mutating do
|
with_lock do
|
||||||
invoke_options = deploy_options
|
invoke_options = deploy_options
|
||||||
|
|
||||||
say "Ensure Docker is installed...", :magenta
|
say "Ensure Docker is installed...", :magenta
|
||||||
invoke "kamal:cli:server:bootstrap", [], invoke_options
|
invoke "kamal:cli:server:bootstrap", [], invoke_options
|
||||||
|
|
||||||
say "Push env files...", :magenta
|
say "Evaluate and push env files...", :magenta
|
||||||
invoke "kamal:cli:env:push", [], invoke_options
|
invoke "kamal:cli:main:envify", [], invoke_options
|
||||||
|
|
||||||
invoke "kamal:cli:accessory:boot", [ "all" ], invoke_options
|
invoke "kamal:cli:accessory:boot", [ "all" ], invoke_options
|
||||||
deploy
|
deploy
|
||||||
@@ -22,7 +22,6 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
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 deploy
|
def deploy
|
||||||
runtime = print_runtime do
|
runtime = print_runtime do
|
||||||
mutating do
|
|
||||||
invoke_options = deploy_options
|
invoke_options = deploy_options
|
||||||
|
|
||||||
say "Log into image registry...", :magenta
|
say "Log into image registry...", :magenta
|
||||||
@@ -36,15 +35,11 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
invoke "kamal:cli:build:deliver", [], invoke_options
|
invoke "kamal:cli:build:deliver", [], invoke_options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
if KAMAL.config.role(KAMAL.config.primary_role).running_traefik?
|
|
||||||
say "Ensure app can pass healthcheck...", :magenta
|
|
||||||
invoke "kamal:cli:healthcheck:perform", [], invoke_options
|
|
||||||
end
|
|
||||||
|
|
||||||
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)
|
||||||
@@ -59,11 +54,10 @@ 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
|
||||||
mutating do
|
|
||||||
invoke_options = deploy_options
|
invoke_options = deploy_options
|
||||||
|
|
||||||
if options[:skip_push]
|
if options[:skip_push]
|
||||||
@@ -74,11 +68,9 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
invoke "kamal:cli:build:deliver", [], invoke_options
|
invoke "kamal:cli:build:deliver", [], invoke_options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
with_lock do
|
||||||
run_hook "pre-deploy"
|
run_hook "pre-deploy"
|
||||||
|
|
||||||
say "Ensure app can pass healthcheck...", :magenta
|
|
||||||
invoke "kamal:cli:healthcheck:perform", [], 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)
|
||||||
|
|
||||||
@@ -93,7 +85,7 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
def rollback(version)
|
def rollback(version)
|
||||||
rolled_back = false
|
rolled_back = false
|
||||||
runtime = print_runtime do
|
runtime = print_runtime do
|
||||||
mutating do
|
with_lock do
|
||||||
invoke_options = deploy_options
|
invoke_options = deploy_options
|
||||||
|
|
||||||
KAMAL.config.version = version
|
KAMAL.config.version = version
|
||||||
@@ -115,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
|
||||||
@@ -185,20 +177,24 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
env_path = ".env"
|
env_path = ".env"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if Pathname.new(File.expand_path(env_template_path)).exist?
|
||||||
File.write(env_path, ERB.new(File.read(env_template_path), trim_mode: "-").result, perm: 0600)
|
File.write(env_path, ERB.new(File.read(env_template_path), trim_mode: "-").result, perm: 0600)
|
||||||
|
|
||||||
unless options[:skip_push]
|
unless options[:skip_push]
|
||||||
reload_envs
|
reload_envs
|
||||||
invoke "kamal:cli:env:push", options
|
invoke "kamal:cli:env:push", options
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
puts "Skipping envify (no #{env_template_path} exist)"
|
||||||
|
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
|
||||||
mutating do
|
|
||||||
confirming "This will remove all containers and images. Are you sure?" do
|
confirming "This will remove all containers and images. Are you sure?" do
|
||||||
invoke "kamal:cli:traefik:remove", [], options.without(:confirmed)
|
with_lock do
|
||||||
|
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)
|
||||||
@@ -223,9 +219,6 @@ class Kamal::Cli::Main < Kamal::Cli::Base
|
|||||||
desc "env", "Manage environment files"
|
desc "env", "Manage environment files"
|
||||||
subcommand "env", Kamal::Cli::Env
|
subcommand "env", Kamal::Cli::Env
|
||||||
|
|
||||||
desc "healthcheck", "Healthcheck application"
|
|
||||||
subcommand "healthcheck", Kamal::Cli::Healthcheck
|
|
||||||
|
|
||||||
desc "lock", "Manage the deploy lock"
|
desc "lock", "Manage the deploy lock"
|
||||||
subcommand "lock", Kamal::Cli::Lock
|
subcommand "lock", Kamal::Cli::Lock
|
||||||
|
|
||||||
@@ -238,19 +231,19 @@ 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)
|
||||||
begin
|
begin
|
||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
KAMAL.roles_on(host).each do |role|
|
KAMAL.roles_on(host).each do |role|
|
||||||
container_id = capture_with_info(*KAMAL.app(role: role).container_id_for_version(version))
|
container_id = capture_with_info(*KAMAL.app(role: role, host: host).container_id_for_version(version))
|
||||||
raise "Container not found" unless container_id.present?
|
raise "Container not found" unless container_id.present?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue SSHKit::Runner::ExecuteError => e
|
rescue SSHKit::Runner::ExecuteError, SSHKit::Runner::MultipleExecuteError => e
|
||||||
if e.message =~ /Container not found/
|
if e.message =~ /Container not found/
|
||||||
say "Error looking for container version #{version}: #{e.message}"
|
say "Error looking for container version #{version}: #{e.message}"
|
||||||
return false
|
return false
|
||||||
|
|||||||
120
lib/kamal/cli/proxy.rb
Normal file
120
lib/kamal/cli/proxy.rb
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
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 "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
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
class Kamal::Cli::Prune < Kamal::Cli::Base
|
class Kamal::Cli::Prune < Kamal::Cli::Base
|
||||||
desc "all", "Prune unused images and stopped containers"
|
desc "all", "Prune unused images and stopped containers"
|
||||||
def all
|
def all
|
||||||
mutating do
|
with_lock do
|
||||||
containers
|
containers
|
||||||
images
|
images
|
||||||
end
|
end
|
||||||
@@ -9,7 +9,7 @@ class Kamal::Cli::Prune < Kamal::Cli::Base
|
|||||||
|
|
||||||
desc "images", "Prune unused images"
|
desc "images", "Prune unused images"
|
||||||
def images
|
def images
|
||||||
mutating do
|
with_lock do
|
||||||
on(KAMAL.hosts) do
|
on(KAMAL.hosts) do
|
||||||
execute *KAMAL.auditor.record("Pruned images"), verbosity: :debug
|
execute *KAMAL.auditor.record("Pruned images"), verbosity: :debug
|
||||||
execute *KAMAL.prune.dangling_images
|
execute *KAMAL.prune.dangling_images
|
||||||
@@ -24,11 +24,10 @@ class Kamal::Cli::Prune < Kamal::Cli::Base
|
|||||||
retain = options.fetch(:retain, KAMAL.config.retain_containers)
|
retain = options.fetch(:retain, KAMAL.config.retain_containers)
|
||||||
raise "retain must be at least 1" if retain < 1
|
raise "retain must be at least 1" if retain < 1
|
||||||
|
|
||||||
mutating do
|
with_lock do
|
||||||
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
|
||||||
|
|||||||
@@ -1,6 +1,29 @@
|
|||||||
class Kamal::Cli::Server < Kamal::Cli::Base
|
class Kamal::Cli::Server < Kamal::Cli::Base
|
||||||
|
desc "exec", "Run a custom command on the server (use --help to show options)"
|
||||||
|
option :interactive, type: :boolean, aliases: "-i", default: false, desc: "Run the command interactively (use for console/bash)"
|
||||||
|
def exec(cmd)
|
||||||
|
hosts = KAMAL.hosts | KAMAL.accessory_hosts
|
||||||
|
|
||||||
|
case
|
||||||
|
when options[:interactive]
|
||||||
|
host = KAMAL.primary_host
|
||||||
|
|
||||||
|
say "Running '#{cmd}' on #{host} interactively...", :magenta
|
||||||
|
|
||||||
|
run_locally { exec KAMAL.server.run_over_ssh(cmd, host: host) }
|
||||||
|
else
|
||||||
|
say "Running '#{cmd}' on #{hosts.join(', ')}...", :magenta
|
||||||
|
|
||||||
|
on(hosts) do |host|
|
||||||
|
execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{host}"), verbosity: :debug
|
||||||
|
puts_by_host host, capture_with_info(cmd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
desc "bootstrap", "Set up Docker to run Kamal apps"
|
desc "bootstrap", "Set up Docker to run Kamal apps"
|
||||||
def bootstrap
|
def bootstrap
|
||||||
|
with_lock do
|
||||||
missing = []
|
missing = []
|
||||||
|
|
||||||
on(KAMAL.hosts | KAMAL.accessory_hosts) do |host|
|
on(KAMAL.hosts | KAMAL.accessory_hosts) do |host|
|
||||||
@@ -22,4 +45,5 @@ class Kamal::Cli::Server < Kamal::Cli::Base
|
|||||||
|
|
||||||
run_hook "docker-setup"
|
run_hook "docker-setup"
|
||||||
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
|
|
||||||
mutating 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
|
|
||||||
mutating 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
|
|
||||||
mutating 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
|
|
||||||
mutating 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
|
|
||||||
mutating 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
|
|
||||||
mutating do
|
|
||||||
stop
|
|
||||||
remove_container
|
|
||||||
remove_image
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "remove_container", "Remove Traefik container from servers", hide: true
|
|
||||||
def remove_container
|
|
||||||
mutating 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
|
|
||||||
mutating do
|
|
||||||
on(KAMAL.traefik_hosts) do
|
|
||||||
execute *KAMAL.auditor.record("Removed traefik image"), verbosity: :debug
|
|
||||||
execute *KAMAL.traefik.remove_image
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -2,13 +2,13 @@ require "active_support/core_ext/enumerable"
|
|||||||
require "active_support/core_ext/module/delegation"
|
require "active_support/core_ext/module/delegation"
|
||||||
|
|
||||||
class Kamal::Commander
|
class Kamal::Commander
|
||||||
attr_accessor :verbosity, :holding_lock, :hold_lock_on_error
|
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
|
||||||
self.holding_lock = false
|
self.holding_lock = false
|
||||||
self.hold_lock_on_error = false
|
self.connected = false
|
||||||
@specifics = nil
|
@specifics = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -65,8 +65,8 @@ class Kamal::Commander
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def app(role: nil)
|
def app(role: nil, host: nil)
|
||||||
Kamal::Commands::App.new(config, role: role)
|
Kamal::Commands::App.new(config, role: role, host: host)
|
||||||
end
|
end
|
||||||
|
|
||||||
def accessory(name)
|
def accessory(name)
|
||||||
@@ -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
|
||||||
|
|
||||||
|
|
||||||
@@ -138,8 +134,8 @@ class Kamal::Commander
|
|||||||
self.holding_lock
|
self.holding_lock
|
||||||
end
|
end
|
||||||
|
|
||||||
def hold_lock_on_error?
|
def connected?
|
||||||
self.hold_lock_on_error
|
self.connected
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -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,13 +1,14 @@
|
|||||||
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 ]
|
||||||
|
|
||||||
attr_reader :role, :role
|
attr_reader :role, :host
|
||||||
|
|
||||||
def initialize(config, role: nil)
|
def initialize(config, role: nil, host: nil)
|
||||||
super(config)
|
super(config)
|
||||||
@role = role
|
@role = role
|
||||||
|
@host = host
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(hostname: nil)
|
def run(hostname: nil)
|
||||||
@@ -18,8 +19,7 @@ class Kamal::Commands::App < Kamal::Commands::Base
|
|||||||
*([ "--hostname", hostname ] if hostname),
|
*([ "--hostname", hostname ] if hostname),
|
||||||
"-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,
|
*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,
|
||||||
@@ -56,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}}'"),
|
||||||
@@ -70,11 +74,11 @@ class Kamal::Commands::App < Kamal::Commands::Base
|
|||||||
|
|
||||||
|
|
||||||
def make_env_directory
|
def make_env_directory
|
||||||
make_directory role.env.secrets_directory
|
make_directory role.env(host).secrets_directory
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_env_file
|
def remove_env_file
|
||||||
[ :rm, "-f", role.env.secrets_file ]
|
[ :rm, "-f", role.env(host).secrets_file ]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
module Kamal::Commands::App::Containers
|
module Kamal::Commands::App::Containers
|
||||||
|
DOCKER_HEALTH_LOG_FORMAT = "'{{json .State.Health}}'"
|
||||||
|
|
||||||
def list_containers
|
def list_containers
|
||||||
docker :container, :ls, "--all", *filter_args
|
docker :container, :ls, "--all", *filter_args
|
||||||
end
|
end
|
||||||
@@ -20,4 +22,11 @@ module Kamal::Commands::App::Containers
|
|||||||
def remove_containers
|
def remove_containers
|
||||||
docker :container, :prune, "--force", *filter_args
|
docker :container, :prune, "--force", *filter_args
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def container_endpoint(version:)
|
||||||
|
pipe \
|
||||||
|
container_id_for(container_name: container_name(version)),
|
||||||
|
xargs(docker(:inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'")),
|
||||||
|
[ :sed, "-e", "'s/\\/tcp$//'" ]
|
||||||
|
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
|
|
||||||
@@ -11,7 +11,7 @@ module Kamal::Commands::App::Execution
|
|||||||
docker :run,
|
docker :run,
|
||||||
("-it" if interactive),
|
("-it" if interactive),
|
||||||
"--rm",
|
"--rm",
|
||||||
*role&.env_args,
|
*role&.env_args(host),
|
||||||
*argumentize("--env", env),
|
*argumentize("--env", env),
|
||||||
*config.volume_args,
|
*config.volume_args,
|
||||||
*role&.option_args,
|
*role&.option_args,
|
||||||
@@ -19,11 +19,11 @@ module Kamal::Commands::App::Execution
|
|||||||
*command
|
*command
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute_in_existing_container_over_ssh(*command, host:, env:)
|
def execute_in_existing_container_over_ssh(*command, env:)
|
||||||
run_over_ssh execute_in_existing_container(*command, interactive: true, env: env), host: host
|
run_over_ssh execute_in_existing_container(*command, interactive: true, env: env), host: host
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute_in_new_container_over_ssh(*command, host:, env:)
|
def execute_in_new_container_over_ssh(*command, env:)
|
||||||
run_over_ssh execute_in_new_container(*command, interactive: true, env: env), host: host
|
run_over_ssh execute_in_new_container(*command, interactive: true, env: env), host: host
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module Kamal::Commands::App::Logging
|
module Kamal::Commands::App::Logging
|
||||||
def logs(since: nil, lines: nil, grep: nil)
|
def logs(version: nil, since: nil, lines: nil, grep: nil)
|
||||||
pipe \
|
pipe \
|
||||||
current_running_container_id,
|
version ? container_id_for_version(version) : current_running_container_id,
|
||||||
"xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1",
|
"xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1",
|
||||||
("grep '#{grep}'" if grep)
|
("grep '#{grep}'" if grep)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ module Kamal::Commands
|
|||||||
delegate :sensitive, :argumentize, to: Kamal::Utils
|
delegate :sensitive, :argumentize, to: Kamal::Utils
|
||||||
|
|
||||||
DOCKER_HEALTH_STATUS_FORMAT = "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'"
|
DOCKER_HEALTH_STATUS_FORMAT = "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'"
|
||||||
DOCKER_HEALTH_LOG_FORMAT = "'{{json .State.Health}}'"
|
|
||||||
|
|
||||||
attr_accessor :config
|
attr_accessor :config
|
||||||
|
|
||||||
@@ -78,8 +77,8 @@ module Kamal::Commands
|
|||||||
args.compact.unshift :docker
|
args.compact.unshift :docker
|
||||||
end
|
end
|
||||||
|
|
||||||
def git(*args)
|
def git(*args, path: nil)
|
||||||
args.compact.unshift :git
|
[ :git, *([ "-C", path ] if path), *args.compact ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags(**details)
|
def tags(**details)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ require "active_support/core_ext/string/filters"
|
|||||||
|
|
||||||
class Kamal::Commands::Builder < Kamal::Commands::Base
|
class Kamal::Commands::Builder < Kamal::Commands::Base
|
||||||
delegate :create, :remove, :push, :clean, :pull, :info, :validate_image, to: :target
|
delegate :create, :remove, :push, :clean, :pull, :info, :validate_image, to: :target
|
||||||
|
delegate :clone_directory, :build_directory, to: :"config.builder"
|
||||||
|
|
||||||
def name
|
def name
|
||||||
target.class.to_s.remove("Kamal::Commands::Builder::").underscore.inquiry
|
target.class.to_s.remove("Kamal::Commands::Builder::").underscore.inquiry
|
||||||
@@ -53,6 +54,23 @@ class Kamal::Commands::Builder < Kamal::Commands::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_clone_directory
|
||||||
|
make_directory clone_directory
|
||||||
|
end
|
||||||
|
|
||||||
|
def clone
|
||||||
|
git :clone, Kamal::Git.root, path: clone_directory
|
||||||
|
end
|
||||||
|
|
||||||
|
def clone_reset_steps
|
||||||
|
[
|
||||||
|
git(:remote, "set-url", :origin, Kamal::Git.root, path: build_directory),
|
||||||
|
git(:fetch, :origin, path: build_directory),
|
||||||
|
git(:reset, "--hard", Kamal::Git.revision, path: build_directory),
|
||||||
|
git(:clean, "-fdx", path: build_directory)
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def ensure_local_docker_installed
|
def ensure_local_docker_installed
|
||||||
docker "--version"
|
docker "--version"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
|
|||||||
class BuilderError < StandardError; end
|
class BuilderError < StandardError; end
|
||||||
|
|
||||||
delegate :argumentize, to: Kamal::Utils
|
delegate :argumentize, to: Kamal::Utils
|
||||||
delegate :args, :secrets, :dockerfile, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, :git_archive?, to: :builder_config
|
delegate :args, :secrets, :dockerfile, :target, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, to: :builder_config
|
||||||
|
|
||||||
def clean
|
def clean
|
||||||
docker :image, :rm, "--force", config.absolute_image
|
docker :image, :rm, "--force", config.absolute_image
|
||||||
@@ -13,18 +13,8 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
|
|||||||
docker :pull, config.absolute_image
|
docker :pull, config.absolute_image
|
||||||
end
|
end
|
||||||
|
|
||||||
def push
|
|
||||||
if git_archive?
|
|
||||||
pipe \
|
|
||||||
git(:archive, "--format=tar", :HEAD),
|
|
||||||
build_and_push
|
|
||||||
else
|
|
||||||
build_and_push
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_options
|
def build_options
|
||||||
[ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_ssh ]
|
[ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_target, *build_ssh ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_context
|
def build_context
|
||||||
@@ -73,6 +63,10 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_target
|
||||||
|
argumentize "--target", target if target.present?
|
||||||
|
end
|
||||||
|
|
||||||
def build_ssh
|
def build_ssh
|
||||||
argumentize "--ssh", ssh if ssh.present?
|
argumentize "--ssh", ssh if ssh.present?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,15 @@ class Kamal::Commands::Builder::Multiarch < Kamal::Commands::Builder::Base
|
|||||||
docker(:buildx, :ls)
|
docker(:buildx, :ls)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def push
|
||||||
|
docker :buildx, :build,
|
||||||
|
"--push",
|
||||||
|
"--platform", platform_names,
|
||||||
|
"--builder", builder_name,
|
||||||
|
*build_options,
|
||||||
|
build_context
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def builder_name
|
def builder_name
|
||||||
"kamal-#{config.service}-multiarch"
|
"kamal-#{config.service}-multiarch"
|
||||||
@@ -25,13 +34,4 @@ class Kamal::Commands::Builder::Multiarch < Kamal::Commands::Builder::Base
|
|||||||
"linux/amd64,linux/arm64"
|
"linux/amd64,linux/arm64"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_and_push
|
|
||||||
docker :buildx, :build,
|
|
||||||
"--push",
|
|
||||||
"--platform", platform_names,
|
|
||||||
"--builder", builder_name,
|
|
||||||
*build_options,
|
|
||||||
build_context
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,8 +11,7 @@ class Kamal::Commands::Builder::Native < Kamal::Commands::Builder::Base
|
|||||||
# No-op on native
|
# No-op on native
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def push
|
||||||
def build_and_push
|
|
||||||
combine \
|
combine \
|
||||||
docker(:build, *build_options, build_context),
|
docker(:build, *build_options, build_context),
|
||||||
docker(:push, config.absolute_image),
|
docker(:push, config.absolute_image),
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ class Kamal::Commands::Builder::Native::Cached < Kamal::Commands::Builder::Nativ
|
|||||||
docker :buildx, :rm, builder_name
|
docker :buildx, :rm, builder_name
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def push
|
||||||
def build_and_push
|
|
||||||
docker :buildx, :build,
|
docker :buildx, :build,
|
||||||
"--push",
|
"--push",
|
||||||
*build_options,
|
*build_options,
|
||||||
|
|||||||
@@ -17,6 +17,15 @@ class Kamal::Commands::Builder::Native::Remote < Kamal::Commands::Builder::Nativ
|
|||||||
docker(:buildx, :ls)
|
docker(:buildx, :ls)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def push
|
||||||
|
docker :buildx, :build,
|
||||||
|
"--push",
|
||||||
|
"--platform", platform,
|
||||||
|
"--builder", builder_name,
|
||||||
|
*build_options,
|
||||||
|
build_context
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def builder_name
|
def builder_name
|
||||||
@@ -47,13 +56,4 @@ class Kamal::Commands::Builder::Native::Remote < Kamal::Commands::Builder::Nativ
|
|||||||
def remove_buildx
|
def remove_buildx
|
||||||
docker :buildx, :rm, builder_name
|
docker :buildx, :rm, builder_name
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_and_push
|
|
||||||
docker :buildx, :build,
|
|
||||||
"--push",
|
|
||||||
"--platform", platform,
|
|
||||||
"--builder", builder_name,
|
|
||||||
*build_options,
|
|
||||||
build_context
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,59 +0,0 @@
|
|||||||
class Kamal::Commands::Healthcheck < Kamal::Commands::Base
|
|
||||||
def run
|
|
||||||
primary = config.role(config.primary_role)
|
|
||||||
|
|
||||||
docker :run,
|
|
||||||
"--detach",
|
|
||||||
"--name", container_name_with_version,
|
|
||||||
"--publish", "#{exposed_port}:#{config.healthcheck["port"]}",
|
|
||||||
"--label", "service=#{config.healthcheck_service}",
|
|
||||||
"-e", "KAMAL_CONTAINER_NAME=\"#{config.healthcheck_service}\"",
|
|
||||||
*primary.env_args,
|
|
||||||
*primary.health_check_args(cord: false),
|
|
||||||
*config.volume_args,
|
|
||||||
*primary.option_args,
|
|
||||||
config.absolute_image,
|
|
||||||
primary.cmd
|
|
||||||
end
|
|
||||||
|
|
||||||
def status
|
|
||||||
pipe container_id, xargs(docker(:inspect, "--format", DOCKER_HEALTH_STATUS_FORMAT))
|
|
||||||
end
|
|
||||||
|
|
||||||
def container_health_log
|
|
||||||
pipe container_id, xargs(docker(:inspect, "--format", DOCKER_HEALTH_LOG_FORMAT))
|
|
||||||
end
|
|
||||||
|
|
||||||
def logs
|
|
||||||
pipe container_id, xargs(docker(:logs, "--tail", log_lines, "2>&1"))
|
|
||||||
end
|
|
||||||
|
|
||||||
def stop
|
|
||||||
pipe container_id, xargs(docker(:stop))
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove
|
|
||||||
pipe container_id, xargs(docker(:container, :rm))
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def container_name_with_version
|
|
||||||
"#{config.healthcheck_service}-#{config.version}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def container_id
|
|
||||||
container_id_for(container_name: container_name_with_version)
|
|
||||||
end
|
|
||||||
|
|
||||||
def health_url
|
|
||||||
"http://localhost:#{exposed_port}#{config.healthcheck["path"]}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def exposed_port
|
|
||||||
config.healthcheck["exposed_port"]
|
|
||||||
end
|
|
||||||
|
|
||||||
def log_lines
|
|
||||||
config.healthcheck["log_lines"]
|
|
||||||
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/parachute",
|
||||||
|
*config.logging_args,
|
||||||
|
*proxy_config.docker_options_args,
|
||||||
|
proxy_config.image
|
||||||
|
end
|
||||||
|
|
||||||
|
def start
|
||||||
|
docker :container, :start, container_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop
|
||||||
|
docker :container, :stop, container_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_or_run
|
||||||
|
combine start, run, by: "||"
|
||||||
|
end
|
||||||
|
|
||||||
|
def deploy(service, target:)
|
||||||
|
optionize({ target: target })
|
||||||
|
docker :exec, container_name, :parachute, :deploy, service, *optionize({ target: target }), *proxy_config.deploy_command_args
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove(service, target:)
|
||||||
|
docker :exec, container_name, :parachute, :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
|
||||||
|
docker :container, :prune, "--force", "--filter", container_filter
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_image
|
||||||
|
docker :image, :prune, "--all", "--force", "--filter", image_filter
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def container_filter
|
||||||
|
"label=org.opencontainers.image.title=parachute"
|
||||||
|
end
|
||||||
|
|
||||||
|
def image_filter
|
||||||
|
"label=org.opencontainers.image.title=parachute"
|
||||||
|
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, "exposed_port" => 3999, "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
|
||||||
@@ -233,6 +225,18 @@ class Kamal::Configuration
|
|||||||
raw_config.env || {}
|
raw_config.env || {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def env_tags
|
||||||
|
@env_tags ||= if (tags = raw_config.env["tags"])
|
||||||
|
tags.collect { |name, config| Kamal::Configuration::Env::Tag.new(name, config: config) }
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def env_tag(name)
|
||||||
|
env_tags.detect { |t| t.name == name.to_s }
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version && ensure_retain_containers_valid && ensure_valid_service_name
|
ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version && ensure_retain_containers_valid && ensure_valid_service_name
|
||||||
@@ -252,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
|
||||||
|
|
||||||
@@ -328,7 +331,7 @@ class Kamal::Configuration
|
|||||||
def git_version
|
def git_version
|
||||||
@git_version ||=
|
@git_version ||=
|
||||||
if Kamal::Git.used?
|
if Kamal::Git.used?
|
||||||
if Kamal::Git.uncommitted_changes.present? && !builder.git_archive?
|
if Kamal::Git.uncommitted_changes.present? && !builder.git_clone?
|
||||||
uncommitted_suffix = "_uncommitted_#{SecureRandom.hex(8)}"
|
uncommitted_suffix = "_uncommitted_#{SecureRandom.hex(8)}"
|
||||||
end
|
end
|
||||||
[ Kamal::Git.revision, uncommitted_suffix ].compact.join
|
[ Kamal::Git.revision, uncommitted_suffix ].compact.join
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ class Kamal::Configuration::Builder
|
|||||||
@options = config.raw_config.builder || {}
|
@options = config.raw_config.builder || {}
|
||||||
@image = config.image
|
@image = config.image
|
||||||
@server = config.registry["server"]
|
@server = config.registry["server"]
|
||||||
|
@service = config.service
|
||||||
|
@destination = config.destination
|
||||||
|
|
||||||
valid?
|
valid?
|
||||||
end
|
end
|
||||||
@@ -39,8 +41,12 @@ class Kamal::Configuration::Builder
|
|||||||
@options["dockerfile"] || "Dockerfile"
|
@options["dockerfile"] || "Dockerfile"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def target
|
||||||
|
@options["target"]
|
||||||
|
end
|
||||||
|
|
||||||
def context
|
def context
|
||||||
@options["context"] || (git_archive? ? "-" : ".")
|
@options["context"] || "."
|
||||||
end
|
end
|
||||||
|
|
||||||
def local_arch
|
def local_arch
|
||||||
@@ -85,10 +91,23 @@ class Kamal::Configuration::Builder
|
|||||||
@options["ssh"]
|
@options["ssh"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def git_archive?
|
def git_clone?
|
||||||
Kamal::Git.used? && @options["context"].nil?
|
Kamal::Git.used? && @options["context"].nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def clone_directory
|
||||||
|
@clone_directory ||= File.join Dir.tmpdir, "kamal-clones", [ @service, pwd_sha ].compact.join("-")
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_directory
|
||||||
|
@build_directory ||=
|
||||||
|
if git_clone?
|
||||||
|
File.join clone_directory, repo_basename, repo_relative_pwd
|
||||||
|
else
|
||||||
|
"."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def valid?
|
def valid?
|
||||||
if @options["cache"] && @options["cache"]["type"]
|
if @options["cache"] && @options["cache"]["type"]
|
||||||
@@ -119,4 +138,16 @@ class Kamal::Configuration::Builder
|
|||||||
def cache_to_config_for_registry
|
def cache_to_config_for_registry
|
||||||
[ "type=registry", @options["cache"]&.fetch("options", nil), "ref=#{cache_image_ref}" ].compact.join(",")
|
[ "type=registry", @options["cache"]&.fetch("options", nil), "ref=#{cache_image_ref}" ].compact.join(",")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def repo_basename
|
||||||
|
File.basename(Kamal::Git.root)
|
||||||
|
end
|
||||||
|
|
||||||
|
def repo_relative_pwd
|
||||||
|
Dir.pwd.delete_prefix(Kamal::Git.root)
|
||||||
|
end
|
||||||
|
|
||||||
|
def pwd_sha
|
||||||
|
Digest::SHA256.hexdigest(Dir.pwd)[0..12]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ class Kamal::Configuration::Env
|
|||||||
|
|
||||||
def self.from_config(config:, secrets_file: nil)
|
def self.from_config(config:, secrets_file: nil)
|
||||||
secrets_keys = config.fetch("secret", [])
|
secrets_keys = config.fetch("secret", [])
|
||||||
clear = config.fetch("clear", config.key?("secret") ? {} : config)
|
clear = config.fetch("clear", config.key?("secret") || config.key?("tags") ? {} : config)
|
||||||
|
|
||||||
new clear: clear, secrets_keys: secrets_keys, secrets_file: secrets_file
|
new clear: clear, secrets_keys: secrets_keys, secrets_file: secrets_file
|
||||||
end
|
end
|
||||||
|
|||||||
12
lib/kamal/configuration/env/tag.rb
vendored
Normal file
12
lib/kamal/configuration/env/tag.rb
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class Kamal::Configuration::Env::Tag
|
||||||
|
attr_reader :name, :config
|
||||||
|
|
||||||
|
def initialize(name, config:)
|
||||||
|
@name = name
|
||||||
|
@config = config
|
||||||
|
end
|
||||||
|
|
||||||
|
def env
|
||||||
|
Kamal::Configuration::Env.from_config(config: config)
|
||||||
|
end
|
||||||
|
end
|
||||||
58
lib/kamal/configuration/proxy.rb
Normal file
58
lib/kamal/configuration/proxy.rb
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
class Kamal::Configuration::Proxy
|
||||||
|
DEFAULT_HTTP_PORT = 80
|
||||||
|
DEFAULT_HTTPS_PORT = 443
|
||||||
|
DEFAULT_IMAGE = "basecamp/parachute: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
|
||||||
|
if options.key?(:http_port)
|
||||||
|
options[:http_port]
|
||||||
|
elsif !automatic_tls?
|
||||||
|
DEFAULT_HTTP_PORT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def https_port
|
||||||
|
if options.key?(:http_port)
|
||||||
|
options[:http_port]
|
||||||
|
elsif automatic_tls?
|
||||||
|
DEFAULT_HTTPS_PORT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def container_name
|
||||||
|
"parachute_#{http_port}_#{https_port}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def docker_options_args
|
||||||
|
optionize(options.fetch("options", {}))
|
||||||
|
end
|
||||||
|
|
||||||
|
def publish_args
|
||||||
|
argumentize "--publish", *("#{http_port}:80" if http_port), *("#{https_port}:80" 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
|
||||||
@@ -7,6 +6,7 @@ class Kamal::Configuration::Role
|
|||||||
|
|
||||||
def initialize(name, config:)
|
def initialize(name, config:)
|
||||||
@name, @config = name.inquiry, config
|
@name, @config = name.inquiry, config
|
||||||
|
@tagged_hosts ||= extract_tagged_hosts_from_config
|
||||||
end
|
end
|
||||||
|
|
||||||
def primary_host
|
def primary_host
|
||||||
@@ -14,7 +14,11 @@ class Kamal::Configuration::Role
|
|||||||
end
|
end
|
||||||
|
|
||||||
def hosts
|
def hosts
|
||||||
@hosts ||= extract_hosts_from_config
|
tagged_hosts.keys
|
||||||
|
end
|
||||||
|
|
||||||
|
def env_tags(host)
|
||||||
|
tagged_hosts.fetch(host).collect { |tag| config.env_tag(tag) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def cmd
|
def cmd
|
||||||
@@ -30,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
|
||||||
@@ -50,12 +54,13 @@ class Kamal::Configuration::Role
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def env
|
def env(host)
|
||||||
@env ||= base_env.merge(specialized_env)
|
@envs ||= {}
|
||||||
|
@envs[host] ||= [ base_env, specialized_env, *env_tags(host).map(&:env) ].reduce(:merge)
|
||||||
end
|
end
|
||||||
|
|
||||||
def env_args
|
def env_args(host)
|
||||||
env.args
|
env(host).args
|
||||||
end
|
end
|
||||||
|
|
||||||
def asset_volume_args
|
def asset_volume_args
|
||||||
@@ -63,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
|
||||||
|
|
||||||
@@ -102,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
|
||||||
@@ -145,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)
|
||||||
@@ -164,7 +114,24 @@ class Kamal::Configuration::Role
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
attr_accessor :config
|
attr_accessor :config, :tagged_hosts
|
||||||
|
|
||||||
|
def extract_tagged_hosts_from_config
|
||||||
|
{}.tap do |tagged_hosts|
|
||||||
|
extract_hosts_from_config.map do |host_config|
|
||||||
|
if host_config.is_a?(Hash)
|
||||||
|
raise ArgumentError, "Multiple hosts found: #{host_config.inspect}" unless host_config.size == 1
|
||||||
|
|
||||||
|
host, tags = host_config.first
|
||||||
|
tagged_hosts[host] = Array(tags)
|
||||||
|
elsif host_config.is_a?(String) || host_config.is_a?(Symbol)
|
||||||
|
tagged_hosts[host_config] = []
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Invalid host config: #{host_config.inspect}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def extract_hosts_from_config
|
def extract_hosts_from_config
|
||||||
if config.servers.is_a?(Array)
|
if config.servers.is_a?(Array)
|
||||||
@@ -179,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?
|
||||||
@@ -225,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
|
||||||
|
|||||||
@@ -16,4 +16,8 @@ module Kamal::Git
|
|||||||
def uncommitted_changes
|
def uncommitted_changes
|
||||||
`git status --porcelain`.strip
|
`git status --porcelain`.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def root
|
||||||
|
`git rev-parse --show-toplevel`.strip
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -103,3 +103,39 @@ class SSHKit::Backend::Netssh
|
|||||||
|
|
||||||
prepend LimitConcurrentStartsInstance
|
prepend LimitConcurrentStartsInstance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SSHKit::Runner::Parallel
|
||||||
|
# SSHKit joins the threads in sequence and fails on the first error it encounters, which means that we wait threads
|
||||||
|
# before the first failure to complete but not for ones after.
|
||||||
|
#
|
||||||
|
# We'll patch it to wait for them all to complete, and to record all the threads that errored so we can see when a
|
||||||
|
# problem occurs on multiple hosts.
|
||||||
|
module CompleteAll
|
||||||
|
def execute
|
||||||
|
threads = hosts.map do |host|
|
||||||
|
Thread.new(host) do |h|
|
||||||
|
backend(h, &block).run
|
||||||
|
rescue ::StandardError => e
|
||||||
|
e2 = SSHKit::Runner::ExecuteError.new e
|
||||||
|
raise e2, "Exception while executing #{host.user ? "as #{host.user}@" : "on host "}#{host}: #{e.message}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
exceptions = []
|
||||||
|
threads.each do |t|
|
||||||
|
begin
|
||||||
|
t.join
|
||||||
|
rescue SSHKit::Runner::ExecuteError => e
|
||||||
|
exceptions << e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if exceptions.one?
|
||||||
|
raise exceptions.first
|
||||||
|
elsif exceptions.many?
|
||||||
|
raise exceptions.first, [ "Exceptions on #{exceptions.count} hosts:", exceptions.map(&:message) ].join("\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
prepend CompleteAll
|
||||||
|
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
|
||||||
@@ -54,33 +46,27 @@ class CliAppTest < CliTestCase
|
|||||||
run_command("boot", config: :with_boot_strategy)
|
run_command("boot", config: :with_boot_strategy)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "boot errors leave lock in place" do
|
test "boot errors don't leave lock in place" do
|
||||||
Kamal::Cli::App.any_instance.expects(:using_version).raises(RuntimeError)
|
Kamal::Cli::App.any_instance.expects(:using_version).raises(RuntimeError)
|
||||||
|
|
||||||
assert_not KAMAL.holding_lock?
|
assert_not KAMAL.holding_lock?
|
||||||
assert_raises(RuntimeError) do
|
assert_raises(RuntimeError) do
|
||||||
stderred { run_command("boot") }
|
stderred { run_command("boot") }
|
||||||
end
|
end
|
||||||
assert KAMAL.holding_lock?
|
assert_not KAMAL.holding_lock?
|
||||||
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
|
||||||
@@ -92,7 +78,91 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "boot with host tags" do
|
||||||
|
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
|
||||||
|
|
||||||
|
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") # 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", "'{{.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("boot", config: :with_env_tags).tap do |output|
|
||||||
|
assert_match "docker tag dhh/app:latest dhh/app:latest", output
|
||||||
|
assert_match %r{docker run --detach --restart unless-stopped --name app-web-latest --hostname 1.1.1.1-[0-9a-f]{12} -e KAMAL_CONTAINER_NAME="app-web-latest" -e KAMAL_VERSION="latest" --env-file .kamal/env/roles/app-web.env --env TEST="root" --env EXPERIMENT="disabled" --env SITE="site1"}, output
|
||||||
|
assert_match "docker container ls --all --filter name=^app-web-123$ --quiet | xargs docker stop", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "boot with web barrier opened" do
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("")
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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.4...", output
|
||||||
|
assert_match "First web container is healthy, booting workers on 1.1.1.3", output
|
||||||
|
assert_match "First web container is healthy, booting workers on 1.1.1.4", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "boot with web barrier closed" do
|
||||||
|
Thread.report_on_exception = false
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info).returns("")
|
||||||
|
|
||||||
|
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("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, "parachute_80_", :parachute, :deploy, "app-web", "--target", "\"172.1.0.2:80\"").raises(SSHKit::Command::Failed, "Deploy failed").at_least_once
|
||||||
|
|
||||||
|
stderred do
|
||||||
|
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.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.4", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
Thread.report_on_exception = true
|
||||||
|
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
|
||||||
@@ -236,22 +306,36 @@ class CliAppTest < CliTestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "long hostname" do
|
||||||
|
stub_running
|
||||||
|
|
||||||
|
hostname = "this-hostname-is-really-unacceptably-long-to-be-honest.example.com"
|
||||||
|
|
||||||
|
stdouted { Kamal::Cli::App.start([ "boot", "-c", "test/fixtures/deploy_with_uncommon_hostnames.yml", "--hosts", hostname ]) }.tap do |output|
|
||||||
|
assert_match /docker run --detach --restart unless-stopped --name app-web-latest --hostname this-hostname-is-really-unacceptably-long-to-be-hon-[0-9a-f]{12} /, output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "hostname is trimmed if will end with a period" do
|
||||||
|
stub_running
|
||||||
|
|
||||||
|
hostname = "this-hostname-with-random-part-is-too-long.example.com"
|
||||||
|
|
||||||
|
stdouted { Kamal::Cli::App.start([ "boot", "-c", "test/fixtures/deploy_with_uncommon_hostnames.yml", "--hosts", hostname ]) }.tap do |output|
|
||||||
|
assert_match /docker run --detach --restart unless-stopped --name app-web-latest --hostname this-hostname-with-random-part-is-too-long.example-[0-9a-f]{12} /, output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def run_command(*command, config: :with_accessories)
|
def run_command(*command, config: :with_accessories, host: "1.1.1.1", allowed_error_message: nil)
|
||||||
stdouted { Kamal::Cli::App.start([ *command, "-c", "test/fixtures/deploy_#{config}.yml", "--hosts", "1.1.1.1" ]) }
|
stdouted do
|
||||||
|
Kamal::Cli::App.start([ *command, "-c", "test/fixtures/deploy_#{config}.yml", *([ "--hosts", host ] if host) ])
|
||||||
|
rescue SSHKit::Runner::ExecuteError => e
|
||||||
|
raise e unless allowed_error_message && e.message.include?(allowed_error_message)
|
||||||
|
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
|
||||||
|
|||||||
@@ -9,18 +9,65 @@ class CliBuildTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "push" do
|
test "push" do
|
||||||
|
with_build_directory do
|
||||||
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
|
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
|
||||||
hook_variables = { version: 999, service_version: "app@999", hosts: "1.1.1.1,1.1.1.2,1.1.1.3,1.1.1.4", command: "build", subcommand: "push" }
|
hook_variables = { version: 999, service_version: "app@999", hosts: "1.1.1.1,1.1.1.2,1.1.1.3,1.1.1.4", command: "build", subcommand: "push" }
|
||||||
|
|
||||||
run_command("push", "--verbose").tap do |output|
|
run_command("push", "--verbose").tap do |output|
|
||||||
assert_hook_ran "pre-build", output, **hook_variables
|
assert_hook_ran "pre-build", output, **hook_variables
|
||||||
|
assert_match /Cloning repo into build directory/, output
|
||||||
|
assert_match /git -C #{Dir.tmpdir}\/kamal-clones\/app-#{pwd_sha} clone #{Dir.pwd}/, output
|
||||||
assert_match /docker --version && docker buildx version/, output
|
assert_match /docker --version && docker buildx version/, output
|
||||||
assert_match /git archive -tar HEAD | docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder kamal-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile - as .*@localhost/, output
|
assert_match /docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder kamal-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile \. as .*@localhost/, output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "push reseting clone" do
|
||||||
|
with_build_directory do
|
||||||
|
stub_setup
|
||||||
|
build_dir = "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}/kamal/"
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:docker, "--version", "&&", :docker, :buildx, "version")
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:docker, :buildx, :create, "--use", "--name", "kamal-app-multiarch")
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:mkdir, "-p", "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}")
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
|
.with(:git, "-C", "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}", :clone, Dir.pwd)
|
||||||
|
.raises(SSHKit::Command::Failed.new("fatal: destination path 'kamal' already exists and is not an empty directory"))
|
||||||
|
.then
|
||||||
|
.returns(true)
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:git, "-C", build_dir, :remote, "set-url", :origin, Dir.pwd)
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:git, "-C", build_dir, :fetch, :origin)
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:git, "-C", build_dir, :reset, "--hard", Kamal::Git.revision)
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:git, "-C", build_dir, :clean, "-fdx")
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
|
.with(:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "kamal-app-multiarch", "-t", "dhh/app:999", "-t", "dhh/app:latest", "--label", "service=\"app\"", "--file", "Dockerfile", ".")
|
||||||
|
|
||||||
|
run_command("push", "--verbose").tap do |output|
|
||||||
|
assert_match /Cloning repo into build directory/, output
|
||||||
|
assert_match /Resetting local clone/, output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "push without clone" do
|
||||||
|
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
|
||||||
|
hook_variables = { version: 999, service_version: "app@999", hosts: "1.1.1.1,1.1.1.2,1.1.1.3,1.1.1.4", command: "build", subcommand: "push" }
|
||||||
|
|
||||||
|
run_command("push", "--verbose", fixture: :without_clone).tap do |output|
|
||||||
|
assert_no_match /Cloning repo into build directory/, output
|
||||||
|
assert_hook_ran "pre-build", output, **hook_variables
|
||||||
|
assert_match /docker --version && docker buildx version/, output
|
||||||
|
assert_match /docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder kamal-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile . as .*@localhost/, output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "push without builder" do
|
test "push without builder" do
|
||||||
|
with_build_directory do
|
||||||
stub_setup
|
stub_setup
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
.with(:docker, "--version", "&&", :docker, :buildx, "version")
|
.with(:docker, "--version", "&&", :docker, :buildx, "version")
|
||||||
|
|
||||||
@@ -28,15 +75,20 @@ class CliBuildTest < CliTestCase
|
|||||||
.with(:docker, :buildx, :create, "--use", "--name", "kamal-app-multiarch")
|
.with(:docker, :buildx, :create, "--use", "--name", "kamal-app-multiarch")
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
.with { |*args| p args[0..6]; args[0..6] == [ :git, :archive, "--format=tar", :HEAD, "|", :docker, :buildx ] }
|
.with { |*args| args[0..1] == [ :docker, :buildx ] }
|
||||||
.raises(SSHKit::Command::Failed.new("no builder"))
|
.raises(SSHKit::Command::Failed.new("no builder"))
|
||||||
.then
|
.then
|
||||||
.returns(true)
|
.returns(true)
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with { |*args| args.first.start_with?("git") }
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute).with(:mkdir, "-p", "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}")
|
||||||
|
|
||||||
run_command("push").tap do |output|
|
run_command("push").tap do |output|
|
||||||
assert_match /WARN Missing compatible builder, so creating a new one first/, output
|
assert_match /WARN Missing compatible builder, so creating a new one first/, output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test "push with no buildx plugin" do
|
test "push with no buildx plugin" do
|
||||||
stub_setup
|
stub_setup
|
||||||
@@ -79,6 +131,14 @@ class CliBuildTest < CliTestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "create remote with custom ports" do
|
||||||
|
run_command("create", fixture: :with_remote_builder_and_custom_ports).tap do |output|
|
||||||
|
assert_match "Running /usr/bin/env true on 1.1.1.5", output
|
||||||
|
assert_match "docker context create kamal-app-native-remote-amd64 --description 'kamal-app-native-remote amd64 native host' --docker 'host=ssh://app@1.1.1.5:2122'", output
|
||||||
|
assert_match "docker buildx create --name kamal-app-native-remote kamal-app-native-remote-amd64 --platform linux/amd64", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test "create with error" do
|
test "create with error" do
|
||||||
stub_setup
|
stub_setup
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
@@ -118,4 +178,17 @@ class CliBuildTest < CliTestCase
|
|||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
.with { |*args| args[0..1] == [ :docker, :buildx ] }
|
.with { |*args| args[0..1] == [ :docker, :buildx ] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_build_directory
|
||||||
|
build_directory = File.join Dir.tmpdir, "kamal-clones", "app-#{pwd_sha}", "kamal"
|
||||||
|
FileUtils.mkdir_p build_directory
|
||||||
|
FileUtils.touch File.join build_directory, "Dockerfile"
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
FileUtils.rm_rf build_directory
|
||||||
|
end
|
||||||
|
|
||||||
|
def pwd_sha
|
||||||
|
Digest::SHA256.hexdigest(Dir.pwd)[0..12]
|
||||||
|
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
|
||||||
|
|||||||
@@ -1,82 +0,0 @@
|
|||||||
require_relative "cli_test_case"
|
|
||||||
|
|
||||||
class CliHealthcheckTest < CliTestCase
|
|
||||||
test "perform" do
|
|
||||||
# Prevent expected failures from outputting to terminal
|
|
||||||
Thread.report_on_exception = false
|
|
||||||
|
|
||||||
Kamal::Cli::Healthcheck::Poller.stubs(:sleep) # No sleeping when retrying
|
|
||||||
Kamal::Configuration.any_instance.stubs(:run_id).returns("12345678901234567890123456789012")
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :stop, raise_on_non_zero_exit: false)
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
|
||||||
.with(:docker, :run, "--detach", "--name", "healthcheck-app-999", "--publish", "3999:3000", "--label", "service=healthcheck-app", "-e", "KAMAL_CONTAINER_NAME=\"healthcheck-app\"", "--env-file", ".kamal/env/roles/app-web.env", "--health-cmd", "\"curl -f http://localhost:3000/up || exit 1\"", "--health-interval", "\"1s\"", "dhh/app:999")
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :container, :rm, raise_on_non_zero_exit: false)
|
|
||||||
|
|
||||||
# Fail twice to test retry logic
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("starting")
|
|
||||||
.then
|
|
||||||
.returns("unhealthy")
|
|
||||||
.then
|
|
||||||
.returns("healthy")
|
|
||||||
|
|
||||||
run_command("perform").tap do |output|
|
|
||||||
assert_match "container not ready (starting), retrying in 1s (attempt 1/7)...", output
|
|
||||||
assert_match "container not ready (unhealthy), retrying in 2s (attempt 2/7)...", output
|
|
||||||
assert_match "Container is healthy!", output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "perform failing to become healthy" do
|
|
||||||
# Prevent expected failures from outputting to terminal
|
|
||||||
Thread.report_on_exception = false
|
|
||||||
|
|
||||||
Kamal::Cli::Healthcheck::Poller.stubs(:sleep) # No sleeping when retrying
|
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :stop, raise_on_non_zero_exit: false)
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
|
||||||
.with(:docker, :run, "--detach", "--name", "healthcheck-app-999", "--publish", "3999:3000", "--label", "service=healthcheck-app", "-e", "KAMAL_CONTAINER_NAME=\"healthcheck-app\"", "--env-file", ".kamal/env/roles/app-web.env", "--health-cmd", "\"curl -f http://localhost:3000/up || exit 1\"", "--health-interval", "\"1s\"", "dhh/app:999")
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :container, :rm, raise_on_non_zero_exit: false)
|
|
||||||
|
|
||||||
# Continually report unhealthy
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'")
|
|
||||||
.returns("unhealthy")
|
|
||||||
|
|
||||||
# Capture logs when failing
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_info)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :logs, "--tail", 50, "2>&1")
|
|
||||||
.returns("some log output")
|
|
||||||
|
|
||||||
# Capture container health log when failing
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture_with_pretty_json)
|
|
||||||
.with(:docker, :container, :ls, "--all", "--filter", "name=^healthcheck-app-999$", "--quiet", "|", :xargs, :docker, :inspect, "--format", "'{{json .State.Health}}'")
|
|
||||||
.returns('{"Status":"unhealthy","Log":[{"ExitCode": 1,"Output": "/bin/sh: 1: curl: not found\n"}]}"')
|
|
||||||
|
|
||||||
exception = assert_raises do
|
|
||||||
run_command("perform")
|
|
||||||
end
|
|
||||||
assert_match "container not ready (unhealthy)", exception.message
|
|
||||||
end
|
|
||||||
|
|
||||||
test "raises an exception if primary does not have traefik" do
|
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).never
|
|
||||||
|
|
||||||
exception = assert_raises do
|
|
||||||
run_command("perform", config_file: "test/fixtures/deploy_workers_only.yml")
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal "The primary host is not configured to run Traefik", exception.message
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def run_command(*command, config_file: "test/fixtures/deploy_with_accessories.yml")
|
|
||||||
stdouted { Kamal::Cli::Healthcheck.start([ *command, "-c", config_file ]) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -5,13 +5,13 @@ class CliMainTest < CliTestCase
|
|||||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
|
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
|
||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:server:bootstrap", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:server:bootstrap", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:main:envify", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:boot", [ "all" ], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:boot", [ "all" ], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:deploy)
|
Kamal::Cli::Main.any_instance.expects(:deploy)
|
||||||
|
|
||||||
run_command("setup").tap do |output|
|
run_command("setup").tap do |output|
|
||||||
assert_match /Ensure Docker is installed.../, output
|
assert_match /Ensure Docker is installed.../, output
|
||||||
assert_match /Push env files.../, output
|
assert_match /Evaluate and push env files.../, output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -19,26 +19,24 @@ class CliMainTest < CliTestCase
|
|||||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
|
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
|
||||||
|
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:server:bootstrap", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:server:bootstrap", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push", [], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:main:envify", [], invoke_options)
|
||||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:boot", [ "all" ], invoke_options)
|
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:boot", [ "all" ], invoke_options)
|
||||||
# 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:healthcheck:perform", [], 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)
|
||||||
|
|
||||||
run_command("setup", "--skip_push").tap do |output|
|
run_command("setup", "--skip_push").tap do |output|
|
||||||
assert_match /Ensure Docker is installed.../, output
|
assert_match /Ensure Docker is installed.../, output
|
||||||
assert_match /Push env files.../, output
|
assert_match /Evaluate and push env files.../, output
|
||||||
# deploy
|
# deploy
|
||||||
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 /Ensure app can pass healthcheck/, 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
|
||||||
@@ -50,8 +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:healthcheck:perform", [], 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)
|
||||||
@@ -64,8 +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 /Ensure app can pass healthcheck/, 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
|
||||||
@@ -77,8 +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:healthcheck:perform", [], 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)
|
||||||
@@ -87,8 +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 /Ensure app can pass healthcheck/, 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
|
||||||
@@ -98,6 +92,9 @@ class CliMainTest < CliTestCase
|
|||||||
test "deploy when locked" do
|
test "deploy when locked" do
|
||||||
Thread.report_on_exception = false
|
Thread.report_on_exception = false
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
|
Dir.stubs(:chdir)
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
.with { |*args| args == [ :mkdir, "-p", ".kamal" ] }
|
.with { |*args| args == [ :mkdir, "-p", ".kamal" ] }
|
||||||
|
|
||||||
@@ -119,6 +116,9 @@ class CliMainTest < CliTestCase
|
|||||||
test "deploy error when locking" do
|
test "deploy error when locking" do
|
||||||
Thread.report_on_exception = false
|
Thread.report_on_exception = false
|
||||||
|
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
|
Dir.stubs(:chdir)
|
||||||
|
|
||||||
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
|
||||||
.with { |*args| args == [ :mkdir, "-p", ".kamal" ] }
|
.with { |*args| args == [ :mkdir, "-p", ".kamal" ] }
|
||||||
|
|
||||||
@@ -153,8 +153,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:healthcheck:perform", [], 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)
|
||||||
@@ -164,28 +163,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:healthcheck:perform", [], 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)
|
||||||
@@ -197,7 +180,6 @@ class CliMainTest < CliTestCase
|
|||||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false, "verbose" => true }
|
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false, "verbose" => true }
|
||||||
|
|
||||||
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:healthcheck:perform", [], 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)
|
||||||
|
|
||||||
@@ -210,7 +192,6 @@ class CliMainTest < CliTestCase
|
|||||||
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 /Running the pre-deploy hook.../, output
|
assert_match /Running the pre-deploy hook.../, output
|
||||||
assert_match /Ensure app can pass healthcheck/, output
|
|
||||||
assert_hook_ran "post-deploy", output, **hook_variables, runtime: true
|
assert_hook_ran "post-deploy", output, **hook_variables, runtime: true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -219,13 +200,11 @@ class CliMainTest < CliTestCase
|
|||||||
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
|
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
|
||||||
|
|
||||||
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:healthcheck:perform", [], 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)
|
||||||
|
|
||||||
run_command("redeploy", "--skip_push").tap do |output|
|
run_command("redeploy", "--skip_push").tap do |output|
|
||||||
assert_match /Pull app image/, output
|
assert_match /Pull app image/, output
|
||||||
assert_match /Ensure app can pass healthcheck/, output
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -241,7 +220,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)
|
||||||
@@ -252,18 +230,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" }
|
||||||
@@ -280,8 +251,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
|
||||||
@@ -289,8 +258,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
|
||||||
@@ -299,7 +268,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" ])
|
||||||
|
|
||||||
@@ -427,6 +396,7 @@ class CliMainTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "envify" do
|
test "envify" do
|
||||||
|
Pathname.any_instance.expects(:exist?).returns(true).times(3)
|
||||||
File.expects(:read).with(".env.erb").returns("HELLO=<%= 'world' %>")
|
File.expects(:read).with(".env.erb").returns("HELLO=<%= 'world' %>")
|
||||||
File.expects(:write).with(".env", "HELLO=world", perm: 0600)
|
File.expects(:write).with(".env", "HELLO=world", perm: 0600)
|
||||||
|
|
||||||
@@ -441,6 +411,7 @@ class CliMainTest < CliTestCase
|
|||||||
<% end -%>
|
<% end -%>
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
Pathname.any_instance.expects(:exist?).returns(true).times(3)
|
||||||
File.expects(:read).with(".env.erb").returns(file.strip)
|
File.expects(:read).with(".env.erb").returns(file.strip)
|
||||||
File.expects(:write).with(".env", "HELLO=world\nKEY=value\n", perm: 0600)
|
File.expects(:write).with(".env", "HELLO=world\nKEY=value\n", perm: 0600)
|
||||||
|
|
||||||
@@ -448,6 +419,7 @@ class CliMainTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "envify with destination" do
|
test "envify with destination" do
|
||||||
|
Pathname.any_instance.expects(:exist?).returns(true).times(4)
|
||||||
File.expects(:read).with(".env.world.erb").returns("HELLO=<%= 'world' %>")
|
File.expects(:read).with(".env.world.erb").returns("HELLO=<%= 'world' %>")
|
||||||
File.expects(:write).with(".env.world", "HELLO=world", perm: 0600)
|
File.expects(:write).with(".env.world", "HELLO=world", perm: 0600)
|
||||||
|
|
||||||
@@ -455,6 +427,7 @@ class CliMainTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "envify with skip_push" do
|
test "envify with skip_push" do
|
||||||
|
Pathname.any_instance.expects(:exist?).returns(true).times(1)
|
||||||
File.expects(:read).with(".env.erb").returns("HELLO=<%= 'world' %>")
|
File.expects(:read).with(".env.erb").returns("HELLO=<%= 'world' %>")
|
||||||
File.expects(:write).with(".env", "HELLO=world", perm: 0600)
|
File.expects(:write).with(".env", "HELLO=world", perm: 0600)
|
||||||
|
|
||||||
@@ -464,9 +437,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 parachute/, 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=parachute/, 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=parachute/, 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
|
||||||
|
|||||||
94
test/cli/proxy_test.rb
Normal file
94
test/cli/proxy_test.rb
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
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 parachute_80_ --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume parachute_80_:/root/.config/parachute --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 parachute", output
|
||||||
|
assert_match "docker container prune --force --filter label=org.opencontainers.image.title=parachute", output
|
||||||
|
assert_match "docker run --name parachute_80_ --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume parachute_80_:/root/.config/parachute --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=parachute on 1.1.1.1", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "start" do
|
||||||
|
run_command("start").tap do |output|
|
||||||
|
assert_match "docker container start parachute", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "stop" do
|
||||||
|
run_command("stop").tap do |output|
|
||||||
|
assert_match "docker container stop parachute", 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=^parachute_80_$", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "logs" do
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||||
|
.with(:docker, :logs, "parachute_80_", " --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 parachute_80_ --timestamps --tail 10 --follow 2>&1'")
|
||||||
|
|
||||||
|
assert_match "docker logs parachute_80_ --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=parachute", 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=parachute", 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,14 +1,30 @@
|
|||||||
require_relative "cli_test_case"
|
require_relative "cli_test_case"
|
||||||
|
|
||||||
class CliServerTest < CliTestCase
|
class CliServerTest < CliTestCase
|
||||||
|
test "running a command with exec" do
|
||||||
|
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||||
|
.with("date", verbosity: 1)
|
||||||
|
.returns("Today")
|
||||||
|
|
||||||
|
hosts = "1.1.1.1".."1.1.1.4"
|
||||||
|
run_command("exec", "date").tap do |output|
|
||||||
|
hosts.map do |host|
|
||||||
|
assert_match "Running 'date' on #{hosts.to_a.join(', ')}...", output
|
||||||
|
assert_match "App Host: #{host}\nToday", output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test "bootstrap already installed" do
|
test "bootstrap already installed" do
|
||||||
|
stub_setup
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(true).at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(true).at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once
|
||||||
|
|
||||||
assert_equal "", run_command("bootstrap")
|
assert_equal "Acquiring the deploy lock...\nReleasing the deploy lock...", run_command("bootstrap")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "bootstrap install as non-root user" do
|
test "bootstrap install as non-root user" do
|
||||||
|
stub_setup
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(false).at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(false).at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with('[ "${EUID:-$(id -u)}" -eq 0 ] || command -v sudo >/dev/null || command -v su >/dev/null', raise_on_non_zero_exit: false).returns(false).at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with('[ "${EUID:-$(id -u)}" -eq 0 ] || command -v sudo >/dev/null || command -v su >/dev/null', raise_on_non_zero_exit: false).returns(false).at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once
|
||||||
@@ -19,11 +35,13 @@ class CliServerTest < CliTestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "bootstrap install as root user" do
|
test "bootstrap install as root user" do
|
||||||
|
stub_setup
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(false).at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(false).at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with('[ "${EUID:-$(id -u)}" -eq 0 ] || command -v sudo >/dev/null || command -v su >/dev/null', raise_on_non_zero_exit: false).returns(true).at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with('[ "${EUID:-$(id -u)}" -eq 0 ] || command -v sudo >/dev/null || command -v su >/dev/null', raise_on_non_zero_exit: false).returns(true).at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:sh, "-c", "'curl -fsSL https://get.docker.com || wget -O - https://get.docker.com || echo \"exit 1\"'", "|", :sh).at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:sh, "-c", "'curl -fsSL https://get.docker.com || wget -O - https://get.docker.com || echo \"exit 1\"'", "|", :sh).at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once
|
||||||
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
|
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
|
||||||
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(".kamal/hooks/pre-connect", anything).at_least_once
|
||||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(".kamal/hooks/docker-setup", anything).at_least_once
|
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(".kamal/hooks/docker-setup", anything).at_least_once
|
||||||
|
|
||||||
run_command("bootstrap").tap do |output|
|
run_command("bootstrap").tap do |output|
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -99,6 +99,11 @@ class CommanderTest < ActiveSupport::TestCase
|
|||||||
assert_equal [ "workers" ], @kamal.roles_on("1.1.1.3").map(&:name)
|
assert_equal [ "workers" ], @kamal.roles_on("1.1.1.3").map(&:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "roles_on web comes first" do
|
||||||
|
configure_with(:deploy_with_two_roles_one_host)
|
||||||
|
assert_equal [ "web", "workers" ], @kamal.roles_on("1.1.1.1").map(&:name)
|
||||||
|
end
|
||||||
|
|
||||||
test "default group strategy" do
|
test "default group strategy" do
|
||||||
assert_empty @kamal.boot_strategy
|
assert_empty @kamal.boot_strategy
|
||||||
end
|
end
|
||||||
@@ -131,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
|
||||||
|
|
||||||
@@ -60,14 +36,14 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
@config[:servers] = { "web" => [ "1.1.1.1" ], "jobs" => { "hosts" => [ "1.1.1.2" ], "cmd" => "bin/jobs", "options" => { "mount" => "somewhere", "cap-add" => true } } }
|
@config[:servers] = { "web" => [ "1.1.1.1" ], "jobs" => { "hosts" => [ "1.1.1.2" ], "cmd" => "bin/jobs", "options" => { "mount" => "somewhere", "cap-add" => true } } }
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"docker run --detach --restart unless-stopped --name app-jobs-999 -e KAMAL_CONTAINER_NAME=\"app-jobs-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-jobs.env --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"jobs\" --label destination --mount \"somewhere\" --cap-add dhh/app:999 bin/jobs",
|
"docker run --detach --restart unless-stopped --name app-jobs-999 -e KAMAL_CONTAINER_NAME=\"app-jobs-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/env/roles/app-jobs.env --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"jobs\" --label destination --mount \"somewhere\" --cap-add dhh/app:999 bin/jobs",
|
||||||
new_command(role: "jobs").run.join(" ")
|
new_command(role: "jobs", host: "1.1.1.2").run.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "run with logging config" do
|
test "run with logging config" do
|
||||||
@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,16 @@ 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(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "run with tags" do
|
||||||
|
@config[:servers] = [ { "1.1.1.1" => "tag1" } ]
|
||||||
|
@config[:env]["tags"] = { "tag1" => { "ENV1" => "value1" } }
|
||||||
|
|
||||||
|
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\" --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
|
||||||
|
|
||||||
@@ -183,6 +168,15 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
new_command.execute_in_new_container("bin/rails", "db:setup", env: { "foo" => "bar" }).join(" ")
|
new_command.execute_in_new_container("bin/rails", "db:setup", env: { "foo" => "bar" }).join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "execute in new container with tags" do
|
||||||
|
@config[:servers] = [ { "1.1.1.1" => "tag1" } ]
|
||||||
|
@config[:env]["tags"] = { "tag1" => { "ENV1" => "value1" } }
|
||||||
|
|
||||||
|
assert_equal \
|
||||||
|
"docker run --rm --env-file .kamal/env/roles/app-web.env --env ENV1=\"value1\" dhh/app:999 bin/rails db:setup",
|
||||||
|
new_command.execute_in_new_container("bin/rails", "db:setup", env: {}).join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
test "execute in new container with custom options" do
|
test "execute in new container with custom options" do
|
||||||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere", "cap-add" => true } } }
|
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere", "cap-add" => true } } }
|
||||||
assert_equal \
|
assert_equal \
|
||||||
@@ -204,18 +198,26 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
test "execute in new container over ssh" do
|
test "execute in new container over ssh" do
|
||||||
assert_match %r{docker run -it --rm --env-file .kamal/env/roles/app-web.env dhh/app:999 bin/rails c},
|
assert_match %r{docker run -it --rm --env-file .kamal/env/roles/app-web.env dhh/app:999 bin/rails c},
|
||||||
new_command.execute_in_new_container_over_ssh("bin/rails", "c", host: "app-1", env: {})
|
new_command.execute_in_new_container_over_ssh("bin/rails", "c", env: {})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "execute in new container over ssh with tags" do
|
||||||
|
@config[:servers] = [ { "1.1.1.1" => "tag1" } ]
|
||||||
|
@config[:env]["tags"] = { "tag1" => { "ENV1" => "value1" } }
|
||||||
|
|
||||||
|
assert_equal "ssh -t root@1.1.1.1 -p 22 'docker run -it --rm --env-file .kamal/env/roles/app-web.env --env ENV1=\"value1\" dhh/app:999 bin/rails c'",
|
||||||
|
new_command.execute_in_new_container_over_ssh("bin/rails", "c", env: {})
|
||||||
end
|
end
|
||||||
|
|
||||||
test "execute in new container with custom options over ssh" do
|
test "execute in new container with custom options over ssh" do
|
||||||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere", "cap-add" => true } } }
|
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere", "cap-add" => true } } }
|
||||||
assert_match %r{docker run -it --rm --env-file .kamal/env/roles/app-web.env --mount \"somewhere\" --cap-add dhh/app:999 bin/rails c},
|
assert_match %r{docker run -it --rm --env-file .kamal/env/roles/app-web.env --mount \"somewhere\" --cap-add dhh/app:999 bin/rails c},
|
||||||
new_command.execute_in_new_container_over_ssh("bin/rails", "c", host: "app-1", env: {})
|
new_command.execute_in_new_container_over_ssh("bin/rails", "c", env: {})
|
||||||
end
|
end
|
||||||
|
|
||||||
test "execute in existing container over ssh" do
|
test "execute in existing container over ssh" do
|
||||||
assert_match %r{docker exec -it app-web-999 bin/rails c},
|
assert_match %r{docker exec -it app-web-999 bin/rails c},
|
||||||
new_command.execute_in_existing_container_over_ssh("bin/rails", "c", host: "app-1", env: {})
|
new_command.execute_in_existing_container_over_ssh("bin/rails", "c", env: {})
|
||||||
end
|
end
|
||||||
|
|
||||||
test "run over ssh" do
|
test "run over ssh" do
|
||||||
@@ -372,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", "&&",
|
||||||
@@ -418,8 +406,8 @@ class CommandsAppTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def new_command(role: "web", **additional_config)
|
def new_command(role: "web", host: "1.1.1.1", **additional_config)
|
||||||
config = Kamal::Configuration.new(@config.merge(additional_config), destination: @destination, version: "999")
|
config = Kamal::Configuration.new(@config.merge(additional_config), destination: @destination, version: "999")
|
||||||
Kamal::Commands::App.new(config, role: config.role(role))
|
Kamal::Commands::App.new(config, role: config.role(role), host: host)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
builder = new_builder_command(builder: { "cache" => { "type" => "gha" } })
|
builder = new_builder_command(builder: { "cache" => { "type" => "gha" } })
|
||||||
assert_equal "multiarch", builder.name
|
assert_equal "multiarch", builder.name
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker buildx build --push --platform linux/amd64,linux/arm64 --builder kamal-app-multiarch -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile -",
|
"docker buildx build --push --platform linux/amd64,linux/arm64 --builder kamal-app-multiarch -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile .",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
builder = new_builder_command(builder: { "multiarch" => false })
|
builder = new_builder_command(builder: { "multiarch" => false })
|
||||||
assert_equal "native", builder.name
|
assert_equal "native", builder.name
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile - && docker push dhh/app:123 && docker push dhh/app:latest",
|
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile . && docker push dhh/app:123 && docker push dhh/app:latest",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
builder = new_builder_command(builder: { "multiarch" => false, "cache" => { "type" => "gha" } })
|
builder = new_builder_command(builder: { "multiarch" => false, "cache" => { "type" => "gha" } })
|
||||||
assert_equal "native/cached", builder.name
|
assert_equal "native/cached", builder.name
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker buildx build --push -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile -",
|
"docker buildx build --push -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile .",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
builder = new_builder_command(builder: { "local" => {}, "remote" => {}, "cache" => { "type" => "gha" } })
|
builder = new_builder_command(builder: { "local" => {}, "remote" => {}, "cache" => { "type" => "gha" } })
|
||||||
assert_equal "multiarch/remote", builder.name
|
assert_equal "multiarch/remote", builder.name
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker buildx build --push --platform linux/amd64,linux/arm64 --builder kamal-app-multiarch-remote -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile -",
|
"docker buildx build --push --platform linux/amd64,linux/arm64 --builder kamal-app-multiarch-remote -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile .",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
builder = new_builder_command(builder: { "local" => { "arch" => "amd64" } })
|
builder = new_builder_command(builder: { "local" => { "arch" => "amd64" } })
|
||||||
assert_equal "multiarch", builder.name
|
assert_equal "multiarch", builder.name
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker buildx build --push --platform linux/amd64 --builder kamal-app-multiarch -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile -",
|
"docker buildx build --push --platform linux/amd64 --builder kamal-app-multiarch -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile .",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" }, "cache" => { "type" => "gha" } })
|
builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" }, "cache" => { "type" => "gha" } })
|
||||||
assert_equal "native/remote", builder.name
|
assert_equal "native/remote", builder.name
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker buildx build --push --platform linux/amd64 --builder kamal-app-native-remote -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile -",
|
"docker buildx build --push --platform linux/amd64 --builder kamal-app-native-remote -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile .",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -83,6 +83,13 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "build target" do
|
||||||
|
builder = new_builder_command(builder: { "target" => "prod" })
|
||||||
|
assert_equal \
|
||||||
|
"-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile --target prod",
|
||||||
|
builder.target.build_options.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
test "build context" do
|
test "build context" do
|
||||||
builder = new_builder_command(builder: { "context" => ".." })
|
builder = new_builder_command(builder: { "context" => ".." })
|
||||||
assert_equal \
|
assert_equal \
|
||||||
@@ -93,21 +100,21 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
test "native push with build args" do
|
test "native push with build args" do
|
||||||
builder = new_builder_command(builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } })
|
builder = new_builder_command(builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } })
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile - && docker push dhh/app:123 && docker push dhh/app:latest",
|
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile . && docker push dhh/app:123 && docker push dhh/app:latest",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "multiarch push with build args" do
|
test "multiarch push with build args" do
|
||||||
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
|
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker buildx build --push --platform linux/amd64,linux/arm64 --builder kamal-app-multiarch -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile -",
|
"docker buildx build --push --platform linux/amd64,linux/arm64 --builder kamal-app-multiarch -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile .",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "native push with build secrets" do
|
test "native push with build secrets" do
|
||||||
builder = new_builder_command(builder: { "multiarch" => false, "secrets" => [ "a", "b" ] })
|
builder = new_builder_command(builder: { "multiarch" => false, "secrets" => [ "a", "b" ] })
|
||||||
assert_equal \
|
assert_equal \
|
||||||
"git archive --format=tar HEAD | docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"a\" --secret id=\"b\" --file Dockerfile - && docker push dhh/app:123 && docker push dhh/app:latest",
|
"docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"a\" --secret id=\"b\" --file Dockerfile . && docker push dhh/app:123 && docker push dhh/app:latest",
|
||||||
builder.push.join(" ")
|
builder.push.join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -155,4 +162,8 @@ class CommandsBuilderTest < ActiveSupport::TestCase
|
|||||||
def new_builder_command(additional_config = {})
|
def new_builder_command(additional_config = {})
|
||||||
Kamal::Commands::Builder.new(Kamal::Configuration.new(@config.merge(additional_config), version: "123"))
|
Kamal::Commands::Builder.new(Kamal::Configuration.new(@config.merge(additional_config), version: "123"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_directory
|
||||||
|
"#{Dir.tmpdir}/kamal-clones/app/kamal/"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,114 +0,0 @@
|
|||||||
require "test_helper"
|
|
||||||
|
|
||||||
class CommandsHealthcheckTest < ActiveSupport::TestCase
|
|
||||||
setup do
|
|
||||||
@config = {
|
|
||||||
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
|
|
||||||
|
|
||||||
test "run" do
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --env-file .kamal/env/roles/app-web.env --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" dhh/app:123",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with custom port" do
|
|
||||||
@config[:healthcheck] = { "port" => 3001 }
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3001 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --env-file .kamal/env/roles/app-web.env --health-cmd \"curl -f http://localhost:3001/up || exit 1\" --health-interval \"1s\" dhh/app:123",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with destination" do
|
|
||||||
@destination = "staging"
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --name healthcheck-app-staging-123 --publish 3999:3000 --label service=healthcheck-app-staging -e KAMAL_CONTAINER_NAME=\"healthcheck-app-staging\" --env-file .kamal/env/roles/app-web-staging.env --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" dhh/app:123",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with custom healthcheck" do
|
|
||||||
@config[:healthcheck] = { "cmd" => "/bin/up" }
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --env-file .kamal/env/roles/app-web.env --health-cmd \"/bin/up\" --health-interval \"1s\" dhh/app:123",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "run with custom options" do
|
|
||||||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere" } } }
|
|
||||||
@config[:healthcheck] = { "exposed_port" => 4999 }
|
|
||||||
assert_equal \
|
|
||||||
"docker run --detach --name healthcheck-app-123 --publish 4999:3000 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --env-file .kamal/env/roles/app-web.env --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --mount \"somewhere\" dhh/app:123",
|
|
||||||
new_command.run.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "status" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'",
|
|
||||||
new_command.status.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "container_health_log" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker inspect --format '{{json .State.Health}}'",
|
|
||||||
new_command.container_health_log.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "stop" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker stop",
|
|
||||||
new_command.stop.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "stop with destination" do
|
|
||||||
@destination = "staging"
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-staging-123$ --quiet | xargs docker stop",
|
|
||||||
new_command.stop.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "remove" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker container rm",
|
|
||||||
new_command.remove.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "remove with destination" do
|
|
||||||
@destination = "staging"
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-staging-123$ --quiet | xargs docker container rm",
|
|
||||||
new_command.remove.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "logs" do
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker logs --tail 50 2>&1",
|
|
||||||
new_command.logs.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "logs with custom lines number" do
|
|
||||||
@config[:healthcheck] = { "log_lines" => 150 }
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-123$ --quiet | xargs docker logs --tail 150 2>&1",
|
|
||||||
new_command.logs.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
test "logs with destination" do
|
|
||||||
@destination = "staging"
|
|
||||||
|
|
||||||
assert_equal \
|
|
||||||
"docker container ls --all --filter name=^healthcheck-app-staging-123$ --quiet | xargs docker logs --tail 50 2>&1",
|
|
||||||
new_command.logs.join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def new_command
|
|
||||||
Kamal::Commands::Healthcheck.new(Kamal::Configuration.new(@config, destination: @destination, version: "123"))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -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 parachute_80_ --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume parachute_80_:/root/.config/parachute --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 parachute_80_ --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume parachute_80_:/root/.config/parachute --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 parachute_80_ --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume parachute_80_:/root/.config/parachute --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 parachute_80_ --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume parachute_80_:/root/.config/parachute --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 parachute_80_",
|
||||||
|
new_command.start.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy stop" do
|
||||||
|
assert_equal \
|
||||||
|
"docker container stop parachute_80_",
|
||||||
|
new_command.stop.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy info" do
|
||||||
|
assert_equal \
|
||||||
|
"docker ps --filter name=^parachute_80_$",
|
||||||
|
new_command.info.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs parachute_80_ --timestamps 2>&1",
|
||||||
|
new_command.logs.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs since 2h" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs parachute_80_ --since 2h --timestamps 2>&1",
|
||||||
|
new_command.logs(since: "2h").join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs last 10 lines" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs parachute_80_ --tail 10 --timestamps 2>&1",
|
||||||
|
new_command.logs(lines: 10).join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy logs with grep hello!" do
|
||||||
|
assert_equal \
|
||||||
|
"docker logs parachute_80_ --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=parachute",
|
||||||
|
new_command.remove_container.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy remove image" do
|
||||||
|
assert_equal \
|
||||||
|
"docker image prune --all --force --filter label=org.opencontainers.image.title=parachute",
|
||||||
|
new_command.remove_image.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy follow logs" do
|
||||||
|
assert_equal \
|
||||||
|
"ssh -t root@1.1.1.1 -p 22 'docker logs parachute_80_ --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 parachute_80_ --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 parachute_80_ parachute 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 parachute_80_ parachute 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
|
|
||||||
@@ -140,7 +140,7 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "context" do
|
test "context" do
|
||||||
assert_equal "-", @config.builder.context
|
assert_equal ".", @config.builder.context
|
||||||
end
|
end
|
||||||
|
|
||||||
test "setting context" do
|
test "setting context" do
|
||||||
|
|||||||
112
test/configuration/env/tags_test.rb
vendored
Normal file
112
test/configuration/env/tags_test.rb
vendored
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ConfigurationEnvTagsTest < ActiveSupport::TestCase
|
||||||
|
setup do
|
||||||
|
@deploy = {
|
||||||
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
|
||||||
|
servers: [ { "1.1.1.1" => "odd" }, { "1.1.1.2" => "even" }, { "1.1.1.3" => [ "odd", "three" ] } ],
|
||||||
|
env: {
|
||||||
|
"clear" => { "REDIS_URL" => "redis://x/y", "THREE" => "false" },
|
||||||
|
"tags" => {
|
||||||
|
"odd" => { "TYPE" => "odd" },
|
||||||
|
"even" => { "TYPE" => "even" },
|
||||||
|
"three" => { "THREE" => "true" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@config = Kamal::Configuration.new(@deploy)
|
||||||
|
|
||||||
|
@deploy_with_roles = @deploy.dup.merge({
|
||||||
|
servers: {
|
||||||
|
"web" => [ { "1.1.1.1" => "odd" }, "1.1.1.2" ],
|
||||||
|
"workers" => {
|
||||||
|
"hosts" => [ { "1.1.1.3" => [ "odd", "oddjob" ] }, "1.1.1.4" ],
|
||||||
|
"cmd" => "bin/jobs",
|
||||||
|
"env" => {
|
||||||
|
"REDIS_URL" => "redis://a/b",
|
||||||
|
"WEB_CONCURRENCY" => 4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
env: {
|
||||||
|
"tags" => {
|
||||||
|
"odd" => { "TYPE" => "odd" },
|
||||||
|
"oddjob" => { "TYPE" => "oddjob" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
@config_with_roles = Kamal::Configuration.new(@deploy_with_roles)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "tags" do
|
||||||
|
assert_equal 3, @config.env_tags.size
|
||||||
|
assert_equal %w[ odd even three ], @config.env_tags.map(&:name)
|
||||||
|
assert_equal({ "TYPE" => "odd" }, @config.env_tag("odd").env.clear)
|
||||||
|
assert_equal({ "TYPE" => "even" }, @config.env_tag("even").env.clear)
|
||||||
|
assert_equal({ "THREE" => "true" }, @config.env_tag("three").env.clear)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "tags with roles" do
|
||||||
|
assert_equal 2, @config_with_roles.env_tags.size
|
||||||
|
assert_equal %w[ odd oddjob ], @config_with_roles.env_tags.map(&:name)
|
||||||
|
assert_equal({ "TYPE" => "odd" }, @config_with_roles.env_tag("odd").env.clear)
|
||||||
|
assert_equal({ "TYPE" => "oddjob" }, @config_with_roles.env_tag("oddjob").env.clear)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "tag overrides env" do
|
||||||
|
assert_equal "false", @config.role("web").env("1.1.1.1").clear["THREE"]
|
||||||
|
assert_equal "true", @config.role("web").env("1.1.1.3").clear["THREE"]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "later tag wins" do
|
||||||
|
deploy = {
|
||||||
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
|
||||||
|
servers: [ { "1.1.1.1" => [ "first", "second" ] } ],
|
||||||
|
env: {
|
||||||
|
"tags" => {
|
||||||
|
"first" => { "TYPE" => "first" },
|
||||||
|
"second" => { "TYPE" => "second" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config = Kamal::Configuration.new(deploy)
|
||||||
|
assert_equal "second", config.role("web").env("1.1.1.1").clear["TYPE"]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "tag secret env" do
|
||||||
|
ENV["PASSWORD"] = "hello"
|
||||||
|
|
||||||
|
deploy = {
|
||||||
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
|
||||||
|
servers: [ { "1.1.1.1" => "secrets" } ],
|
||||||
|
env: {
|
||||||
|
"tags" => {
|
||||||
|
"secrets" => { "secret" => [ "PASSWORD" ] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config = Kamal::Configuration.new(deploy)
|
||||||
|
assert_equal "hello", config.role("web").env("1.1.1.1").secrets["PASSWORD"]
|
||||||
|
ensure
|
||||||
|
ENV.delete "PASSWORD"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "tag clear env" do
|
||||||
|
deploy = {
|
||||||
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
|
||||||
|
servers: [ { "1.1.1.1" => "clearly" } ],
|
||||||
|
env: {
|
||||||
|
"tags" => {
|
||||||
|
"clearly" => { "clear" => { "FOO" => "bar" } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config = Kamal::Configuration.new(deploy)
|
||||||
|
assert_equal "bar", config.role("web").env("1.1.1.1").clear["FOO"]
|
||||||
|
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,24 +56,11 @@ 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.clear["REDIS_URL"]
|
assert_equal "redis://a/b", @config_with_roles.role(:workers).env("1.1.1.3").clear["REDIS_URL"]
|
||||||
|
|
||||||
assert_equal "\n", @config_with_roles.role(:workers).env.secrets_io.string
|
assert_equal "\n", @config_with_roles.role(:workers).env("1.1.1.3").secrets_io.string
|
||||||
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args
|
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args("1.1.1.3")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "container name" do
|
test "container name" do
|
||||||
@@ -86,7 +73,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "env args" do
|
test "env args" do
|
||||||
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args
|
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args("1.1.1.3")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "env secret overwritten by role" do
|
test "env secret overwritten by role" do
|
||||||
@@ -117,8 +104,8 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
DB_PASSWORD=secret&\"123
|
DB_PASSWORD=secret&\"123
|
||||||
ENV
|
ENV
|
||||||
|
|
||||||
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env.secrets_io.string
|
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env("1.1.1.3").secrets_io.string
|
||||||
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args
|
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args("1.1.1.3")
|
||||||
ensure
|
ensure
|
||||||
ENV["REDIS_PASSWORD"] = nil
|
ENV["REDIS_PASSWORD"] = nil
|
||||||
ENV["DB_PASSWORD"] = nil
|
ENV["DB_PASSWORD"] = nil
|
||||||
@@ -141,8 +128,8 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
DB_PASSWORD=secret123
|
DB_PASSWORD=secret123
|
||||||
ENV
|
ENV
|
||||||
|
|
||||||
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env.secrets_io.string
|
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env("1.1.1.3").secrets_io.string
|
||||||
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args
|
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args("1.1.1.3")
|
||||||
ensure
|
ensure
|
||||||
ENV["DB_PASSWORD"] = nil
|
ENV["DB_PASSWORD"] = nil
|
||||||
end
|
end
|
||||||
@@ -163,8 +150,8 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
REDIS_PASSWORD=secret456
|
REDIS_PASSWORD=secret456
|
||||||
ENV
|
ENV
|
||||||
|
|
||||||
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env.secrets_io.string
|
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env("1.1.1.3").secrets_io.string
|
||||||
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args
|
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://a/b\"", "--env", "WEB_CONCURRENCY=\"4\"" ], @config_with_roles.role(:workers).env_args("1.1.1.3")
|
||||||
ensure
|
ensure
|
||||||
ENV["REDIS_PASSWORD"] = nil
|
ENV["REDIS_PASSWORD"] = nil
|
||||||
end
|
end
|
||||||
@@ -191,34 +178,14 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
|
|||||||
REDIS_PASSWORD=secret456
|
REDIS_PASSWORD=secret456
|
||||||
ENV
|
ENV
|
||||||
|
|
||||||
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env.secrets_io.string
|
assert_equal expected_secrets_file, @config_with_roles.role(:workers).env("1.1.1.3").secrets_io.string
|
||||||
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://c/d\"" ], @config_with_roles.role(:workers).env_args
|
assert_equal [ "--env-file", ".kamal/env/roles/app-workers.env", "--env", "REDIS_URL=\"redis://c/d\"" ], @config_with_roles.role(:workers).env_args("1.1.1.3")
|
||||||
ensure
|
ensure
|
||||||
ENV["REDIS_PASSWORD"] = nil
|
ENV["REDIS_PASSWORD"] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
test "env secrets_file" do
|
test "env secrets_file" do
|
||||||
assert_equal ".kamal/env/roles/app-workers.env", @config_with_roles.role(:workers).env.secrets_file
|
assert_equal ".kamal/env/roles/app-workers.env", @config_with_roles.role(:workers).env("1.1.1.3").secrets_file
|
||||||
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
|
end
|
||||||
|
|
||||||
test "asset path and volume args" do
|
test "asset path and volume args" do
|
||||||
|
|||||||
@@ -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, "exposed_port" => 3999, "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
|
||||||
|
|||||||
28
test/fixtures/deploy_with_env_tags.yml
vendored
Normal file
28
test/fixtures/deploy_with_env_tags.yml
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
service: app
|
||||||
|
image: dhh/app
|
||||||
|
servers:
|
||||||
|
web:
|
||||||
|
- 1.1.1.1: site1
|
||||||
|
- 1.1.1.2: [ site1 experimental ]
|
||||||
|
- 1.2.1.1: site2
|
||||||
|
- 1.2.1.2: site2
|
||||||
|
workers:
|
||||||
|
- 1.1.1.3: site1
|
||||||
|
- 1.1.1.4: site1
|
||||||
|
- 1.2.1.3: site2
|
||||||
|
- 1.2.1.4: [ site2 experimental ]
|
||||||
|
env:
|
||||||
|
clear:
|
||||||
|
TEST: "root"
|
||||||
|
EXPERIMENT: "disabled"
|
||||||
|
tags:
|
||||||
|
site1:
|
||||||
|
SITE: site1
|
||||||
|
site2:
|
||||||
|
SITE: site2
|
||||||
|
experimental:
|
||||||
|
EXPERIMENT: "enabled"
|
||||||
|
|
||||||
|
registry:
|
||||||
|
username: user
|
||||||
|
password: pw
|
||||||
45
test/fixtures/deploy_with_remote_builder_and_custom_ports.yml
vendored
Normal file
45
test/fixtures/deploy_with_remote_builder_and_custom_ports.yml
vendored
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
service: app
|
||||||
|
image: dhh/app
|
||||||
|
servers:
|
||||||
|
web:
|
||||||
|
- "1.1.1.1"
|
||||||
|
- "1.1.1.2"
|
||||||
|
workers:
|
||||||
|
- "1.1.1.3"
|
||||||
|
- "1.1.1.4"
|
||||||
|
registry:
|
||||||
|
username: user
|
||||||
|
password: pw
|
||||||
|
|
||||||
|
accessories:
|
||||||
|
mysql:
|
||||||
|
image: mysql:5.7
|
||||||
|
host: 1.1.1.3
|
||||||
|
port: 3306
|
||||||
|
env:
|
||||||
|
clear:
|
||||||
|
MYSQL_ROOT_HOST: '%'
|
||||||
|
secret:
|
||||||
|
- MYSQL_ROOT_PASSWORD
|
||||||
|
files:
|
||||||
|
- test/fixtures/files/my.cnf:/etc/mysql/my.cnf
|
||||||
|
directories:
|
||||||
|
- data:/var/lib/mysql
|
||||||
|
redis:
|
||||||
|
image: redis:latest
|
||||||
|
roles:
|
||||||
|
- web
|
||||||
|
port: 6379
|
||||||
|
directories:
|
||||||
|
- data:/data
|
||||||
|
|
||||||
|
readiness_delay: 0
|
||||||
|
|
||||||
|
ssh:
|
||||||
|
user: root
|
||||||
|
port: 22
|
||||||
|
|
||||||
|
builder:
|
||||||
|
remote:
|
||||||
|
arch: amd64
|
||||||
|
host: ssh://app@1.1.1.5:2122
|
||||||
15
test/fixtures/deploy_with_two_roles_one_host.yml
vendored
Normal file
15
test/fixtures/deploy_with_two_roles_one_host.yml
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
service: app
|
||||||
|
image: dhh/app
|
||||||
|
servers:
|
||||||
|
workers:
|
||||||
|
hosts:
|
||||||
|
- 1.1.1.1
|
||||||
|
web:
|
||||||
|
hosts:
|
||||||
|
- 1.1.1.1
|
||||||
|
env:
|
||||||
|
REDIS_URL: redis://x/y
|
||||||
|
registry:
|
||||||
|
server: registry.digitalocean.com
|
||||||
|
username: user
|
||||||
|
password: pw
|
||||||
8
test/fixtures/deploy_with_uncommon_hostnames.yml
vendored
Normal file
8
test/fixtures/deploy_with_uncommon_hostnames.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
service: app
|
||||||
|
image: dhh/app
|
||||||
|
servers:
|
||||||
|
- "this-hostname-with-random-part-is-too-long.example.com"
|
||||||
|
- "this-hostname-is-really-unacceptably-long-to-be-honest.example.com"
|
||||||
|
registry:
|
||||||
|
username: user
|
||||||
|
password: pw
|
||||||
39
test/fixtures/deploy_without_clone.yml
vendored
Normal file
39
test/fixtures/deploy_without_clone.yml
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
service: app
|
||||||
|
image: dhh/app
|
||||||
|
servers:
|
||||||
|
web:
|
||||||
|
- "1.1.1.1"
|
||||||
|
- "1.1.1.2"
|
||||||
|
workers:
|
||||||
|
- "1.1.1.3"
|
||||||
|
- "1.1.1.4"
|
||||||
|
registry:
|
||||||
|
username: user
|
||||||
|
password: pw
|
||||||
|
|
||||||
|
accessories:
|
||||||
|
mysql:
|
||||||
|
image: mysql:5.7
|
||||||
|
host: 1.1.1.3
|
||||||
|
port: 3306
|
||||||
|
env:
|
||||||
|
clear:
|
||||||
|
MYSQL_ROOT_HOST: '%'
|
||||||
|
secret:
|
||||||
|
- MYSQL_ROOT_PASSWORD
|
||||||
|
files:
|
||||||
|
- test/fixtures/files/my.cnf:/etc/mysql/my.cnf
|
||||||
|
directories:
|
||||||
|
- data:/var/lib/mysql
|
||||||
|
redis:
|
||||||
|
image: redis:latest
|
||||||
|
roles:
|
||||||
|
- web
|
||||||
|
port: 6379
|
||||||
|
directories:
|
||||||
|
- data:/data
|
||||||
|
|
||||||
|
readiness_delay: 0
|
||||||
|
|
||||||
|
builder:
|
||||||
|
context: "."
|
||||||
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
|
||||||
|
|||||||
33
test/integration/broken_deploy_test.rb
Normal file
33
test/integration/broken_deploy_test.rb
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
require_relative "integration_test"
|
||||||
|
|
||||||
|
class BrokenDeployTest < IntegrationTest
|
||||||
|
test "deploying a bad image" do
|
||||||
|
@app = "app_with_roles"
|
||||||
|
|
||||||
|
kamal :envify
|
||||||
|
|
||||||
|
first_version = latest_app_version
|
||||||
|
|
||||||
|
kamal :deploy
|
||||||
|
|
||||||
|
assert_app_is_up version: first_version
|
||||||
|
assert_container_running host: :vm3, name: "app-workers-#{first_version}"
|
||||||
|
|
||||||
|
second_version = break_app
|
||||||
|
|
||||||
|
output = kamal :deploy, raise_on_error: false, capture: true
|
||||||
|
|
||||||
|
assert_failed_deploy output
|
||||||
|
assert_app_is_up version: first_version
|
||||||
|
assert_container_running host: :vm3, name: "app-workers-#{first_version}"
|
||||||
|
assert_container_not_running host: :vm3, name: "app-workers-#{second_version}"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def assert_failed_deploy(output)
|
||||||
|
assert_match "Waiting for the first healthy web container before booting workers on vm3...", 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 "nginx: [emerg] unexpected end of file, expecting \";\" or \"}\" in /etc/nginx/conf.d/default.conf:2", output
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1 +1,2 @@
|
|||||||
SECRET_TOKEN='1234 with "中文"'
|
SECRET_TOKEN='1234 with "中文"'
|
||||||
|
SECRET_TAG='TAGME'
|
||||||
|
|||||||
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
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,20 @@ service: app
|
|||||||
image: app
|
image: app
|
||||||
servers:
|
servers:
|
||||||
- vm1
|
- vm1
|
||||||
- vm2
|
- vm2: [ tag1, tag2 ]
|
||||||
env:
|
env:
|
||||||
clear:
|
clear:
|
||||||
CLEAR_TOKEN: 4321
|
CLEAR_TOKEN: 4321
|
||||||
|
CLEAR_TAG: ""
|
||||||
HOST_TOKEN: "${HOST_TOKEN}"
|
HOST_TOKEN: "${HOST_TOKEN}"
|
||||||
secret:
|
secret:
|
||||||
- SECRET_TOKEN
|
- SECRET_TOKEN
|
||||||
|
tags:
|
||||||
|
tag1:
|
||||||
|
CLEAR_TAG: tagged
|
||||||
|
tag2:
|
||||||
|
secret:
|
||||||
|
- SECRET_TAG
|
||||||
asset_path: /usr/share/nginx/html/versions
|
asset_path: /usr/share/nginx/html/versions
|
||||||
|
|
||||||
registry:
|
registry:
|
||||||
@@ -19,13 +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/parachute:latest
|
||||||
traefik:
|
http_port: 80
|
||||||
args:
|
https_port: 443
|
||||||
accesslog: true
|
debug: true
|
||||||
accesslog.format: json
|
|
||||||
image: registry:4443/traefik:v2.10
|
|
||||||
accessories:
|
accessories:
|
||||||
busybox:
|
busybox:
|
||||||
service: custom-busybox
|
service: custom-busybox
|
||||||
@@ -34,3 +39,4 @@ accessories:
|
|||||||
roles:
|
roles:
|
||||||
- web
|
- web
|
||||||
stop_wait_time: 1
|
stop_wait_time: 1
|
||||||
|
readiness_delay: 0
|
||||||
|
|||||||
@@ -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,13 +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/parachute:latest
|
||||||
traefik:
|
|
||||||
args:
|
|
||||||
accesslog: true
|
|
||||||
accesslog.format: json
|
|
||||||
image: registry:4443/traefik:v2.10
|
|
||||||
accessories:
|
accessories:
|
||||||
busybox:
|
busybox:
|
||||||
service: custom-busybox
|
service: custom-busybox
|
||||||
@@ -35,3 +30,4 @@ accessories:
|
|||||||
roles:
|
roles:
|
||||||
- web
|
- web
|
||||||
stop_wait_time: 1
|
stop_wait_time: 1
|
||||||
|
readiness_delay: 0
|
||||||
|
|||||||
3
test/integration/docker/deployer/break_app.sh
Executable file
3
test/integration/docker/deployer/break_app.sh
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
cd $1 && echo "bad nginx config" > default.conf && git commit -am 'Broken'
|
||||||
@@ -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/parachute 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)
|
||||||
@@ -78,6 +78,11 @@ class IntegrationTest < ActiveSupport::TestCase
|
|||||||
latest_app_version
|
latest_app_version
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def break_app
|
||||||
|
deployer_exec "./break_app.sh #{@app}", workdir: "/"
|
||||||
|
latest_app_version
|
||||||
|
end
|
||||||
|
|
||||||
def latest_app_version
|
def latest_app_version
|
||||||
deployer_exec("git rev-parse HEAD", capture: true)
|
deployer_exec("git rev-parse HEAD", capture: true)
|
||||||
end
|
end
|
||||||
@@ -96,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}"
|
||||||
@@ -124,11 +129,23 @@ 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
|
||||||
|
|
||||||
|
def assert_container_running(host:, name:)
|
||||||
|
assert container_running?(host: host, name: name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_container_not_running(host:, name:)
|
||||||
|
assert_not container_running?(host: host, name: name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def container_running?(host:, name:)
|
||||||
|
docker_compose("exec #{host} docker ps --filter=name=#{name} | tail -n+2", capture: true).tap { |x| p [ x, x.strip, x.strip.present? ] }.strip.present?
|
||||||
|
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,22 +1,20 @@
|
|||||||
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_local_env_file "SECRET_TOKEN='1234 with \"中文\"'"
|
assert_env_files
|
||||||
assert_remote_env_file "SECRET_TOKEN=1234 with \"中文\""
|
|
||||||
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
|
||||||
assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "post-deploy"
|
assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "post-deploy"
|
||||||
assert_env :CLEAR_TOKEN, "4321", version: first_version
|
assert_envs version: first_version
|
||||||
assert_env :HOST_TOKEN, "abcd", version: first_version
|
|
||||||
assert_env :SECRET_TOKEN, "1234 with \"中文\"", version: first_version
|
|
||||||
|
|
||||||
second_version = update_app_rev
|
second_version = update_app_rev
|
||||||
|
|
||||||
@@ -31,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\/parachute: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
|
||||||
@@ -48,17 +46,24 @@ 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
|
||||||
|
|
||||||
assert_app_is_up version: version
|
assert_app_is_up version: version
|
||||||
assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "post-deploy"
|
assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "post-deploy"
|
||||||
assert_container_running host: :vm3, name: "app-workers-#{version}"
|
assert_container_running host: :vm3, name: "app-workers-#{version}"
|
||||||
|
|
||||||
|
second_version = update_app_rev
|
||||||
|
|
||||||
|
kamal :redeploy
|
||||||
|
assert_app_is_up version: second_version
|
||||||
|
assert_container_running host: :vm3, name: "app-workers-#{second_version}"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "config" do
|
test "config" do
|
||||||
@@ -76,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" => 7, "exposed_port" => 3999, "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
|
||||||
@@ -97,16 +101,38 @@ class MainTest < IntegrationTest
|
|||||||
assert_equal contents, deployer_exec("cat .env", capture: true)
|
assert_equal contents, deployer_exec("cat .env", capture: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_env(key, value, version:)
|
def assert_envs(version:)
|
||||||
assert_equal "#{key}=#{value}", docker_compose("exec vm1 docker exec app-web-#{version} env | grep #{key}", capture: true)
|
assert_env :CLEAR_TOKEN, "4321", version: version, vm: :vm1
|
||||||
|
assert_env :HOST_TOKEN, "abcd", version: version, vm: :vm1
|
||||||
|
assert_env :SECRET_TOKEN, "1234 with \"中文\"", version: version, vm: :vm1
|
||||||
|
assert_no_env :CLEAR_TAG, version: version, vm: :vm1
|
||||||
|
assert_no_env :SECRET_TAG, version: version, vm: :vm11
|
||||||
|
assert_env :CLEAR_TAG, "tagged", version: version, vm: :vm2
|
||||||
|
assert_env :SECRET_TAG, "TAGME", version: version, vm: :vm2
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_env(key, value, vm:, version:)
|
||||||
|
assert_equal "#{key}=#{value}", docker_compose("exec #{vm} docker exec app-web-#{version} env | grep #{key}", capture: true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_no_env(key, vm:, version:)
|
||||||
|
assert_raises(RuntimeError, /exit 1/) do
|
||||||
|
docker_compose("exec #{vm} docker exec app-web-#{version} env | grep #{key}", capture: true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_env_files
|
||||||
|
assert_local_env_file "SECRET_TOKEN='1234 with \"中文\"'\nSECRET_TAG='TAGME'"
|
||||||
|
assert_remote_env_file "SECRET_TOKEN=1234 with \"中文\"", vm: :vm1
|
||||||
|
assert_remote_env_file "SECRET_TOKEN=1234 with \"中文\"\nSECRET_TAG=TAGME", vm: :vm2
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_local_env_file
|
def remove_local_env_file
|
||||||
deployer_exec("rm .env")
|
deployer_exec("rm .env")
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_remote_env_file(contents)
|
def assert_remote_env_file(contents, vm:)
|
||||||
assert_equal contents, docker_compose("exec vm1 cat /root/.kamal/env/roles/app-web.env", capture: true)
|
assert_equal contents, docker_compose("exec #{vm} cat /root/.kamal/env/roles/app-web.env", capture: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_no_remote_env_file
|
def assert_no_remote_env_file
|
||||||
@@ -138,8 +164,4 @@ class MainTest < IntegrationTest
|
|||||||
assert vm1_image_ids.any?
|
assert vm1_image_ids.any?
|
||||||
assert vm1_container_ids.any?
|
assert vm1_container_ids.any?
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_container_running(host:, name:)
|
|
||||||
assert docker_compose("exec #{host} docker ps --filter=name=#{name} -q", capture: true).strip.present?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user