chiark / gitweb /
adt-virt-schroot: actually become root on testbed if root-on-testbed
[autopkgtest.git] / virt-subproc / adt-virt-schroot
1 #!/usr/bin/python2.4
2 #
3 # adt-virt-schroot is part of autopkgtest
4 # autopkgtest is a tool for testing Debian binary packages
5 #
6 # autopkgtest is Copyright (C) 2006-2007 Canonical Ltd.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #
22 # See the file CREDITS for a full list of credits information (often
23 # installed as /usr/share/doc/autopkgtest/CREDITS).
24
25 import sys
26 import os
27 import string
28 import re as regexp
29 import grp
30 import pwd
31 from optparse import OptionParser
32
33 try: our_base = os.environ['AUTOPKGTEST_BASE']
34 except KeyError: our_base = '/usr/share/autopkgtest';
35 sys.path.insert(1, our_base+'/python')
36
37 import VirtSubproc as vsp
38 capabilities = []
39
40 def parse_args():
41         global schroot, debuglevel
42
43         usage = "%prog [<options>] <schroot>"
44         parser = OptionParser(usage=usage)
45         pa = parser.add_option
46         pe = parser.error
47
48         pa('-d', '--debug', action='store_true', dest='debug');
49
50         (opts,args) = parser.parse_args()
51         if len(args) != 1: pe("need exactly one arg, schroot name")
52
53         schroot = args[0]
54
55         info = vsp.execute('schroot --config -c', [schroot],
56                 downp=False, outp=True)
57         cfg = { }
58         ignore_re = regexp.compile('\#|\[|\s*$')
59         for entry in info.split("\n"):
60                 if ignore_re.match(entry): continue
61                 (key,val) = entry.split("=",2)
62                 cfg[key] = val
63
64         vsp.debuglevel = opts.debug
65
66         if regexp.search('snapshot',cfg['type']):
67                 capabilities.append('revert')
68
69         if [True
70                 for exp_name in cfg['root-users'].split()
71                 for got_uid in [os.getuid()]
72                 if got_uid == pwd.getpwnam(exp_name).pw_uid
73         ] or [True
74                 for exp_name in cfg['root-groups'].split()
75                 for got_gid in [os.getgid()] + os.getgroups()
76                 if got_gid == grp.getgrnam(exp_name).gr_gid
77         ]:
78                 capabilities.append('root-on-testbed')
79
80 def hook_open():
81         global schroot, sessid, downtmp
82         sessid = vsp.execute('schroot -b -c',[schroot], downp=False, outp=True)
83         vsp.down = ['schroot','-r','-c',sessid]
84         if 'root-on-testbed' in capabilities: vsp.down += ['-u','root']
85         vsp.down += ['--','sh','-c']
86         return None
87
88 def hook_cleanup():
89         global schroot, sessid, downtmp
90         vsp.execute('schroot -e -c',[sessid], downp=False)
91
92 def hook_forked_inchild():
93         pass
94
95 def hook_capabilities():
96         return capabilities
97
98 parse_args()
99 vsp.main()