forked from clalancette/oz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oz-customize
executable file
·116 lines (98 loc) · 3.55 KB
/
oz-customize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# Copyright (C) 2010,2011 Chris Lalancette <[email protected]>
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import getopt
import os
import logging
import stat
import ConfigParser
import oz.TDL
import oz.GuestFactory
def usage():
print "Usage: oz-customize [OPTIONS] <tdl> <libvirt_xml_file>"
print " OPTIONS:"
print " -c <config>\tGet config from <config> (default is /etc/oz/oz.cfg)"
print " -d <level>\tTurn up logging level. The levels are:"
print "\t\t\t0 - errors only (this is the default)"
print "\t\t\t1 - errors and warnings"
print "\t\t\t2 - errors, warnings, and information"
print "\t\t\t3 - all messages"
print "\t\t\t4 - all messages, prepended with the level and classname"
print " -h\t\tPrint this help message"
print " Currently supported architectures are:"
print " i386, x86_64"
print " Currently supported distros are:"
oz.GuestFactory.distrolist()
sys.exit(1)
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'c:d:h', ['config', 'debug',
'help'])
except getopt.GetoptError, err:
print str(err)
usage()
loglevel = logging.ERROR
logformat = "%(message)s"
config_file = "/etc/oz/oz.cfg"
for o, a in opts:
if o in ("-c", "--config"):
config_file = a
elif o in ("-d", "--debug"):
try:
d_int = int(a)
except ValueError:
usage()
if d_int == 0:
loglevel = logging.ERROR
elif d_int == 1:
loglevel = logging.WARNING
elif d_int == 2:
loglevel = logging.INFO
elif d_int == 3:
loglevel = logging.DEBUG
elif d_int >= 4:
loglevel = logging.DEBUG
logformat = logging.BASIC_FORMAT
elif o in ("-h", "--help"):
usage()
else:
assert False, "unhandled option"
if os.geteuid() != 0:
print "%s must be run as root" % (sys.argv[0])
sys.exit(2)
if len(args) != 2:
usage()
try:
tdlfile = args[0]
libvirt_xml_file = args[1]
logging.basicConfig(level=loglevel, format=logformat)
tdl = oz.TDL.TDL(open(tdlfile, 'r').read())
config = ConfigParser.SafeConfigParser()
if os.access(config_file, os.F_OK):
config.read(config_file)
guest = oz.GuestFactory.guest_factory(tdl, config, None)
# Arbitrarily limit the size of the XML file that we will support to 5MB.
# this should be plenty for a normal libvirt XML, and this should prevent us
# from causing OOMs on bogus files
if os.stat(libvirt_xml_file)[stat.ST_SIZE] > (5 * 1024 * 1024):
raise Exception, "libvirt XML file is too big!"
guest.customize(open(libvirt_xml_file, 'r').read())
except Exception, exc:
if loglevel > logging.DEBUG:
print ""
print "ERROR: %s" % (str(exc))
print ""
print "(use -d3 to get the full backtrace)"
print ""
else:
raise