Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destination aware container names #71

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mrsk/cli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def boot
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
Expand Down
36 changes: 20 additions & 16 deletions lib/mrsk/commands/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def run(role: :web)
"--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,
Expand All @@ -15,7 +15,7 @@ def run(role: :web)
end

def start
docker :start, service_with_version
docker :start, service_with_version_and_destination
end

def stop(version: nil)
Expand All @@ -25,7 +25,7 @@ def stop(version: nil)
end

def info
docker :ps, *service_filter
docker :ps, *service_filter_with_destination
end


Expand All @@ -50,7 +50,7 @@ def follow_logs(host:, grep: nil)
def execute_in_existing_container(*command, interactive: false)
docker :exec,
("-it" if interactive),
config.service_with_version,
service_with_version_and_destination,
*command
end

Expand All @@ -74,13 +74,13 @@ def execute_in_new_container_over_ssh(*command, host:)


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
Expand All @@ -99,7 +99,7 @@ def all_versions_from_available_containers


def list_containers
docker :container, :ls, "--all", *service_filter
docker :container, :ls, "--all", *service_filter_with_destination
end

def list_container_names
Expand All @@ -108,12 +108,12 @@ def list_container_names

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
Expand All @@ -126,19 +126,23 @@ def remove_images


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
6 changes: 5 additions & 1 deletion lib/mrsk/commands/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def reveal

private
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
dhh marked this conversation as resolved.
Show resolved Hide resolved
end

def tagged_record_line(line)
Expand Down
4 changes: 2 additions & 2 deletions lib/mrsk/commands/healthcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ def remove

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
Expand Down
6 changes: 4 additions & 2 deletions lib/mrsk/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,7 +20,7 @@ def create_from(base_config_file, destination: nil, version: "missing")
config.deep_merge! \
load_config_file destination_config_file(base_config_file, destination)
end
end, version: version)
end, destination: destination, version: version)
end

private
Expand All @@ -37,8 +38,9 @@ def destination_config_file(base_config_file, destination)
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
Expand Down
6 changes: 5 additions & 1 deletion lib/mrsk/configuration/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def extract_hosts_from_config
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
Expand Down
10 changes: 9 additions & 1 deletion test/commands/auditor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class CommandsAuditorTest < ActiveSupport::TestCase
new_command.record("app removed container").join(" ")
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
assert_match \
/bin\/audit_broadcast '\[.*\] app removed container'/,
Expand All @@ -22,6 +30,6 @@ class CommandsAuditorTest < ActiveSupport::TestCase

private
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
40 changes: 39 additions & 1 deletion test/commands/healthcheck_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase
new_command.run.join(" ")
end

test "run with destination" do
@destination = "staging"

assert_equal \
"docker run --detach --name healthcheck-app-staging-123 --publish 3999:3000 --label service=healthcheck-app-staging dhh/app:123",
new_command.run.join(" ")
end

test "curl" do
assert_equal \
"curl --silent --output /dev/null --write-out '%{http_code}' --max-time 2 http://localhost:3999/up",
Expand All @@ -42,14 +50,44 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase
new_command.stop.join(" ")
end

test "stop with destination" do
@destination = "staging"

assert_equal \
"docker container ls --all --filter name=healthcheck-app-staging --quiet | xargs docker stop",
new_command.stop.join(" ")
end

test "remove" do
assert_equal \
"docker container ls --all --filter name=healthcheck-app --quiet | xargs docker container rm",
new_command.remove.join(" ")
end

test "remove with destination" do
@destination = "staging"

assert_equal \
"docker container ls --all --filter name=healthcheck-app-staging --quiet | xargs docker container rm",
new_command.remove.join(" ")
end

test "logs" do
assert_equal \
"docker container ls --all --filter name=healthcheck-app --quiet | xargs docker logs --tail 50 2>&1",
new_command.logs.join(" ")
end

test "logs with destination" do
@destination = "staging"

assert_equal \
"docker container ls --all --filter name=healthcheck-app-staging --quiet | xargs docker logs --tail 50 2>&1",
new_command.logs.join(" ")
end

private
def new_command
Mrsk::Commands::Healthcheck.new(Mrsk::Configuration.new(@config, version: "123"))
Mrsk::Commands::Healthcheck.new(Mrsk::Configuration.new(@config, destination: @destination, version: "123"))
end
end