summaryrefslogtreecommitdiff
path: root/src/conf_mode/dhcpv6_server.py
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-04-05 18:26:58 +0200
committerChristian Poessinger <christian@poessinger.com>2020-04-05 18:28:10 +0200
commita6100cde7c9f045175b5c958055df59bab6afe1a (patch)
treea6a5e0bf651c59c6b64d185dff40dafb7d007fb0 /src/conf_mode/dhcpv6_server.py
parenta7c70fae028bc64164502c9c29a606f7260e31a3 (diff)
downloadvyos-1x-a6100cde7c9f045175b5c958055df59bab6afe1a.tar.gz
vyos-1x-a6100cde7c9f045175b5c958055df59bab6afe1a.zip
dhcpv6-server: T2230: move inlined templates to dedicated files
Diffstat (limited to 'src/conf_mode/dhcpv6_server.py')
-rwxr-xr-xsrc/conf_mode/dhcpv6_server.py122
1 files changed, 16 insertions, 106 deletions
diff --git a/src/conf_mode/dhcpv6_server.py b/src/conf_mode/dhcpv6_server.py
index 44a927789..10b40baa4 100755
--- a/src/conf_mode/dhcpv6_server.py
+++ b/src/conf_mode/dhcpv6_server.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
@@ -13,18 +13,17 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-#
-#
-import sys
import os
import ipaddress
-import jinja2
-
-import vyos.validate
+from sys import exit
+from copy import deepcopy
+from jinja2 import FileSystemLoader, Environment
from vyos.config import Config
+from vyos.defaults import directories as vyos_data_dir
+from vyos.validate import is_subnet_connected
from vyos import ConfigError
config_file = r'/etc/dhcp/dhcpdv6.conf'
@@ -32,100 +31,6 @@ lease_file = r'/config/dhcpdv6.leases'
pid_file = r'/var/run/dhcpdv6.pid'
daemon_config_file = r'/etc/default/isc-dhcpv6-server'
-# Please be careful if you edit the template.
-config_tmpl = """
-### Autogenerated by dhcpv6_server.py ###
-
-# For options please consult the following website:
-# https://www.isc.org/wp-content/uploads/2017/08/dhcp43options.html
-
-log-facility local7;
-{%- if preference %}
-option dhcp6.preference {{ preference }};
-{%- endif %}
-
-# Shared network configration(s)
-{% for network in shared_network %}
-{%- if not network.disabled -%}
-shared-network {{ network.name }} {
- {%- for subnet in network.subnet %}
- subnet6 {{ subnet.network }} {
- {%- for range in subnet.range6_prefix %}
- range6 {{ range.prefix }}{{ " temporary" if range.temporary }};
- {%- endfor %}
- {%- for range in subnet.range6 %}
- range6 {{ range.start }} {{ range.stop }};
- {%- endfor %}
- {%- if subnet.domain_search %}
- option dhcp6.domain-search {{ subnet.domain_search | join(', ') }};
- {%- endif %}
- {%- if subnet.lease_def %}
- default-lease-time {{ subnet.lease_def }};
- {%- endif %}
- {%- if subnet.lease_max %}
- max-lease-time {{ subnet.lease_max }};
- {%- endif %}
- {%- if subnet.lease_min %}
- min-lease-time {{ subnet.lease_min }};
- {%- endif %}
- {%- if subnet.dns_server %}
- option dhcp6.name-servers {{ subnet.dns_server | join(', ') }};
- {%- endif %}
- {%- if subnet.nis_domain %}
- option dhcp6.nis-domain-name "{{ subnet.nis_domain }}";
- {%- endif %}
- {%- if subnet.nis_server %}
- option dhcp6.nis-servers {{ subnet.nis_server | join(', ') }};
- {%- endif %}
- {%- if subnet.nisp_domain %}
- option dhcp6.nisp-domain-name "{{ subnet.nisp_domain }}";
- {%- endif %}
- {%- if subnet.nisp_server %}
- option dhcp6.nisp-servers {{ subnet.nisp_server | join(', ') }};
- {%- endif %}
- {%- if subnet.sip_address %}
- option dhcp6.sip-servers-addresses {{ subnet.sip_address | join(', ') }};
- {%- endif %}
- {%- if subnet.sip_hostname %}
- option dhcp6.sip-servers-names {{ subnet.sip_hostname | join(', ') }};
- {%- endif %}
- {%- if subnet.sntp_server %}
- option dhcp6.sntp-servers {{ subnet.sntp_server | join(', ') }};
- {%- endif %}
- {%- for host in subnet.static_mapping %}
- {% if not host.disabled -%}
- host {{ network.name }}_{{ host.name }} {
- {%- if host.client_identifier %}
- host-identifier option dhcp6.client-id {{ host.client_identifier }};
- {%- endif %}
- {%- if host.ipv6_address %}
- fixed-address6 {{ host.ipv6_address }};
- {%- endif %}
- }
- {%- endif %}
- {%- endfor %}
- }
- {%- endfor %}
- on commit {
- set shared-networkname = "{{ network.name }}";
- }
-}
-{%- endif %}
-{% endfor %}
-
-"""
-
-daemon_tmpl = """
-### Autogenerated by dhcpv6_server.py ###
-
-# sourced by /etc/init.d/isc-dhcpv6-server
-
-DHCPD_CONF={{ config_file }}
-DHCPD_PID={{ pid_file }}
-OPTIONS="-6 -lf {{ lease_file }}"
-INTERFACES=""
-"""
-
default_config_data = {
'lease_file': lease_file,
'preference': '',
@@ -134,7 +39,7 @@ default_config_data = {
}
def get_config():
- dhcpv6 = default_config_data
+ dhcpv6 = deepcopy(default_config_data)
conf = Config()
if not conf.exists('service dhcpv6-server'):
return None
@@ -409,7 +314,7 @@ def verify(dhcpv6):
# There must be one subnet connected to a listen interface if network is not disabled.
if not network['disabled']:
- if vyos.validate.is_subnet_connected(subnet['network']):
+ if is_subnet_connected(subnet['network']):
listen_ok = True
# DHCPv6 subnet must not overlap. ISC DHCP also complains about overlapping
@@ -437,12 +342,17 @@ def generate(dhcpv6):
print('Warning: DHCPv6 server will be deactivated because it is disabled')
return None
- tmpl = jinja2.Template(config_tmpl)
+ # Prepare Jinja2 template loader from files
+ tmpl_path = os.path.join(vyos_data_dir['data'], 'templates', 'dhcpv6-server')
+ fs_loader = FileSystemLoader(tmpl_path)
+ env = Environment(loader=fs_loader)
+
+ tmpl = env.get_template('dhcpdv6.conf.tmpl')
config_text = tmpl.render(dhcpv6)
with open(config_file, 'w') as f:
f.write(config_text)
- tmpl = jinja2.Template(daemon_tmpl)
+ tmpl = env.get_template('daemon.tmpl')
config_text = tmpl.render(dhcpv6)
with open(daemon_config_file, 'w') as f:
f.write(config_text)
@@ -474,4 +384,4 @@ if __name__ == '__main__':
apply(c)
except ConfigError as e:
print(e)
- sys.exit(1)
+ exit(1)