xen-vtx-unstable

view tools/python/xen/xm/shutdown.py @ 6774:4d899a738d59

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Sep 13 15:05:49 2005 +0000 (2005-09-13)
parents dd668f7527cb
children
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 string
21 import sys
22 import time
24 from xen.xend.XendClient import server
25 from xen.xm.opts import *
27 DOM0_NAME = 'Domain-0'
28 DOM0_ID = '0'
30 gopts = Opts(use="""[options] [DOM]
32 Shutdown one or more domains gracefully.
33 """)
35 gopts.opt('help', short='h',
36 fn=set_true, default=0,
37 use="Print this help.")
39 gopts.opt('all', short='a',
40 fn=set_true, default=0,
41 use="Shutdown all domains.")
43 gopts.opt('wait', short='w',
44 fn=set_true, default=0,
45 use='Wait for shutdown to complete.')
47 gopts.opt('halt', short='H',
48 fn=set_true, default=0,
49 use='Shutdown without reboot.')
51 gopts.opt('reboot', short='R',
52 fn=set_true, default=0,
53 use='Shutdown and reboot.')
55 def shutdown(opts, doms, mode, wait):
56 if doms == None: doms = server.xend_domains()
57 for x in [DOM0_NAME, DOM0_ID]:
58 if x in doms:
59 doms.remove(x)
60 for d in doms:
61 server.xend_domain_shutdown(d, mode)
62 if wait:
63 while doms:
64 alive = server.xend_domains()
65 dead = []
66 for d in doms:
67 if d in alive: continue
68 dead.append(d)
69 for d in dead:
70 opts.info("Domain %s terminated" % d)
71 doms.remove(d)
72 time.sleep(1)
73 opts.info("All domains terminated")
75 def shutdown_mode(opts):
76 mode = 'poweroff'
77 if opts.vals.wait:
78 mode = 'halt'
79 if opts.vals.reboot:
80 opts.err("Can't specify wait and reboot")
81 else:
82 if opts.vals.halt and opts.vals.reboot:
83 opts.err("Can't specify halt and reboot")
84 if opts.vals.halt:
85 mode = 'halt'
86 elif opts.vals.reboot:
87 mode = 'reboot'
88 return mode
90 def main_all(opts, args):
91 mode = shutdown_mode(opts)
92 shutdown(opts, None, mode, opts.vals.wait)
94 def main_dom(opts, args):
95 if len(args) < 1: opts.err('Missing domain')
96 dom = args[0]
97 mode = shutdown_mode(opts)
98 shutdown(opts, [ dom ], mode, opts.vals.wait)
100 def main(argv):
101 opts = gopts
102 args = opts.parse(argv)
103 if opts.vals.help:
104 opts.usage()
105 return
106 if opts.vals.all:
107 main_all(opts, args)
108 else:
109 main_dom(opts, args)