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 XenSource Ltd |
---|
16 | #============================================================================ |
---|
17 | |
---|
18 | import os |
---|
19 | import xmlrpclib |
---|
20 | |
---|
21 | from xen.xend import PrettyPrint |
---|
22 | from xen.xend import sxp |
---|
23 | from xen.xend import XendClient |
---|
24 | |
---|
25 | from xen.xm.main import serverType, SERVER_XEN_API |
---|
26 | from xen.xm.xenapi_create import * |
---|
27 | |
---|
28 | from opts import * |
---|
29 | from create import * |
---|
30 | |
---|
31 | def make_unstarted_domain(opts, config): |
---|
32 | """Create an unstarted domain. |
---|
33 | |
---|
34 | @param opts: options |
---|
35 | @param config: configuration |
---|
36 | """ |
---|
37 | try: |
---|
38 | server.xend.domain.new(config) |
---|
39 | except xmlrpclib.Fault, ex: |
---|
40 | import signal |
---|
41 | if vncpid: |
---|
42 | os.kill(vncpid, signal.SIGKILL) |
---|
43 | if ex.faultCode == XendClient.ERROR_INVALID_DOMAIN: |
---|
44 | err("the domain '%s' does not exist." % ex.faultString) |
---|
45 | else: |
---|
46 | err("%s" % ex.faultString) |
---|
47 | except Exception, ex: |
---|
48 | import signal |
---|
49 | if vncpid: |
---|
50 | os.kill(vncpid, signal.SIGKILL) |
---|
51 | err(str(ex)) |
---|
52 | |
---|
53 | |
---|
54 | def main(argv): |
---|
55 | try: |
---|
56 | (opts, config) = parseCommandLine(argv) |
---|
57 | except StandardError, ex: |
---|
58 | err(str(ex)) |
---|
59 | |
---|
60 | if not opts: |
---|
61 | return |
---|
62 | |
---|
63 | if type(config) == str: |
---|
64 | try: |
---|
65 | config = sxp.parse(file(config))[0] |
---|
66 | except IOError, exn: |
---|
67 | raise OptionError("Cannot read file %s: %s" % (config, exn[1])) |
---|
68 | |
---|
69 | if opts.vals.dryrun: |
---|
70 | PrettyPrint.prettyprint(config) |
---|
71 | return |
---|
72 | |
---|
73 | if serverType == SERVER_XEN_API: |
---|
74 | sxp2xml_inst = sxp2xml() |
---|
75 | doc = sxp2xml_inst.convert_sxp_to_xml(config) |
---|
76 | |
---|
77 | xenapi_create_inst = xenapi_create() |
---|
78 | vm_refs = xenapi_create_inst.create(document = doc) |
---|
79 | else: |
---|
80 | make_unstarted_domain(opts, config) |
---|
81 | |
---|
82 | if __name__ == '__main__': |
---|
83 | main(sys.argv) |
---|
84 | |
---|