2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui.
6 # Copyright (C) 2009-2010 HDE, Inc.
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.
17 from optparse import OptionParser
19 from ksscommand import KssCommand, KssCommandException
25 from karesansui import __version__
26 from karesansui.lib.virt.snapshot import KaresansuiVirtSnapshot
27 from karesansui.lib.utils import load_locale
29 print >>sys.stderr, "[Error] karesansui package was not found."
34 usage = '%prog [options]'
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()
44 raise KssCommandOptException('ERROR: -n or --name option is required.')
46 raise KssCommandOptException('ERROR: -i or --id option is required.')
48 class ApplySnapshot(KssCommand):
51 (opts, args) = getopts()
56 kvs = KaresansuiVirtSnapshot(readonly=False)
61 domain = kvs.whichDomain(opts.id)
63 msg = _("Snapshot '%s' not found in domain '%s'.") % (opts.id,opts.name,)
64 self.logger.error(msg)
65 raise KssCommandException(msg)
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)
72 ret = kvs.revertSnapshot(opts.id)
74 msg = _("Can't revert to snapshot '%s'.") % (opts.id,)
75 self.logger.error(msg)
76 raise KssCommandException(msg)
80 msg = _("Domain snapshot '%s' reverted.") % (opts.id,)
82 print >>sys.stderr, msg
84 except KssCommandException, e:
85 raise KssCommandException(''.join(e.args))
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)
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))
99 if __name__ == "__main__":
100 target = ApplySnapshot()
101 sys.exit(target.run())