summaryrefslogtreecommitdiff
path: root/src/conf_mode/dynamic_dns.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/conf_mode/dynamic_dns.py')
-rwxr-xr-xsrc/conf_mode/dynamic_dns.py84
1 files changed, 23 insertions, 61 deletions
diff --git a/src/conf_mode/dynamic_dns.py b/src/conf_mode/dynamic_dns.py
index 4663987b4..b54d76b06 100755
--- a/src/conf_mode/dynamic_dns.py
+++ b/src/conf_mode/dynamic_dns.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2018-2019 VyOS maintainers and contributors
+# Copyright (C) 2018-2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
@@ -15,68 +15,22 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
-import sys
-import jinja2
+from sys import exit
+from copy import deepcopy
+from jinja2 import FileSystemLoader, Environment
from stat import S_IRUSR, S_IWUSR
+
from vyos.config import Config
+from vyos.defaults import directories as vyos_data_dir
from vyos import ConfigError
+from vyos.util import call
+
config_file = r'/etc/ddclient/ddclient.conf'
cache_file = r'/var/cache/ddclient/ddclient.cache'
pid_file = r'/var/run/ddclient/ddclient.pid'
-config_tmpl = """
-### Autogenerated by dynamic_dns.py ###
-daemon=1m
-syslog=yes
-ssl=yes
-pid={{ pid_file }}
-cache={{ cache_file }}
-
-{% for interface in interfaces -%}
-
-#
-# ddclient configuration for interface "{{ interface.interface }}":
-#
-{% if interface.web_url -%}
-use=web, web='{{ interface.web_url}}' {%- if interface.web_skip %}, web-skip='{{ interface.web_skip }}'{% endif %}
-{% else -%}
-use=if, if={{ interface.interface }}
-{% endif -%}
-
-{% for rfc in interface.rfc2136 -%}
-{% for record in rfc.record %}
-# RFC2136 dynamic DNS configuration for {{ record }}.{{ rfc.zone }}
-server={{ rfc.server }}
-protocol=nsupdate
-password={{ rfc.keyfile }}
-ttl={{ rfc.ttl }}
-zone={{ rfc.zone }}
-{{ record }}
-{% endfor -%}
-{% endfor -%}
-
-{% for srv in interface.service %}
-{% for host in srv.host %}
-# DynDNS provider configuration for {{ host }}
-protocol={{ srv.protocol }},
-max-interval=28d,
-login={{ srv.login }},
-password='{{ srv.password }}',
-{% if srv.server -%}
-server={{ srv.server }},
-{% endif -%}
-{% if srv.zone -%}
-zone={{ srv.zone }},
-{% endif -%}
-{{ host }}
-{% endfor %}
-{% endfor %}
-
-{% endfor %}
-"""
-
# Mapping of service name to service protocol
default_service_protocol = {
'afraid': 'freedns',
@@ -100,7 +54,7 @@ default_config_data = {
}
def get_config():
- dyndns = default_config_data
+ dyndns = deepcopy(default_config_data)
conf = Config()
base_level = ['service', 'dns', 'dynamic']
@@ -198,8 +152,8 @@ def get_config():
node['service'].append(srv)
- # set config level back to top level
- conf.set_level(base_level)
+ # Set config back to appropriate level for these options
+ conf.set_level(base_level + ['interface', interface])
# Additional settings in CLI
if conf.exists(['use-web', 'skip']):
@@ -208,6 +162,9 @@ def get_config():
if conf.exists(['use-web', 'url']):
node['web_url'] = conf.return_value(['use-web', 'url'])
+ # set config level back to top level
+ conf.set_level(base_level)
+
dyndns['interfaces'].append(node)
return dyndns
@@ -269,6 +226,11 @@ def generate(dyndns):
return None
+ # Prepare Jinja2 template loader from files
+ tmpl_path = os.path.join(vyos_data_dir['data'], 'templates', 'dynamic-dns')
+ fs_loader = FileSystemLoader(tmpl_path)
+ env = Environment(loader=fs_loader)
+
dirname = os.path.dirname(dyndns['pid_file'])
if not os.path.exists(dirname):
os.mkdir(dirname)
@@ -277,7 +239,7 @@ def generate(dyndns):
if not os.path.exists(dirname):
os.mkdir(dirname)
- tmpl = jinja2.Template(config_tmpl)
+ tmpl = env.get_template('ddclient.conf.tmpl')
config_text = tmpl.render(dyndns)
with open(config_file, 'w') as f:
f.write(config_text)
@@ -295,11 +257,11 @@ def apply(dyndns):
os.unlink('/etc/ddclient.conf')
if dyndns['deleted']:
- os.system('/etc/init.d/ddclient stop')
+ call('/etc/init.d/ddclient stop')
if os.path.exists(dyndns['pid_file']):
os.unlink(dyndns['pid_file'])
else:
- os.system('/etc/init.d/ddclient restart')
+ call('/etc/init.d/ddclient restart')
return None
@@ -311,4 +273,4 @@ if __name__ == '__main__':
apply(c)
except ConfigError as e:
print(e)
- sys.exit(1)
+ exit(1)