Skip to content

Commit

Permalink
Fixed docker entrypoint, added more checks, added example
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Sep 23, 2018
1 parent 034c9b5 commit f471163
Show file tree
Hide file tree
Showing 22 changed files with 228 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM alpine:3.8

RUN apk --update add python3 py3-tornado py3-argparse bash perl curl wget grep sed
RUN apk --update add python3 py3-tornado py3-argparse bash perl curl wget grep sed docker sudo
ADD ./app/ /app
ADD ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# tests
RUN set -x && cd /app && ./test.sh

ENTRYPOINT ["/bin/bash", "-c", "cd /app && python3 ./infracheck/bin.py --server --server-port 8000 $@"]
ENTRYPOINT ["/entrypoint.sh"]
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,30 @@ How it works?
Running
-------

See a working example in the `./example` directory.

```bash
# from this directory
python ./infracheck/bin.py --help
```

Docker-compose:

```yaml
version: '2'
services:
healthcheck:
image: wolnosciowiec/infracheck
command: " --directory=/data --server-path-prefix=/some-prefix"
volumes:
# place your health checks structure at ./healthchecks
- "./healthchecks:/data"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
ports:
- "8000:8000"
#labels:
# - "traefik.frontend.rule=Host: health.localhost; PathPrefix: /some-prefix"
# - "traefik.enable=true"
# - "traefik.basic.protocol=${PROTO}"
# - "traefik.port=8000"
```
21 changes: 21 additions & 0 deletions app/checks/dir-present
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

#
# Directory presence check
#
# @author Krzysztof Wesołowski
# @url https://iwa-ait.org
#

if [[ ! ${DIR} ]]; then
echo "DIR parameter is missing"
exit 1
fi

if [[ ! -d ${DIR} ]]; then
echo "Failed asserting that directory at '${DIR}' is present"
exit 1
fi

echo "'${DIR}' directory is present"
exit 0
25 changes: 25 additions & 0 deletions app/checks/docker-health
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

#
# Docker services status check
#
# @author Krzysztof Wesołowski
# @url https://iwa-ait.org
#

if ! command -v docker > /dev/null; then
echo "Docker command is not available, cannot perform a check without it"
exit 1
fi

output=$(docker ps --filter "name=${DOCKER_ENV_NAME}" --filter "health=unhealthy" 2>&1|grep "unhealthy")
exit_code=$?

if [[ ${exit_code} == 0 ]]; then
echo "There seems to be at least one unhealthy service at '${DOCKER_ENV_NAME}' space"
docker ps --filter "name=${DOCKER_ENV_NAME}" --filter "health=unhealthy"
exit 1
fi

echo "Docker daemon reports that there is no 'unhealthy' service running in '${DOCKER_ENV_NAME}' space"
exit 0
21 changes: 21 additions & 0 deletions app/checks/file-present
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

#
# File presence check
#
# @author Krzysztof Wesołowski
# @url https://iwa-ait.org
#

if [[ ! ${FILE_PATH} ]]; then
echo "FILE_PATH parameter is missing"
exit 1
fi

if [[ ! -f ${FILE_PATH} ]]; then
echo "Failed asserting that file at '${FILE_PATH}' is present"
exit 1
fi

echo "'${FILE_PATH}' file is present"
exit 0
21 changes: 21 additions & 0 deletions app/checks/port-open
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

#
# File presence check
#
# @author Krzysztof Wesołowski
# @url https://iwa-ait.org
#

if [[ ! ${PO_HOST} ]] || [[ ! ${PO_PORT} ]] || [[ ! ${PO_TIMEOUT} ]]; then
echo "PO_HOST, PO_PORT or PO_TIMEOUT parameter is missing"
exit 1
fi

if ! nc -z -v -w${PO_TIMEOUT} ${PO_HOST} ${PO_PORT}; then
echo "Port ${PO_PORT} at ${PO_HOST} seems to be not active"
exit 1
fi

echo "Port ${PO_PORT} at ${PO_HOST} looks OK"
exit 0
2 changes: 1 addition & 1 deletion app/infracheck/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _assert_valid_format(config_name: str, data):

def _assert_has_valid_type(self, type_name: str):
if not self._find_file_path('/checks/', type_name, ''):
raise Exception('Invalid check type "' + type_name + '"')
raise Exception('Invalid check type "' + type_name + '", was looking in: ' + str(self.paths))

return

Expand Down
18 changes: 17 additions & 1 deletion app/infracheck/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from .config import ConfigLoader
from .server import HttpServer

import os


class Controller:
project_dirs = None # type: list
Expand Down Expand Up @@ -56,4 +58,18 @@ def perform_checks(self):

@staticmethod
def _combine_project_dirs(project_dir: str) -> list:
return [project_dir]
paths = [
# directory specified by eg. the "--directory" commandline parameter
project_dir,

# standalone application running from cloned repository
os.path.dirname(os.path.realpath(__file__)) + '/../',

# official docker container
'/app',

# current directory
os.getcwd(),
]

return list(filter(lambda path: os.path.isdir(path), paths))
6 changes: 6 additions & 0 deletions app/template/data-dir-present.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "dir-present",
"input": {
"dir": "/data"
}
}
6 changes: 6 additions & 0 deletions app/template/docker-health.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "docker-health",
"input": {
"docker_env_name": ""
}
}
6 changes: 6 additions & 0 deletions app/template/file-present.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "file-present",
"input": {
"file_path": "/usr/bin/env"
}
}
8 changes: 8 additions & 0 deletions app/template/port-open.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "port-open",
"input": {
"po_port": "80",
"po_host": "google.com",
"po_timeout": "1"
}
}
4 changes: 4 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

cd /app
exec python3 ./infracheck/bin.py --server --server-port 8000 $@
12 changes: 12 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
InfraCheck example
==================

```bash
sudo docker-compose up -d
curl http://localhost:8000/some-prefix/

# debugging
sudo docker-compose logs

# enjoy!
```
11 changes: 11 additions & 0 deletions example/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '2'
services:
healthcheck:
image: wolnosciowiec/infracheck
command: " --directory=/data --server-path-prefix=/some-prefix"
volumes:
# place your health checks structure at ./healthchecks
- "./healthchecks:/data"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
ports:
- "8000:8000"
Empty file.
7 changes: 7 additions & 0 deletions example/healthchecks/checks/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

echo "Hello! This is an example check, you can write your own 'check' scripts that takes parameters from JSON and environment variables"
echo "The word is: ${WORD}"
env

exit 0
7 changes: 7 additions & 0 deletions example/healthchecks/configured/disk-space.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "disk-space",
"input": {
"dir": "/",
"min_req_space": "6"
}
}
6 changes: 6 additions & 0 deletions example/healthchecks/configured/docker-health.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "docker-health",
"input": {
"docker_env_name": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "hello",
"input": {
"word": "Hi there, Im a passed parameter"
}
}
8 changes: 8 additions & 0 deletions example/healthchecks/configured/is_dd_accessible.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "http",
"input": {
"url": "https://duckduckgo.com",
"expect_keyword": "duck",
"not_expect_keyword": "Server error"
}
}
8 changes: 8 additions & 0 deletions example/healthchecks/configured/some_port_is_open.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "port-open",
"input": {
"po_port": "80",
"po_host": "iwa-ait.org",
"po_timeout": "1"
}
}

0 comments on commit f471163

Please sign in to comment.