Skip to content

Commit da2ccbf

Browse files
author
Ritwick DSouza
committed
[raspbian] Added support for multiple template rendering
1 parent 6fc19f7 commit da2ccbf

9 files changed

Lines changed: 127 additions & 86 deletions

File tree

netjsonconfig/backends/raspbian/raspbian.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from . import converters
2-
from .renderer import Raspbian
2+
from .renderer import *
33
from ..base.backend import BaseBackend
44
from .schema import schema
55

@@ -18,4 +18,22 @@ class Raspbian(BaseBackend):
1818
converters.DnsSearch,
1919
converters.Ntp
2020
]
21-
renderer = Raspbian
21+
renderer = [
22+
Hostname,
23+
Hostapd,
24+
Interfaces,
25+
Resolv,
26+
Ntp,
27+
Commands
28+
]
29+
30+
def render(self, files=True):
31+
self.validate()
32+
if self.intermediate_data is None:
33+
self.to_intermediate()
34+
output = ''
35+
for renderer_class in self.renderer:
36+
renderer = renderer_class(self)
37+
output += renderer.render()
38+
del renderer
39+
return output
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
from ..base.renderer import BaseRenderer
22

33

4-
class Raspbian(BaseRenderer):
5-
def cleanup(self, output):
6-
return output
4+
class RaspbianRenderer(BaseRenderer):
5+
pass
6+
7+
8+
class Commands(RaspbianRenderer):
9+
pass
10+
11+
12+
class Hostname(RaspbianRenderer):
13+
pass
14+
15+
16+
class Hostapd(RaspbianRenderer):
17+
pass
18+
19+
20+
class Interfaces(RaspbianRenderer):
21+
pass
22+
23+
24+
class Resolv(RaspbianRenderer):
25+
pass
26+
27+
28+
class Ntp(RaspbianRenderer):
29+
pass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% for i, j in data.items() %}
2+
{% if i|string() == 'general' %}
3+
{% for general in j %}
4+
{% if general.get('timezone') %}
5+
run commands:
6+
$ timedatectl set-timezone {{ general.get('timezone') }}
7+
8+
{% endif %}
9+
{% endfor %}
10+
{% endif %}
11+
{% endfor%}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% for i, j in data.items() %}
2+
{% if i|string() == 'wireless' %}
3+
{% for wireless in j %}
4+
config: /etc/hostapd/hostapd.conf
5+
interface={{ wireless.get('ifname') }}
6+
driver=nl80211
7+
hw_mode={{ wireless.get('hwmode')}}
8+
channel={{ wireless.get('channel')}}
9+
{% if wireless.get('protocol') == '80211n' %}
10+
ieee80211n=1
11+
{% endif %}
12+
ssid={{ wireless.get('ssid') }}
13+
{% if wireless.get('encryption') != {} %}
14+
auth_algs={{ wireless.get('encryption').get('auth_algs') }}
15+
wpa={{ wireless.get('encryption').get('wpa') }}
16+
wpa_key_mgmt={{ wireless.get('encryption').get('wpa_key_mgmt') }}
17+
wpa_passphrase={{ wireless.get('encryption').get('wpa_passphrase') }}
18+
{% if wireless.get('encryption', None).get('wpa_pairwise') != 'AUTO' %}
19+
wpa_pairwise={{ wireless.get('encryption').get('wpa_pairwise')}}
20+
{% endif %}
21+
{% endif %}
22+
{% endfor %}
23+
{% else %}
24+
{% endif %}
25+
{% endfor %}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% for i, j in data.items() %}
2+
{% if i|string() == 'general' %}
3+
{% for general in j %}
4+
{% if general.get('hostname') %}
5+
config: /etc/hostname
6+
{{ general.get('hostname') }}
7+
8+
{% endif %}
9+
{% endfor %}
10+
{% endif %}
11+
{% endfor %}

netjsonconfig/backends/raspbian/templates/raspbian.jinja2 renamed to netjsonconfig/backends/raspbian/templates/interfaces.jinja2

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
11
{% for i, j in data.items() %}
2-
{% if i|string() == 'general' %}
3-
{% for general in j %}
4-
{% if general.get('hostname') %}
5-
config: /etc/hostname
6-
{{ general.get('hostname') }}
7-
8-
{% endif %}
9-
{% endfor %}
10-
{% endif %}
11-
{% endfor %}
12-
{% for i, j in data.items() %}
13-
{% if i|string() == 'general' %}
14-
{% for general in j %}
15-
{% if general.get('timezone') %}
16-
run commands:
17-
$ timedatectl set-timezone {{ general.get('timezone') }}
18-
19-
{% endif %}
20-
{% endfor %}
21-
{% endif %}
22-
{% endfor%}
23-
{% for i, j in data.items() %}
242
{% if i|string() == 'interfaces' %}
253
{% for interface in j %}
264
{% if interface.get('iftype') == 'wireless' %}
@@ -103,54 +81,3 @@ iface {{ interface.get('ifname') }} inet6 {{ address.get('proto') }}
10381
{% endfor %}
10482
{% endif %}
10583
{% endfor%}
106-
{% set count = [1] %}
107-
{% for i, j in data.items() %}
108-
{% if i|string() in ['dns_servers', 'dns_search'] %}
109-
{% if count == [1] %}
110-
config: /etc/resolv.conf
111-
{% if count.append(count.pop() + 1) %}{% endif %}
112-
{% endif %}
113-
{% endif %}
114-
{% if i|string() == 'dns_servers' %}
115-
{% for ip in j %}
116-
nameserver {{ ip }}
117-
{% endfor %}
118-
{% elif i|string() == 'dns_search' %}
119-
{% for domain in j %}
120-
search {{ domain }}
121-
{% endfor %}
122-
{% endif %}
123-
{% endfor%}
124-
{% for i, j in data.items() %}
125-
{% if i|string() == 'wireless' %}
126-
{% for wireless in j %}
127-
config: /etc/hostapd/hostapd.conf
128-
interface={{ wireless.get('ifname') }}
129-
driver=nl80211
130-
hw_mode={{ wireless.get('hwmode')}}
131-
channel={{ wireless.get('channel')}}
132-
{% if wireless.get('protocol') == '80211n' %}
133-
ieee80211n=1
134-
{% endif %}
135-
ssid={{ wireless.get('ssid') }}
136-
{% if wireless.get('encryption') != {} %}
137-
auth_algs={{ wireless.get('encryption').get('auth_algs') }}
138-
wpa={{ wireless.get('encryption').get('wpa') }}
139-
wpa_key_mgmt={{ wireless.get('encryption').get('wpa_key_mgmt') }}
140-
wpa_passphrase={{ wireless.get('encryption').get('wpa_passphrase') }}
141-
{% if wireless.get('encryption', None).get('wpa_pairwise') != 'AUTO' %}
142-
wpa_pairwise={{ wireless.get('encryption').get('wpa_pairwise')}}
143-
{% endif %}
144-
{% endif %}
145-
{% endfor %}
146-
{% else %}
147-
{% endif %}
148-
{% endfor %}
149-
{% for i, j in data.items() %}
150-
{% if i|string() == 'ntp' %}
151-
config: /etc/ntp.conf
152-
{% for server in j %}
153-
server {{ server }}
154-
{% endfor %}
155-
{% endif %}
156-
{% endfor %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% for i, j in data.items() %}
2+
{% if i|string() == 'ntp' %}
3+
config: /etc/ntp.conf
4+
{% for server in j %}
5+
server {{ server }}
6+
{% endfor %}
7+
{% endif %}
8+
{% endfor %}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% set count = [1] %}
2+
{% for i, j in data.items() %}
3+
{% if i|string() in ['dns_servers', 'dns_search'] %}
4+
{% if count == [1] %}
5+
config: /etc/resolv.conf
6+
{% if count.append(count.pop() + 1) %}{% endif %}
7+
{% endif %}
8+
{% endif %}
9+
{% if i|string() == 'dns_servers' %}
10+
{% for ip in j %}
11+
nameserver {{ ip }}
12+
{% endfor %}
13+
{% elif i|string() == 'dns_search' %}
14+
{% for domain in j %}
15+
search {{ domain }}
16+
{% endfor %}
17+
{% endif %}
18+
{% endfor%}

tests/raspbian/test_hostapd.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def test_wpa2_personal(self):
3737
]
3838
})
3939

40-
expected = '''
41-
config: /etc/hostapd/hostapd.conf
40+
expected = '''config: /etc/hostapd/hostapd.conf
4241
interface=wlan0
4342
driver=nl80211
4443
hw_mode=g
@@ -50,6 +49,7 @@ def test_wpa2_personal(self):
5049
wpa_key_mgmt=WPA-PSK
5150
wpa_passphrase=passphrase012345
5251
wpa_pairwise=TKIP CCMP
52+
5353
'''
5454
self.assertEqual(o.render(), expected)
5555

@@ -84,8 +84,7 @@ def test_wpa_personal(self):
8484
]
8585
})
8686

87-
expected = '''
88-
config: /etc/hostapd/hostapd.conf
87+
expected = '''config: /etc/hostapd/hostapd.conf
8988
interface=wlan0
9089
driver=nl80211
9190
hw_mode=g
@@ -96,6 +95,7 @@ def test_wpa_personal(self):
9695
wpa=1
9796
wpa_key_mgmt=WPA-PSK
9897
wpa_passphrase=passphrase012345
98+
9999
'''
100100
self.assertEqual(o.render(), expected)
101101

@@ -199,14 +199,14 @@ def test_encryption_disabled(self):
199199
]
200200
})
201201

202-
expected = '''
203-
config: /etc/hostapd/hostapd.conf
202+
expected = '''config: /etc/hostapd/hostapd.conf
204203
interface=wlan0
205204
driver=nl80211
206205
hw_mode=g
207206
channel=3
208207
ieee80211n=1
209208
ssid=MyNetwork
209+
210210
'''
211211
self.assertEqual(o.render(), expected)
212212

@@ -237,14 +237,14 @@ def test_no_encryption(self):
237237
]
238238
})
239239

240-
expected = '''
241-
config: /etc/hostapd/hostapd.conf
240+
expected = '''config: /etc/hostapd/hostapd.conf
242241
interface=wlan0
243242
driver=nl80211
244243
hw_mode=g
245244
channel=3
246245
ieee80211n=1
247246
ssid=open
247+
248248
'''
249249
self.assertEqual(o.render(), expected)
250250

0 commit comments

Comments
 (0)