Merge branch 'main' into pr/154
* main: (32 commits) Inline default as with other options Symbols! Fix tests test stop with custom stop wait time No need to replicate Docker default Describe purpose rather than elements Style and ordering Customizable stop wait time Fix tests Ensure it also works when configuring just log options without setting a driver Add accessory test Undo change Improve test Update README Ensure default log option `max-size=10m` #142 Allow to customize container options in accessories Fix flaky test Fix tests More resilient tests Fix other tests ...
This commit is contained in:
@@ -8,24 +8,26 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
|
||||
cli = self
|
||||
|
||||
MRSK.config.roles.each do |role|
|
||||
on(role.hosts) do |host|
|
||||
execute *MRSK.auditor.record("Booted app version #{version}"), verbosity: :debug
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role).record("Booted app version #{version}"), verbosity: :debug
|
||||
|
||||
begin
|
||||
old_version = capture_with_info(*MRSK.app.current_running_version).strip
|
||||
execute *MRSK.app.run(role: role.name)
|
||||
old_version = capture_with_info(*MRSK.app(role: role).current_running_version).strip
|
||||
execute *MRSK.app(role: role).run
|
||||
sleep MRSK.config.readiness_delay
|
||||
execute *MRSK.app.stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
execute *MRSK.app(role: role).stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
|
||||
rescue SSHKit::Command::Failed => e
|
||||
if e.message =~ /already in use/
|
||||
error "Rebooting container with same version #{version} already deployed on #{host} (may cause gap in zero-downtime promise!)"
|
||||
execute *MRSK.auditor.record("Rebooted app version #{version}"), verbosity: :debug
|
||||
execute *MRSK.auditor(role: role).record("Rebooted app version #{version}"), verbosity: :debug
|
||||
|
||||
execute *MRSK.app.stop(version: version)
|
||||
execute *MRSK.app.remove_container(version: version)
|
||||
execute *MRSK.app.run(role: role.name)
|
||||
execute *MRSK.app(role: role).stop(version: version)
|
||||
execute *MRSK.app(role: role).remove_container(version: version)
|
||||
execute *MRSK.app(role: role).run
|
||||
else
|
||||
raise
|
||||
end
|
||||
@@ -39,9 +41,13 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
desc "start", "Start existing app container on servers"
|
||||
def start
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Started app version #{MRSK.config.version}"), verbosity: :debug
|
||||
execute *MRSK.app.start, raise_on_non_zero_exit: false
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor.record("Started app version #{MRSK.config.version}"), verbosity: :debug
|
||||
execute *MRSK.app(role: role).start, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -49,9 +55,13 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
desc "stop", "Stop app container on servers"
|
||||
def stop
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Stopped app"), verbosity: :debug
|
||||
execute *MRSK.app.stop, raise_on_non_zero_exit: false
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role).record("Stopped app"), verbosity: :debug
|
||||
execute *MRSK.app(role: role).stop, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -59,7 +69,13 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
# FIXME: Drop in favor of just containers?
|
||||
desc "details", "Show details about app containers"
|
||||
def details
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.info) }
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
puts_by_host host, capture_with_info(*MRSK.app(role: role).info)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "exec [CMD]", "Execute a custom command on servers (use --help to show options)"
|
||||
@@ -71,7 +87,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
say "Get current version of running container...", :magenta unless options[:version]
|
||||
using_version(options[:version] || current_running_version) do |version|
|
||||
say "Launching interactive command with version #{version} via SSH from existing container on #{MRSK.primary_host}...", :magenta
|
||||
run_locally { exec MRSK.app.execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
|
||||
run_locally { exec MRSK.app(role: "web").execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
|
||||
end
|
||||
|
||||
when options[:interactive]
|
||||
@@ -87,8 +103,12 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
say "Launching command with version #{version} from existing container...", :magenta
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
||||
puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd))
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role).record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
||||
puts_by_host host, capture_with_info(*MRSK.app(role: role).execute_in_existing_container(cmd))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -156,9 +176,13 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
desc "remove_container [VERSION]", "Remove app container with given version from servers", hide: true
|
||||
def remove_container(version)
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed app container with version #{version}"), verbosity: :debug
|
||||
execute *MRSK.app.remove_container(version: version)
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role).record("Removed app container with version #{version}"), verbosity: :debug
|
||||
execute *MRSK.app(role: role).remove_container(version: version)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -166,9 +190,13 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
desc "remove_containers", "Remove all app containers from servers", hide: true
|
||||
def remove_containers
|
||||
with_lock do
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed all app containers"), verbosity: :debug
|
||||
execute *MRSK.app.remove_containers
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role).record("Removed all app containers"), verbosity: :debug
|
||||
execute *MRSK.app(role: role).remove_containers
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,7 +9,6 @@ class Mrsk::Commander
|
||||
self.lock_count = 0
|
||||
end
|
||||
|
||||
|
||||
def config
|
||||
@config ||= Mrsk::Configuration.create_from(**@config_kwargs).tap do |config|
|
||||
@config_kwargs = nil
|
||||
@@ -21,23 +20,38 @@ class Mrsk::Commander
|
||||
@config, @config_kwargs = nil, kwargs
|
||||
end
|
||||
|
||||
|
||||
attr_accessor :specific_hosts
|
||||
attr_reader :specific_roles, :specific_hosts
|
||||
|
||||
def specific_primary!
|
||||
self.specific_hosts = [ config.primary_web_host ]
|
||||
end
|
||||
|
||||
def specific_roles=(role_names)
|
||||
self.specific_hosts = config.roles.select { |r| role_names.include?(r.name) }.flat_map(&:hosts) if role_names.present?
|
||||
@specific_roles = config.roles.select { |r| role_names.include?(r.name) } if role_names.present?
|
||||
end
|
||||
|
||||
def specific_hosts=(hosts)
|
||||
@specific_hosts = config.all_hosts & hosts if hosts.present?
|
||||
end
|
||||
|
||||
def primary_host
|
||||
specific_hosts&.first || config.primary_web_host
|
||||
end
|
||||
|
||||
def roles
|
||||
(specific_roles || config.roles).select do |role|
|
||||
((specific_hosts || config.all_hosts) & role.hosts).any?
|
||||
end
|
||||
end
|
||||
|
||||
def hosts
|
||||
specific_hosts || config.all_hosts
|
||||
(specific_hosts || config.all_hosts).select do |host|
|
||||
(specific_roles || config.roles).flat_map(&:hosts).include?(host)
|
||||
end
|
||||
end
|
||||
|
||||
def roles_on(host)
|
||||
roles.select { |role| role.hosts.include?(host.to_s) }.map(&:name)
|
||||
end
|
||||
|
||||
def traefik_hosts
|
||||
@@ -53,16 +67,16 @@ class Mrsk::Commander
|
||||
end
|
||||
|
||||
|
||||
def app
|
||||
@app ||= Mrsk::Commands::App.new(config)
|
||||
def app(role: nil)
|
||||
Mrsk::Commands::App.new(config, role: role)
|
||||
end
|
||||
|
||||
def accessory(name)
|
||||
Mrsk::Commands::Accessory.new(config, name: name)
|
||||
end
|
||||
|
||||
def auditor
|
||||
@auditor ||= Mrsk::Commands::Auditor.new(config)
|
||||
def auditor(role: nil)
|
||||
Mrsk::Commands::Auditor.new(config, role: role)
|
||||
end
|
||||
|
||||
def builder
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
||||
attr_reader :accessory_config
|
||||
delegate :service_name, :image, :host, :port, :files, :directories, :publish_args, :env_args, :volume_args, :label_args, to: :accessory_config
|
||||
delegate :service_name, :image, :host, :port, :files, :directories, :publish_args, :env_args, :volume_args,
|
||||
:label_args, :option_args, to: :accessory_config
|
||||
|
||||
def initialize(config, name:)
|
||||
super(config)
|
||||
@@ -12,11 +13,12 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
||||
"--name", service_name,
|
||||
"--detach",
|
||||
"--restart", "unless-stopped",
|
||||
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
|
||||
*config.logging_args,
|
||||
*publish_args,
|
||||
*env_args,
|
||||
*volume_args,
|
||||
*label_args,
|
||||
*option_args,
|
||||
image
|
||||
end
|
||||
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def run(role: :web)
|
||||
role = config.role(role)
|
||||
attr_reader :role
|
||||
|
||||
def initialize(config, role: nil)
|
||||
super(config)
|
||||
@role = role
|
||||
end
|
||||
|
||||
def run
|
||||
role = config.role(self.role)
|
||||
|
||||
docker :run,
|
||||
"--detach",
|
||||
"--restart unless-stopped",
|
||||
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
|
||||
"--name", service_with_version_and_destination,
|
||||
"-e", "MRSK_CONTAINER_NAME=\"#{service_with_version_and_destination}\"",
|
||||
"--name", container_name,
|
||||
"-e", "MRSK_CONTAINER_NAME=\"#{container_name}\"",
|
||||
*role.env_args,
|
||||
*config.logging_args,
|
||||
*config.volume_args,
|
||||
*role.label_args,
|
||||
*role.option_args,
|
||||
@@ -17,13 +24,13 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def start
|
||||
docker :start, service_with_version_and_destination
|
||||
docker :start, container_name
|
||||
end
|
||||
|
||||
def stop(version: nil)
|
||||
pipe \
|
||||
version ? container_id_for_version(version) : current_container_id,
|
||||
xargs(docker(:stop))
|
||||
xargs(config.stop_wait_time ? docker(:stop, "-t", config.stop_wait_time) : docker(:stop))
|
||||
end
|
||||
|
||||
def info
|
||||
@@ -52,7 +59,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def execute_in_existing_container(*command, interactive: false)
|
||||
docker :exec,
|
||||
("-it" if interactive),
|
||||
service_with_version_and_destination,
|
||||
container_name,
|
||||
*command
|
||||
end
|
||||
|
||||
@@ -97,7 +104,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
def remove_container(version:)
|
||||
pipe \
|
||||
container_id_for(container_name: service_with_version_and_destination(version)),
|
||||
container_id_for(container_name: container_name(version)),
|
||||
xargs(docker(:container, :rm))
|
||||
end
|
||||
|
||||
@@ -115,12 +122,12 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
|
||||
private
|
||||
def service_with_version_and_destination(version = nil)
|
||||
[ config.service, config.destination, version || config.version ].compact.join("-")
|
||||
def container_name(version = nil)
|
||||
[ config.service, role, config.destination, version || config.version ].compact.join("-")
|
||||
end
|
||||
|
||||
def container_id_for_version(version)
|
||||
container_id_for(container_name: service_with_version_and_destination(version))
|
||||
container_id_for(container_name: container_name(version))
|
||||
end
|
||||
|
||||
def filter_args
|
||||
@@ -130,6 +137,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def filters
|
||||
[ "label=service=#{config.service}" ].tap do |filters|
|
||||
filters << "label=destination=#{config.destination}" if config.destination
|
||||
filters << "label=role=#{role}" if role
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
require "active_support/core_ext/time/conversions"
|
||||
|
||||
class Mrsk::Commands::Auditor < Mrsk::Commands::Base
|
||||
attr_reader :role
|
||||
|
||||
def initialize(config, role: nil)
|
||||
super(config)
|
||||
@role = role
|
||||
end
|
||||
|
||||
# Runs remotely
|
||||
def record(line)
|
||||
append \
|
||||
@@ -25,18 +32,26 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def tagged_record_line(line)
|
||||
"'#{recorded_at_tag} #{performer_tag} #{line}'"
|
||||
tagged_line recorded_at_tag, performer_tag, role_tag, line
|
||||
end
|
||||
|
||||
def tagged_broadcast_line(line)
|
||||
"'#{performer_tag} #{line}'"
|
||||
tagged_line performer_tag, role_tag, line
|
||||
end
|
||||
|
||||
def tagged_line(*tags_and_line)
|
||||
"'#{tags_and_line.compact.join(" ")}'"
|
||||
end
|
||||
|
||||
def recorded_at_tag
|
||||
"[#{Time.now.to_fs(:db)}]"
|
||||
end
|
||||
|
||||
def performer_tag
|
||||
"[#{`whoami`.strip}]"
|
||||
end
|
||||
|
||||
def recorded_at_tag
|
||||
"[#{Time.now.to_fs(:db)}]"
|
||||
def role_tag
|
||||
"[#{role}]" if role
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,8 +2,6 @@ module Mrsk::Commands
|
||||
class Base
|
||||
delegate :redact, :argumentize, to: Mrsk::Utils
|
||||
|
||||
MAX_LOG_SIZE = "10m"
|
||||
|
||||
attr_accessor :config
|
||||
|
||||
def initialize(config)
|
||||
|
||||
@@ -7,9 +7,9 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||
docker :run, "--name traefik",
|
||||
"--detach",
|
||||
"--restart", "unless-stopped",
|
||||
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
|
||||
"--publish", port,
|
||||
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
|
||||
*config.logging_args,
|
||||
*docker_options_args,
|
||||
"traefik",
|
||||
"--providers.docker",
|
||||
@@ -50,7 +50,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||
docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik"
|
||||
end
|
||||
|
||||
def port
|
||||
def port
|
||||
"#{host_port}:#{CONTAINER_PORT}"
|
||||
end
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ require "erb"
|
||||
require "net/ssh/proxy/jump"
|
||||
|
||||
class Mrsk::Configuration
|
||||
delegate :service, :image, :servers, :env, :labels, :registry, :builder, to: :raw_config, allow_nil: true
|
||||
delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
|
||||
delegate :service, :image, :servers, :env, :labels, :registry, :builder, :stop_wait_time, to: :raw_config, allow_nil: true
|
||||
delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils
|
||||
|
||||
attr_accessor :destination
|
||||
attr_accessor :raw_config
|
||||
@@ -76,7 +76,7 @@ class Mrsk::Configuration
|
||||
|
||||
|
||||
def all_hosts
|
||||
roles.flat_map(&:hosts)
|
||||
roles.flat_map(&:hosts).uniq
|
||||
end
|
||||
|
||||
def primary_web_host
|
||||
@@ -84,7 +84,7 @@ class Mrsk::Configuration
|
||||
end
|
||||
|
||||
def traefik_hosts
|
||||
roles.select(&:running_traefik?).flat_map(&:hosts)
|
||||
roles.select(&:running_traefik?).flat_map(&:hosts).uniq
|
||||
end
|
||||
|
||||
|
||||
@@ -121,6 +121,15 @@ class Mrsk::Configuration
|
||||
end
|
||||
end
|
||||
|
||||
def logging_args
|
||||
if raw_config.logging.present?
|
||||
optionize({ "log-driver" => raw_config.logging["driver"] }.compact) +
|
||||
argumentize("--log-opt", raw_config.logging["options"])
|
||||
else
|
||||
argumentize("--log-opt", { "max-size" => "10m" })
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def ssh_user
|
||||
if raw_config.ssh.present?
|
||||
@@ -173,6 +182,7 @@ class Mrsk::Configuration
|
||||
ssh_options: ssh_options,
|
||||
builder: raw_config.builder,
|
||||
accessories: raw_config.accessories,
|
||||
logging: logging_args,
|
||||
healthcheck: healthcheck
|
||||
}.compact
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Mrsk::Configuration::Accessory
|
||||
delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
|
||||
delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils
|
||||
|
||||
attr_accessor :name, :specifics
|
||||
|
||||
@@ -67,6 +67,14 @@ class Mrsk::Configuration::Accessory
|
||||
argumentize "--volume", volumes
|
||||
end
|
||||
|
||||
def option_args
|
||||
if args = specifics["options"]
|
||||
optionize args
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
attr_accessor :config
|
||||
|
||||
|
||||
Reference in New Issue
Block a user