1
0
Fork 0

Merge pull request #16 from byjg/refactory-labels

Organizing code to refactory labels
This commit is contained in:
Joao M 2022-08-14 10:25:51 -05:00 committed by GitHub
commit 4ede25e70b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 227 additions and 117 deletions

19
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
]
}

View file

@ -1,4 +1,4 @@
FROM alpine:3.14
FROM alpine:3.16
WORKDIR /scripts

View file

@ -28,7 +28,7 @@ The basic command line to run is:
docker run -d \
--name easy-haproxy-container \
-v /var/run/docker.sock:/var/run/docker.sock \
-e DISCOVER="swarm|docker|static" \
-e EASYHAPROXY_DISCOVER="swarm|docker|static" \
# + Environment Variables \
# + ports mapped to the host \
byjg/easy-haproxy
@ -39,15 +39,15 @@ The mapping to `/var/run/docker.sock` is necessary to discover the docker contai
The environment variables will setup the HAProxy.
| Environment Variable | Description |
|----------------------|-------------------------------------------------------------------------------|
| DISCOVER | How `haproxy.cfg` will be created: `static`, `docker` or `swarm` |
| LOOKUP_LABEL | (Optional) The key will search to match resources. Default: `easyhaproxy`. |
|--------------------------|-------------------------------------------------------------------------------|
| EASYHAPROXY_DISCOVER | How `haproxy.cfg` will be created: `static`, `docker` or `swarm` |
| EASYHAPROXY_LABEL_PREFIX | (Optional) The key will search to match resources. Default: `easyhaproxy`. |
| HAPROXY_USERNAME | (Optional) The HAProxy username to the statistics. Default: `admin` |
| HAPROXY_PASSWORD | The HAProxy password to the statistics. If not set disable stats. |
| HAPROXY_STATS_PORT | (Optional) The HAProxy port to the statistics. Default: `1936` |
| HAPROXY_CUSTOMERRORS | (Optional) If HAProxy will use custom HTML errors. true/false. Default: false |
| HAPROXY_CUSTOMERRORS. | (Optional) If HAProxy will use custom HTML errors. true/false. Default: false |
The environment variable `DISCOVER` will define where is located your containers (see below more details):
The environment variable `EASYHAPROXY_DISCOVER` will define where is located your containers (see below more details):
- docker
- swarm
@ -57,7 +57,7 @@ The environment variable `DISCOVER` will define where is located your containers
Easy HAProxy can discover automatically the container services running in the same network of Docker or in a Docker Swarm cluster.
### DISCOVER: docker
### EASYHAPROXY_DISCOVER: docker
This method will use a regular docker installation to discover the containers and configure the HAProxy.
@ -75,7 +75,7 @@ docker run --network easyhaproxy byjg/easyhaproxy
docker run --network easyhaproxy myimage
```
### DISCOVER: swarm
### EASYHAPROXY_DISCOVER: swarm
This method requires a functional Docker Swarm Cluster. The system will search for the labels in all containers on all
swarm nodes.
@ -87,15 +87,14 @@ Important: easyhaproxy needs to be in the same network of the containers or othe
### Tags to be attached in the Docker Container (Swarm or Docker)
| Tag | Description | Example |
|------------------------------------|---------------------------------------------------------------------------------------------------------|--------------|
| easyhaproxy.definitions | A Comma delimited list with the definitions. Each name requires the definition of the parameters below. | service,service2 |
| easyhaproxy.mode.[definition] | (Optional) Is this `http` or `tcp` mode in HAProxy. (Defaults to http) | http |
| easyhaproxy.port.[definition] | (Optional) What is the port that the HAProxy will listen to. (Defaults to 80) | 80 |
| easyhaproxy.localport.[definition] | (Optional) What is the port that the container is listening. (Defaults to 80) | 8080 |
| easyhaproxy.host.[definition] | What is the host that the HAProxy will listen to. | somehost.com |
| easyhaproxy.redirect.[definition] | (Optional) Host redirects from connections in the port defined above. | foo.com--https://bla.com,bar.com--https://bar.org |
| easyhaproxy.sslcert.[definition] | (Optional) Cert PEM Base64 encoded. | |
| easyhaproxy.health-check.[definition] | (Optional) `ssl`, enable health check via SSL in `mode tcp` (Defaults to "empty") | |
|---------------------------------------|---------------------------------------------------------------------------------------------------------|--------------|
| easyhaproxy.[definition].mode | (Optional) Is this `http` or `tcp` mode in HAProxy. (Defaults to http) | http |
| easyhaproxy.[definition].port | (Optional) What is the port that the HAProxy will listen to. (Defaults to 80) | 80 |
| easyhaproxy.[definition].localport. | (Optional) What is the port that the container is listening. (Defaults to 80) | 8080 |
| easyhaproxy.[definition].host | What is the host that the HAProxy will listen to. | somehost.com |
| easyhaproxy.[definition].redirect | (Optional) Host redirects from connections in the port defined above. | foo.com--https://bla.com,bar.com--https://bar.org |
| easyhaproxy.[definition].sslcert | (Optional) Cert PEM Base64 encoded. | |
| easyhaproxy.[definition].health-check | (Optional) `ssl`, enable health check via SSL in `mode tcp` (Defaults to "empty") | |
### Defining the labels in Docker Swarm
@ -106,7 +105,8 @@ services:
foo:
deploy:
labels:
easyhaproxy.definitions: "service1,service2"
easyhaproxy.my.host: "www.example.org"
easyhaproxy.my.localport: 8080
...
```
@ -114,9 +114,8 @@ services:
```bash
docker run \
-l easyhaproxy.definitions=webapi \
-l easyhaproxy.port.webapi=80\
-l easyhaproxy.host.webapi=byjg.com.br \
-l easyhaproxy.webapi.port=80\
-l easyhaproxy.webapi.host=byjg.com.br \
....
```
@ -124,15 +123,13 @@ docker run \
```bash
docker run \
-l easyhaproxy.definitions=express,admin \
-l easyhaproxy.express.port=80 \
-l easyhaproxy.express.localport=3000 \
-l easyhaproxy.express.host=express.byjg.com.br \
-l easyhaproxy.port.express=80 \
-l easyhaproxy.localport.express=3000 \
-l easyhaproxy.host.express=express.byjg.com.br \
-l easyhaproxy.port.admin=80 \
-l easyhaproxy.localport.admin=3001 \
-l easyhaproxy.host.admin=admin.byjg.com.br \
-l easyhaproxy.admin.port=80 \
-l easyhaproxy.admin.localport=3001 \
-l easyhaproxy.admin.host=admin.byjg.com.br \
.... \
some/myimage
```
@ -143,10 +140,9 @@ Used to pass on SSL-termination to a backend. Alternatively, you can enable heal
```bash
docker run \
-l easyhaproxy.defintions=example \
-l easyhaproxy.mode.example=tcp \
-l easyhaproxy.health-check.example=ssl \
-l easyhaproxy.port.example=443
-l easyhaproxy.example.mode=tcp \
-l easyhaproxy.example.health-check=ssl \
-l easyhaproxy.example.port=443
.... \
some/tcp-service
```
@ -155,10 +151,10 @@ docker run \
```bash
docker run \
-l easyhaproxy.redirect.<defintion>=www.byjg.com.br--http://byjg.com.br,byjg.com--http://byjg.com.br
-l easyhaproxy.[definition].redirect=www.byjg.com.br--http://byjg.com.br,byjg.com--http://byjg.com.br
```
## DISCOVER: static
## EASYHAPROXY_DISCOVER: static
This method expects a YAML file to setup the `haproxy.cfg`
@ -235,7 +231,7 @@ MIIEojCCA4qgAwIBAgIUegW2BimwuL4RzRZ2WYkHA6U5nkAwDQYJKoZIhvcNAQEL
cat single.pem | base64 -w0
```
3. Use this string to define the label `easyhaproxy.sslcert.[definition]`
3. Use this string to define the label `easyhaproxy.[definition].sslcert`
## Setting Custom Errors

View file

@ -4,7 +4,7 @@ cd /scripts
RELOAD="true"
if [[ "$DISCOVER" == "static" ]]; then
if [[ "$EASYHAPROXY_DISCOVER" == "static" ]]; then
CONTROL_FILE="/etc/haproxy/haproxy.cfg"
touch ${CONTROL_FILE}
cp ${CONTROL_FILE} ${CONTROL_FILE}.old
@ -15,7 +15,7 @@ else
mv ${CONTROL_FILE} ${CONTROL_FILE}.old
touch ${CONTROL_FILE}
if [[ "$DISCOVER" == "docker" ]]; then
if [[ "$EASYHAPROXY_DISCOVER" == "docker" ]]; then
CONTAINERS=$(docker ps -q)
LABEL_PATH=".Config.Labels"

View file

@ -16,7 +16,7 @@ if os.getenv("HAPROXY_PASSWORD"):
"port": os.getenv("HAPROXY_STATS_PORT") if os.getenv("HAPROXY_STATS_PORT") else "1936",
}
result["lookup_label"] = os.getenv("LOOKUP_LABEL") if os.getenv("LOOKUP_LABEL") else "easyhaproxy"
result["lookup_label"] = os.getenv("EASYHAPROXY_LABEL_PREFIX") if os.getenv("EASYHAPROXY_LABEL_PREFIX") else "easyhaproxy"
cfg = HaproxyConfigGenerator(result)
print(cfg.generate(lineList))

View file

@ -3,11 +3,14 @@ import hashlib
from jinja2 import Environment, FileSystemLoader
import json
import os
import re
class DockerLabelHandler:
def __init__(self, label):
self.__label_base = label
def get_lookup_label(self):
return self.__label_base
def create(self, key):
if isinstance(key, str):
@ -41,10 +44,10 @@ class HaproxyConfigGenerator:
os.makedirs(self.ssl_cert_folder, exist_ok=True)
def generate(self, lineList = []):
def generate(self, line_list = []):
# static?
if len(lineList) > 0:
self.mapping["easymapping"] = self.__parse(lineList)
if len(line_list) > 0:
self.mapping["easymapping"] = self.parse(line_list)
else:
for d in self.mapping["easymapping"]:
for name, hosts in d.get('hosts', {}).items():
@ -63,42 +66,49 @@ class HaproxyConfigGenerator:
return template.render(data=self.mapping)
def __parse(self, lineList):
def parse(self, line_list):
easymapping = dict()
for line in lineList:
for line in line_list:
line = line.strip()
i = line.find("=")
container = line[:i]
jsonStr = line[i+1:]
d = json.loads(jsonStr)
json_str = line[i+1:]
d = json.loads(json_str)
if self.label.create("definitions") not in d.keys():
# Extract the definitions dynamically
definitions = {}
r = re.compile(self.label.get_lookup_label() + r"\.(.*)\..*")
for key in d.keys():
if r.match(key):
definitions[r.search(key).group(1)] = 1
if len(definitions.keys()) == 0:
continue
self.label.set_data(d)
definitions = d[self.label.create("definitions")].split(",")
for definition in definitions:
# Parse each definition found.
for definition in definitions.keys():
mode = self.label.get(
self.label.create(["mode", definition]),
self.label.create([definition, "mode"]),
"http"
)
# TODO: we can ignore "host" in TCP, but it would break the template
host_label = self.label.create(["host", definition])
host_label = self.label.create([definition, "host"])
if not self.label.has_label(host_label):
continue
port = self.label.get(
self.label.create(["port", definition]),
self.label.create([definition, "port"]),
"80"
)
hash = ""
if self.label.create(["sslcert", definition]) in d:
if self.label.create([definition, "sslcert"]) in d:
hash = hashlib.md5(
d[self.label.create(["sslcert", definition])].encode('utf-8')
d[self.label.create([definition, "sslcert"])].encode('utf-8')
).hexdigest()
key = port if not hash else port + "_" + hash
@ -114,12 +124,12 @@ class HaproxyConfigGenerator:
# TODO: this could use `EXPOSE` from `Dockerfile`?
ct_port = self.label.get(
self.label.create(["localport", definition]),
self.label.create([definition, "localport"]),
"80"
)
easymapping[key]["health-check"] = self.label.get(
self.label.create(["health-check", definition]),
self.label.create([definition, "health-check"]),
""
)
@ -127,7 +137,7 @@ class HaproxyConfigGenerator:
easymapping[key]["hosts"][d[host_label]] += ["{}:{}".format(container, ct_port)]
# handle SSL
ssl_label = self.label.create(["sslcert", definition])
ssl_label = self.label.create([definition, "sslcert"])
if self.label.has_label(ssl_label):
self.ssl_cert_increment += 1
filename = "{}/{}.{}.pem".format(
@ -141,7 +151,7 @@ class HaproxyConfigGenerator:
# handle redirects
redirect = self.label.get(
self.label.create(["redirect", definition])
self.label.create([definition, "redirect"])
)
if len(redirect) > 0:
for r in redirect.split(","):

File diff suppressed because one or more lines are too long

View file

@ -15,7 +15,7 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
DISCOVER: docker
EASYHAPROXY_DISCOVER: docker
HAPROXY_CUSTOMERRORS: "true"
HAPROXY_USERNAME: admin
HAPROXY_PASSWORD: password
@ -30,10 +30,8 @@ services:
deploy:
replicas: 2
labels:
easyhaproxy.definitions: "http"
easyhaproxy.redirect.http: google.helloworld.com--www.google.com
easyhaproxy.host.http: www.helloworld.com
easyhaproxy.port.http: 19901
easyhaproxy.localport.http: 80
easyhaproxy.http.redirect: google.helloworld.com--www.google.com
easyhaproxy.http.host: www.helloworld.com
easyhaproxy.http.port: 19901
easyhaproxy.http.localport: 80

File diff suppressed because one or more lines are too long

View file

@ -1,3 +1,6 @@
# To test:
# curl -k -H "Host: host1.local" https://127.0.0.1/
version: "3"
services:
@ -8,7 +11,7 @@ services:
- ./host1.local.pem:/etc/certs/host1.local.pem
- /var/run/docker.sock:/var/run/docker.sock
environment:
DISCOVER: static
EASYHAPROXY_DISCOVER: static
ports:
- "80:80/tcp"
- "443:443/tcp"

View file

@ -1,6 +1,6 @@
portainer-agent_agent={"com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"portainer-agent"}
my-stack_agent={"easyhaproxy.definitions":"agent","easyhaproxy.host.agent":"agent.quantum.example.org","easyhaproxy.localport.agent":"9001","easyhaproxy.mode.agent":"tcp","easyhaproxy.port.agent":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_cadvisor={"easyhaproxy.definitions":"cadvisor","easyhaproxy.host.cadvisor":"cadvisor.quantum.example.org","easyhaproxy.localport.cadvisor":"8080","easyhaproxy.port.cadvisor":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_node-exporter={"easyhaproxy.definitions":"exp","easyhaproxy.host.exp":"node-exporter.quantum.example.org","easyhaproxy.localport.exp":"9100","easyhaproxy.port.exp":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_agent={"easyhaproxy.agent.host":"agent.quantum.example.org","easyhaproxy.agent.localport":"9001","easyhaproxy.agent.mode":"tcp","easyhaproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_cadvisor={"easyhaproxy.cadvisor.host":"cadvisor.quantum.example.org","easyhaproxy.cadvisor.localport":"8080","easyhaproxy.cadvisor.port":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_node-exporter={"easyhaproxy.exp.host":"node-exporter.quantum.example.org","easyhaproxy.exp.localport":"9100","easyhaproxy.exp.port":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_reverse-proxy={"com.docker.stack.image":"quay.io/pngmbh/easy-haproxy:tcp-mode","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
some-service={"easyhaproxy.definitions":"http,https","easyhaproxy.port.http":"80","easyhaproxy.host.http":"www.somehost.com.br","easyhaproxy.localport.http":"80","easyhaproxy.redirect.http":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.port.https":"443","easyhaproxy.host.https":"www.somehost.com.br","easyhaproxy.localport.https":"80","easyhaproxy.redirect.https":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.sslcert.https":"U29tZSBQRU0gQ2VydGlmaWNhdGU="}
some-service={"easyhaproxy.http.port":"80","easyhaproxy.http.host":"www.somehost.com.br","easyhaproxy.http.localport":"80","easyhaproxy.http.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.https.port":"443","easyhaproxy.https.host":"www.somehost.com.br","easyhaproxy.https.localport":"80","easyhaproxy.https.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","easyhaproxy.https.sslcert":"U29tZSBQRU0gQ2VydGlmaWNhdGU="}

View file

@ -1,6 +1,6 @@
portainer-agent_agent={"com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"portainer-agent"}
my-stack_agent={"haproxy.definitions":"agent","haproxy.host.agent":"agent.quantum.example.org","haproxy.localport.agent":"9001","haproxy.mode.agent":"tcp","haproxy.port.agent":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_cadvisor={"haproxy.definitions":"cadvisor","haproxy.host.cadvisor":"cadvisor.quantum.example.org","haproxy.localport.cadvisor":"8080","haproxy.port.cadvisor":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_node-exporter={"haproxy.definitions":"exp","haproxy.host.exp":"node-exporter.quantum.example.org","haproxy.localport.exp":"9100","haproxy.port.exp":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_agent={"haproxy.agent.host":"agent.quantum.example.org","haproxy.agent.localport":"9001","haproxy.agent.mode":"tcp","haproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_cadvisor={"haproxy.cadvisor.host":"cadvisor.quantum.example.org","haproxy.cadvisor.localport":"8080","haproxy.cadvisor.port":"31337","com.docker.stack.image":"gcr.io/google-containers/cadvisor:v0.34.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_node-exporter={"haproxy.exp.host":"node-exporter.quantum.example.org","haproxy.exp.localport":"9100","haproxy.exp.port":"31337","com.docker.stack.image":"stefanprodan/swarmprom-node-exporter:v0.16.0","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
my-stack_reverse-proxy={"com.docker.stack.image":"quay.io/pngmbh/easy-haproxy:tcp-mode","com.docker.stack.namespace":"my-stack","com.planetary-quantum":"monitoring"}
some-service={"haproxy.definitions":"http,https","haproxy.port.http":"80","haproxy.host.http":"www.somehost.com.br","haproxy.localport.http":"80","haproxy.redirect.http":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.port.https":"443","haproxy.host.https":"www.somehost.com.br","haproxy.localport.https":"80","haproxy.redirect.https":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.sslcert.https":"U29tZSBQRU0gQ2VydGlmaWNhdGU="}
some-service={"haproxy.http.port":"80","haproxy.http.host":"www.somehost.com.br","haproxy.http.localport":"80","haproxy.http.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.https.port":"443","haproxy.https.host":"www.somehost.com.br","haproxy.https.localport":"80","haproxy.https.redirect":"somehost.com.br--https://www.somehost.com.br,somehost.com--https://www.somehost.com.br,www.somehost.com--https://www.somehost.com.br,byjg.ca--https://www.somehost.com.br,www.byjg.ca--https://www.somehost.com.br","haproxy.https.sslcert":"U29tZSBQRU0gQ2VydGlmaWNhdGU="}

View file

@ -1,2 +1,2 @@
test_nginx.2.t5r94mjlced7m3t5orfjbowmm={"easyhaproxy.definitions":"http","easyhaproxy.host.http":"www.helloworld.com","easyhaproxy.localport.http":"80","easyhaproxy.port.http":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"}
test_nginx.1.p552hqxkdx88narjrp5kouwb2={"easyhaproxy.definitions":"http","easyhaproxy.host.http":"www.helloworld.com","easyhaproxy.localport.http":"80","easyhaproxy.port.http":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"}
test_nginx.2.t5r94mjlced7m3t5orfjbowmm={"easyhaproxy.http.host":"www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"}
test_nginx.1.p552hqxkdx88narjrp5kouwb2={"easyhaproxy.http.host":"www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","com.docker.stack.image":"stenote/nginx-hostname","com.docker.stack.namespace":"test"}

View file

@ -1,2 +1,2 @@
test_agent={"easyhaproxy.definitions":"agent","easyhaproxy.host.agent":"agent.quantum.local","easyhaproxy.localport.agent":"9001","easyhaproxy.mode.agent":"tcp","easyhaproxy.port.agent":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"test", "easyhaproxy.health-check.agent":"ssl"}
test_agent={"easyhaproxy.agent.host":"agent.quantum.local","easyhaproxy.agent.localport":"9001","easyhaproxy.agent.mode":"tcp","easyhaproxy.agent.port":"31339","com.docker.stack.image":"portainer/agent:1.5.1","com.docker.stack.namespace":"test", "easyhaproxy.agent.health-check":"ssl"}
test_proxy={"com.docker.stack.image":"byjg/easy-haproxy:local","com.docker.stack.namespace":"test"}

View file

@ -1,4 +1,4 @@
from .context import easymapping
import easymapping
import pytest
import os
import yaml
@ -7,20 +7,20 @@ import yaml
def load_fixture(file):
path = os.path.dirname(os.path.realpath(__file__))
with open(path + "/fixtures/" + file, 'r') as content_file:
lineList = content_file.readlines()
line_list = content_file.readlines()
return lineList
return line_list
def test_parser_doesnt_crash():
lineList = load_fixture("no-services")
line_list = load_fixture("no-services")
result = {
"customerrors": False
}
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
haproxy_config = cfg.generate(lineList)
haproxy_config = cfg.generate(line_list)
assert len(haproxy_config) > 0
path = os.path.dirname(os.path.realpath(__file__))
@ -29,7 +29,7 @@ def test_parser_doesnt_crash():
def test_parser_finds_services():
lineList = load_fixture("services")
line_list = load_fixture("services")
result = {
"customerrors": False
@ -40,7 +40,7 @@ def test_parser_finds_services():
os.remove(cert_file)
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
haproxy_config = cfg.generate(lineList)
haproxy_config = cfg.generate(line_list)
assert len(haproxy_config) > 0
path = os.path.dirname(os.path.realpath(__file__))
@ -51,7 +51,7 @@ def test_parser_finds_services():
assert expected_file.read() == "Some PEM Certificate"
def test_parser_finds_services_changed_label():
lineList = load_fixture("services-changed-label")
line_list = load_fixture("services-changed-label")
result = {
"customerrors": False,
@ -63,7 +63,7 @@ def test_parser_finds_services_changed_label():
os.remove(cert_file)
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
haproxy_config = cfg.generate(lineList)
haproxy_config = cfg.generate(line_list)
assert len(haproxy_config) > 0
path = os.path.dirname(os.path.realpath(__file__))
@ -73,6 +73,91 @@ def test_parser_finds_services_changed_label():
with open(cert_file, 'r') as expected_file:
assert expected_file.read() == "Some PEM Certificate"
def test_parser_finds_services_raw():
line_list = load_fixture("services")
result = {
"customerrors": False
}
cert_file = "/tmp/www.somehost.com.br.1.pem"
if os.path.exists(cert_file):
os.remove(cert_file)
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
parsed_object = [
{
"mode":"tcp",
"health-check":"",
"port":"31339",
"hosts":{
"agent.quantum.example.org":[
"my-stack_agent:9001"
]
},
"redirect":{
}
},
{
"mode":"http",
"health-check":"",
"port":"31337",
"hosts":{
"cadvisor.quantum.example.org":[
"my-stack_cadvisor:8080"
],
"node-exporter.quantum.example.org":[
"my-stack_node-exporter:9100"
]
},
"redirect":{
}
},
{
"mode":"http",
"health-check":"",
"port":"80",
"hosts":{
"www.somehost.com.br":[
"some-service:80"
]
},
"redirect":{
"somehost.com.br":"https://www.somehost.com.br",
"somehost.com":"https://www.somehost.com.br",
"www.somehost.com":"https://www.somehost.com.br",
"byjg.ca":"https://www.somehost.com.br",
"www.byjg.ca":"https://www.somehost.com.br"
}
},
{
"mode":"http",
"health-check":"",
"port":"443",
"hosts":{
"www.somehost.com.br":[
"some-service:80"
]
},
"redirect":{
"somehost.com.br":"https://www.somehost.com.br",
"somehost.com":"https://www.somehost.com.br",
"www.somehost.com":"https://www.somehost.com.br",
"byjg.ca":"https://www.somehost.com.br",
"www.byjg.ca":"https://www.somehost.com.br"
},
"ssl_cert":"/tmp/www.somehost.com.br.1.pem"
}
]
processed = list(cfg.parse(line_list))
assert parsed_object == processed
def test_parser_static():
path = os.path.dirname(os.path.realpath(__file__))
@ -88,14 +173,14 @@ def test_parser_static():
def test_parser_tcp():
lineList = load_fixture("services-tcp")
line_list = load_fixture("services-tcp")
result = {
"customerrors": False
}
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
haproxy_config = cfg.generate(lineList)
haproxy_config = cfg.generate(line_list)
# print(haproxy_config)
assert len(haproxy_config) > 0
@ -104,17 +189,20 @@ def test_parser_tcp():
assert expected_file.read() == haproxy_config
def test_parser_multi_containers():
lineList = load_fixture("services-multi-containers")
line_list = load_fixture("services-multi-containers")
result = {
"customerrors": False
}
cfg = easymapping.HaproxyConfigGenerator(result, "/tmp")
haproxy_config = cfg.generate(lineList)
haproxy_config = cfg.generate(line_list)
assert len(haproxy_config) > 0
path = os.path.dirname(os.path.realpath(__file__))
with open(path + "/expected/services-multi-containers.txt", 'r') as expected_file:
assert expected_file.read() == haproxy_config
#test_parser_finds_services_raw()
#test_parser_tcp()