]> arthur.barton.de Git - bup.git/blob - cmd/on-cmd.py
cleanup-mounts-under: Don't fail when /proc/mounts isn't readable
[bup.git] / cmd / on-cmd.py
1 #!/usr/bin/env python
2 import sys, os, struct, getopt, subprocess, signal
3 from bup import options, ssh, path
4 from bup.helpers import *
5
6 optspec = """
7 bup on <hostname> index ...
8 bup on <hostname> save ...
9 bup on <hostname> split ...
10 """
11 o = options.Options(optspec, optfunc=getopt.getopt)
12 (opt, flags, extra) = o.parse(sys.argv[1:])
13 if len(extra) < 2:
14     o.fatal('arguments expected')
15
16 class SigException(Exception):
17     def __init__(self, signum):
18         self.signum = signum
19         Exception.__init__(self, 'signal %d received' % signum)
20 def handler(signum, frame):
21     raise SigException(signum)
22
23 signal.signal(signal.SIGTERM, handler)
24 signal.signal(signal.SIGINT, handler)
25
26 sp = None
27 p = None
28 ret = 99
29
30 try:
31     hp = extra[0].split(':')
32     if len(hp) == 1:
33         (hostname, port) = (hp[0], None)
34     else:
35         (hostname, port) = hp
36
37     argv = extra[1:]
38     p = ssh.connect(hostname, port, 'on--server')
39
40     argvs = '\0'.join(['bup'] + argv)
41     p.stdin.write(struct.pack('!I', len(argvs)) + argvs)
42     p.stdin.flush()
43
44     sp = subprocess.Popen([path.exe(), 'server'],
45                           stdin=p.stdout, stdout=p.stdin)
46     p.stdin.close()
47     p.stdout.close()
48
49 finally:
50     while 1:
51         # if we get a signal while waiting, we have to keep waiting, just
52         # in case our child doesn't die.
53         try:
54             ret = p.wait()
55             sp.wait()
56             break
57         except SigException, e:
58             log('\nbup on: %s\n' % e)
59             os.kill(p.pid, e.signum)
60             ret = 84
61 sys.exit(ret)