OSDN Git Service

Merge branch 'dev' of ssh://raid.local.hde.co.jp/hde/karesansui/karesansui into dev
[karesansui/karesansui.git] / bin / suspend_guest.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 2009-2010 HDE, Inc.
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13
14 import os
15 import sys
16 import logging
17 from optparse import OptionParser
18
19 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
20
21 import __cmd__
22
23 try:
24     import karesansui
25     from karesansui import __version__
26     from karesansui.lib.virt.virt import KaresansuiVirtConnection, \
27                  VIR_DOMAIN_PAUSED
28     from karesansui.lib.utils import load_locale
29 except ImportError:
30     print >>sys.stderr, "[Error] karesansui package was not found."
31     sys.exit(1)
32
33 _ = load_locale()
34
35 usage = '%prog [options]'
36
37 def getopts():
38     optp = OptionParser(usage=usage, version=__version__)
39     optp.add_option('-n', '--name', dest='name', help=_('Domain Name'))
40     return optp.parse_args()
41
42 def chkopts(opts):
43     if not opts.name:
44         raise KssCommandOptException('ERROR: -n or --name option is required.')
45
46 class SuspendGuest(KssCommand):
47
48     def process(self):
49         (opts, args) = getopts()
50         chkopts(opts)
51         self.up_progress(10)
52
53         conn = KaresansuiVirtConnection(readonly=False)
54         try:
55             conn.set_domain_name(opts.name)
56
57             active_guests = conn.list_active_guest()
58             inactive_guests = conn.list_inactive_guest()
59
60             if opts.name in active_guests or opts.name in inactive_guests:
61                 try:
62                     self.up_progress(10)
63                     conn.suspend_guest()
64                     self.up_progress(40)
65                 except:
66                     self.logger.error('Failed to suspend guest. - dom=%s' % (opts.name))
67                     raise
68
69                 self.up_progress(10)
70                 status = conn.guest.status()
71                 self.up_progress(10)
72                 if status == VIR_DOMAIN_PAUSED:
73                     self.logger.info('Succeeded to suspend guest. - dom=%s' % (opts.name))
74                     print >>sys.stderr, _('Succeeded to suspend guest. - dom=%s') % (opts.name)
75
76             else:
77                 raise KssCommandException(
78                     'Could not find guest. - dom=%s' % (opts.name))
79
80             return True
81                 
82         finally:
83             conn.close()
84
85
86 if __name__ == "__main__":
87     target = SuspendGuest()
88     sys.exit(target.run())