OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / apply_snapshot.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
20
21 import __cmd__
22
23 try:
24     import karesansui
25     from karesansui import __version__
26     from karesansui.lib.virt.snapshot import KaresansuiVirtSnapshot
27     from karesansui.lib.utils import load_locale
28 except ImportError:
29     print >>sys.stderr, "[Error] karesansui package was not found."
30     sys.exit(1)
31
32 _ = load_locale()
33
34 usage = '%prog [options]'
35
36 def getopts():
37     optp = OptionParser(usage=usage, version=__version__)
38     optp.add_option('-n', '--name', dest='name', help=_('Domain name'))
39     optp.add_option('-i', '--id', dest='id', help=_('Snapshot serial ID'))
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     if not opts.id:
46         raise KssCommandOptException('ERROR: -i or --id option is required.')
47
48 class ApplySnapshot(KssCommand):
49
50     def process(self):
51         (opts, args) = getopts()
52         chkopts(opts)
53         self.up_progress(10)
54
55
56         kvs = KaresansuiVirtSnapshot(readonly=False)
57         try:
58             self.up_progress(10)
59             try:
60
61                 domain = kvs.whichDomain(opts.id)
62                 if domain is False:
63                     msg = _("Snapshot '%s' not found in domain '%s'.") % (opts.id,opts.name,)
64                     self.logger.error(msg)
65                     raise KssCommandException(msg)
66
67                 if domain != opts.name:
68                     msg = _("Snapshot '%s' not found in domain '%s'.") % (opts.id,opts.name,)
69                     self.logger.error(msg)
70                     raise KssCommandException(msg)
71
72                 ret = kvs.revertSnapshot(opts.id)
73                 if ret is False:
74                     msg = _("Can't revert to snapshot '%s'.") % (opts.id,)
75                     self.logger.error(msg)
76                     raise KssCommandException(msg)
77
78                 self.up_progress(50)
79
80                 msg = _("Domain snapshot '%s' reverted.") % (opts.id,)
81                 self.logger.info(msg)
82                 print >>sys.stderr, msg
83
84             except KssCommandException, e:
85                 raise KssCommandException(''.join(e.args))
86
87             except Exception, e:
88                 msg = _("Failed to revert to snapshot '%s'.") % (opts.id,)
89                 msg += ": detail %s" % ''.join(str(e.args))
90                 self.logger.error(msg)
91                 raise KssCommandException(msg)
92
93             self.logger.info('Complete adaptation of the snapshot. - id=%s,name=%s' % (opts.id, opts.name))
94             print >>sys.stdout, _('Complete adaptation of the snapshot. - id=%s,name=%s' % (opts.id, opts.name))
95             return True
96         finally:
97             kvs.finish()
98
99 if __name__ == "__main__":
100     target = ApplySnapshot()
101     sys.exit(target.run())