Run boot and console on relevant versions

Instead of just defaulting to local hash version
This commit is contained in:
David Heinemeier Hansson
2023-02-02 18:05:03 +01:00
parent c8f673ef7c
commit 6d80005f5d
4 changed files with 76 additions and 18 deletions

View File

@@ -3,16 +3,18 @@ require "mrsk/cli/base"
class Mrsk::Cli::App < Mrsk::Cli::Base class Mrsk::Cli::App < Mrsk::Cli::Base
desc "boot", "Boot app on servers (or start them if they've already been booted)" desc "boot", "Boot app on servers (or start them if they've already been booted)"
def boot def boot
MRSK.config.roles.each do |role| using_most_recent_version_available do
on(role.hosts) do |host| MRSK.config.roles.each do |role|
begin on(role.hosts) do |host|
execute *MRSK.app.run(role: role.name) begin
rescue SSHKit::Command::Failed => e execute *MRSK.app.run(role: role.name)
if e.message =~ /already in use/ rescue SSHKit::Command::Failed => e
error "Container with same version already deployed on #{host}, starting that instead" if e.message =~ /already in use/
execute *MRSK.app.start, host: host error "Container with same version already deployed on #{host}, starting that instead"
else execute *MRSK.app.start, host: host
raise else
raise
end
end end
end end
end end
@@ -69,9 +71,15 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
desc "console", "Start Rails Console on primary host (or specific host set by --hosts)" desc "console", "Start Rails Console on primary host (or specific host set by --hosts)"
def console def console
run_locally do using_most_recent_version_available do |version|
info "Launching Rails console on #{MRSK.primary_host}" run_locally do
exec MRSK.app.console(host: MRSK.primary_host) if version
info "Launching Rails console on #{MRSK.primary_host} [Version: #{version}]"
exec MRSK.app.console(host: MRSK.primary_host)
else
error "No image available for #{MRSK.config.repository}"
end
end
end end
end end
@@ -144,8 +152,41 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
on(MRSK.hosts) { execute *MRSK.app.remove_containers } on(MRSK.hosts) { execute *MRSK.app.remove_containers }
end end
desc "remove_images [NAME]", "Remove app images from servers" desc "remove_images", "Remove app images from servers"
def remove_images def remove_images
on(MRSK.hosts) { execute *MRSK.app.remove_images } on(MRSK.hosts) { execute *MRSK.app.remove_images }
end end
private
def using_most_recent_version_available(host: MRSK.primary_host)
using_version(most_recent_version_available(host: host)) do |version|
yield version
end
end
def using_current_running_version(host: MRSK.primary_host)
using_version(current_running_version(host: host)) do |version|
yield version
end
end
def using_version(new_version)
old_version = MRSK.config.version
MRSK.config.version = new_version
yield new_version
ensure
MRSK.config.version = old_version
end
def most_recent_version_available(host:)
version = nil
on(host) { version = capture_with_info(*MRSK.app.most_recent_version_from_available_images).strip }
version
end
def current_running_version(host:)
version = nil
on(host) { version = capture_with_info(*MRSK.app.current_running_version).strip }
version
end
end end

View File

@@ -1,6 +1,9 @@
require "mrsk/commands/base" require "mrsk/commands/base"
require "mrsk/commands/concerns/repository"
class Mrsk::Commands::App < Mrsk::Commands::Base class Mrsk::Commands::App < Mrsk::Commands::Base
include Mrsk::Commands::Concerns::Repository
def run(role: :web) def run(role: :web)
role = config.role(role) role = config.role(role)

View File

@@ -0,0 +1,17 @@
module Mrsk::Commands::Concerns
module Repository
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=hey", "--format", '"{{.Names}}"'),
"sed 's/-/\n/g'",
"tail -n 1"
end
def most_recent_version_from_available_images
pipe \
docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository),
"head -n 1"
end
end
end

View File

@@ -9,6 +9,7 @@ class Mrsk::Configuration
delegate :service, :image, :servers, :env, :labels, :registry, :builder, to: :raw_config, allow_nil: true delegate :service, :image, :servers, :env, :labels, :registry, :builder, to: :raw_config, allow_nil: true
delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils delegate :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
attr_accessor :version
attr_accessor :raw_config attr_accessor :raw_config
class << self class << self
@@ -73,10 +74,6 @@ class Mrsk::Configuration
end end
def version
@version
end
def repository def repository
[ raw_config.registry["server"], image ].compact.join("/") [ raw_config.registry["server"], image ].compact.join("/")
end end