-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
354 lines (306 loc) · 12.5 KB
/
utils.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from subprocess import Popen, PIPE, STDOUT
from threading import Thread
import netifaces
import logging
import os.path
from configparser import NoOptionError, NoSectionError
import shlex
config = None
class InvalidConf(Exception):
pass
class DiskError(Exception):
pass
# ------------------------------------
# ----- Useful functions -------------
# ------------------------------------
# Run the specified command redirecting all output to logging
# Returns the command return code
def runcmd1(cmd, ign_out=False, ign_err=False, err_to_out=False):
logging.info("Running '%s'" % (" ".join(cmd)))
if ign_out and ign_err:
p = Popen(cmd, stdout=open('/dev/null', 'w'), stderr=STDOUT, encoding='utf8')
elif ign_out:
p = Popen(cmd, stdout=open('/dev/null', 'w'), stderr=PIPE, encoding='utf8')
for eline in p.stderr:
logging.error('['+cmd[0]+'] '+eline.strip())
elif ign_err:
p = Popen(cmd, stdout=PIPE, stderr=open('/dev/null', 'w'), encoding='utf8')
for iline in p.stdout:
logging.info('['+cmd[0]+'] '+iline.strip())
elif err_to_out:
p = Popen(cmd, stdout=PIPE, stderr=STDOUT, encoding='utf8')
for iline in p.stdout:
logging.info('['+cmd[0]+'] '+iline.strip())
else:
p = Popen(cmd, stdout=PIPE, stderr=PIPE, encoding='utf8')
def follow_stderr():
for eline in p.stderr:
logging.error('['+cmd[0]+'] '+eline.strip())
t = Thread(target=follow_stderr)
t.start()
for iline in p.stdout:
logging.info('['+cmd[0]+'] '+iline.strip())
t.join()
p.wait()
return p.returncode
# Run the specified command with specified input redirecting all output to logging
# Returns the command return code
def runcmd2(cmd, cmd_input, ign_out=False, ign_err=False, err_to_out=False):
logging.info("Running '%s' with this input: %s" % (" ".join(cmd), cmd_input.replace("\n", "|")))
if ign_out and ign_err:
p = Popen(cmd, stdin=PIPE, stdout=open('/dev/null', 'w'), stderr=STDOUT, encoding='utf8')
p.stdin.write(cmd_input)
p.stdin.close()
elif ign_out:
p = Popen(cmd, stdin=PIPE, stdout=open('/dev/null', 'w'), stderr=PIPE, encoding='utf8')
p.stdin.write(cmd_input)
p.stdin.close()
for eline in p.stderr:
logging.error('['+cmd[0]+'] '+eline.strip())
elif ign_err:
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=open('/dev/null', 'w'), encoding='utf8')
p.stdin.write(cmd_input)
p.stdin.close()
for iline in p.stdout:
logging.info('['+cmd[0]+'] '+iline.strip())
elif err_to_out:
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, encoding='utf8')
p.stdin.write(cmd_input)
p.stdin.close()
for iline in p.stdout:
logging.info('['+cmd[0]+'] '+iline.strip())
else:
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, encoding='utf8')
p.stdin.write(cmd_input)
p.stdin.close()
def follow_stderr():
for eline in p.stderr:
logging.error('['+cmd[0]+'] '+eline.strip())
t = Thread(target=follow_stderr)
t.start()
for iline in p.stdout:
logging.info('['+cmd[0]+'] '+iline.strip())
t.join()
p.wait()
return p.returncode
# Run the specified command redirecting only stderr to logging
# Returns the process returncode and the complete output (array containing lines)
def runcmd3(cmd, ign_err=False):
logging.info("Running '%s'" % (" ".join(cmd)))
output = []
if ign_err:
p = Popen(cmd, stdout=PIPE, stderr=open('/dev/null', 'w'), encoding='utf8')
for iline in p.stdout:
output.append(iline)
else:
p = Popen(cmd, stdout=PIPE, stderr=PIPE, encoding='utf8')
def follow_stderr():
for eline in p.stderr:
logging.error('['+cmd[0]+'] '+eline.strip())
t = Thread(target=follow_stderr)
t.start()
for iline in p.stdout:
output.append(iline)
t.join()
p.wait()
return p.returncode, output
# write /etc/network/interfaces from configuration
def configure_network():
global config
logging.info('Writing /etc/network/interfaces')
with open('/etc/network/interfaces', 'a') as o:
o.write(gen_network_config())
if config.has_option('dns', 'nameservers') or config.has_option('dns', 'search'):
logging.info('Writing /etc/resolv.conf')
with open('/etc/resolv.conf', 'w') as o:
for ns in get_check_conf('dns', 'nameservers', '').split(','):
o.write('nameserver %s\n' % (ns, ))
if config.has_option('dns', 'search'):
o.write('search %s\n' % (config.get('dns', 'search'), ))
def gen_network_config():
global config
res = ''
for s, i in (('wan', 'eth0'), ('lan', 'eth1')):
res += '\nallow-hotplug %s' % (i, )
p = config.get(s, 'proto').strip()
if p == 'static' and (not config.has_option(s, 'ipaddr') or not config.has_option(s, 'netmask')):
logging.warning('Missing ipaddr or netmask for static configuration of %s ; falling back to DHCP' % (s, ))
p = 'dhcp'
res += '\niface %s inet %s' % (i, p)
if p == 'static':
res += '\n address %s\n netmask %s' % (config.get(s, 'ipaddr').strip(), config.get(s, 'netmask').strip())
if config.has_option(s, 'gateway'):
res += '\n gateway %s' % (config.get(s, 'gateway').strip())
res += '\n'
return res
# Return the relevant numbers from the first address ip found on eth0/eth1
# class C : last number; class B : last two numbers, etc.
def get_first_ip_relevant_numbers():
for i in 'eth0', 'eth1':
addrs = netifaces.ifaddresses(i)
if netifaces.AF_INET not in addrs:
continue
if len(addrs[netifaces.AF_INET]) >= 1 and 'addr' in addrs[netifaces.AF_INET][0]:
ip = addrs[netifaces.AF_INET][0]['addr'].split('.')
nm = addrs[netifaces.AF_INET][0]['netmask'].split('.')
res = []
for r, n in zip(ip, nm):
if n == '0':
res.append(r)
return res
return None
# Detect if we're running on a Bubba|2
def is_b2():
return os.path.exists('/sys/devices/platform/bubbatwo')
# Detect if we're running on a B3
def is_b3():
return os.path.exists('/sys/class/leds/bubba3:blue:active')
if is_b2():
from b2_led_manager import *
elif is_b3():
from b3_led_manager import *
else:
from test_led_manager import *
def loop_ip_forever(error=False):
if not is_b3():
if error:
led_error()
else:
led_rescue()
return # not supported on the b2
ip = None
while True:
if error:
led_error()
else:
led_rescue()
sleep(2)
if not ip:
ip = get_first_ip_relevant_numbers()
if ip:
first = True
for n in ip:
if first:
first = False
else:
b3_set_color('cyan')
sleep(1)
b3_set_color('black')
sleep(0.5)
b3_print_integer(n)
sleep(0.2)
def get_check_conf(section, option, default=None, values=[]):
global config
try:
res = config.get(section, option)
except NoSectionError:
if default is not None:
return default
else:
logging.error('%s section missing !' % (section, ))
raise InvalidConf('%s section missing !' % (section, ))
except NoOptionError:
if default is not None:
return default
else:
logging.error('%s value missing from section %s !' % (option, section))
raise InvalidConf('%s value missing from section %s!' % (option, section))
if values and res not in values:
logging.error('Invalid value for %s in section %s (possible values: %s)' % (option, section, ','.join(values)))
raise InvalidConf('Invalid value for %s in section %s (possible values: %s)'
% (option, section, ','.join(values)))
return res
def getint_check_conf(section, option, default=None, min_value=None, max_value=None):
global config
try:
res = config.getint(section, option)
except NoSectionError:
if default is not None:
return default
else:
logging.error('%s section missing !' % (section, ))
raise InvalidConf('%s section missing !' % (section, ))
except NoOptionError:
if default is not None:
return default
else:
logging.error('%s value missing from section %s!' % (option, section))
raise InvalidConf('%s value missing from section %s!' % (option, section))
except ValueError:
logging.error('Malformed integer value for %s in section %s' % (option, section))
raise InvalidConf('Malformed integer value for %s in section %s' % (option, section))
if min_value and res < min_value:
logging.error('Invalid integer value for %s in section %s (minimum value: %i)' % (option, section, min_value))
raise InvalidConf('Invalid integer value for %s in section %s (minimum value: %i)'
% (option, section, min_value))
if max_value and res > max_value:
logging.error('Invalid integer value for %s in section %s (maximum value: %i)' % (option, section, max_value))
raise InvalidConf('Invalid integer value for %s in section %s (maximum value: %i)'
% (option, section, max_value))
return res
def getfloat_check_conf(section, option, default=None, min_value=None, max_value=None):
global config
try:
res = config.getfloat(section, option)
except NoSectionError:
if default is not None:
return default
else:
logging.error('%s section missing !' % (section, ))
raise InvalidConf('%s section missing !' % (section, ))
except NoOptionError:
if default is not None:
return default
else:
logging.error('%s value missing from section %s!' % (option, section))
raise InvalidConf('%s value missing from section %s!' % (option, section))
except ValueError:
logging.error('Malformed float value for %s in section %s' % (option, section))
raise InvalidConf('Malformed float value for %s in section %s' % (option, section))
if min_value and res < min_value:
logging.error('Invalid float value for %s in section %s (minimum value: %i)' % (option, section, min_value))
raise InvalidConf('Invalid float value for %s in section %s (minimum value: %i)'
% (option, section, min_value))
if max_value and res > max_value:
logging.error('Invalid float value for %s in section %s (maximum value: %i)' % (option, section, max_value))
raise InvalidConf('Invalid float value for %s in section %s (maximum value: %i)'
% (option, section, max_value))
return res
def getboolean_check_conf(section, option, default=None):
global config
try:
return config.getboolean(section, option)
except NoSectionError:
if default is not None:
return default
else:
logging.error('%s section missing !' % (section, ))
raise InvalidConf('%s section missing !' % (section, ))
except NoOptionError:
if default is not None:
return default
else:
logging.error('%s value missing from section %s!' % (option, section))
raise InvalidConf('%s value missing from section %s!' % (option, section))
except ValueError:
logging.error('Malformed booleam value for %s in section %s' % (option, section))
raise InvalidConf('Malformed boolean value for %s in section %s' % (option, section))
def get_blkid_info(dev):
rc, output = runcmd3(['blkid', dev], True)
res = {}
if output:
l = output[0]
for var in shlex.split(l[len(dev)+2:]):
kv = var.split('=')
if kv[1] == 'LVM2_member':
kv[1] = 'lvm'
res[kv[0]] = kv[1]
return res
def sizeof_fmt(num, suffix='B'):
if num == 'full':
return num
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix)