Added -y option to kamal traefik reboot command

This commit is contained in:
Igor Alexandrov
2024-03-20 21:11:06 +04:00
parent 474b76cf47
commit bbf952952d
5 changed files with 29 additions and 16 deletions

View File

@@ -177,7 +177,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
if name == "all" if name == "all"
KAMAL.accessory_names.each { |accessory_name| remove(accessory_name) } KAMAL.accessory_names.each { |accessory_name| remove(accessory_name) }
else else
if options[:confirmed] || ask("This will remove all containers, images and data directories for #{name}. Are you sure?", limited_to: %w[ y N ], default: "N") == "y" confirming "This will remove all containers, images and data directories for #{name}. Are you sure?" do
with_accessory(name) do with_accessory(name) do
stop(name) stop(name)
remove_container(name) remove_container(name)

View File

@@ -103,6 +103,16 @@ module Kamal::Cli
release_lock release_lock
end end
def confirming(question)
return yield if options[:confirmed]
if ask(question, limited_to: %w[ y N ], default: "N") == "y"
yield
else
say "Aborted", :red
end
end
def acquire_lock def acquire_lock
raise_if_locked do raise_if_locked do
say "Acquiring the deploy lock...", :magenta say "Acquiring the deploy lock...", :magenta

View File

@@ -197,7 +197,7 @@ class Kamal::Cli::Main < Kamal::Cli::Base
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 mutating do
if options[:confirmed] || ask("This will remove all containers and images. Are you sure?", limited_to: %w[ y N ], default: "N") == "y" confirming "This will remove all containers and images. Are you sure?" do
invoke "kamal:cli:traefik:remove", [], options.without(:confirmed) invoke "kamal:cli:traefik: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

View File

@@ -11,20 +11,23 @@ class Kamal::Cli::Traefik < Kamal::Cli::Base
desc "reboot", "Reboot Traefik on servers (stop container, remove container, start new container)" 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 :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 def reboot
mutating do confirming "Reboot Traefik on hosts? (stop container, remove container, start new container). This will cause a brief outage." do
host_groups = options[:rolling] ? KAMAL.traefik_hosts : [ KAMAL.traefik_hosts ] mutating do
host_groups.each do |hosts| host_groups = options[:rolling] ? KAMAL.traefik_hosts : [ KAMAL.traefik_hosts ]
host_list = Array(hosts).join(",") host_groups.each do |hosts|
run_hook "pre-traefik-reboot", hosts: host_list host_list = Array(hosts).join(",")
on(hosts) do run_hook "pre-traefik-reboot", hosts: host_list
execute *KAMAL.auditor.record("Rebooted traefik"), verbosity: :debug on(hosts) do
execute *KAMAL.registry.login execute *KAMAL.auditor.record("Rebooted traefik"), verbosity: :debug
execute *KAMAL.traefik.stop, raise_on_non_zero_exit: false execute *KAMAL.registry.login
execute *KAMAL.traefik.remove_container execute *KAMAL.traefik.stop, raise_on_non_zero_exit: false
execute *KAMAL.traefik.run execute *KAMAL.traefik.remove_container
execute *KAMAL.traefik.run
end
run_hook "post-traefik-reboot", hosts: host_list
end end
run_hook "post-traefik-reboot", hosts: host_list
end end
end end
end end

View File

@@ -11,7 +11,7 @@ class CliTraefikTest < CliTestCase
test "reboot" do test "reboot" do
Kamal::Commands::Registry.any_instance.expects(:login).twice Kamal::Commands::Registry.any_instance.expects(:login).twice
run_command("reboot").tap do |output| run_command("reboot", "-y").tap do |output|
assert_match "docker container stop traefik", 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 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 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
@@ -21,7 +21,7 @@ class CliTraefikTest < CliTestCase
test "reboot --rolling" do test "reboot --rolling" do
Object.any_instance.stubs(:sleep) Object.any_instance.stubs(:sleep)
run_command("reboot", "--rolling").tap do |output| 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 assert_match "Running docker container prune --force --filter label=org.opencontainers.image.title=Traefik on 1.1.1.1", output
end end
end end