Skip to content

Commit 019b02e

Browse files
author
Ritwick DSouza
committed
[raspbian] Added support for generating tar.gz file
1 parent d15f874 commit 019b02e

11 files changed

Lines changed: 52 additions & 0 deletions

File tree

netjsonconfig/backends/raspbian/raspbian.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from . import converters
23
from ..base.backend import BaseBackend
34
from .renderer import Commands, Hostapd, Hostname, Interfaces, Ntp, Resolv
@@ -25,3 +26,17 @@ class Raspbian(BaseBackend):
2526
Ntp,
2627
Commands
2728
]
29+
30+
def _generate_contents(self, tar):
31+
text = self.render(files=False)
32+
config_files_pattern = re.compile('^# config:\s', flags=re.MULTILINE)
33+
config_files = config_files_pattern.split(text)
34+
if '' in config_files:
35+
config_files.remove('')
36+
for config_file in config_files:
37+
lines = config_file.split('\n')
38+
file_name = lines[0]
39+
text_contents = '\n'.join(lines[2:])
40+
self._add_file(tar=tar,
41+
name='{0}'.format(file_name),
42+
contents=text_contents)

netjsonconfig/backends/raspbian/templates/hostapd.jinja2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{% if i|string() == 'wireless' %}
33
{% for wireless in j %}
44
# config: /etc/hostapd/hostapd.conf
5+
56
interface={{ wireless.get('ifname') }}
67
driver=nl80211
78
{% if wireless.get('protocol') == 'a' or 'b' or 'g' %}

netjsonconfig/backends/raspbian/templates/hostname.jinja2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% for general in j %}
44
{% if general.get('hostname') %}
55
# config: /etc/hostname
6+
67
{{ general.get('hostname') }}
78

89
{% endif %}

netjsonconfig/backends/raspbian/templates/interfaces.jinja2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{% if interface.get('iftype') == 'wireless' %}
55
{% if interface.get('mode') == 'adhoc' %}
66
# config: /etc/network/interfaces
7+
78
auto {{ interface.get('iftype') }}
89
iface {{ interface.get('iftype') }} inet static
910
address 172.128.1.1
@@ -14,6 +15,7 @@
1415
{% endif %}
1516
{% elif interface.get('iftype') in ['ethernet', 'bridge', 'loopback'] %}
1617
# config: /etc/network/interfaces
18+
1719
auto {{ interface.get('ifname') }}
1820
{% if interface.get('address') != None %}
1921
{% for address in interface.get('address') %}

netjsonconfig/backends/raspbian/templates/ntp.jinja2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{% for i, j in data.items() %}
22
{% if i|string() == 'ntp' %}
33
# config: /etc/ntp.conf
4+
45
{% for server in j %}
56
server {{ server }}
67
{% endfor %}

netjsonconfig/backends/raspbian/templates/resolv.jinja2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% if i|string() in ['dns_servers', 'dns_search'] %}
44
{% if count == [1] %}
55
# config: /etc/resolv.conf
6+
67
{% if count.append(count.pop() + 1) %}{% endif %}
78
{% endif %}
89
{% endif %}

tests/raspbian/test_hostapd.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def test_wpa2_personal(self):
3838
})
3939

4040
expected = '''# config: /etc/hostapd/hostapd.conf
41+
4142
interface=wlan0
4243
driver=nl80211
4344
hw_mode=g
@@ -85,6 +86,7 @@ def test_wpa_personal(self):
8586
})
8687

8788
expected = '''# config: /etc/hostapd/hostapd.conf
89+
8890
interface=wlan0
8991
driver=nl80211
9092
hw_mode=g
@@ -200,6 +202,7 @@ def test_encryption_disabled(self):
200202
})
201203

202204
expected = '''# config: /etc/hostapd/hostapd.conf
205+
203206
interface=wlan0
204207
driver=nl80211
205208
hw_mode=g
@@ -238,6 +241,7 @@ def test_no_encryption(self):
238241
})
239242

240243
expected = '''# config: /etc/hostapd/hostapd.conf
244+
241245
interface=wlan0
242246
driver=nl80211
243247
hw_mode=g

tests/raspbian/test_interfaces.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_ipv4_static(self):
2525
})
2626

2727
expected = '''# config: /etc/network/interfaces
28+
2829
auto eth0
2930
iface eth0 inet static
3031
address 10.0.0.1
@@ -52,6 +53,7 @@ def test_ipv6_static(self):
5253
})
5354

5455
expected = '''# config: /etc/network/interfaces
56+
5557
auto eth0
5658
iface eth0 inet6 static
5759
address fe80::ba27:ebff:fe1c:5477
@@ -85,6 +87,7 @@ def test_multiple_static(self):
8587
})
8688

8789
expected = '''# config: /etc/network/interfaces
90+
8891
auto eth0
8992
iface eth0 inet static
9093
address 10.0.0.1
@@ -113,6 +116,7 @@ def test_ipv4_dhcp(self):
113116
})
114117

115118
expected = '''# config: /etc/network/interfaces
119+
116120
auto eth0
117121
iface eth0 inet dhcp
118122
@@ -136,6 +140,7 @@ def test_ipv6_dhcp(self):
136140
})
137141

138142
expected = '''# config: /etc/network/interfaces
143+
139144
auto eth0
140145
iface eth0 inet6 dhcp
141146
@@ -164,6 +169,7 @@ def test_multiple_dhcp(self):
164169
})
165170

166171
expected = '''# config: /etc/network/interfaces
172+
167173
auto eth0
168174
iface eth0 inet dhcp
169175
iface eth0 inet6 dhcp
@@ -203,6 +209,7 @@ def test_multiple_ip(self):
203209
})
204210

205211
expected = '''# config: /etc/network/interfaces
212+
206213
auto eth0.1
207214
iface eth0.1 inet static
208215
address 192.168.1.1
@@ -235,6 +242,7 @@ def test_mtu(self):
235242
})
236243

237244
expected = '''# config: /etc/network/interfaces
245+
238246
auto eth1
239247
iface eth1 inet dhcp
240248
pre-up /sbin/ifconfig $IFACE mtu 1500
@@ -260,6 +268,7 @@ def test_mac(self):
260268
})
261269

262270
expected = '''# config: /etc/network/interfaces
271+
263272
auto eth1
264273
iface eth1 inet dhcp
265274
hwaddress 52:54:00:56:46:c0
@@ -297,6 +306,7 @@ def test_multiple_ip_and_dhcp(self):
297306
})
298307

299308
expected = '''# config: /etc/network/interfaces
309+
300310
auto eth0
301311
iface eth0 inet dhcp
302312
iface eth0 inet static
@@ -330,12 +340,14 @@ def test_interface_with_resolv(self):
330340
})
331341

332342
expected = '''# config: /etc/network/interfaces
343+
333344
auto eth0
334345
iface eth0 inet static
335346
address 192.168.1.1
336347
netmask 255.255.255.0
337348
338349
# config: /etc/resolv.conf
350+
339351
nameserver 10.11.12.13
340352
nameserver 8.8.8.8
341353
search netjson.org
@@ -362,6 +374,7 @@ def test_loopback(self):
362374
})
363375

364376
expected = '''# config: /etc/network/interfaces
377+
365378
auto lo
366379
iface lo inet static
367380
address 127.0.0.1
@@ -387,6 +400,7 @@ def test_adhoc_wireless(self):
387400
})
388401

389402
expected = '''# config: /etc/network/interfaces
403+
390404
auto wireless
391405
iface wireless inet static
392406
address 172.128.1.1
@@ -414,6 +428,7 @@ def test_simple_bridge(self):
414428
})
415429

416430
expected = '''# config: /etc/network/interfaces
431+
417432
auto br-lan
418433
bridge_ports eth0 eth1
419434
@@ -444,6 +459,7 @@ def test_complex_bridge(self):
444459
})
445460

446461
expected = '''# config: /etc/network/interfaces
462+
447463
auto brwifi
448464
iface brwifi inet6 static
449465
address fe80::8029:23ff:fe7d:c214

tests/raspbian/test_radios.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_radio_multi(self):
4444
})
4545

4646
expected = '''# config: /etc/hostapd/hostapd.conf
47+
4748
interface=wlan0
4849
driver=nl80211
4950
hw_mode=g
@@ -81,6 +82,7 @@ def test_radio_n_24ghz(self):
8182
})
8283

8384
expected = '''# config: /etc/hostapd/hostapd.conf
85+
8486
interface=wlan0
8587
driver=nl80211
8688
hw_mode=g
@@ -118,6 +120,7 @@ def test_radio_n_5ghz(self):
118120
})
119121

120122
expected = '''# config: /etc/hostapd/hostapd.conf
123+
121124
interface=wlan0
122125
driver=nl80211
123126
hw_mode=a
@@ -154,6 +157,7 @@ def test_radio_ac(self):
154157
})
155158

156159
expected = '''# config: /etc/hostapd/hostapd.conf
160+
157161
interface=wlan0
158162
driver=nl80211
159163
hw_mode=a
@@ -190,6 +194,7 @@ def test_radio_a(self):
190194
})
191195

192196
expected = '''# config: /etc/hostapd/hostapd.conf
197+
193198
interface=wlan0
194199
driver=nl80211
195200
hw_mode=a
@@ -225,6 +230,7 @@ def test_radio_g(self):
225230
})
226231

227232
expected = '''# config: /etc/hostapd/hostapd.conf
233+
228234
interface=wlan0
229235
driver=nl80211
230236
hw_mode=g

tests/raspbian/test_resolv.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_dns_server(self):
1515
})
1616

1717
expected = '''# config: /etc/resolv.conf
18+
1819
nameserver 10.254.0.1
1920
nameserver 10.254.0.2
2021
'''
@@ -28,6 +29,7 @@ def test_dns_search(self):
2829
})
2930

3031
expected = '''# config: /etc/resolv.conf
32+
3133
search domain.com
3234
'''
3335
self.assertEqual(o.render(), expected)
@@ -44,6 +46,7 @@ def test_dns_server_and_dns_search(self):
4446
})
4547

4648
expected = '''# config: /etc/resolv.conf
49+
4750
nameserver 10.11.12.13
4851
nameserver 8.8.8.8
4952
search netjson.org

0 commit comments

Comments
 (0)