diff --git a/.gitignore b/.gitignore index 7a63f2c..4c78e58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea *~ -venv \ No newline at end of file +venv +.docker_data \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5a6e50a..7ed7c32 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,8 @@ RUN apk add --no-cache bash python3 py-yaml COPY entrypoint.* / COPY conf.d /etc/haproxy/conf.d COPY assets/errors-custom/* /etc/haproxy/errors-custom/ +RUN pip install --upgrade pip \ + && pip install -r requirements.txt ENTRYPOINT [ "/entrypoint.sh" ] CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"] \ No newline at end of file diff --git a/README.md b/README.md index 92e8c48..2efd1ec 100644 --- a/README.md +++ b/README.md @@ -5,53 +5,57 @@ This Docker image will create dynamically the `haproxy.cfg` based on very simple # Features - Enable or disable Stats on port 1936 with custom password -- Simple mapping host => host:port -- Simple redirect host => host:port +- Discover and setup haproxy from Docker Tag +- Discover and setup haproxy redirect from Docker Tag # Basic Usage -Create a yaml file in your machine called `easyconfig.cfg` and put the contents: - -```yaml -stats: - username: admin - password: password - port: 1936 # Optional (default 1936) - -customerrors: true # Optional (default false) - -easymapping: - - port: 80 - hosts: - host1.com.br: container:5000 - host2.com.br: other:3000 - redirect: - www.host1.com.br: http://host1.com.br - - - port: 443 - ssl_cert: /etc/easyconfig/mycert.pem - hosts: - host1.com.br: container:80 - - - port: 8080 - hosts: - host3.com.br: domain:8181 -``` - -Then run (remember to enable the proper ports): +Docker run: ```bash docker run \ - -v /path/to/local:/etc/easyconfig \ -p 80:80 \ -p 8080:8080 \ -p 1936:1936 \ --name easy-haproxy-instance \ + -e DISCOVER=swarm \ + -e HAPROXY_USERNAME=admin \ + -e HAPROXY_PASSWORD=password \ + -e HAPROXY_STATS_PORT=1936 \ + -e REFRESH_STATE=1 \ -d byjg/easy-haproxy ``` +The `DISCOVER` environment variable will define where is located your containers (see below more details): +- swarm +- awsecs +# Tags to defined: + +| Tag | Description | +|--------------------------------------------|---------------------------------------------------------------------------------------------------------| +| com.byjg.easyhaproxy.definitions | A Comma delimited list with the definitions. Each name requires the definition of the parameters below. | +| com.byjg.easyhaproxy.port. | What is the port that the HAProxy will listen to. | +| com.byjg.easyhaproxy.host. | What is the host that the HAProxy will listen to. | +| com.byjg.easyhaproxy.redirect. | Host redirects from connections in the port defined above. | +| com.byjg.easyhaproxy.sslcert. | Cert PEM Base64 encoded. | + +E.g. + +``` +docker run \ + -l com.byjg.easyhaproxy.definitions=http \ + -l com.byjg.easyhaproxy.port.http=80\ + -l com.byjg.easyhaproxy.host.http=byjg.com.br \ + .... +``` + +## Redirect Example: + +```text +www.byjg.com.br=>http://byjg.com.br,byjg.com=>http://byjg.com.br +``` # Mapping custom .cfg files @@ -114,3 +118,18 @@ is the http error code (e.g. 503.http) docker build -t byjg/easy-haproxy . ``` + + +# Docker Swarm + + +# AWS + +Easy HAProxy will try to get tasks from the tasks running in the CLUSTER. +It is important that easy haproxy run in the ECS Cluster and can connect to the containers. + + +You'll have to pass also the `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` or setup a role in the instance in order and +the `ECS_CLUSTER` with the arn of the cluster + + diff --git a/discover.py b/discover.py new file mode 100644 index 0000000..a077fad --- /dev/null +++ b/discover.py @@ -0,0 +1,39 @@ +import os +import json + +path = os.path.dirname(os.path.realpath(__file__)) +with open(path + "/.docker_data", 'r') as content_file: + lineList = content_file.readlines() + +result = dict() + +for line in lineList: + line = line.strip() + i = line.find("=") + container = line[:i] + jsonStr = line[i+1:] + d = json.loads(jsonStr) + + if "com.byjg.easyhaproxy.definitions" in d.keys(): + definitions = d["com.byjg.easyhaproxy.definitions"].split(",") + + for definition in definitions: + port = d["com.byjg.easyhaproxy.port." + definition] if "com.byjg.easyhaproxy.port." + definition in d else "80" + if port not in result: + result[port] = dict() + result[port]["hosts"] = dict() + result[port]["redirect"] = dict() + result[port]["ssl_cert"] = None + + result[port]["hosts"][d["com.byjg.easyhaproxy.host." + definition] if "com.byjg.easyhaproxy.host." + definition in d else ""] = container + ":" + (d["com.byjg.easyhaproxy.localport." + definition] if "com.byjg.easyhaproxy.localport." + definition in d else "80") + + redirect = d["com.byjg.easyhaproxy.redirect." + definition] if "com.byjg.easyhaproxy.redirect." + definition in d else "" + for r in redirect.split(","): + r_parts = r.split("=>") + result[port]["redirect"][r_parts[0]] = r_parts[1] + + +print(result) +# print(jsonStr) + + diff --git a/discover.sh b/discover.sh new file mode 100644 index 0000000..d860b7f --- /dev/null +++ b/discover.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + + +#docker run \ +# -l com.byjg.easyhaproxy.definitions=http \ +# -l com.byjg.easyhaproxy.port.http=80 \ +# -l com.byjg.easyhaproxy.port.localport=80 \ +# -l com.byjg.easyhaproxy.host.http=byjg.com.br \ +# -l com.byjg.easyhaproxy.redirect.http=byjg.com.br%http://www.byjg.com.br,byjg.com%http://www.byjg.com.br \ +#-d byjg/php:7.3-fpm-nginx + + +touch ./.docker_data ./docker_data_old + +mv ./.docker_data ./docker_data_old + +CONTAINERS=$(docker node ps $(docker node ls -q) --format "{{ .Name }}" --filter desired-state=running | cut -d. -f1 | sort | uniq) + +for container in $CONTAINERS; do + docker inspect --format "{{ json .Spec.Labels }}" $container | xargs -I % echo $container=% >> ./.docker_data +done + + +if cmp -s ./.docker_data ./.docker_data_old ; then + echo "Nothing changed" +else + echo "Something changed" +fi + +# byjg_site={"com.byjg.easyhaproxy.definition":"http","com.byjg.easyhaproxy.host.http":"byjg.com.br","com.byjg.easyhaproxy.port.http":"80","com.byjg.easyhaproxy.redirect.http":"byjg.com.br%http://www.byjg.com.br,byjg.com%http://www.byjg.com.br","maintainer":"Joao Gilberto Magalhaes"} diff --git a/requirements.txt b/requirements.txt index 4818cc5..949d85b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -pyyaml \ No newline at end of file +pyyaml +docker \ No newline at end of file