ia64/xen-unstable

view tools/python/xen/xm/shutdown.py @ 12810:cf1ca0615414

Added support for configuration file for xm, and use that to specify
contact and authentication details for the Xen-API server.

The default behaviour is still to use the existing legacy XML-RPC server.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Wed Dec 06 11:02:32 2006 +0000 (2006-12-06)
parents a49f9c33aa93
children 6b3bb79cb1c9
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) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 """Domain shutdown.
19 """
20 import time
22 from xen.xend import sxp
23 from opts import *
24 from main import server
26 gopts = Opts(use="""[options] [DOM]
28 Shutdown one or more domains gracefully.
29 """)
31 gopts.opt('help', short='h',
32 fn=set_true, default=0,
33 use="Print this help.")
35 gopts.opt('all', short='a',
36 fn=set_true, default=0,
37 use="Shutdown all domains.")
39 gopts.opt('wait', short='w',
40 fn=set_true, default=0,
41 use='Wait for shutdown to complete.')
43 gopts.opt('halt', short='H',
44 fn=set_true, default=0,
45 use='Shutdown without reboot.')
47 gopts.opt('reboot', short='R',
48 fn=set_true, default=0,
49 use='Shutdown and reboot.')
51 def wait_reboot(opts, doms, rcs):
52 while doms:
53 alive = server.xend.domains(0)
54 reboot = []
55 for d in doms:
56 if d in alive:
57 rc = server.xend.domain.getRestartCount(d)
58 if rc == rcs[d]: continue
59 reboot.append(d)
60 else:
61 opts.info("Domain %s destroyed for failed in rebooting" % d)
62 doms.remove(d)
63 for d in reboot:
64 opts.info("Domain %s rebooted" % d)
65 doms.remove(d)
66 time.sleep(1)
67 opts.info("All domains rebooted")
69 def wait_shutdown(opts, doms):
70 while doms:
71 alive = server.xend.domains(0)
72 dead = []
73 for d in doms:
74 if d in alive: continue
75 dead.append(d)
76 for d in dead:
77 opts.info("Domain %s terminated" % d)
78 doms.remove(d)
79 time.sleep(1)
80 opts.info("All domains terminated")
82 def shutdown(opts, doms, mode, wait):
83 rcs = {}
84 for d in doms:
85 rcs[d] = server.xend.domain.getRestartCount(d)
86 server.xend.domain.shutdown(d, mode)
88 if wait:
89 if mode == 'reboot':
90 wait_reboot(opts, doms, rcs)
91 else:
92 wait_shutdown(opts, doms)
94 def shutdown_mode(opts):
95 if opts.vals.halt and opts.vals.reboot:
96 opts.err("Can't specify halt and reboot")
98 if opts.vals.halt:
99 return 'halt'
100 elif opts.vals.reboot:
101 return 'reboot'
102 else:
103 return 'poweroff'
105 def main_all(opts, args):
106 doms = server.xend.domains(0)
107 dom0_name = sxp.child_value(server.xend.domain(0), 'name')
108 doms.remove(dom0_name)
109 mode = shutdown_mode(opts)
110 shutdown(opts, doms, mode, opts.vals.wait)
112 def main_dom(opts, args):
113 if len(args) == 0: opts.err('No domain parameter given')
114 if len(args) > 1: opts.err('No multiple domain parameters allowed')
115 dom = args[0]
116 mode = shutdown_mode(opts)
117 shutdown(opts, [ dom ], mode, opts.vals.wait)
119 def main(argv):
120 opts = gopts
121 args = opts.parse(argv)
122 if opts.vals.help:
123 return
124 if opts.vals.all:
125 main_all(opts, args)
126 else:
127 main_dom(opts, args)