Add support for plugin configurations and minor refactoring
- Added `plugin_configs` attribute to test cases. - Improved handling of enabled plugins in `easymapping/__init__.py`. - Updated templates directory path resolution for robustness. - Refactored example files to enhance formatting consistency.
This commit is contained in:
parent
90e3df2b75
commit
113b90a697
13 changed files with 88 additions and 20 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
@ -91,9 +92,10 @@ class HaproxyConfigGenerator:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get enabled plugins from config
|
# Get enabled plugins from config
|
||||||
enabled_list = self.mapping.get("plugins", {}).get("enabled")
|
enabled_list = self.mapping.get("plugins", {}).get("enabled", [])
|
||||||
|
# If enabled list contains only empty string, treat as no plugins enabled
|
||||||
if enabled_list and len(enabled_list) > 0 and enabled_list[0] == "":
|
if enabled_list and len(enabled_list) > 0 and enabled_list[0] == "":
|
||||||
enabled_list = None
|
enabled_list = []
|
||||||
|
|
||||||
global_results = self.plugin_manager.execute_global_plugins(global_context, enabled_list)
|
global_results = self.plugin_manager.execute_global_plugins(global_context, enabled_list)
|
||||||
self.global_plugin_configs = [r.haproxy_config for r in global_results if r.haproxy_config]
|
self.global_plugin_configs = [r.haproxy_config for r in global_results if r.haproxy_config]
|
||||||
|
|
@ -101,7 +103,8 @@ class HaproxyConfigGenerator:
|
||||||
import logging
|
import logging
|
||||||
logging.warning(f"Failed to execute global plugins: {e}")
|
logging.warning(f"Failed to execute global plugins: {e}")
|
||||||
|
|
||||||
file_loader = FileSystemLoader('templates')
|
templates_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'templates')
|
||||||
|
file_loader = FileSystemLoader(templates_dir)
|
||||||
env = Environment(loader=file_loader)
|
env = Environment(loader=file_loader)
|
||||||
env.trim_blocks = True
|
env.trim_blocks = True
|
||||||
env.lstrip_blocks = True
|
env.lstrip_blocks = True
|
||||||
|
|
@ -207,7 +210,7 @@ class HaproxyConfigGenerator:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if plugins are enabled for this domain (from labels)
|
# Check if plugins are enabled for this domain (from labels)
|
||||||
enabled_plugins = None
|
enabled_plugins = []
|
||||||
if self.label.has_label(self.label.create([definition, "plugins"])):
|
if self.label.has_label(self.label.create([definition, "plugins"])):
|
||||||
enabled_plugins = self.label.get(
|
enabled_plugins = self.label.get(
|
||||||
self.label.create([definition, "plugins"]),
|
self.label.create([definition, "plugins"]),
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ defaults
|
||||||
timeout server 10m
|
timeout server 10m
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
backend certbot_backend
|
backend certbot_backend
|
||||||
mode http
|
mode http
|
||||||
server certbot 127.0.0.1:2080
|
server certbot 127.0.0.1:2080
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ defaults
|
||||||
errorfile 503 /etc/haproxy/errors-custom/503.http
|
errorfile 503 /etc/haproxy/errors-custom/503.http
|
||||||
errorfile 504 /etc/haproxy/errors-custom/504.http
|
errorfile 504 /etc/haproxy/errors-custom/504.http
|
||||||
|
|
||||||
|
|
||||||
frontend stats
|
frontend stats
|
||||||
bind *:1936
|
bind *:1936
|
||||||
mode http
|
mode http
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ defaults
|
||||||
timeout server 10m
|
timeout server 10m
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
frontend http_in_19901
|
frontend http_in_19901
|
||||||
bind *:19901
|
bind *:19901
|
||||||
mode http
|
mode http
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ defaults
|
||||||
errorfile 503 /etc/haproxy/errors-custom/503.http
|
errorfile 503 /etc/haproxy/errors-custom/503.http
|
||||||
errorfile 504 /etc/haproxy/errors-custom/504.http
|
errorfile 504 /etc/haproxy/errors-custom/504.http
|
||||||
|
|
||||||
|
|
||||||
frontend stats
|
frontend stats
|
||||||
bind *:1937
|
bind *:1937
|
||||||
mode http
|
mode http
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ defaults
|
||||||
timeout server 10m
|
timeout server 10m
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
frontend http_in_80
|
frontend http_in_80
|
||||||
bind *:80
|
bind *:80
|
||||||
mode http
|
mode http
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ defaults
|
||||||
timeout server 10m
|
timeout server 10m
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
frontend tcp_in_31339
|
frontend tcp_in_31339
|
||||||
bind *:31339
|
bind *:31339
|
||||||
mode tcp
|
mode tcp
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ defaults
|
||||||
timeout server 10m
|
timeout server 10m
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
frontend tcp_in_31339
|
frontend tcp_in_31339
|
||||||
bind *:31339
|
bind *:31339
|
||||||
mode tcp
|
mode tcp
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ defaults
|
||||||
timeout client 10s
|
timeout client 10s
|
||||||
timeout server 10m
|
timeout server 10m
|
||||||
|
|
||||||
|
|
||||||
frontend stats
|
frontend stats
|
||||||
bind *:1936
|
bind *:1936
|
||||||
mode http
|
mode http
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ defaults
|
||||||
timeout server 10m
|
timeout server 10m
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
backend certbot_backend
|
backend certbot_backend
|
||||||
mode http
|
mode http
|
||||||
server certbot 127.0.0.1:2080
|
server certbot 127.0.0.1:2080
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ defaults
|
||||||
errorfile 503 /etc/haproxy/errors-custom/503.http
|
errorfile 503 /etc/haproxy/errors-custom/503.http
|
||||||
errorfile 504 /etc/haproxy/errors-custom/504.http
|
errorfile 504 /etc/haproxy/errors-custom/504.http
|
||||||
|
|
||||||
|
|
||||||
frontend stats
|
frontend stats
|
||||||
bind *:1936
|
bind *:1936
|
||||||
mode http
|
mode http
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,12 @@ def test_container_env_empty():
|
||||||
"server": False,
|
"server": False,
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False}
|
"manual_auth_hook": False},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
|
|
||||||
# os.environ['CERTBOT_LOG_LEVEL'] = 'warn'
|
# os.environ['CERTBOT_LOG_LEVEL'] = 'warn'
|
||||||
|
|
@ -45,7 +50,12 @@ def test_container_env_customerrors():
|
||||||
"server": False,
|
"server": False,
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False}
|
"manual_auth_hook": False},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
del os.environ['HAPROXY_CUSTOMERRORS']
|
del os.environ['HAPROXY_CUSTOMERRORS']
|
||||||
|
|
@ -70,7 +80,12 @@ def test_container_env_sslmode():
|
||||||
"server": False,
|
"server": False,
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False}
|
"manual_auth_hook": False},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
del os.environ['EASYHAPROXY_SSL_MODE']
|
del os.environ['EASYHAPROXY_SSL_MODE']
|
||||||
|
|
@ -96,7 +111,12 @@ def test_container_env_stats():
|
||||||
"server": False,
|
"server": False,
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False}
|
"manual_auth_hook": False},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
del os.environ['HAPROXY_USERNAME']
|
del os.environ['HAPROXY_USERNAME']
|
||||||
|
|
@ -128,7 +148,12 @@ def test_container_env_stats_password():
|
||||||
"server": False,
|
"server": False,
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False}
|
"manual_auth_hook": False},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
del os.environ['HAPROXY_PASSWORD']
|
del os.environ['HAPROXY_PASSWORD']
|
||||||
|
|
@ -160,7 +185,12 @@ def test_container_env_stats_password_2():
|
||||||
"server": False,
|
"server": False,
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False}
|
"manual_auth_hook": False},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
del os.environ['HAPROXY_USERNAME']
|
del os.environ['HAPROXY_USERNAME']
|
||||||
|
|
@ -189,6 +219,11 @@ def test_container_env_certbot_email():
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False
|
"manual_auth_hook": False
|
||||||
|
},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
}
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
|
|
@ -222,6 +257,11 @@ def test_container_env_certbot_full():
|
||||||
'retry_count': 10,
|
'retry_count': 10,
|
||||||
"preferred_challenges": "dns",
|
"preferred_challenges": "dns",
|
||||||
"manual_auth_hook": "something_manual_auth_hook"
|
"manual_auth_hook": "something_manual_auth_hook"
|
||||||
|
},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
}
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
|
|
@ -257,6 +297,11 @@ def test_container_log_level():
|
||||||
"retry_count": 60,
|
"retry_count": 60,
|
||||||
"preferred_challenges": "http",
|
"preferred_challenges": "http",
|
||||||
"manual_auth_hook": False
|
"manual_auth_hook": False
|
||||||
|
},
|
||||||
|
"plugins": {
|
||||||
|
"abort_on_error": False,
|
||||||
|
"config": {},
|
||||||
|
"enabled": []
|
||||||
}
|
}
|
||||||
} == ContainerEnv.read()
|
} == ContainerEnv.read()
|
||||||
finally:
|
finally:
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,8 @@ def test_parser_finds_services_raw():
|
||||||
"my-stack_agent:9001"
|
"my-stack_agent:9001"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"redirect": {
|
"redirect": {
|
||||||
|
|
@ -142,7 +143,8 @@ def test_parser_finds_services_raw():
|
||||||
"my-stack_cadvisor:8080"
|
"my-stack_cadvisor:8080"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
},
|
},
|
||||||
"node-exporter.quantum.example.org":{
|
"node-exporter.quantum.example.org":{
|
||||||
"balance": "roundrobin",
|
"balance": "roundrobin",
|
||||||
|
|
@ -150,7 +152,8 @@ def test_parser_finds_services_raw():
|
||||||
"my-stack_node-exporter:9100"
|
"my-stack_node-exporter:9100"
|
||||||
],
|
],
|
||||||
"certbot": True,
|
"certbot": True,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"redirect": {
|
"redirect": {
|
||||||
|
|
@ -168,7 +171,8 @@ def test_parser_finds_services_raw():
|
||||||
"my-stack_node-exporter:9100"
|
"my-stack_node-exporter:9100"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
},
|
},
|
||||||
"www.somehost.com.br":{
|
"www.somehost.com.br":{
|
||||||
"balance": "roundrobin",
|
"balance": "roundrobin",
|
||||||
|
|
@ -176,7 +180,8 @@ def test_parser_finds_services_raw():
|
||||||
"some-service:80"
|
"some-service:80"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"redirect": {
|
"redirect": {
|
||||||
|
|
@ -199,7 +204,8 @@ def test_parser_finds_services_raw():
|
||||||
"some-service:80"
|
"some-service:80"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"redirect": {
|
"redirect": {
|
||||||
|
|
@ -465,7 +471,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
||||||
"10.152.183.215:8080"
|
"10.152.183.215:8080"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
},
|
},
|
||||||
"valida.me":{
|
"valida.me":{
|
||||||
"balance":"roundrobin",
|
"balance":"roundrobin",
|
||||||
|
|
@ -473,7 +480,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
||||||
"10.152.183.62:8080"
|
"10.152.183.62:8080"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
},
|
},
|
||||||
"www.valida.me":{
|
"www.valida.me":{
|
||||||
"balance":"roundrobin",
|
"balance":"roundrobin",
|
||||||
|
|
@ -481,7 +489,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
||||||
"10.152.183.62:8080"
|
"10.152.183.62:8080"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"mode": "http",
|
"mode": "http",
|
||||||
|
|
@ -499,7 +508,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
||||||
"10.152.183.215:8080"
|
"10.152.183.215:8080"
|
||||||
],
|
],
|
||||||
"certbot": False,
|
"certbot": False,
|
||||||
"redirect_ssl": False
|
"redirect_ssl": False,
|
||||||
|
"plugin_configs": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"mode": "http",
|
"mode": "http",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue