-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from mrsked/health-check-with-deploy
Add healthcheck before deploy
- Loading branch information
Showing
11 changed files
with
178 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base | ||
desc "perform", "Health check the current version of the app" | ||
def perform | ||
on(MRSK.primary_host) do | ||
begin | ||
execute *MRSK.healthcheck.run | ||
|
||
target = "Health check against #{MRSK.config.healthcheck["path"]}" | ||
|
||
if capture_with_info(*MRSK.healthcheck.curl) == "200" | ||
info "#{target} succeeded with 200 OK!" | ||
else | ||
# Catches 1xx, 2xx, 3xx | ||
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!" | ||
end | ||
rescue SSHKit::Command::Failed => e | ||
if e.message =~ /curl/ | ||
# Catches 4xx, 5xx | ||
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!" | ||
else | ||
raise | ||
end | ||
ensure | ||
execute *MRSK.healthcheck.stop, raise_on_non_zero_exit: false | ||
execute *MRSK.healthcheck.remove, raise_on_non_zero_exit: false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Mrsk::Commands::Healthcheck < Mrsk::Commands::Base | ||
EXPOSED_PORT = 3999 | ||
|
||
def run | ||
web = config.role(:web) | ||
|
||
docker :run, | ||
"-d", | ||
"--name", container_name_with_version, | ||
"-p", "#{EXPOSED_PORT}:#{config.healthcheck["port"]}", | ||
"--label", "service=#{container_name}", | ||
*web.env_args, | ||
*config.volume_args, | ||
config.absolute_image, | ||
web.cmd | ||
end | ||
|
||
def curl | ||
[ :curl, "--silent", "--output", "/dev/null", "--write-out", "'%{http_code}'", health_url ] | ||
end | ||
|
||
def stop | ||
pipe \ | ||
container_id_for(container_name: container_name), | ||
xargs(docker(:stop)) | ||
end | ||
|
||
def remove | ||
pipe \ | ||
container_id_for(container_name: container_name), | ||
xargs(docker(:container, :rm)) | ||
end | ||
|
||
private | ||
def container_name | ||
"healthcheck-#{config.service}" | ||
end | ||
|
||
def container_name_with_version | ||
"healthcheck-#{config.service_with_version}" | ||
end | ||
|
||
def health_url | ||
"http://localhost:#{EXPOSED_PORT}#{config.healthcheck["path"]}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require "test_helper" | ||
|
||
class CommandsHealthcheckTest < ActiveSupport::TestCase | ||
setup do | ||
@config = { | ||
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ], | ||
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } } | ||
} | ||
end | ||
|
||
test "run" do | ||
assert_equal \ | ||
"docker run -d --name healthcheck-app-123 -p 3999:3000 --label service=healthcheck-app dhh/app:123", | ||
new_command.run.join(" ") | ||
end | ||
|
||
test "run with custom port" do | ||
@config[:healthcheck] = { "port" => 3001 } | ||
|
||
assert_equal \ | ||
"docker run -d --name healthcheck-app-123 -p 3999:3001 --label service=healthcheck-app dhh/app:123", | ||
new_command.run.join(" ") | ||
end | ||
|
||
test "curl" do | ||
assert_equal \ | ||
"curl --silent --output /dev/null --write-out '%{http_code}' http://localhost:3999/up", | ||
new_command.curl.join(" ") | ||
end | ||
|
||
test "curl with custom path" do | ||
@config[:healthcheck] = { "path" => "/healthz" } | ||
|
||
assert_equal \ | ||
"curl --silent --output /dev/null --write-out '%{http_code}' http://localhost:3999/healthz", | ||
new_command.curl.join(" ") | ||
end | ||
|
||
test "stop" do | ||
assert_equal \ | ||
"docker container ls -a -f name=healthcheck-app -q | xargs docker stop", | ||
new_command.stop.join(" ") | ||
end | ||
|
||
test "remove" do | ||
assert_equal \ | ||
"docker container ls -a -f name=healthcheck-app -q | xargs docker container rm", | ||
new_command.remove.join(" ") | ||
end | ||
|
||
private | ||
def new_command | ||
Mrsk::Commands::Healthcheck.new(Mrsk::Configuration.new(@config, version: "123")) | ||
end | ||
end |