Merge pull request #71 from tbuehlmann/destination-awareness

Destination aware container names
This commit is contained in:
David Heinemeier Hansson
2023-03-09 13:25:49 +00:00
committed by GitHub
8 changed files with 81 additions and 25 deletions

View File

@@ -6,7 +6,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
say "Start container with version #{version} using a #{MRSK.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta
cli = self
MRSK.config.roles.each do |role|
on(role.hosts) do |host|
execute *MRSK.auditor.record("Booted app version #{version}"), verbosity: :debug

View File

@@ -6,7 +6,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
"--detach",
"--restart unless-stopped",
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
"--name", service_with_version,
"--name", service_with_version_and_destination,
*role.env_args,
*config.volume_args,
*role.label_args,
@@ -16,7 +16,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
end
def start
docker :start, service_with_version
docker :start, service_with_version_and_destination
end
def stop(version: nil)
@@ -26,7 +26,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
end
def info
docker :ps, *service_filter
docker :ps, *service_filter_with_destination
end
@@ -51,7 +51,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
def execute_in_existing_container(*command, interactive: false)
docker :exec,
("-it" if interactive),
config.service_with_version,
service_with_version_and_destination,
*command
end
@@ -75,13 +75,13 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
def current_container_id
docker :ps, "--quiet", *service_filter
docker :ps, "--quiet", *service_filter_with_destination
end
def current_running_version
# FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail!
pipe \
docker(:ps, "--filter", "label=service=#{config.service}", "--format", '"{{.Names}}"'),
docker(:ps, *service_filter_with_destination, "--format", '"{{.Names}}"'),
%(sed 's/-/\\n/g'),
"tail -n 1"
end
@@ -100,7 +100,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
def list_containers
docker :container, :ls, "--all", *service_filter
docker :container, :ls, "--all", *service_filter_with_destination
end
def list_container_names
@@ -109,12 +109,12 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
def remove_container(version:)
pipe \
container_id_for(container_name: service_with_version(version)),
container_id_for(container_name: service_with_version_and_destination(version)),
xargs(docker(:container, :rm))
end
def remove_containers
docker :container, :prune, "--force", *service_filter
docker :container, :prune, "--force", *service_filter_with_destination
end
def list_images
@@ -127,19 +127,23 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
private
def service_with_version(version = nil)
if version
"#{config.service}-#{version}"
else
config.service_with_version
end
def service_with_version_and_destination(version = nil)
[ config.service, config.destination, version || config.version ].compact.join("-")
end
def container_id_for_version(version)
container_id_for(container_name: service_with_version(version))
container_id_for(container_name: service_with_version_and_destination(version))
end
def service_filter
[ "--filter", "label=service=#{config.service}" ]
end
def service_filter_with_destination
if config.destination
service_filter << "label=destination=#{config.destination}"
else
service_filter
end
end
end

View File

@@ -21,7 +21,7 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
private
def audit_log_file
"mrsk-#{config.service}-audit.log"
[ "mrsk", config.service, config.destination, "audit.log" ].compact.join("-")
end
def tagged_record_line(line)

View File

@@ -33,11 +33,11 @@ class Mrsk::Commands::Healthcheck < Mrsk::Commands::Base
private
def container_name
"healthcheck-#{config.service}"
[ "healthcheck", config.service, config.destination ].compact.join("-")
end
def container_name_with_version
"healthcheck-#{config.service_with_version}"
"#{container_name}-#{config.version}"
end
def container_id

View File

@@ -10,6 +10,7 @@ class Mrsk::Configuration
delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
attr_accessor :version
attr_accessor :destination
attr_accessor :raw_config
class << self
@@ -19,7 +20,7 @@ class Mrsk::Configuration
config.deep_merge! \
load_config_file destination_config_file(base_config_file, destination)
end
end, version: version)
end, destination: destination, version: version)
end
private
@@ -37,8 +38,9 @@ class Mrsk::Configuration
end
end
def initialize(raw_config, version: "missing", validate: true)
def initialize(raw_config, destination: nil, version: "missing", validate: true)
@raw_config = ActiveSupport::InheritableOptions.new(raw_config)
@destination = destination
@version = version
valid? if validate
end

View File

@@ -60,7 +60,11 @@ class Mrsk::Configuration::Role
end
def default_labels
{ "service" => config.service, "role" => name }
if config.destination
{ "service" => config.service, "role" => name, "destination" => config.destination }
else
{ "service" => config.service, "role" => name }
end
end
def traefik_labels