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 json
|
||||
import os
|
||||
import re
|
||||
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
|
@ -91,9 +92,10 @@ class HaproxyConfigGenerator:
|
|||
)
|
||||
|
||||
# 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] == "":
|
||||
enabled_list = None
|
||||
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]
|
||||
|
|
@ -101,7 +103,8 @@ class HaproxyConfigGenerator:
|
|||
import logging
|
||||
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.trim_blocks = True
|
||||
env.lstrip_blocks = True
|
||||
|
|
@ -207,7 +210,7 @@ class HaproxyConfigGenerator:
|
|||
)
|
||||
|
||||
# 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"])):
|
||||
enabled_plugins = self.label.get(
|
||||
self.label.create([definition, "plugins"]),
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ defaults
|
|||
timeout server 10m
|
||||
|
||||
|
||||
|
||||
backend certbot_backend
|
||||
mode http
|
||||
server certbot 127.0.0.1:2080
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ defaults
|
|||
errorfile 503 /etc/haproxy/errors-custom/503.http
|
||||
errorfile 504 /etc/haproxy/errors-custom/504.http
|
||||
|
||||
|
||||
frontend stats
|
||||
bind *:1936
|
||||
mode http
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ defaults
|
|||
timeout server 10m
|
||||
|
||||
|
||||
|
||||
frontend http_in_19901
|
||||
bind *:19901
|
||||
mode http
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ defaults
|
|||
errorfile 503 /etc/haproxy/errors-custom/503.http
|
||||
errorfile 504 /etc/haproxy/errors-custom/504.http
|
||||
|
||||
|
||||
frontend stats
|
||||
bind *:1937
|
||||
mode http
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ defaults
|
|||
timeout server 10m
|
||||
|
||||
|
||||
|
||||
frontend http_in_80
|
||||
bind *:80
|
||||
mode http
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ defaults
|
|||
timeout server 10m
|
||||
|
||||
|
||||
|
||||
frontend tcp_in_31339
|
||||
bind *:31339
|
||||
mode tcp
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ defaults
|
|||
timeout server 10m
|
||||
|
||||
|
||||
|
||||
frontend tcp_in_31339
|
||||
bind *:31339
|
||||
mode tcp
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ defaults
|
|||
timeout client 10s
|
||||
timeout server 10m
|
||||
|
||||
|
||||
frontend stats
|
||||
bind *:1936
|
||||
mode http
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ defaults
|
|||
timeout server 10m
|
||||
|
||||
|
||||
|
||||
backend certbot_backend
|
||||
mode http
|
||||
server certbot 127.0.0.1:2080
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ defaults
|
|||
errorfile 503 /etc/haproxy/errors-custom/503.http
|
||||
errorfile 504 /etc/haproxy/errors-custom/504.http
|
||||
|
||||
|
||||
frontend stats
|
||||
bind *:1936
|
||||
mode http
|
||||
|
|
|
|||
|
|
@ -20,7 +20,12 @@ def test_container_env_empty():
|
|||
"server": False,
|
||||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False}
|
||||
"manual_auth_hook": False},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
|
||||
# os.environ['CERTBOT_LOG_LEVEL'] = 'warn'
|
||||
|
|
@ -45,7 +50,12 @@ def test_container_env_customerrors():
|
|||
"server": False,
|
||||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False}
|
||||
"manual_auth_hook": False},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
del os.environ['HAPROXY_CUSTOMERRORS']
|
||||
|
|
@ -70,7 +80,12 @@ def test_container_env_sslmode():
|
|||
"server": False,
|
||||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False}
|
||||
"manual_auth_hook": False},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
del os.environ['EASYHAPROXY_SSL_MODE']
|
||||
|
|
@ -96,7 +111,12 @@ def test_container_env_stats():
|
|||
"server": False,
|
||||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False}
|
||||
"manual_auth_hook": False},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
del os.environ['HAPROXY_USERNAME']
|
||||
|
|
@ -128,7 +148,12 @@ def test_container_env_stats_password():
|
|||
"server": False,
|
||||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False}
|
||||
"manual_auth_hook": False},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
del os.environ['HAPROXY_PASSWORD']
|
||||
|
|
@ -160,7 +185,12 @@ def test_container_env_stats_password_2():
|
|||
"server": False,
|
||||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False}
|
||||
"manual_auth_hook": False},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
del os.environ['HAPROXY_USERNAME']
|
||||
|
|
@ -189,6 +219,11 @@ def test_container_env_certbot_email():
|
|||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False
|
||||
},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
|
|
@ -222,6 +257,11 @@ def test_container_env_certbot_full():
|
|||
'retry_count': 10,
|
||||
"preferred_challenges": "dns",
|
||||
"manual_auth_hook": "something_manual_auth_hook"
|
||||
},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
|
|
@ -257,6 +297,11 @@ def test_container_log_level():
|
|||
"retry_count": 60,
|
||||
"preferred_challenges": "http",
|
||||
"manual_auth_hook": False
|
||||
},
|
||||
"plugins": {
|
||||
"abort_on_error": False,
|
||||
"config": {},
|
||||
"enabled": []
|
||||
}
|
||||
} == ContainerEnv.read()
|
||||
finally:
|
||||
|
|
|
|||
|
|
@ -124,7 +124,8 @@ def test_parser_finds_services_raw():
|
|||
"my-stack_agent:9001"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
}
|
||||
},
|
||||
"redirect": {
|
||||
|
|
@ -142,7 +143,8 @@ def test_parser_finds_services_raw():
|
|||
"my-stack_cadvisor:8080"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
},
|
||||
"node-exporter.quantum.example.org":{
|
||||
"balance": "roundrobin",
|
||||
|
|
@ -150,7 +152,8 @@ def test_parser_finds_services_raw():
|
|||
"my-stack_node-exporter:9100"
|
||||
],
|
||||
"certbot": True,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
}
|
||||
},
|
||||
"redirect": {
|
||||
|
|
@ -168,7 +171,8 @@ def test_parser_finds_services_raw():
|
|||
"my-stack_node-exporter:9100"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
},
|
||||
"www.somehost.com.br":{
|
||||
"balance": "roundrobin",
|
||||
|
|
@ -176,7 +180,8 @@ def test_parser_finds_services_raw():
|
|||
"some-service:80"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
}
|
||||
},
|
||||
"redirect": {
|
||||
|
|
@ -199,7 +204,8 @@ def test_parser_finds_services_raw():
|
|||
"some-service:80"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
}
|
||||
},
|
||||
"redirect": {
|
||||
|
|
@ -465,7 +471,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
|||
"10.152.183.215:8080"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
},
|
||||
"valida.me":{
|
||||
"balance":"roundrobin",
|
||||
|
|
@ -473,7 +480,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
|||
"10.152.183.62:8080"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
},
|
||||
"www.valida.me":{
|
||||
"balance":"roundrobin",
|
||||
|
|
@ -481,7 +489,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
|||
"10.152.183.62:8080"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
}
|
||||
},
|
||||
"mode": "http",
|
||||
|
|
@ -499,7 +508,8 @@ def test_parser_finds_services_clone_to_ssl_raw():
|
|||
"10.152.183.215:8080"
|
||||
],
|
||||
"certbot": False,
|
||||
"redirect_ssl": False
|
||||
"redirect_ssl": False,
|
||||
"plugin_configs": []
|
||||
}
|
||||
},
|
||||
"mode": "http",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue