OSDN Git Service

ignore inactive pools. (ticket#94)
[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 """ 
15 <description>
16
17 @file:   apply_snapshot.py
18 @author: Taizo ITO <taizo@karesansui-project.info>
19 @copyright:    
20
21 <comment-ja>
22 指定したスナップショットに戻す
23
24  使用方法: apply_snapshot.py [オプション]
25
26   オプション:
27     --version             プログラムのバージョンを表示
28     -h, --help            使用方法を表示
29     -n NAME, --name=NAME  ドメイン名を指定
30     -i ID, --id=ID        スナップショットのIDを指定
31 </comment-ja>
32 <comment-en>
33 Revert to snapshot.
34
35  usage: apply_snapshot.py [options]
36
37   options:
38     --version             show program's version number and exit
39     -h, --help            show this help message and exit
40     -n NAME, --name=NAME  Domain name
41     -i ID, --id=ID        Snapshot serial ID
42 </comment-en>
43 """
44
45 import os
46 import sys
47 import logging
48 from optparse import OptionParser
49
50 from ksscommand import KssCommand, KssCommandException
51
52 import __cmd__
53
54 try:
55     import karesansui
56     from karesansui import __version__
57     from karesansui.lib.virt.snapshot import KaresansuiVirtSnapshot
58     from karesansui.lib.utils import load_locale
59 except ImportError:
60     print >>sys.stderr, "[Error] karesansui package was not found."
61     sys.exit(1)
62
63 _ = load_locale()
64
65 usage = '%prog [options]'
66
67 def getopts():
68     optp = OptionParser(usage=usage, version=__version__)
69     optp.add_option('-n', '--name', dest='name', help=_('Domain name'))
70     optp.add_option('-i', '--id', dest='id', help=_('Snapshot serial ID'))
71     return optp.parse_args()
72
73 def chkopts(opts):
74     if not opts.name:
75         raise KssCommandOptException('ERROR: -n or --name option is required.')
76     if not opts.id:
77         raise KssCommandOptException('ERROR: -i or --id option is required.')
78
79 class ApplySnapshot(KssCommand):
80
81     def process(self):
82         (opts, args) = getopts()
83         chkopts(opts)
84         self.up_progress(10)
85
86
87         kvs = KaresansuiVirtSnapshot(readonly=False)
88         try:
89             self.up_progress(10)
90             try:
91
92                 domain = kvs.whichDomain(opts.id)
93                 if domain is False:
94                     msg = _("Snapshot '%s' not found in domain '%s'.") % (opts.id,opts.name,)
95                     self.logger.error(msg)
96                     raise KssCommandException(msg)
97
98                 if domain != opts.name:
99                     msg = _("Snapshot '%s' not found in domain '%s'.") % (opts.id,opts.name,)
100                     self.logger.error(msg)
101                     raise KssCommandException(msg)
102
103                 ret = kvs.revertSnapshot(opts.id)
104                 if ret is False:
105                     msg = _("Can't revert to snapshot '%s'.") % (opts.id,)
106                     self.logger.error(msg)
107                     raise KssCommandException(msg)
108
109                 self.up_progress(50)
110
111                 msg = _("Domain snapshot '%s' reverted.") % (opts.id,)
112                 self.logger.info(msg)
113                 print >>sys.stderr, msg
114
115             except KssCommandException, e:
116                 raise KssCommandException(''.join(e.args))
117             except Exception, e:
118                 msg = _("Failed to revert to snapshot '%s'.") % (opts.id,)
119                 msg += ": detail %s" % ''.join(str(e.args))
120                 self.logger.error(msg)
121                 raise KssCommandException(msg)
122
123         finally:
124             kvs.finish()
125
126         return True
127
128 if __name__ == "__main__":
129     target = ApplySnapshot()
130     sys.exit(target.run())