direct-io.hg

view tools/python/xen/xm/shutdown.py @ 14397:b926f66f29ab

Replace tab with 4 whitespaces in xm/shutdown.py.
Signed-off-by: Masaki Kanno <kanno.masaki@jp.fujitsu.com>
author kfraser@localhost.localdomain
date Thu Mar 15 16:54:00 2007 +0000 (2007-03-15)
parents 192bee4b09a6
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 time
22 from xen.xend import sxp
23 from opts import *
24 from main import server, serverType, SERVER_XEN_API, get_single_vm
25 from xen.xend.XendAPIConstants import *
27 gopts = Opts(use="""[options] [DOM]
29 Shutdown one or more domains gracefully.
30 """)
32 gopts.opt('help', short='h',
33 fn=set_true, default=0,
34 use="Print this help.")
36 gopts.opt('all', short='a',
37 fn=set_true, default=0,
38 use="Shutdown all domains.")
40 gopts.opt('wait', short='w',
41 fn=set_true, default=0,
42 use='Wait for shutdown to complete.')
44 gopts.opt('halt', short='H',
45 fn=set_true, default=0,
46 use='Shutdown without reboot.')
48 gopts.opt('reboot', short='R',
49 fn=set_true, default=0,
50 use='Shutdown and reboot.')
52 def wait_reboot(opts, doms, rcs):
53 if serverType == SERVER_XEN_API:
54 opts.err("Cannot wait for reboot w/ XenAPI (yet)")
56 while doms:
57 alive = server.xend.domains(0)
58 reboot = []
59 for d in doms:
60 if d in alive:
61 rc = server.xend.domain.getRestartCount(d)
62 if rc == rcs[d]: continue
63 reboot.append(d)
64 else:
65 opts.info("Domain %s destroyed for failed in rebooting" % d)
66 doms.remove(d)
67 for d in reboot:
68 opts.info("Domain %s rebooted" % d)
69 doms.remove(d)
70 time.sleep(1)
71 opts.info("All domains rebooted")
73 def wait_shutdown(opts, doms):
74 while doms:
75 if serverType == SERVER_XEN_API:
76 alive = [dom for dom in server.xenapi.VM.get_all()
77 if server.xenapi.VM.get_power_state(dom) ==
78 XEN_API_VM_POWER_STATE[XEN_API_VM_POWER_STATE_RUNNING]]
79 else:
80 alive = server.xend.domains(0)
81 dead = []
82 for d in doms:
83 if d in alive: continue
84 dead.append(d)
85 for d in dead:
86 opts.info("Domain %s terminated" % d)
87 doms.remove(d)
88 time.sleep(1)
89 opts.info("All domains terminated")
91 def shutdown(opts, doms, mode, wait):
92 rcs = {}
93 for d in doms:
94 if serverType == SERVER_XEN_API:
95 if mode == 'halt':
96 server.xenapi.VM.clean_shutdown(d)
97 if mode == 'reboot':
98 server.xenapi.VM.clean_reboot(d)
99 if mode == 'poweroff':
100 server.xenapi.VM.clean_shutdown(d)
101 else:
102 rcs[d] = server.xend.domain.getRestartCount(d)
103 server.xend.domain.shutdown(d, mode)
105 if wait:
106 if mode == 'reboot':
107 wait_reboot(opts, doms, rcs)
108 else:
109 wait_shutdown(opts, doms)
111 def shutdown_mode(opts):
112 if opts.vals.halt and opts.vals.reboot:
113 opts.err("Can't specify halt and reboot")
115 if opts.vals.halt:
116 return 'halt'
117 elif opts.vals.reboot:
118 return 'reboot'
119 else:
120 return 'poweroff'
122 def main_all(opts, args):
123 if serverType == SERVER_XEN_API:
124 doms = [dom for dom in server.xenapi.VM.get_all()
125 if not server.xenapi.VM.get_is_control_domain(dom)]
126 else:
127 doms = server.xend.domains(0)
128 dom0_name = sxp.child_value(server.xend.domain(0), 'name')
129 doms.remove(dom0_name)
130 mode = shutdown_mode(opts)
131 shutdown(opts, doms, mode, opts.vals.wait)
133 def main_dom(opts, args):
134 if len(args) == 0: opts.err('No domain parameter given')
135 if len(args) > 1: opts.err('No multiple domain parameters allowed')
136 dom = args[0]
137 if serverType == SERVER_XEN_API:
138 dom = get_single_vm(dom)
139 mode = shutdown_mode(opts)
140 shutdown(opts, [ dom ], mode, opts.vals.wait)
142 def main(argv):
143 opts = gopts
144 opts.reset()
145 args = opts.parse(argv)
146 if opts.vals.help:
147 return
148 if opts.vals.all:
149 main_all(opts, args)
150 else:
151 main_dom(opts, args)