Merge branch 'main' into global-logging-config

This commit is contained in:
Samuel Sieg
2023-03-24 15:24:06 +01:00
18 changed files with 252 additions and 154 deletions

View File

@@ -7,24 +7,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
@@ -36,24 +38,38 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
desc "start", "Start existing app container on servers"
def start
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
desc "stop", "Stop app container on servers"
def stop
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
# 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)"
@@ -65,7 +81,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]
@@ -81,8 +97,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
@@ -147,17 +167,25 @@ 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)
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
desc "remove_containers", "Remove all app containers from servers", hide: true
def remove_containers
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

View File

@@ -8,7 +8,6 @@ class Mrsk::Commander
self.verbosity = :info
end
def config
@config ||= Mrsk::Configuration.create_from(**@config_kwargs).tap do |config|
@config_kwargs = nil
@@ -20,23 +19,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
@@ -52,16 +66,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

View File

@@ -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)
@@ -17,6 +18,7 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
*env_args,
*volume_args,
*label_args,
*option_args,
image
end

View File

@@ -1,12 +1,19 @@
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",
"--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,
@@ -17,7 +24,7 @@ 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)
@@ -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

View File

@@ -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

View File

@@ -78,7 +78,7 @@ class Mrsk::Configuration
def all_hosts
roles.flat_map(&:hosts)
roles.flat_map(&:hosts).uniq
end
def primary_web_host
@@ -86,7 +86,7 @@ class Mrsk::Configuration
end
def traefik_hosts
roles.select(&:running_traefik?).flat_map(&:hosts)
roles.select(&:running_traefik?).flat_map(&:hosts).uniq
end

View File

@@ -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