chiark / gitweb /
adt-virt-schroot: better handling of root-users/groups (#667001)
[autopkgtest.git] / virt-subproc / adt-virt-schroot
1 #!/usr/bin/python2.6
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']+'/lib'
34 except KeyError: our_base = '/usr/share/autopkgtest/python';
35 sys.path.insert(1, our_base)
36
37 import VirtSubproc as vsp
38 capabilities = []
39
40 def pw_uid(exp_name):
41         try:
42                 return pwd.getpwnam(exp_name).pw_uid
43         except KeyError:
44                 return None
45
46 def gr_gid(exp_name):
47         try:
48                 return grp.getgrnam(exp_name).gr_gid
49         except KeyError:
50                 return None
51
52 def match(exp_names, ids, extract_id):
53         for exp_name in [n for n in exp_names.split(',') if n]:
54                 if extract_id(exp_name) in ids:
55                         return True
56         return False
57
58 def parse_args():
59         global schroot, debuglevel
60
61         usage = "%prog [<options>] <schroot>"
62         parser = OptionParser(usage=usage)
63         pa = parser.add_option
64         pe = parser.error
65
66         pa('-d', '--debug', action='store_true', dest='debug');
67
68         (opts,args) = parser.parse_args()
69         if len(args) != 1: pe("need exactly one arg, schroot name")
70
71         schroot = args[0]
72
73         info = vsp.execute('schroot --config -c', [schroot],
74                 downp=False, outp=True)
75         cfg = { }
76         ignore_re = regexp.compile('\#|\[|\s*$')
77         for entry in info.split("\n"):
78                 if ignore_re.match(entry): continue
79                 (key,val) = entry.split("=",2)
80                 cfg[key] = val
81
82         vsp.debuglevel = opts.debug
83
84         if regexp.search('snapshot',cfg['type']):
85                 capabilities.append('revert')
86
87         if (match(cfg['root-users'], [os.getuid()], pw_uid) or
88             match(cfg['root-groups'], [os.getgid()] + os.getgroups(), gr_gid)):
89                 capabilities.append('root-on-testbed')
90
91 def hook_open():
92         global schroot, sessid, downtmp
93         sessid = vsp.execute('schroot -b -c',[schroot], downp=False, outp=True)
94         vsp.down = ['schroot','-r','-d','/','-c',sessid]
95         if 'root-on-testbed' in capabilities: vsp.down += ['-u','root']
96         vsp.down += ['--']
97         vsp.downkind = 'auxverb'
98
99 def hook_downtmp():
100         return vsp.downtmp_mktemp()
101
102 def hook_revert():
103         vsp.downtmp_remove()
104         hook_cleanup()
105         hook_open()
106
107 def hook_cleanup():
108         global schroot, sessid, downtmp
109         vsp.downtmp_remove()
110         vsp.execute('schroot -e -c',[sessid], downp=False)
111
112 def hook_forked_inchild():
113         pass
114
115 def hook_capabilities():
116         return capabilities
117
118 parse_args()
119 vsp.main()