ia64/xen-unstable

view tools/python/xen/xm/rmlabel.py @ 11454:956e9aaf88c9

This patch fixes return codes for the acm-related Xen management scripts
(error conditions) and addresses minor issues that 'pycheck' complains
about. It is tested with xm-test and by manually running the xm
commands. It also handles the return case (reported by Masaki Kanno)
that was missed in the originally patch.

Signed-off by: Reiner Sailer <sailer@us.ibm.com>
author Steven Hand <steven@xensource.com>
date Tue Sep 12 11:09:22 2006 +0100 (2006-09-12)
parents 8922c1fbe684
children ad22c711ccb7
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2006 International Business Machines Corp.
16 # Author: Bryan D. Payne <bdpayne@us.ibm.com>
17 #============================================================================
19 """Remove a label from a domain configuration file or a resoruce.
20 """
21 import sys, os, re
22 from xen.util import dictio
23 from xen.util import security
25 def usage():
26 print "\nUsage: xm rmlabel dom <configfile>"
27 print " xm rmlabel res <resource>\n"
28 print " This program removes an acm_label entry from the 'configfile'"
29 print " for a domain or from the global resource label file for a"
30 print " resource. If the label does not exist for the given domain or"
31 print " resource, then rmlabel fails.\n"
32 security.err("Usage")
35 def rm_resource_label(resource):
36 """Removes a resource label from the global resource label file.
37 """
38 # read in the resource file
39 file = security.res_label_filename
40 try:
41 access_control = dictio.dict_read("resources", file)
42 except:
43 security.err("Resource file not found, cannot remove label!")
45 # remove the entry and update file
46 if access_control.has_key(resource):
47 del access_control[resource]
48 dictio.dict_write(access_control, "resources", file)
49 else:
50 security.err("Resource not labeled.")
53 def rm_domain_label(configfile):
54 # open the domain config file
55 fd = None
56 file = None
57 if configfile[0] == '/':
58 fd = open(configfile, "rb")
59 else:
60 for prefix in [".", "/etc/xen"]:
61 file = prefix + "/" + configfile
62 if os.path.isfile(file):
63 fd = open(file, "rb")
64 break
65 if not fd:
66 security.err("Configuration file '"+configfile+"' not found.")
68 # read in the domain config file, removing label
69 ac_entry_re = re.compile("^access_control\s*=.*", re.IGNORECASE)
70 ac_exit_re = re.compile(".*'\].*")
71 file_contents = ""
72 comment = 0
73 removed = 0
74 for line in fd.readlines():
75 if ac_entry_re.match(line):
76 comment = 1
77 if comment:
78 removed = 1
79 line = "#"+line
80 if comment and ac_exit_re.match(line):
81 comment = 0
82 file_contents = file_contents + line
83 fd.close()
85 # send error message if we didn't find anything to remove
86 if not removed:
87 security.err("Domain not labeled.")
89 # write the data back out to the file
90 fd = open(file, "wb")
91 fd.writelines(file_contents)
92 fd.close()
95 def main (argv):
96 try:
97 if len(argv) != 3:
98 usage()
100 if argv[1].lower() == "dom":
101 configfile = argv[2]
102 rm_domain_label(configfile)
103 elif argv[1].lower() == "res":
104 resource = argv[2]
105 rm_resource_label(resource)
106 else:
107 usage()
109 except security.ACMError:
110 sys.exit(-1)
113 if __name__ == '__main__':
114 main(sys.argv)