Configurable Kamal directory

To avoid polluting the default SSH directory with lots of Kamal config,
we'll default to putting them in a `kamal` sub directory.

But also make the directory configurable with the `run_directory` key,
so for example you can set it as `/var/run/kamal/`

The directory is created during bootstrap or before any command that
will need to access a file.
This commit is contained in:
Donal McBreen
2023-08-28 16:12:56 +01:00
parent 9363b6a464
commit bcfa1d83e8
17 changed files with 101 additions and 23 deletions

View File

@@ -79,6 +79,8 @@ module Kamal::Cli
run_hook "pre-connect"
ensure_run_directory
acquire_lock
begin
@@ -167,5 +169,11 @@ module Kamal::Cli
def first_invocation
instance_variable_get("@_invocations").first
end
def ensure_run_directory
on(KAMAL.hosts) do
execute(*KAMAL.server.ensure_run_directory)
end
end
end
end

View File

@@ -2,7 +2,10 @@ class Kamal::Cli::Lock < Kamal::Cli::Base
desc "status", "Report lock status"
def status
handle_missing_lock do
on(KAMAL.primary_host) { puts capture_with_debug(*KAMAL.lock.status) }
on(KAMAL.primary_host) do
execute *KAMAL.server.ensure_run_directory
puts capture_with_debug(*KAMAL.lock.status)
end
end
end
@@ -11,7 +14,10 @@ class Kamal::Cli::Lock < Kamal::Cli::Base
def acquire
message = options[:message]
raise_if_locked do
on(KAMAL.primary_host) { execute *KAMAL.lock.acquire(message, KAMAL.config.version), verbosity: :debug }
on(KAMAL.primary_host) do
execute *KAMAL.server.ensure_run_directory
execute *KAMAL.lock.acquire(message, KAMAL.config.version), verbosity: :debug
end
say "Acquired the deploy lock"
end
end
@@ -19,7 +25,10 @@ class Kamal::Cli::Lock < Kamal::Cli::Base
desc "release", "Release the deploy lock"
def release
handle_missing_lock do
on(KAMAL.primary_host) { execute *KAMAL.lock.release, verbosity: :debug }
on(KAMAL.primary_host) do
execute *KAMAL.server.ensure_run_directory
execute *KAMAL.lock.release, verbosity: :debug
end
say "Released the deploy lock"
end
end

View File

@@ -14,6 +14,10 @@ class Kamal::Cli::Server < Kamal::Cli::Base
end
end
on(KAMAL.hosts) do
execute(*KAMAL.server.ensure_run_directory)
end
if missing.any?
raise "Docker is not installed on #{missing.join(", ")} and can't be automatically installed without having root access and the `curl` command available. Install Docker manually: https://docs.docker.com/engine/install/"
end

View File

@@ -116,6 +116,10 @@ class Kamal::Commander
@registry ||= Kamal::Commands::Registry.new(config)
end
def server
@server ||= Kamal::Commands::Server.new(config)
end
def traefik
@traefik ||= Kamal::Commands::Traefik.new(config)
end

View File

@@ -19,7 +19,9 @@ class Kamal::Commands::Auditor < Kamal::Commands::Base
private
def audit_log_file
[ "kamal", config.service, config.destination, "audit.log" ].compact.join("-")
file = [ config.service, config.destination, "audit.log" ].compact.join("-")
"#{config.run_directory}/#{file}"
end
def audit_tags(**details)

View File

@@ -40,7 +40,7 @@ class Kamal::Commands::Lock < Kamal::Commands::Base
end
def lock_dir
"kamal_lock-#{config.service}"
"#{config.run_directory}/lock-#{config.service}"
end
def lock_details_file

View File

@@ -0,0 +1,5 @@
class Kamal::Commands::Server < Kamal::Commands::Base
def ensure_run_directory
[:mkdir, "-p", config.run_directory]
end
end

View File

@@ -57,6 +57,10 @@ class Kamal::Configuration
Kamal::Utils.abbreviate_version(version)
end
def run_directory
raw_config.run_directory || "kamal"
end
def roles
@roles ||= role_names.collect { |role_name| Role.new(role_name, config: self) }