Merge pull request #786 from YunoHost/dyndns_update_force

"yunohost dyndns update" "--force" and "--dry-run"
This commit is contained in:
Alexandre Aubin 2019-09-01 16:23:14 +02:00 committed by GitHub
commit 3d10e8fef2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View file

@ -1533,6 +1533,14 @@ dyndns:
-i: -i:
full: --ipv4 full: --ipv4
help: IP address to send help: IP address to send
-f:
full: --force
help: Force the update (for debugging only)
action: store_true
-D:
full: --dry-run
help: Only display the generated zone
action: store_true
-6: -6:
full: --ipv6 full: --ipv6
help: IPv6 address to send help: IPv6 address to send

View file

@ -33,7 +33,7 @@ import subprocess
from moulinette import m18n from moulinette import m18n
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.filesystem import write_to_file from moulinette.utils.filesystem import write_to_file, read_file
from moulinette.utils.network import download_json from moulinette.utils.network import download_json
from moulinette.utils.process import check_output from moulinette.utils.process import check_output
@ -176,7 +176,7 @@ def dyndns_subscribe(operation_logger, subscribe_host="dyndns.yunohost.org", dom
@is_unit_operation() @is_unit_operation()
def dyndns_update(operation_logger, dyn_host="dyndns.yunohost.org", domain=None, key=None, def dyndns_update(operation_logger, dyn_host="dyndns.yunohost.org", domain=None, key=None,
ipv4=None, ipv6=None): ipv4=None, ipv6=None, force=False, dry_run=False):
""" """
Update IP on DynDNS platform Update IP on DynDNS platform
@ -249,7 +249,7 @@ def dyndns_update(operation_logger, dyn_host="dyndns.yunohost.org", domain=None,
logger.debug("Requested IPv4/v6 are (%s, %s)" % (ipv4, ipv6)) logger.debug("Requested IPv4/v6 are (%s, %s)" % (ipv4, ipv6))
# no need to update # no need to update
if old_ipv4 == ipv4 and old_ipv6 == ipv6: if (not force and not dry_run) and (old_ipv4 == ipv4 and old_ipv6 == ipv6):
logger.info("No updated needed.") logger.info("No updated needed.")
return return
else: else:
@ -279,7 +279,7 @@ def dyndns_update(operation_logger, dyn_host="dyndns.yunohost.org", domain=None,
# should be muc.the.domain.tld. or the.domain.tld # should be muc.the.domain.tld. or the.domain.tld
if record["value"] == "@": if record["value"] == "@":
record["value"] = domain record["value"] = domain
record["value"] = record["value"].replace(";", "\;") record["value"] = record["value"].replace(";", r"\;")
action = "update add {name}.{domain}. {ttl} {type} {value}".format(domain=domain, **record) action = "update add {name}.{domain}. {ttl} {type} {value}".format(domain=domain, **record)
action = action.replace(" @.", " ") action = action.replace(" @.", " ")
@ -296,6 +296,7 @@ def dyndns_update(operation_logger, dyn_host="dyndns.yunohost.org", domain=None,
logger.debug("Now pushing new conf to DynDNS host...") logger.debug("Now pushing new conf to DynDNS host...")
if not dry_run:
try: try:
command = ["/usr/bin/nsupdate", "-k", key, DYNDNS_ZONE] command = ["/usr/bin/nsupdate", "-k", key, DYNDNS_ZONE]
subprocess.check_call(command) subprocess.check_call(command)
@ -303,6 +304,10 @@ def dyndns_update(operation_logger, dyn_host="dyndns.yunohost.org", domain=None,
raise YunohostError('dyndns_ip_update_failed') raise YunohostError('dyndns_ip_update_failed')
logger.success(m18n.n('dyndns_ip_updated')) logger.success(m18n.n('dyndns_ip_updated'))
else:
print(read_file(DYNDNS_ZONE))
print("")
print("Warning: dry run, this is only the generated config, it won't be applied")
def dyndns_installcron(): def dyndns_installcron():