Skip to content

Commit

Permalink
Add ddns settings to the OpenWRT backend
Browse files Browse the repository at this point in the history
  • Loading branch information
okraits committed Apr 2, 2019
1 parent c23ce97 commit 4b34020
Show file tree
Hide file tree
Showing 6 changed files with 448 additions and 3 deletions.
159 changes: 157 additions & 2 deletions docs/source/backends/openwrt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,12 @@ Will be rendered as follows::

package network

config interface 'eth0' option ifname 'eth0' option ip6addr 'fdb4:5f35:e8fd::1/48' option ipaddr '10.27.251.1' option netmask '255.255.255.0' option proto 'static'
config interface 'eth0'
option ifname 'eth0'
option ip6addr 'fdb4:5f35:e8fd::1/48'
option ipaddr '10.27.251.1'
option netmask '255.255.255.0'
option proto 'static'

DNS servers and search domains
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -431,7 +436,22 @@ Will return the following UCI output::

package network

config interface 'eth0' option dns '10.11.12.13 8.8.8.8' option dns_search 'openwisp.org netjson.org' option ifname 'eth0' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option proto 'static' config interface 'eth1' option dns_search 'openwisp.org netjson.org' option ifname 'eth1' option proto 'dhcp' config interface 'eth1_31' option ifname 'eth1.31' option proto 'none'
config interface 'eth0'
option dns '10.11.12.13 8.8.8.8'
option dns_search 'openwisp.org netjson.org'
option ifname 'eth0'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'
option proto 'static'

config interface 'eth1'
option dns_search 'openwisp.org netjson.org'
option ifname 'eth1'
option proto 'dhcp'

config interface 'eth1_31'
option ifname 'eth1.31'
option proto 'none'

DHCP ipv6 ethernet interface
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -1815,6 +1835,141 @@ Will be rendered as follows::
option sysfs 'tp-link:blue:wlan2g'
option trigger 'phy0tpt'
DDNS settings
-------------
The ddns settings reside in the ``ddns`` key of the *configuration dictionary*,
which is a custom NetJSON extension not present in the original NetJSON RFC.
The ``ddns`` key must contain a dictionary, the allowed keys are:
+---------------------+---------+
| key name | type |
+=====================+=========+
| ``upd_privateip`` | boolean |
+---------------------+---------+
| ``ddns_dateformat`` | string |
+---------------------+---------+
| ``ddns_rundir`` | string |
+---------------------+---------+
| ``ddns_logdir`` | string |
+---------------------+---------+
| ``ddns_loglines`` | integer |
+---------------------+---------+
| ``use_curl`` | boolean |
+---------------------+---------+
| ``providers`` | list |
+---------------------+---------+
The ``providers`` key itself contains a list of dictionaries, the allowed keys are:
+---------------------+---------+
| key name | type |
+=====================+=========+
| ``enabled`` | boolean |
+---------------------+---------+
| ``interface`` | string |
+---------------------+---------+
| ``ip_source`` | string |
+---------------------+---------+
| ``lookup_host`` | string |
+---------------------+---------+
| ``domain`` | string |
+---------------------+---------+
| ``username`` | string |
+---------------------+---------+
| ``password`` | string |
+---------------------+---------+
| ``service_name`` | string |
+---------------------+---------+
| ``update_url`` | string |
+---------------------+---------+
| ``update_script`` | string |
+---------------------+---------+
| ``ip_network`` | string |
+---------------------+---------+
| ``ip_url`` | string |
+---------------------+---------+
| ``ip_interface`` | string |
+---------------------+---------+
| ``ip_script`` | string |
+---------------------+---------+
| ``use_syslog`` | integer |
+---------------------+---------+
| ``use_logfile`` | boolean |
+---------------------+---------+
The required keys are:
* ``enabled``
* ``interface``
* ``ip_source``
* ``lookup_host``
* ``domain``
* ``username``
* ``password``
For the function and meaning of each key consult the relevant
`OpenWrt documentation about ddns directives <https://openwrt.org/docs/guide-user/base-system/ddns>`_.
DDNS settings example
~~~~~~~~~~~~~~~~~~~~~
The following *configuration dictionary*:
.. code-block:: python
{
"ddns": {
"ddns_logdir": "/var/log/ddns",
"ddns_rundir": "/var/run/ddns",
"use_curl": false,
"upd_privateip": false,
"ddns_dateformat": "%F %R",
"ddns_loglines": 250,
"providers": [
{
"enabled": true,
"lookup_host": "myhost.dyndns.org",
"service_name": "dyndns.org",
"domain": "myhost.dyndns.org",
"username": "myuser",
"password": "mypassword",
"use_logfile": true,
"ip_source": "interface",
"ip_interface": "pppoe-xdsl",
"use_syslog": 2,
"interface": "xdsl"
}
],
}
}
Will be rendered as follows::
package ddns
config ddns 'global'
option ddns_logdir '/var/log/ddns'
option ddns_rundir '/var/run/ddns'
option use_curl '0'
option upd_privateip '0'
option ddns_dateformat '%F %R'
option ddns_loglines '250'
config service 'myhost_dyndns_org'
option enabled '1'
option lookup_host 'myhost.dyndns.org'
option service_name 'dyndns.org'
option domain 'myhost.dyndns.org'
option username 'myuser'
option password 'mypassword'
option use_logfile '1'
option ip_source 'interface'
option ip_interface 'pppoe-xdsl'
option use_syslog '2'
option interface 'xdsl'
Including custom options
------------------------
Expand Down
3 changes: 2 additions & 1 deletion netjsonconfig/backends/openwrt/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from .rules import Rules
from .switch import Switch
from .wireless import Wireless
from .ddns import Ddns

__all__ = ['Default', 'Interfaces', 'General',
'Led', 'Ntp', 'OpenVpn', 'Radios',
'Routes', 'Rules', 'Switch',
'Wireless']
'Wireless', 'Ddns']
47 changes: 47 additions & 0 deletions netjsonconfig/backends/openwrt/converters/ddns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from collections import OrderedDict

from ..schema import schema
from .base import OpenWrtConverter


class Ddns(OpenWrtConverter):
netjson_key = 'ddns'
intermediate_key = 'ddns'
_uci_types = ['ddns', 'service']
_schema = schema['properties']['ddns']

def to_intermediate_loop(self, block, result, index=None):
if block:
provider_list = self.__intermediate_providers(block.pop('providers', {}))
block.update({
'.type': 'ddns',
'.name': block.pop('id', 'global'),
})
result.setdefault('ddns', [])
result['ddns'] = [self.sorted_dict(block)] + provider_list
return result

def __intermediate_providers(self, providers):
"""
converts NetJSON provider to
UCI intermediate data structure
"""
result = []
for provider in providers:
uci_name = self._get_uci_name(provider['lookup_host'])
resultdict = OrderedDict((('.name', uci_name),
('.type', 'service')))
resultdict.update(provider)
result.append(resultdict)
return result

def to_netjson_loop(self, block, result, index):
result['ddns'] = self.__netjson_ddns(block)
return result

def __netjson_ddns(self, ddns):
del ddns['.type']
_name = ddns.pop('.name')
if _name != 'ddns':
ddns['id'] = _name
return self.type_cast(ddns)
1 change: 1 addition & 0 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class OpenWrt(BaseBackend):
converters.Radios,
converters.Wireless,
converters.OpenVpn,
converters.Ddns,
converters.Default,
]
parser = OpenWrtParser
Expand Down
Loading

0 comments on commit 4b34020

Please sign in to comment.