Merge branch 'main' into pr/53

* main:
  Bump version for 0.8.0
  Remove images of the same name before pulling a new one
  Changed to a timeout
  Better language
  Switch to ruby-based retry
This commit is contained in:
David Heinemeier Hansson
2023-02-20 18:14:47 +01:00
9 changed files with 48 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
mrsk (0.7.2) mrsk (0.8.0)
activesupport (>= 7.0) activesupport (>= 7.0)
dotenv (~> 2.8) dotenv (~> 2.8)
sshkit (~> 1.21) sshkit (~> 1.21)

View File

@@ -30,6 +30,7 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
def pull def pull
on(MRSK.hosts) do on(MRSK.hosts) do
execute *MRSK.auditor.record("Pulled image with version #{MRSK.version}"), verbosity: :debug execute *MRSK.auditor.record("Pulled image with version #{MRSK.version}"), verbosity: :debug
execute *MRSK.builder.clean, raise_on_non_zero_exit: false
execute *MRSK.builder.pull execute *MRSK.builder.pull
end end
end end

View File

@@ -1,4 +1,6 @@
class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base
MAX_ATTEMPTS = 5
default_command :perform default_command :perform
desc "perform", "Health check current app version" desc "perform", "Health check current app version"
@@ -8,18 +10,31 @@ class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base
execute *MRSK.healthcheck.run execute *MRSK.healthcheck.run
target = "Health check against #{MRSK.config.healthcheck["path"]}" target = "Health check against #{MRSK.config.healthcheck["path"]}"
attempt = 1
if capture_with_info(*MRSK.healthcheck.curl) == "200" begin
info "#{target} succeeded with 200 OK!" status = capture_with_info(*MRSK.healthcheck.curl)
else
# Catches 1xx, 2xx, 3xx if status == "200"
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!" info "#{target} succeeded with 200 OK!"
else
raise "#{target} failed with status #{status}"
end
rescue SSHKit::Command::Failed
if attempt <= MAX_ATTEMPTS
info "#{target} failed to respond, retrying in #{attempt}s..."
sleep attempt
attempt += 1
retry
else
raise
end
end end
rescue SSHKit::Command::Failed => e rescue SSHKit::Command::Failed => e
error capture_with_info(*MRSK.healthcheck.logs) error capture_with_info(*MRSK.healthcheck.logs)
if e.message =~ /curl/ if e.message =~ /curl/
# Catches 4xx, 5xx
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!" raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!"
else else
raise raise

View File

@@ -1,5 +1,5 @@
class Mrsk::Commands::Builder < Mrsk::Commands::Base class Mrsk::Commands::Builder < Mrsk::Commands::Base
delegate :create, :remove, :push, :pull, :info, to: :target delegate :create, :remove, :push, :clean, :pull, :info, to: :target
def name def name
target.class.to_s.remove("Mrsk::Commands::Builder::").underscore target.class.to_s.remove("Mrsk::Commands::Builder::").underscore

View File

@@ -1,6 +1,10 @@
class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
delegate :argumentize, to: Mrsk::Utils delegate :argumentize, to: Mrsk::Utils
def clean
docker :image, :rm, "--force", config.absolute_image
end
def pull def pull
docker :pull, config.absolute_image docker :pull, config.absolute_image
end end

View File

@@ -16,11 +16,11 @@ class Mrsk::Commands::Healthcheck < Mrsk::Commands::Base
end end
def curl def curl
[ :curl, "--silent", "--output", "/dev/null", "--write-out", "'%{http_code}'", "--retry-max-time", 10, "--retry", 5, health_url ] [ :curl, "--silent", "--output", "/dev/null", "--write-out", "'%{http_code}'", "--max-time", "2", health_url ]
end end
def logs def logs
pipe container_id, xargs(docker(:logs, "2>&1")) pipe container_id, xargs(docker(:logs, "--tail", 50, "2>&1"))
end end
def stop def stop

View File

@@ -1,3 +1,3 @@
module Mrsk module Mrsk
VERSION = "0.7.2" VERSION = "0.8.0"
end end

15
test/cli/build_test.rb Normal file
View File

@@ -0,0 +1,15 @@
require_relative "cli_test_case"
class CliBuildTest < CliTestCase
test "pull" do
run_command("pull").tap do |output|
assert_match /docker image rm --force dhh\/app:999/, output
assert_match /docker pull dhh\/app:999/, output
end
end
private
def run_command(*command)
stdouted { Mrsk::Cli::Build.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
end
end

View File

@@ -24,7 +24,7 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase
test "curl" do test "curl" do
assert_equal \ assert_equal \
"curl --silent --output /dev/null --write-out '%{http_code}' --retry-max-time 10 --retry 5 http://localhost:3999/up", "curl --silent --output /dev/null --write-out '%{http_code}' --max-time 2 http://localhost:3999/up",
new_command.curl.join(" ") new_command.curl.join(" ")
end end
@@ -32,7 +32,7 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase
@config[:healthcheck] = { "path" => "/healthz" } @config[:healthcheck] = { "path" => "/healthz" }
assert_equal \ assert_equal \
"curl --silent --output /dev/null --write-out '%{http_code}' --retry-max-time 10 --retry 5 http://localhost:3999/healthz", "curl --silent --output /dev/null --write-out '%{http_code}' --max-time 2 http://localhost:3999/healthz",
new_command.curl.join(" ") new_command.curl.join(" ")
end end