Skip to content

Instantly share code, notes, and snippets.

@latenssi
Created April 10, 2019 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latenssi/d204654567f94efc499f45534ae1091d to your computer and use it in GitHub Desktop.
Save latenssi/d204654567f94efc499f45534ae1091d to your computer and use it in GitHub Desktop.
Generate healthchecks
from __future__ import print_function
import os
import json
try:
from urllib.request import Request, urlopen # Python 3
except ImportError:
from urllib2 import Request, urlopen # Python 2
req = Request(os.getenv("HC_API_URL", ""))
req.add_header('X-Api-Key', os.getenv("HC_API_KEY", ""))
checks = [
{"name": c["name"], "url":c["ping_url"]}
for c in json.load(urlopen(req))["checks"]
]
def generateCmd(name, url):
str = 'if [ -z `sudo docker ps -q --no-trunc | grep $(sudo docker-compose ps -q {name})` ]; '
str += 'then curl -fsS --retry 3 {url}/fail; '
str += 'else curl -fsS --retry 3 {url}; '
str += 'fi'
return str.format(name=name, url=url)
cmds = [generateCmd(c["name"], c["url"]) for c in checks]
print("\n".join(cmds))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment