| 1 | from __future__ import annotations |
| 2 | |
| 3 | import logging |
| 4 | import warnings |
| 5 | from typing import Any, Optional |
| 6 | |
| 7 | log = logging.getLogger(__name__) |
| 8 | |
| 9 | |
| 10 | class Check: |
| 11 | """ |
| 12 | There are three different kinds of checks: script, http and ttl |
| 13 | """ |
| 14 | |
| 15 | @classmethod |
| 16 | def script(cls, args, interval, deregister=None) -> dict[str, Any]: |
| 17 | """ |
| 18 | Run the script *args* every *interval* (e.g. "10s") to peform health |
| 19 | check |
| 20 | """ |
| 21 | if isinstance(args, (str, bytes)): |
| 22 | warnings.warn("Check.script should take a list of args", DeprecationWarning) |
| 23 | args = ["sh", "-c", args] |
| 24 | ret = {"args": args, "interval": interval} |
| 25 | if deregister: |
| 26 | ret["DeregisterCriticalServiceAfter"] = deregister |
| 27 | return ret |
| 28 | |
| 29 | @classmethod |
| 30 | def http(cls, url, interval, timeout=None, deregister=None, header=None) -> dict[str, Any]: |
| 31 | """ |
| 32 | Peform a HTTP GET against *url* every *interval* (e.g. "10s") to peform |
| 33 | health check with an optional *timeout* and optional *deregister* after |
| 34 | which a failing service will be automatically deregistered. Optional |
| 35 | parameter *header* specifies headers sent in HTTP request. *header* |
| 36 | paramater is in form of map of lists of strings, |
| 37 | e.g. {"x-foo": ["bar", "baz"]}. |
| 38 | """ |
| 39 | ret = {"http": url, "interval": interval} |
| 40 | if timeout: |
| 41 | ret["timeout"] = timeout |
| 42 | if deregister: |
| 43 | ret["DeregisterCriticalServiceAfter"] = deregister |
| 44 | if header: |
| 45 | ret["header"] = header |
| 46 | return ret |
| 47 | |
| 48 | @classmethod |
| 49 | def tcp(cls, host: str, port: int, interval, timeout=None, deregister=None) -> dict[str, Any]: |
| 50 | """ |
| 51 | Attempt to establish a tcp connection to the specified *host* and |
| 52 | *port* at a specified *interval* with optional *timeout* and optional |
| 53 | *deregister* after which a failing service will be automatically |
| 54 | deregistered. |
| 55 | """ |
| 56 | ret = {"tcp": f"{host:s}:{port:d}", "interval": interval} |
| 57 | if timeout: |
| 58 | ret["timeout"] = timeout |
| 59 | if deregister: |
| 60 | ret["DeregisterCriticalServiceAfter"] = deregister |
| 61 | return ret |
| 62 | |
| 63 | @classmethod |
| 64 | def ttl(cls, ttl: str) -> dict[str, Any]: |
| 65 | """ |
| 66 | Set check to be marked as critical after *ttl* (e.g. "10s") unless the |
| 67 | check is periodically marked as passing. |
| 68 | """ |
| 69 | return {"ttl": ttl} |
| 70 | |
| 71 | @classmethod |
| 72 | def docker(cls, container_id, shell, script, interval, deregister=None) -> dict[str, Any]: |
| 73 | """ |
| 74 | Invoke *script* packaged within a running docker container with |
| 75 | *container_id* at a specified *interval* on the configured |
| 76 | *shell* using the Docker Exec API. Optional *register* after which a |
| 77 | failing service will be automatically deregistered. |
| 78 | """ |
| 79 | ret = {"docker_container_id": container_id, "shell": shell, "script": script, "interval": interval} |
| 80 | if deregister: |
| 81 | ret["DeregisterCriticalServiceAfter"] = deregister |
| 82 | return ret |
| 83 | |
| 84 | @classmethod |
| 85 | def _compat( |
| 86 | cls, script=None, interval=None, ttl: Optional[int] = None, http=None, timeout=None, deregister=None |
| 87 | ) -> dict[str, Any]: |
| 88 | if not script and not http and not ttl: |
| 89 | return {} |
| 90 | |
| 91 | log.warning("DEPRECATED: use consul.Check.script/http/ttl to specify check") |
| 92 | |
| 93 | ret: dict[str, Any] = {"check": {}} |
| 94 | |
| 95 | if script: |
| 96 | assert interval |
| 97 | assert not ttl |
| 98 | assert not http |
| 99 | ret["check"] = {"script": script, "interval": interval} |
| 100 | if ttl: |
| 101 | assert not interval or script |
| 102 | assert not http |
| 103 | ret["check"] = {"ttl": ttl} |
| 104 | if http: |
| 105 | assert interval |
| 106 | assert not script |
| 107 | assert not ttl |
| 108 | ret["check"] = {"http": http, "interval": interval} |
| 109 | if timeout: |
| 110 | assert http |
| 111 | ret["check"]["timeout"] = timeout |
| 112 | |
| 113 | if deregister: |
| 114 | ret["check"]["DeregisterCriticalServiceAfter"] = deregister |
| 115 | |
| 116 | return ret |