From 940af74c2d58fa9a0b055c7c6cbb4dfdd4202e03 Mon Sep 17 00:00:00 2001 From: "theo@manjaro" Date: Wed, 6 Jul 2022 15:52:27 +0200 Subject: [PATCH] `yunohost domain dns push` now accepts an --auto option Domains can be configured to be auto-pushed by a cron job --- hooks/conf_regen/01-yunohost | 5 +++-- locales/en.json | 2 ++ share/actionsmap.yml | 10 ++++++++-- share/config_domain.toml | 7 +++++++ src/dns.py | 9 +++++---- src/domain.py | 8 +++++--- src/dyndns.py | 4 ++++ 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/hooks/conf_regen/01-yunohost b/hooks/conf_regen/01-yunohost index 29da2b183..55accc4f4 100755 --- a/hooks/conf_regen/01-yunohost +++ b/hooks/conf_regen/01-yunohost @@ -116,8 +116,9 @@ SHELL=/bin/bash # - (sleep random 60 is here to spread requests over a 1-min window) # - if ip.yunohost.org answers ping (basic check to validate that we're connected to the internet and yunohost infra aint down) # - and if lock ain't already taken by another command -# - trigger yunohost dyndns update -*/10 * * * * root : YunoHost DynDNS update ; sleep \$((RANDOM\\%60)); ! ping -q -W5 -c1 ip.yunohost.org >/dev/null 2>&1 || test -e /var/run/moulinette_yunohost.lock || yunohost domain list --exclude-subdomains --output json | jq --raw-output '.domains[]' | grep -E "\.(noho\.st|nohost\.me|ynh\.fr)$" | xargs -I {} yunohost domain dns push "{}" >> /dev/null +# - check if some domains are flagged as autopush +# - trigger yunohost domain dns push --auto +*/10 * * * * root : YunoHost DynDNS update ; sleep \$((RANDOM\\%60)); ! ping -q -W5 -c1 ip.yunohost.org >/dev/null 2>&1 || test -e /var/run/moulinette_yunohost.lock || !(grep -nR "autopush: 1" /etc/yunohost/domains/*.yml > /dev/null) || yunohost domain dns push --auto >> /dev/null EOF else # (Delete cron if no dyndns domain found) diff --git a/locales/en.json b/locales/en.json index 8e3d5c2b6..d58790ba2 100644 --- a/locales/en.json +++ b/locales/en.json @@ -312,6 +312,8 @@ "domain_config_auth_token": "Authentication token", "domain_config_default_app": "Default app", "domain_config_features_disclaimer": "So far, enabling/disabling mail or XMPP features only impact the recommended and automatic DNS configuration, not system configurations!", + "domain_config_autopush": "Auto-push", + "domain_config_autopush_help": "Automatically update the domain's record", "domain_config_mail_in": "Incoming emails", "domain_config_mail_out": "Outgoing emails", "domain_config_xmpp": "Instant messaging (XMPP)", diff --git a/share/actionsmap.yml b/share/actionsmap.yml index d69a35f1f..17b4c1f96 100644 --- a/share/actionsmap.yml +++ b/share/actionsmap.yml @@ -443,6 +443,9 @@ domain: --exclude-subdomains: help: Filter out domains that are obviously subdomains of other declared domains action: store_true + --auto-push: + help: Only display domains that are pushed automatically + action: store_true ### domain_add() add: @@ -689,8 +692,8 @@ domain: action_help: Push DNS records to registrar api: POST /domains//dns/push arguments: - domain: - help: Domain name to push DNS conf for + domains: + help: Domain names to push DNS conf for nargs: "*" extra: pattern: *pattern_domain @@ -704,6 +707,9 @@ domain: --purge: help: Delete all records action: store_true + --auto: + help: Push only domains that should be pushed automatically + action: store_true cert: subcategory_help: Manage domain certificates diff --git a/share/config_domain.toml b/share/config_domain.toml index 65e755365..ba0706749 100644 --- a/share/config_domain.toml +++ b/share/config_domain.toml @@ -46,6 +46,13 @@ i18n = "domain_config" default = 0 [dns] + + [dns.zone] + + [dns.zone.autopush] + type = "boolean" + default = 0 + help = "" [dns.registrar] optional = true diff --git a/src/dns.py b/src/dns.py index 0a7ce7ea2..8ba46011e 100644 --- a/src/dns.py +++ b/src/dns.py @@ -623,10 +623,11 @@ def _get_registar_settings(domain): @is_unit_operation() -def domain_dns_push(operation_logger, domains, dry_run=False, force=False, purge=False): - # If we provide only a domain as an argument - if isinstance(domains, str): - domains = [domains] +def domain_dns_push(operation_logger, domains, dry_run=False, force=False, purge=False, auto=False): + if auto: + domains = domain_list(exclude_subdomains=True,auto_push=True)["domains"] + elif len(domains)==0: + domains = domain_list(exclude_subdomains=True)["domains"] error_domains = [] for domain in domains: try: diff --git a/src/domain.py b/src/domain.py index f9597b813..df40577da 100644 --- a/src/domain.py +++ b/src/domain.py @@ -52,7 +52,7 @@ DOMAIN_SETTINGS_DIR = "/etc/yunohost/domains" domain_list_cache: Dict[str, Any] = {} -def domain_list(exclude_subdomains=False): +def domain_list(exclude_subdomains=False,auto_push=False): """ List domains @@ -78,6 +78,8 @@ def domain_list(exclude_subdomains=False): parent_domain = domain.split(".", 1)[1] if parent_domain in result: continue + if auto_push and not domain_config_get(domain, key="dns.zone.autopush"): + continue result_list.append(domain) @@ -611,7 +613,7 @@ def domain_dns_suggest(domain): return domain_dns_suggest(domain) -def domain_dns_push(domain, dry_run=None, force=None, purge=None): +def domain_dns_push(domains, dry_run=None, force=None, purge=None, auto=False): from yunohost.dns import domain_dns_push - return domain_dns_push(domain, dry_run=dry_run, force=force, purge=purge) + return domain_dns_push(domains, dry_run=dry_run, force=force, purge=purge, auto=auto) diff --git a/src/dyndns.py b/src/dyndns.py index f5531d518..4ddbf7396 100644 --- a/src/dyndns.py +++ b/src/dyndns.py @@ -170,6 +170,10 @@ def dyndns_subscribe(operation_logger, domain=None, key=None, password=None): error = f'Server error, code: {r.status_code}. (Message: "{r.text}")' raise YunohostError("dyndns_registration_failed", error=error) + # Set the domain's config to autopush + from yunohost.domain import domain_config_set + domain_config_set(domain,key="dns.zone.autopush",value=1) + # Yunohost regen conf will add the dyndns cron job if a key exists # in /etc/yunohost/dyndns regen_conf(["yunohost"])