Split domains.yml into domains/{domain}.yml

This commit is contained in:
Paco 2021-05-29 19:15:13 +02:00
parent ced4da4171
commit 00098075fd

View file

@ -53,7 +53,7 @@ from yunohost.hook import hook_callback
logger = getActionLogger("yunohost.domain") logger = getActionLogger("yunohost.domain")
DOMAIN_SETTINGS_PATH = "/etc/yunohost/domains.yml" DOMAIN_SETTINGS_DIR = "/etc/yunohost/domains"
REGISTRAR_LIST_PATH = "/usr/share/yunohost/other/providers_list.yml" REGISTRAR_LIST_PATH = "/usr/share/yunohost/other/providers_list.yml"
@ -732,39 +732,38 @@ def _load_domain_settings():
Retrieve entries in domains.yml Retrieve entries in domains.yml
And fill the holes if any And fill the holes if any
""" """
# Retrieve entries in the YAML # Retrieve actual domain list
old_domains = None get_domain_list = domain_list()
if os.path.exists(DOMAIN_SETTINGS_PATH) and os.path.isfile(DOMAIN_SETTINGS_PATH):
old_domains = yaml.load(open(DOMAIN_SETTINGS_PATH, "r+"))
if old_domains is None:
old_domains = dict()
# Create sanitized data # Create sanitized data
new_domains = dict() new_domains = dict()
get_domain_list = domain_list()
# Load main domain # Load main domain
maindomain = get_domain_list["main"] maindomain = get_domain_list["main"]
for domain in get_domain_list["domains"]: for domain in get_domain_list["domains"]:
# Retrieve entries in the YAML
filepath = f"{DOMAIN_SETTINGS_DIR}/{domain}.yml"
old_domain = {}
if os.path.exists(filepath) and os.path.isfile(filepath):
old_domain = yaml.load(open(filepath, "r+"))
# If the file is empty or "corrupted"
if not type(old_domain) is set:
old_domain = {}
is_maindomain = domain == maindomain is_maindomain = domain == maindomain
default_owned_dns_zone = True if domain == get_public_suffix(domain) else False default_owned_dns_zone = True if domain == get_public_suffix(domain) else False
domain_in_old_domains = domain in old_domains.keys()
# Update each setting if not present # Update each setting if not present
new_domains[domain] = {} new_domains[domain] = {}
# new_domains[domain] = { "main": is_maindomain }
# Set other values (default value if missing) # Set other values (default value if missing)
for setting, default in [ for setting, default in [
("xmpp", is_maindomain), ("xmpp", is_maindomain),
("mail", True), ("mail", True),
("owned_dns_zone", default_owned_dns_zone), ("owned_dns_zone", default_owned_dns_zone),
("ttl", 3600), ("ttl", 3600),
("provider", False), ("provider", {}),
]: ]:
if domain_in_old_domains and setting in old_domains[domain].keys(): if old_domain != {} and setting in old_domain.keys():
new_domains[domain][setting] = old_domains[domain][setting] new_domains[domain][setting] = old_domain[setting]
else: else:
new_domains[domain][setting] = default new_domains[domain][setting] = default
@ -858,11 +857,13 @@ def _set_domain_settings(domain, domain_settings):
if not domain in domains.keys(): if not domain in domains.keys():
raise YunohostError("domain_name_unknown", domain=domain) raise YunohostError("domain_name_unknown", domain=domain)
domains[domain] = domain_settings # First create the DOMAIN_SETTINGS_DIR if it doesn't exist
if not os.path.exists(DOMAIN_SETTINGS_DIR):
os.mkdir(DOMAIN_SETTINGS_DIR)
# Save the settings to the .yaml file # Save the settings to the .yaml file
with open(DOMAIN_SETTINGS_PATH, "w") as file: filepath = f"{DOMAIN_SETTINGS_DIR}/{domain}.yml"
yaml.dump(domains, file, default_flow_style=False) with open(filepath, "w") as file:
yaml.dump(domain_settings, file, default_flow_style=False)
# def domain_get_registrar(): # def domain_get_registrar():
@ -901,8 +902,7 @@ def domain_registrar_set(domain, registrar, args):
domain_settings["provider"] = domain_provider domain_settings["provider"] = domain_provider
# Save the settings to the .yaml file # Save the settings to the .yaml file
with open(DOMAIN_SETTINGS_PATH, "w") as file: _set_domain_settings(domain, domain_settings)
yaml.dump(domains, file, default_flow_style=False)
def domain_push_config(domain): def domain_push_config(domain):