-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.py
221 lines (188 loc) · 6.65 KB
/
providers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
__author__ = 'Brian Wiborg <[email protected]>'
__license__ = 'public domain'
__date__ = '2013-12-27'
import os
import logging
import commands
import utils
import exceptions
import ipaddr
from wifi import Cell
DHCP_PROVIDERS = {
'dhcpcd': '/sbin/dhcpcd',
'dhclient': '/sbin/dhclient',
}
class Iface(object):
def __init__(self, name):
self.logger = logging.getLogger('iface')
self.logger.debug('initializing: %s' % name)
if name not in utils.get_available_interfaces():
raise exceptions.UnknownInterface(name)
self.name = name
self.logger.debug('utilizing: %s' % self.name)
def __repr__(self):
return self.name
def is_wired(self):
return self.name.startswith('eth')
def is_wireless(self):
return self.name.startswith('wlan')
def is_virtual(self):
return bool(not self.is_wired() and not self.is_wireless())
def ifup(self):
self.logger.info('ifup: %s' % self.name)
status, output = commands.getstatusoutput(
'ip link set %s up' % self.name
)
if status:
self.logger.error(output)
raise NetconfException(output)
def ifdown(self):
self.logger.info('ifdown: %s' % self.name)
status, output = commands.getstatusoutput(
'ip link set %s down' % self.name
)
if status:
self.logger.error(output)
raise NetconfException(output)
def flush(self):
self.logger.info('flushing addresses: %s' % self.name)
status, output = commands.getstatusoutput(
'ip addr flush dev %s' % self.name
)
if status:
self.logger.error(output)
raise NetconfException(output)
def print_config(self):
print commands.getoutput(
'ip addr show %s | tail -n +3' % self.name
)
print
print ' ' * 4 + commands.getoutput(
'ip r | grep "^default"'
)
print
output = commands.getoutput(
'grep -v "^#" /etc/resolv.conf | grep -v "^$"'
)
for line in output.split('\n'):
print ' ' * 4 + line
class Dhcp(object):
def __init__(self, provider):
self.logger = logging.getLogger('dhcp')
self.logger.debug('inititalizing')
if provider not in DHCP_PROVIDERS:
raise exceptions.UnknownDhcp(provider)
self.provider = DHCP_PROVIDERS[provider]
self.logger.debug('utilizing: %s' % self.provider)
def __call__(self, iface):
if not isinstance(iface, Iface):
raise ValueError(
'must be of type: netconf.providers.Iface'
)
self.logger.info('requesting lease via %s' % self.provider)
status, output = commands.getstatusoutput(
'%s %s' % (self.provider, iface)
)
if status:
self.logger.error(output)
raise exceptions.NetconfException(output)
class Address(object):
def __init__(self, cidr):
self.logger = logging.getLogger('address')
self.logger.debug('initializing...')
self.address = ipaddr.IPNetwork(cidr)
self.logger.debug('utilizing: %s' % self.address)
def __call__(self, iface):
if not isinstance(iface, Iface):
raise ValueError(
'Must be of type: netconf.providers.Iface'
)
iface.ifup()
self.logger.info(
'configuring: %s -> %s' % (self.address, iface)
)
status, output = commands.getstatusoutput(
'ip a a %s dev %s' % (self.address.with_prefixlen, iface)
)
if status:
self.logger.error(output)
raise exceptions.NetconfException(output)
class Gateway(object):
def __init__(self, gateway):
self.logger = logging.getLogger('gateway')
self.logger.debug('initializing...')
self.gateway = ipaddr.IPAddress(gateway)
self.logger.debug('utilizing: %s' % self.gateway)
def __call__(self):
self.logger.info('routing via: %s' % self.gateway)
status, output = commands.getstatusoutput(
'ip r a default via %s' % self.gateway
)
if status:
self.logger.error(output)
raise NetconfException(output)
class Resolver(object):
def __init__(self, address, domain=None, search=None, clear=True):
self.logger = logging.getLogger('resolver')
self.logger.debug('initializing...')
self.address = ipaddr.IPAddress(address)
self.domain = domain
self.search = search
self.clear = clear
def __call__(self):
self.logger.info('resolving via: %s' % self.address)
if self.clear:
fd = open('/etc/resolv.conf', 'w')
fd.write('nameserver %s\n' % self.address)
else:
fd = open('/etc/resolv.conf', 'a')
fd.write('nameserver %s\n' % self.address)
if self.domain:
fd.write('domain %s\n' % self.domain)
if self.search:
fd.write('search %s\n' % self.search)
fd.close()
class Wifi(object):
def __init__(self, essid):
self.logger = logging.getLogger('iwlist')
self.logger.debug('initializing...')
self.essid = essid
def __call__(self, iface):
if not isinstance(iface, Iface):
raise ValueError(
'must be of type: net_conf.providers.Iface'
)
if not iface.is_wireless():
raise TypeError(
'must be of type: wireless'
)
cell = Cell([])
cell.essid = self.essid
cell(iface)
class Wpa(object):
def __init__(self, config, driver='wext'):
self.logger = logging.getLogger('supplicant')
self.logger.debug('initializing...')
if not os.path.exists(config):
raise IOError('Broken path: %s' % config)
self.config = config
self.driver = driver
self.logger.debug('utilizing: %s' % self.config)
def __call__(self, iface):
if not isinstance(iface, Iface):
raise ValueError(
'must be of type: netconf.providers.Iface'
)
if not iface.is_wireless():
raise TypeError('must be of type: wireless')
iface.ifup()
self.logger.info('connecting: %s' % self.config)
status, output = commands.getstatusoutput(
'wpa_supplicant -i %s -D %s -c %s -B' % (iface, self.driver,
self.config)
)
if status:
self.logger.error(output)
raise NetconfException(output)