Ejemplo n.º 1
0
def retry():
    global retries
    global max_retries

    if retries < max_retries:
        retries += 1
        return status_code(503)
    else:
        retries = 0
        return status_code(200)
Ejemplo n.º 2
0
def connectivity():
    response = dict()
    data = request.get_json() or {}
    # Test DNS
    nameservers = data.get("nameservers", ["8.8.8.8", "8.8.4.4"])
    if not isinstance(nameservers, list):
        return status_code(400)
    query = data.get("query", "google.com")
    resolver = dns.resolver.Resolver()
    resolver.nameservers = nameservers
    try:
        resolver.query(query)
        response["dns"] = {"status": "ok"}
    except dns.exception.DNSException as e:
        response["dns"] = {"status": "error", "reason": e.__class__.__name__}
    # Test external connectivity
    egress_url = data.get("egress_url", "https://www.google.com")
    try:
        requests.get(egress_url, timeout=3)
        response["egress"] = {"status": "ok"}
    except requests.exceptions.RequestException as e:
        response["egress"] = {
            "status": "error",
            "reason": e.__class__.__name__
        }
    return jsonify(response)
Ejemplo n.º 3
0
def aws(metadata_categories):
    try:
        r = requests.get(AWS_METADATA_ENDPOINT + metadata_categories,
                         timeout=3)
    except requests.exceptions.ConnectionError:
        return jsonify({"message": "aws metadata endpoint not available"}), 501
    if r.status_code == 404:
        return status_code(404)
    return jsonify({metadata_categories: r.text})
Ejemplo n.º 4
0
def proxy():
    data = request.get_json()
    if not data or not isinstance(data, list):
        return status_code(400)

    http_proxy = os.getenv("http_proxy", None)
    if http_proxy:
        proxies = {
            "http": "http://{}".format(http_proxy),
            "https": "http://{}".format(http_proxy)
        }
    else:
        proxies = None

    response = dict()
    for e in data:
        method = e.get("method", "GET")
        payload = e.get("payload", None)
        url = e.get("url", None)
        if url:
            try:
                r = requests.request(method=method.upper(),
                                     url=url,
                                     data=payload,
                                     timeout=5,
                                     proxies=proxies)
                response[url] = {
                    "status": "ok",
                    "status_code": r.status_code,
                    # r.headers is of type requests.structures.CaseInsensitiveDict
                    # We want to convert it to a dictionary to return it into the response
                    "headers": dict(**r.headers)
                }
            except requests.exceptions.RequestException as e:
                response[url] = {
                    "status": "error",
                    # Print the class exception name that should be self explanatory
                    "reason": e.__class__.__name__
                }
        else:
            return jsonify({"message": "url missing"}), 400
    return jsonify(response)
Ejemplo n.º 5
0
def healthcheck_readiness():
    global readiness_healthy
    if readiness_healthy:
        return jsonify({"message": "readiness probe healthy"})
    else:
        return status_code(503)
Ejemplo n.º 6
0
def healthcheck_liveness_fail():
    global liveness_healthy
    liveness_healthy = False
    return status_code(204)
Ejemplo n.º 7
0
def healthcheck_liveness_pass():
    global liveness_healthy
    liveness_healthy = True
    return status_code(204)
Ejemplo n.º 8
0
def healthcheck_liveness():
    global liveness_healthy
    if liveness_healthy:
        return jsonify({"message": "liveness probe healthy"})
    else:
        return status_code(503)
Ejemplo n.º 9
0
def status(code):
    return status_code(code)
Ejemplo n.º 10
0
def delay(sec):
    global max_delay
    time.sleep(min(sec, max_delay))
    return status_code(200)
Ejemplo n.º 11
0
def env(env_var):
    value = os.getenv(env_var)
    if value is None:
        return status_code(404)
    else:
        return jsonify({env_var: value})
Ejemplo n.º 12
0
def healthcheck_readiness_fail():
    global readiness_healthy
    readiness_healthy = False
    return status_code(204)
Ejemplo n.º 13
0
def healthcheck_readiness_pass():
    global readiness_healthy
    readiness_healthy = True
    return status_code(204)