Let App be aware of destination

This commit is contained in:
Tobias Bühlmann
2023-03-03 15:29:00 +01:00
parent cec3468f50
commit 170562c7e7
5 changed files with 43 additions and 21 deletions

View File

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

View File

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

View File

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

View File

@@ -52,7 +52,11 @@ class Mrsk::Configuration::Role
end end
def default_labels 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 end
def traefik_labels def traefik_labels

View File

@@ -14,6 +14,14 @@ class CommandsAuditorTest < ActiveSupport::TestCase
new_command.record("app removed container").join(" ") new_command.record("app removed container").join(" ")
end end
test "record with destination" do
@destination = "staging"
assert_match \
/echo '.* app removed container' >> mrsk-app-staging-audit.log/,
new_command.record("app removed container").join(" ")
end
test "broadcast" do test "broadcast" do
assert_match \ assert_match \
/bin\/audit_broadcast '\[.*\] app removed container'/, /bin\/audit_broadcast '\[.*\] app removed container'/,
@@ -22,6 +30,6 @@ class CommandsAuditorTest < ActiveSupport::TestCase
private private
def new_command def new_command
Mrsk::Commands::Auditor.new(Mrsk::Configuration.new(@config, version: "123")) Mrsk::Commands::Auditor.new(Mrsk::Configuration.new(@config, destination: @destination, version: "123"))
end end
end end