OSDN Git Service

Merge branch 'dev' of ssh://raid.local.hde.co.jp/hde/karesansui/karesansui into dev
[karesansui/karesansui.git] / bin / take_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 time
17 import logging
18 from optparse import OptionParser
19
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
21
22 import __cmd__
23
24 try:
25     import karesansui
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
32 except ImportError:
33     print >>sys.stderr, "[Error] karesansui package was not found."
34     sys.exit(1)
35
36 _ = load_locale()
37
38 usage = '%prog [options]'
39
40 def getopts():
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()
45
46 def chkopts(opts):
47     if not opts.name:
48         raise KssCommandOptException('ERROR: -n or --name option is required.')
49     #if not opts.id:
50     #    raise KssCommandOptException('ERROR: -i or --id option is required.')
51
52 class TakeSnapshot(KssCommand):
53
54     def process(self):
55         (opts, args) = getopts()
56         chkopts(opts)
57         self.up_progress(10)
58
59         xml = None
60         if opts.id:
61             xml = "<domainsnapshot><name>%s</name></domainsnapshot>" % opts.id
62
63         kvs = KaresansuiVirtSnapshot(readonly=False)
64         try:
65             self.up_progress(10)
66             try:
67                 xmlDesc = kvs.createSnapshot(opts.name, xml)
68
69                 self.up_progress(50)
70                 if xmlDesc is not False:
71                     doc = XMLParse(xmlDesc)
72                     snapshot_name = XMLXpath(doc, '/domainsnapshot/name/text()')
73
74                     msg = _("Domain snapshot '%s' created. - domain=%s") % (str(snapshot_name),opts.name,)
75                     self.logger.info(msg)
76                     print >>sys.stdout, msg
77                 else:
78                     msg = _("Failed to create snapshot. - domain=%s") % (opts.name,)
79                     self.logger.error(msg)
80                     raise KssCommandException(msg)
81
82             except KssCommandException, e:
83                 raise KssCommandException(''.join(e.args))
84             except Exception, e:
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)
89
90         finally:
91             kvs.finish()
92
93         return True
94
95 if __name__ == "__main__":
96     target = TakeSnapshot()
97     sys.exit(target.run())