mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2025-12-24 07:11:32 +00:00
restructure configuration directories
This commit is contained in:
@@ -30,25 +30,27 @@ class BootstrapBase:
|
||||
self.mysql_conn = None
|
||||
self.redis_conn = None
|
||||
|
||||
def render_config(self, config_file):
|
||||
def render_config(self, config_dir):
|
||||
"""
|
||||
Renders multiple Jinja2 templates based on a JSON config file.
|
||||
Renders multiple Jinja2 templates from a config.json file in a given directory.
|
||||
|
||||
Each config entry must include:
|
||||
- template (str): the template filename
|
||||
- output (str): absolute path to the output file
|
||||
Args:
|
||||
config_dir (str or Path): Path to the directory containing config.json
|
||||
|
||||
Optional:
|
||||
- clean_blank_lines (bool): remove empty lines from output
|
||||
- if_not_exists (bool): skip rendering if output file already exists
|
||||
Behavior:
|
||||
- Renders each template defined in config.json
|
||||
- Writes the result to the specified output path
|
||||
- Also copies the rendered file to: <config_dir>/rendered_configs/<relative_output_path>
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
config_dir = Path(config_dir)
|
||||
config_path = config_dir / "config.json"
|
||||
|
||||
config_path = Path(config_file)
|
||||
if not config_path.exists():
|
||||
print(f"Template config file not found: {config_path}")
|
||||
print(f"config.json not found in: {config_dir}")
|
||||
return
|
||||
|
||||
with config_path.open("r") as f:
|
||||
@@ -76,7 +78,11 @@ class BootstrapBase:
|
||||
with output_path.open("w") as f:
|
||||
f.write(rendered)
|
||||
|
||||
print(f"Rendered {template_name} to {output_path}")
|
||||
rendered_copy_path = config_dir / "rendered_configs" / output_path.name
|
||||
rendered_copy_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self.copy_file(output_path, rendered_copy_path)
|
||||
|
||||
print(f"Rendered {template_name} → {output_path}")
|
||||
|
||||
def prepare_template_vars(self, overwrite_path, extra_vars = None):
|
||||
"""
|
||||
|
||||
@@ -32,8 +32,8 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/etc/clamav/custom_templates',
|
||||
'/etc/clamav/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -47,7 +47,7 @@ class Bootstrap(BootstrapBase):
|
||||
self.set_timezone()
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/etc/clamav/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
# Fix permissions
|
||||
self.set_owner("/var/lib/clamav", "clamav", "clamav", recursive=True)
|
||||
|
||||
@@ -31,8 +31,8 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/etc/dovecot/custom_templates',
|
||||
'/etc/dovecot/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -55,7 +55,7 @@ class Bootstrap(BootstrapBase):
|
||||
self.set_timezone()
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/etc/dovecot/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
files = [
|
||||
"/etc/dovecot/mail_plugins",
|
||||
|
||||
@@ -31,8 +31,8 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/etc/mysql/conf.d/custom_templates',
|
||||
'/etc/mysql/conf.d/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -46,7 +46,7 @@ class Bootstrap(BootstrapBase):
|
||||
self.set_timezone()
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/etc/mysql/conf.d/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
def start_temporary(self, socket):
|
||||
"""
|
||||
|
||||
@@ -23,8 +23,8 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/etc/nginx/conf.d/custom_templates',
|
||||
'/etc/nginx/conf.d/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -41,7 +41,7 @@ class Bootstrap(BootstrapBase):
|
||||
self.set_timezone()
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/etc/nginx/conf.d/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
def get_valid_cert_dirs(self):
|
||||
ssl_dir = '/etc/ssl/mail/'
|
||||
|
||||
@@ -16,8 +16,8 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/php-conf/custom_templates',
|
||||
'/php-conf/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -44,7 +44,7 @@ class Bootstrap(BootstrapBase):
|
||||
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/php-conf/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
self.copy_file("/usr/local/etc/php/conf.d/opcache-recommended.ini", "/php-conf/opcache-recommended.ini")
|
||||
self.copy_file("/usr/local/etc/php-fpm.d/z-pools.conf", "/php-conf/pools.conf")
|
||||
|
||||
@@ -18,15 +18,15 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/opt/postfix/conf/custom_templates',
|
||||
'/opt/postfix/conf/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
trim_blocks=True
|
||||
)
|
||||
with open("/opt/postfix/conf/extra.cf", "r") as f:
|
||||
extra_config = f.read()
|
||||
extra_config_path = Path("/opt/postfix/conf/extra.cf")
|
||||
extra_config = extra_config_path.read_text() if extra_config_path.exists() else ""
|
||||
extra_vars = {
|
||||
"VALID_CERT_DIRS": self.get_valid_cert_dirs(),
|
||||
"EXTRA_CF": extra_config
|
||||
@@ -40,7 +40,7 @@ class Bootstrap(BootstrapBase):
|
||||
self.set_syslog_redis()
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/opt/postfix/conf/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
# Create SNI Config
|
||||
self.run_command(["postmap", "-F", "hash:/opt/postfix/conf/sni.map"])
|
||||
|
||||
@@ -61,8 +61,8 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/etc/rspamd/custom_templates',
|
||||
'/etc/rspamd/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -80,7 +80,7 @@ class Bootstrap(BootstrapBase):
|
||||
self.set_timezone()
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/etc/rspamd/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
# Fix missing default global maps, if any
|
||||
# These exists in mailcow UI and should not be removed
|
||||
|
||||
@@ -28,8 +28,8 @@ class Bootstrap(BootstrapBase):
|
||||
# Setup Jinja2 Environment and load vars
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader([
|
||||
'/etc/sogo/custom_templates',
|
||||
'/etc/sogo/config_templates'
|
||||
'/service_config/custom_templates',
|
||||
'/service_config/config_templates'
|
||||
]),
|
||||
keep_trailing_newline=True,
|
||||
lstrip_blocks=True,
|
||||
@@ -48,7 +48,7 @@ class Bootstrap(BootstrapBase):
|
||||
self.set_syslog_redis()
|
||||
|
||||
print("Render config")
|
||||
self.render_config("/etc/sogo/config.json")
|
||||
self.render_config("/service_config")
|
||||
|
||||
print("Fix permissions")
|
||||
self.set_owner("/var/lib/sogo", "sogo", "sogo", recursive=True)
|
||||
|
||||
Reference in New Issue
Block a user