Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 66 lines (49 sloc) 1.726 kb
c51d76b5 » lirazsiri
2012-08-06 implemented restore cache (depends on new duplicity version)
1 import os
2
3 import socket
4 import command
5
6 import signal
7
8 import errno
9
413ce172 » lirazsiri
2012-08-10 fixed location of embedded squid binary + added sanity checking for r…
10 PATH_DEPS = os.environ.get('TKLBAM_DEPS', '/usr/lib/tklbam/deps')
11 SQUID_BIN = os.path.join(PATH_DEPS, "usr/sbin/tklbam-squid")
12
13 def _is_listening(localport):
14 sock = socket.socket()
15 try:
16 sock.connect(('127.0.0.1', localport))
17 return True
18 except socket.error, e:
19 if e.errno == errno.ECONNREFUSED:
20 return False
767facb8 » lirazsiri
2012-08-08 cleanup whitespace
21
413ce172 » lirazsiri
2012-08-10 fixed location of embedded squid binary + added sanity checking for r…
22 def _find_free_port(port_from):
c51d76b5 » lirazsiri
2012-08-06 implemented restore cache (depends on new duplicity version)
23 while True:
24 if _is_listening(port_from) is False:
25 return port_from
26
27 port_from += 1
28
413ce172 » lirazsiri
2012-08-10 fixed location of embedded squid binary + added sanity checking for r…
29 class Error(Exception):
30 pass
31
c51d76b5 » lirazsiri
2012-08-06 implemented restore cache (depends on new duplicity version)
32 class Squid:
33 def __init__(self, cache_size, cache_dir):
34
35 self.cache_size = cache_size
36 self.cache_dir = cache_dir
37 self.address = None
38 self.command = None
39
40 def start(self):
41 os.environ['TKLBAM_SQUID_CACHE_DIR'] = self.cache_dir
42
413ce172 » lirazsiri
2012-08-10 fixed location of embedded squid binary + added sanity checking for r…
43 localport = _find_free_port(33128)
44 self.address = "127.0.0.1:%d" % localport
45 self.command = command.Command((SQUID_BIN, self.address, self.cache_size),
c51d76b5 » lirazsiri
2012-08-06 implemented restore cache (depends on new duplicity version)
46 setpgrp=True, pty=True)
47
413ce172 » lirazsiri
2012-08-10 fixed location of embedded squid binary + added sanity checking for r…
48 def cb():
49 if _is_listening(localport):
50 continue_waiting = False
51 else:
52 continue_waiting = True
53
54 return continue_waiting
55
56 finished = self.command.wait(timeout=10, callback=cb)
57 if not self.command.running or not _is_listening(localport):
58 self.command.terminate()
59 raise Error("%s failed to start\n" % SQUID_BIN + self.command.output)
c51d76b5 » lirazsiri
2012-08-06 implemented restore cache (depends on new duplicity version)
60
61 def stop(self):
62 if self.command:
63 self.command.terminate(gracetime=1, sig=signal.SIGINT)
64
65 def __del__(self):
66 self.stop()
Something went wrong with that request. Please try again.