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.
18 from optparse import OptionParser
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
26 from karesansui import __version__
27 from karesansui.lib.virt.snapshot import KaresansuiVirtSnapshot
28 from karesansui.lib.utils import load_locale
29 from karesansui.lib.utils import get_xml_parse as XMLParse
30 from karesansui.lib.utils import get_xml_xpath as XMLXpath
31 from karesansui.lib.utils import get_nums_xml_xpath as XMLXpathNum
33 print >>sys.stderr, "[Error] karesansui package was not found."
38 usage = '%prog [options]'
41 optp = OptionParser(usage=usage, version=__version__)
42 optp.add_option('-n', '--name', dest='name', help=_('Domain name'))
43 optp.add_option('-i', '--id', dest='id', help=_('Snapshot serial ID'))
44 return optp.parse_args()
48 raise KssCommandOptException('ERROR: -n or --name option is required.')
50 # raise KssCommandOptException('ERROR: -i or --id option is required.')
52 class TakeSnapshot(KssCommand):
55 (opts, args) = getopts()
61 xml = "<domainsnapshot><name>%s</name></domainsnapshot>" % opts.id
63 kvs = KaresansuiVirtSnapshot(readonly=False)
67 xmlDesc = kvs.createSnapshot(opts.name, xml)
70 if xmlDesc is not False:
71 doc = XMLParse(xmlDesc)
72 snapshot_name = XMLXpath(doc, '/domainsnapshot/name/text()')
74 msg = _("Domain snapshot '%s' created. - domain=%s") % (str(snapshot_name),opts.name,)
76 print >>sys.stderr, msg
78 msg = _("Failed to create snapshot. - domain=%s") % (opts.name,)
79 self.logger.error(msg)
80 raise KssCommandException(msg)
82 except KssCommandException, e:
83 raise KssCommandException(''.join(e.args))
85 msg = _("Failed to create snapshot. - domain=%s") % (opts.name,)
86 msg += ": detail %s" % ''.join(e.args)
87 self.logger.error(msg)
88 raise KssCommandException(msg)
95 if __name__ == "__main__":
96 target = TakeSnapshot()
97 sys.exit(target.run())