Skip to content

Commit 0b10eef

Browse files
okraitskkreitmair
authored andcommitted
Add ddns settings to the OpenWRT backend
1 parent cadb36b commit 0b10eef

File tree

6 files changed

+451
-4
lines changed

6 files changed

+451
-4
lines changed

docs/source/backends/openwrt.rst

Lines changed: 157 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,12 @@ Will be rendered as follows::
376376

377377
package network
378378

379-
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'
379+
config interface 'eth0'
380+
option ifname 'eth0'
381+
option ip6addr 'fdb4:5f35:e8fd::1/48'
382+
option ipaddr '10.27.251.1'
383+
option netmask '255.255.255.0'
384+
option proto 'static'
380385

381386
DNS servers and search domains
382387
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -431,7 +436,22 @@ Will return the following UCI output::
431436

432437
package network
433438

434-
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'
439+
config interface 'eth0'
440+
option dns '10.11.12.13 8.8.8.8'
441+
option dns_search 'openwisp.org netjson.org'
442+
option ifname 'eth0'
443+
option ipaddr '192.168.1.1'
444+
option netmask '255.255.255.0'
445+
option proto 'static'
446+
447+
config interface 'eth1'
448+
option dns_search 'openwisp.org netjson.org'
449+
option ifname 'eth1'
450+
option proto 'dhcp'
451+
452+
config interface 'eth1_31'
453+
option ifname 'eth1.31'
454+
option proto 'none'
435455

436456
DHCP ipv6 ethernet interface
437457
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1815,6 +1835,141 @@ Will be rendered as follows::
18151835
option sysfs 'tp-link:blue:wlan2g'
18161836
option trigger 'phy0tpt'
18171837
1838+
DDNS settings
1839+
-------------
1840+
1841+
The ddns settings reside in the ``ddns`` key of the *configuration dictionary*,
1842+
which is a custom NetJSON extension not present in the original NetJSON RFC.
1843+
1844+
The ``ddns`` key must contain a dictionary, the allowed keys are:
1845+
1846+
+---------------------+---------+
1847+
| key name | type |
1848+
+=====================+=========+
1849+
| ``upd_privateip`` | boolean |
1850+
+---------------------+---------+
1851+
| ``ddns_dateformat`` | string |
1852+
+---------------------+---------+
1853+
| ``ddns_rundir`` | string |
1854+
+---------------------+---------+
1855+
| ``ddns_logdir`` | string |
1856+
+---------------------+---------+
1857+
| ``ddns_loglines`` | integer |
1858+
+---------------------+---------+
1859+
| ``use_curl`` | boolean |
1860+
+---------------------+---------+
1861+
| ``providers`` | list |
1862+
+---------------------+---------+
1863+
1864+
The ``providers`` key itself contains a list of dictionaries, the allowed keys are:
1865+
1866+
+---------------------+---------+
1867+
| key name | type |
1868+
+=====================+=========+
1869+
| ``enabled`` | boolean |
1870+
+---------------------+---------+
1871+
| ``interface`` | string |
1872+
+---------------------+---------+
1873+
| ``ip_source`` | string |
1874+
+---------------------+---------+
1875+
| ``lookup_host`` | string |
1876+
+---------------------+---------+
1877+
| ``domain`` | string |
1878+
+---------------------+---------+
1879+
| ``username`` | string |
1880+
+---------------------+---------+
1881+
| ``password`` | string |
1882+
+---------------------+---------+
1883+
| ``service_name`` | string |
1884+
+---------------------+---------+
1885+
| ``update_url`` | string |
1886+
+---------------------+---------+
1887+
| ``update_script`` | string |
1888+
+---------------------+---------+
1889+
| ``ip_network`` | string |
1890+
+---------------------+---------+
1891+
| ``ip_url`` | string |
1892+
+---------------------+---------+
1893+
| ``ip_interface`` | string |
1894+
+---------------------+---------+
1895+
| ``ip_script`` | string |
1896+
+---------------------+---------+
1897+
| ``use_syslog`` | integer |
1898+
+---------------------+---------+
1899+
| ``use_logfile`` | boolean |
1900+
+---------------------+---------+
1901+
1902+
The required keys are:
1903+
1904+
* ``enabled``
1905+
* ``interface``
1906+
* ``ip_source``
1907+
* ``lookup_host``
1908+
* ``domain``
1909+
* ``username``
1910+
* ``password``
1911+
1912+
For the function and meaning of each key consult the relevant
1913+
`OpenWrt documentation about ddns directives <https://openwrt.org/docs/guide-user/base-system/ddns>`_.
1914+
1915+
DDNS settings example
1916+
~~~~~~~~~~~~~~~~~~~~~
1917+
1918+
The following *configuration dictionary*:
1919+
1920+
.. code-block:: python
1921+
1922+
{
1923+
"ddns": {
1924+
"ddns_logdir": "/var/log/ddns",
1925+
"ddns_rundir": "/var/run/ddns",
1926+
"use_curl": false,
1927+
"upd_privateip": false,
1928+
"ddns_dateformat": "%F %R",
1929+
"ddns_loglines": 250,
1930+
"providers": [
1931+
{
1932+
"enabled": true,
1933+
"lookup_host": "myhost.dyndns.org",
1934+
"service_name": "dyndns.org",
1935+
"domain": "myhost.dyndns.org",
1936+
"username": "myuser",
1937+
"password": "mypassword",
1938+
"use_logfile": true,
1939+
"ip_source": "interface",
1940+
"ip_interface": "pppoe-xdsl",
1941+
"use_syslog": 2,
1942+
"interface": "xdsl"
1943+
}
1944+
],
1945+
}
1946+
}
1947+
1948+
Will be rendered as follows::
1949+
1950+
package ddns
1951+
1952+
config ddns 'global'
1953+
option ddns_logdir '/var/log/ddns'
1954+
option ddns_rundir '/var/run/ddns'
1955+
option use_curl '0'
1956+
option upd_privateip '0'
1957+
option ddns_dateformat '%F %R'
1958+
option ddns_loglines '250'
1959+
1960+
config service 'myhost_dyndns_org'
1961+
option enabled '1'
1962+
option lookup_host 'myhost.dyndns.org'
1963+
option service_name 'dyndns.org'
1964+
option domain 'myhost.dyndns.org'
1965+
option username 'myuser'
1966+
option password 'mypassword'
1967+
option use_logfile '1'
1968+
option ip_source 'interface'
1969+
option ip_interface 'pppoe-xdsl'
1970+
option use_syslog '2'
1971+
option interface 'xdsl'
1972+
18181973
Including custom options
18191974
------------------------
18201975

netjsonconfig/backends/openwrt/converters/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .rules import Rules
1010
from .switch import Switch
1111
from .wireless import Wireless
12+
from .ddns import Ddns
1213

1314
__all__ = [
1415
'Default',
@@ -22,4 +23,5 @@
2223
'Rules',
2324
'Switch',
2425
'Wireless',
26+
'Ddns',
2527
]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from collections import OrderedDict
2+
3+
from ..schema import schema
4+
from .base import OpenWrtConverter
5+
6+
7+
class Ddns(OpenWrtConverter):
8+
netjson_key = 'ddns'
9+
intermediate_key = 'ddns'
10+
_uci_types = ['ddns', 'service']
11+
_schema = schema['properties']['ddns']
12+
13+
def to_intermediate_loop(self, block, result, index=None):
14+
if block:
15+
provider_list = self.__intermediate_providers(block.pop('providers', {}))
16+
block.update({
17+
'.type': 'ddns',
18+
'.name': block.pop('id', 'global'),
19+
})
20+
result.setdefault('ddns', [])
21+
result['ddns'] = [self.sorted_dict(block)] + provider_list
22+
return result
23+
24+
def __intermediate_providers(self, providers):
25+
"""
26+
converts NetJSON provider to
27+
UCI intermediate data structure
28+
"""
29+
result = []
30+
for provider in providers:
31+
uci_name = self._get_uci_name(provider['lookup_host'])
32+
resultdict = OrderedDict((('.name', uci_name),
33+
('.type', 'service')))
34+
resultdict.update(provider)
35+
result.append(resultdict)
36+
return result
37+
38+
def to_netjson_loop(self, block, result, index):
39+
result['ddns'] = self.__netjson_ddns(block)
40+
return result
41+
42+
def __netjson_ddns(self, ddns):
43+
del ddns['.type']
44+
_name = ddns.pop('.name')
45+
if _name != 'ddns':
46+
ddns['id'] = _name
47+
return self.type_cast(ddns)

netjsonconfig/backends/openwrt/openwrt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class OpenWrt(BaseBackend):
2222
converters.Radios,
2323
converters.Wireless,
2424
converters.OpenVpn,
25+
converters.Ddns,
2526
converters.Default,
2627
]
2728
parser = OpenWrtParser

0 commit comments

Comments
 (0)