New --is-needed option to test whether re6st should be setup or not
[re6stnet.git] / re6st / utils.py
1 import argparse, errno, logging, os, shlex, signal, socket
2 import struct, subprocess, textwrap, threading, time
3 from OpenSSL import crypto
4
5 logging_levels = logging.WARNING, logging.INFO, logging.DEBUG, 5
6
7 class FileHandler(logging.FileHandler):
8
9 _reopen = False
10
11 def release(self):
12 try:
13 if self._reopen:
14 self._reopen = False
15 self.close()
16 self._open()
17 finally:
18 self.lock.release()
19 # In the rare case _reopen is set just before the lock was released
20 if self._reopen and self.lock.acquire(0):
21 self.release()
22
23 def async_reopen(self, *_):
24 self._reopen = True
25 if self.lock.acquire(0):
26 self.release()
27
28 def setupLog(log_level, filename=None, **kw):
29 if log_level and filename:
30 makedirs(os.path.dirname(filename))
31 handler = FileHandler(filename)
32 sig = handler.async_reopen
33 else:
34 handler = logging.StreamHandler()
35 sig = signal.SIG_IGN
36 handler.setFormatter(logging.Formatter(
37 '%(asctime)s %(levelname)-9s %(message)s', '%d-%m-%Y %H:%M:%S'))
38 root = logging.getLogger()
39 root.addHandler(handler)
40 signal.signal(signal.SIGUSR1, sig)
41 if log_level:
42 root.setLevel(logging_levels[log_level-1])
43 else:
44 logging.disable(logging.CRITICAL)
45 logging.addLevelName(5, 'TRACE')
46 logging.trace = lambda *args, **kw: logging.log(5, *args, **kw)
47
48
49 class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
50
51 def _get_help_string(self, action):
52 return super(HelpFormatter, self)._get_help_string(action) \
53 if action.default else action.help
54
55 def _split_lines(self, text, width):
56 """Preserves new lines in option descriptions"""
57 lines = []
58 for text in text.splitlines():
59 lines += textwrap.wrap(text, width)
60 return lines
61
62 def _fill_text(self, text, width, indent):
63 """Preserves new lines in other descriptions"""
64 kw = dict(width=width, initial_indent=indent, subsequent_indent=indent)
65 return '\n'.join(textwrap.fill(t, **kw) for t in text.splitlines())
66
67 class ArgParser(argparse.ArgumentParser):
68
69 class _HelpFormatter(HelpFormatter):
70
71 def _format_actions_usage(self, actions, groups):
72 r = HelpFormatter._format_actions_usage(self, actions, groups)
73 if actions and actions[0].option_strings:
74 r = '[@OPTIONS_FILE] ' + r
75 return r
76
77 _ca_help = "Certificate authority (CA) file in .pem format." \
78 " Serial number defines the prefix of the network."
79
80 def convert_arg_line_to_args(self, arg_line):
81 if arg_line.split('#', 1)[0].rstrip():
82 if arg_line.startswith('@'):
83 yield arg_line
84 return
85 arg_line = shlex.split(arg_line)
86 arg = '--' + arg_line.pop(0)
87 yield arg[arg not in self._option_string_actions:]
88 for arg in arg_line:
89 yield arg
90
91 def __init__(self, **kw):
92 super(ArgParser, self).__init__(formatter_class=self._HelpFormatter,
93 epilog="""Options can be read from a file. For example:
94 $ cat OPTIONS_FILE
95 ca /etc/re6stnet/ca.crt""", **kw)
96
97
98 class Popen(subprocess.Popen):
99
100 def stop(self):
101 self.terminate()
102 t = threading.Timer(5, self.kill)
103 t.start()
104 r = self.wait()
105 t.cancel()
106 return r
107
108
109 def makedirs(path):
110 try:
111 os.makedirs(path)
112 except OSError, e:
113 if e.errno != errno.EEXIST:
114 raise
115
116 def binFromIp(ip):
117 ip1, ip2 = struct.unpack('>QQ', socket.inet_pton(socket.AF_INET6, ip))
118 return bin(ip1)[2:].rjust(64, '0') + bin(ip2)[2:].rjust(64, '0')
119
120
121 def ipFromBin(ip, suffix=''):
122 suffix_len = 128 - len(ip)
123 if suffix_len > 0:
124 ip += suffix.rjust(suffix_len, '0')
125 elif suffix_len:
126 sys.exit("Prefix exceeds 128 bits")
127 return socket.inet_ntop(socket.AF_INET6,
128 struct.pack('>QQ', int(ip[:64], 2), int(ip[64:], 2)))
129
130 def networkFromCa(ca):
131 ca = crypto.load_certificate(crypto.FILETYPE_PEM, ca)
132 return bin(ca.get_serial_number())[3:]
133
134 def subnetFromCert(cert):
135 cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert)
136 return cert.get_subject().CN
137
138 def dump_address(address):
139 return ';'.join(map(','.join, address))
140
141 def parse_address(address_list):
142 for address in address_list.split(';'):
143 try:
144 ip, port, proto = address.split(',')
145 yield ip, str(port), proto
146 except ValueError, e:
147 logging.warning("Failed to parse node address %r (%s)",
148 address, e)
149
150 def binFromSubnet(subnet):
151 p, l = subnet.split('/')
152 return bin(int(p))[2:].rjust(int(l), '0')