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,6 +3,7 @@ require "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)"
def boot
using_most_recent_version_available do
MRSK.config.roles.each do |role|
on(role.hosts) do |host|
begin
@@ -18,6 +19,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
end
end
end
end
desc "reboot", "Reboot app on host (stop container, remove containers, start new container)"
def reboot
@@ -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)"
def console
using_most_recent_version_available do |version|
run_locally do
info "Launching Rails console on #{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
@@ -144,8 +152,41 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
on(MRSK.hosts) { execute *MRSK.app.remove_containers }
end
desc "remove_images [NAME]", "Remove app images from servers"
desc "remove_images", "Remove app images from servers"
def remove_images
on(MRSK.hosts) { execute *MRSK.app.remove_images }
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

View File

@@ -1,6 +1,9 @@
require "mrsk/commands/base"
require "mrsk/commands/concerns/repository"
class Mrsk::Commands::App < Mrsk::Commands::Base
include Mrsk::Commands::Concerns::Repository
def run(role: :web)
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 :argumentize, :argumentize_env_with_secrets, to: Mrsk::Utils
attr_accessor :version
attr_accessor :raw_config
class << self
@@ -73,10 +74,6 @@ class Mrsk::Configuration
end
def version
@version
end
def repository
[ raw_config.registry["server"], image ].compact.join("/")
end