Skip to content

Commit

Permalink
make easier to expand healthchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed Jul 8, 2024
1 parent a5b0dc4 commit 74808d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
33 changes: 25 additions & 8 deletions library/1.0.0/healthchecks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,48 @@ def check_health(test, interval=10, timeout=10, retries=5, start_period=30):
}


def pg_test(user, db, host="127.0.0.1", port=5432):
def pg_test(user, db, config={}):
if not user or not db:
utils.throw_error("Postgres container: [user] and [db] must be set")

host = config.get("host", "127.0.0.1")
port = config.get("port", 5432)

return f"pg_isready -h {host} -p {port} -d {db} -U {user}"


def curl_test(port, path, headers=[], scheme="http", host="127.0.0.1"):
if not port or not path or not host or not scheme:
utils.throw_error("Expected [port], [path], [host] and [scheme] to be set")
def curl_test(port, path, config={}):
if not port or not path:
utils.throw_error("Expected [port] and [path] to be set")

scheme = config.get("scheme", "http")
host = config.get("host", "127.0.0.1")
headers = config.get("headers", [])

opts = []
if scheme == "https":
opts.append("--insecure")

for header in headers:
if not header[0] or not header[1]:
utils.throw_error("Expected [header] to be a list of two items")
opts.append(f'--header "{header[0]}: {header[1]}"')

return f"curl --silent --output /dev/null --show-error --fail {' '.join(opts)} {scheme}://{host}:{port}{path}"


def wget_test(port, path, headers=[], scheme="http", host="127.0.0.1"):
if not port or not path or not host or not scheme:
utils.throw_error("Expected [port], [path], [host] and [scheme] to be set")
def wget_test(port, path, config={}):
if not port or not path:
utils.throw_error("Expected [port] and [path] to be set")

scheme = config.get("scheme", "http")
host = config.get("host", "127.0.0.1")
headers = config.get("headers", [])

opts = []
if scheme == "https":
opts.append("--no-check-certificate")

for header in headers:
if not header[0] or not header[1]:
utils.throw_error("Expected [header] to be a list of two items")
Expand All @@ -49,8 +64,10 @@ def wget_test(port, path, headers=[], scheme="http", host="127.0.0.1"):
return f"wget --spider --quiet {' '.join(opts)} {scheme}://{host}:{port}{path}"


def http_test(port, path, host="127.0.0.1"):
def http_test(port, path, config={}):
if not port or not path:
utils.throw_error("Expected [port] and [path] to be set")

host = config.get("host", "127.0.0.1")

return f"/bin/bash -c 'exec {{health_check_fd}}<>/dev/tcp/{host}/{port} && echo -e \"GET {path} HTTP/1.1\\r\\nHost: {host}\\r\\nConnection: close\\r\\n\\r\\n\" >&$${{health_check_fd}} && cat <&$${{health_check_fd}}'"
2 changes: 1 addition & 1 deletion library/hashes.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0.0.1: f074617a82a86d2a6cc78a4c8a4296fc9d168e456f12713e50c696557b302133
1.0.0: 1644591cef9fa1e1d580a435377ac66d71c472763ab6828839316fd92323019a
1.0.0: 449b47221de4849a92b4f807987adfa6c5fc808bc80c721208366404cd2c5f3e

0 comments on commit 74808d3

Please sign in to comment.