Remove proxy only if no apps are installed

This commit is contained in:
Donal McBreen
2024-09-12 16:47:03 +01:00
parent 35fe9c154d
commit 24031fefb0
16 changed files with 131 additions and 33 deletions

View File

@@ -207,12 +207,12 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
end
end
desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host", hide: true
def remove_service_directory(name)
desc "remove_app_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host", hide: true
def remove_app_directory(name)
with_lock do
with_accessory(name) do |accessory, hosts|
on(hosts) do
execute *accessory.remove_service_directory
execute *accessory.remove_app_directory
end
end
end
@@ -248,7 +248,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
stop(name)
remove_container(name)
remove_image(name)
remove_service_directory(name)
remove_app_directory(name)
end
def prepare(name)

View File

@@ -231,7 +231,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
stop
remove_containers
remove_images
remove_service_directory
remove_app_directory
end
end
@@ -273,15 +273,15 @@ class Kamal::Cli::App < Kamal::Cli::Base
end
end
desc "remove_service_directory", "Remove the service directory from servers", hide: true
def remove_service_directory
desc "remove_app_directory", "Remove the service directory from servers", hide: true
def remove_app_directory
with_lock do
on(KAMAL.hosts) do |host|
roles = KAMAL.roles_on(host)
roles.each do |role|
execute *KAMAL.auditor.record("Removed #{KAMAL.config.service_directory} on all servers", role: role), verbosity: :debug
execute *KAMAL.server.remove_service_directory
execute *KAMAL.auditor.record("Removed #{KAMAL.config.app_directory} on all servers", role: role), verbosity: :debug
execute *KAMAL.server.remove_app_directory
end
end
end

View File

@@ -176,7 +176,7 @@ module Kamal::Cli
def ensure_service_and_locks_directory
on(KAMAL.hosts) do
execute(*KAMAL.server.ensure_service_directory)
execute(*KAMAL.server.ensure_app_directory)
end
on(KAMAL.primary_host) do

View File

@@ -150,11 +150,15 @@ class Kamal::Cli::Proxy < Kamal::Cli::Base
end
desc "remove", "Remove proxy container and image from servers"
option :force, type: :boolean, default: false, desc: "Force removing proxy when apps are still installed"
def remove
with_lock do
stop
remove_container
remove_image
if removal_allowed?(options[:force])
stop
remove_container
remove_image
remove_host_directory
end
end
end
@@ -178,8 +182,37 @@ class Kamal::Cli::Proxy < Kamal::Cli::Base
end
end
desc "remove_host_directory", "Remove proxy directory from servers", hide: true
def remove_host_directory
with_lock do
on(KAMAL.proxy_hosts) do
execute *KAMAL.auditor.record("Removed #{KAMAL.config.proxy_directory}"), verbosity: :debug
execute *KAMAL.proxy.remove_host_directory
end
end
end
private
def reset_invocation(cli_class)
instance_variable_get("@_invocations")[cli_class].pop
end
def removal_allowed?(force)
on(KAMAL.proxy_hosts) do |host|
app_count = capture_with_info(*KAMAL.server.app_directory_count).chomp.to_i
raise "The are other applications installed on #{host}" if app_count > 0
end
true
rescue SSHKit::Runner::ExecuteError => e
raise unless e.message.include?("The are other applications installed on")
if force
say "Forcing, so removing the proxy, even though other apps are installed", :magenta
else
say "Not removing the proxy, as other apps are installed, ignore this check with kamal proxy remove --force", :magenta
end
force
end
end

View File

@@ -90,7 +90,7 @@ class Kamal::Commands::Accessory < Kamal::Commands::Base
end
end
def remove_service_directory
def remove_app_directory
[ :rm, "-rf", service_name ]
end

View File

@@ -67,6 +67,10 @@ class Kamal::Commands::Proxy < Kamal::Commands::Base
docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=kamal-proxy"
end
def remove_host_directory
remove_directory config.proxy_directory
end
def cleanup_traefik
chain \
docker(:container, :stop, "traefik"),

View File

@@ -1,9 +1,15 @@
class Kamal::Commands::Server < Kamal::Commands::Base
def ensure_service_directory
make_directory config.service_directory
def ensure_app_directory
make_directory config.app_directory
end
def remove_service_directory
remove_directory config.service_directory
def remove_app_directory
remove_directory config.app_directory
end
def app_directory_count
pipe \
[ :ls, config.apps_directory ],
[ :wc, "-l" ]
end
end

View File

@@ -194,16 +194,24 @@ class Kamal::Configuration
".kamal"
end
def service_directory
File.join run_directory, "apps", [ service, destination ].compact.join("-")
def apps_directory
File.join run_directory, "apps"
end
def app_directory
File.join apps_directory, [ service, destination ].compact.join("-")
end
def proxy_directory
File.join run_directory, "proxy"
end
def env_directory
File.join service_directory, "env"
File.join app_directory, "env"
end
def assets_directory
File.join service_directory, "assets"
File.join app_directory, "assets"
end

View File

@@ -61,7 +61,7 @@ class Kamal::Configuration::Proxy
def config_volume
Kamal::Configuration::Volume.new \
host_path: File.join(config.run_directory, "proxy", "config"),
host_path: File.join(config.proxy_directory, "config"),
container_path: "/root/.config/kamal-proxy"
end