Skip to content

Commit cd04331

Browse files
author
Ritwick DSouza
committed
[raspbian] Created seprate files for each converter
1 parent 39802d9 commit cd04331

11 files changed

Lines changed: 205 additions & 178 deletions

File tree

netjsonconfig/backends/raspbian/converters.py

Lines changed: 0 additions & 170 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .general import General
2+
from .interfaces import Interfaces
3+
from .ntp import Ntp
4+
from .wireless import Wireless
5+
from .dnsservers import DnsServers
6+
from .dnssearch import DnsSearch
7+
8+
__all__ = ['General', 'Interface', 'Ntp', 'Wireless',
9+
'DnsServers', 'DnsSearch']
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from ...base.converter import BaseConverter
2+
3+
4+
class RaspbianConverter(BaseConverter):
5+
def test_function(self):
6+
return 'Nothing'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from ....utils import get_copy
2+
from .base import RaspbianConverter
3+
4+
5+
class DnsSearch(RaspbianConverter):
6+
netjson_key = 'dns_search'
7+
8+
def to_intermediate(self):
9+
result = []
10+
dns_search = get_copy(self.netjson, self.netjson_key)
11+
for domain in dns_search:
12+
result.append(domain)
13+
return (('dns_search', result),)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from ....utils import get_copy
2+
from .base import RaspbianConverter
3+
4+
5+
class DnsServers(RaspbianConverter):
6+
netjson_key = 'dns_servers'
7+
8+
def to_intermediate(self):
9+
result = []
10+
dns_servers = get_copy(self.netjson, self.netjson_key)
11+
for nameserver in dns_servers:
12+
result.append(nameserver)
13+
return (('dns_servers', result),)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from ....utils import get_copy
2+
from .base import RaspbianConverter
3+
4+
5+
class General(RaspbianConverter):
6+
netjson_key = 'general'
7+
8+
def to_intermediate(self):
9+
result = []
10+
general = get_copy(self.netjson, self.netjson_key)
11+
result.append(general)
12+
return (('general', result),)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from ....utils import get_copy
2+
from .base import RaspbianConverter
3+
from ipaddress import IPv4Interface
4+
5+
6+
class Interfaces(RaspbianConverter):
7+
netjson_key = 'interfaces'
8+
9+
def to_intermediate(self):
10+
result = []
11+
interfaces = get_copy(self.netjson, self.netjson_key)
12+
for interface in interfaces:
13+
new_interface = {}
14+
ifname = interface.get('name')
15+
iftype = interface.get('type')
16+
new_interface.update({
17+
'ifname': ifname,
18+
'iftype': iftype
19+
})
20+
if iftype in ['ethernet', 'bridge', 'loopback']:
21+
addresses = self._get_address(interface)
22+
new_interface.update({
23+
'address': addresses
24+
})
25+
mac = interface.get('mac', False)
26+
if mac:
27+
new_interface.update({'mac': mac})
28+
mtu = interface.get('mtu', False)
29+
if mtu:
30+
new_interface.update({'mtu': mtu})
31+
txqueuelen = interface.get('txqueuelen', False)
32+
if txqueuelen:
33+
new_interface.update({'txqueuelen': txqueuelen})
34+
autostart = interface.get('autostart', False)
35+
if autostart:
36+
new_interface.update({'autostart': autostart})
37+
if iftype == 'wireless' and interface.get('wireless').get('mode') == 'adhoc':
38+
wireless = interface.get('wireless')
39+
new_interface.update({
40+
'essid': wireless.get('ssid'),
41+
'mode': wireless.get('mode')
42+
})
43+
if iftype == 'bridge':
44+
new_interface.update({
45+
'bridge_members': interface.get('bridge_members')
46+
})
47+
result.append(new_interface)
48+
return (('interfaces', result),)
49+
50+
def _get_address(self, interface):
51+
addresses = interface.get('addresses', False)
52+
if addresses:
53+
for address in addresses:
54+
if address.get('proto') == 'static':
55+
if address.get('family') == 'ipv4':
56+
address_mask = str(address.get('address')) + '/' + str(address.get('mask'))
57+
address['netmask'] = IPv4Interface(address_mask).with_netmask.split('/')[1]
58+
del address['mask']
59+
if address.get('family') == 'ipv6':
60+
address['netmask'] = address['mask']
61+
del address['mask']
62+
return addresses
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from ....utils import get_copy
2+
from .base import RaspbianConverter
3+
4+
5+
class Ntp(RaspbianConverter):
6+
netjson_key = 'ntp'
7+
8+
def to_intermediate(self):
9+
result = []
10+
ntp = get_copy(self.netjson, self.netjson_key)
11+
if ntp.get('enabled', False):
12+
for server in ntp.get('server'):
13+
result.append(server)
14+
return (('ntp', result),)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from ....utils import get_copy
2+
from .base import RaspbianConverter
3+
4+
5+
class Wireless(RaspbianConverter):
6+
netjson_key = 'interfaces'
7+
8+
def to_intermediate(self):
9+
result = []
10+
interfaces = get_copy(self.netjson, self.netjson_key)
11+
for interface in interfaces:
12+
if interface['type'] == 'wireless' and interface.get('wireless').get('mode') is not 'adhoc':
13+
new_interface = {
14+
'ifname': interface.get('name'),
15+
'iftype': interface.get('type'),
16+
'ssid': interface['wireless'].get('ssid')
17+
}
18+
wireless = interface.get('wireless')
19+
radio_num = interface['wireless'].get('radio')
20+
radios = get_copy(self.netjson, 'radios')
21+
if radios is not None:
22+
req_radio = [radio for radio in radios if radio['name'] == radio_num][0]
23+
hwmode = self._get_hwmode(req_radio)
24+
channel = req_radio['channel']
25+
protocol = req_radio['protocol'].replace(".", "")
26+
new_interface.update({
27+
'hwmode': hwmode,
28+
'channel': channel,
29+
'protocol': protocol
30+
})
31+
new_interface.update({'encryption': self._get_encryption(wireless)})
32+
result.append(new_interface)
33+
return (('wireless', result),)
34+
35+
def _get_hwmode(self, radio):
36+
protocol = radio['protocol']
37+
if protocol in ['802.11a', '802.11b', '802.11g']:
38+
return protocol[1:]
39+
if radio['channel'] is 0:
40+
return radio.get('hwmode')
41+
elif radio['channel'] <= 13:
42+
return 'g'
43+
else:
44+
return 'a'
45+
46+
def _get_encryption(self, wireless):
47+
encryption = wireless.get('encryption', None)
48+
if encryption is None:
49+
return {}
50+
disabled = encryption.get('disabled', False)
51+
new_encryption = {}
52+
if encryption.get('protocol') is not 'none' and disabled is not True:
53+
protocol, method = encryption.get('protocol').split("_")
54+
if protocol in ['wpa', 'wpa2']:
55+
auth_algs = '1'
56+
wpa = '1' if protocol == 'wpa' else '2'
57+
wpa_key_mgmt = 'WPA-PSK' if method == 'personal' else 'WPA-EAP'
58+
wpa_passphrase = encryption.get('key')
59+
new_encryption.update({
60+
'auth_algs': auth_algs,
61+
'wpa': wpa,
62+
'wpa_key_mgmt': wpa_key_mgmt,
63+
'wpa_passphrase': wpa_passphrase
64+
})
65+
if encryption.get('cipher', None) is not None or 'auto':
66+
wpa_pairwise = str(encryption.get('cipher').replace('+', ' ')).upper()
67+
new_encryption.update({'wpa_pairwise': wpa_pairwise})
68+
return new_encryption

0 commit comments

Comments
 (0)