OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / start_storage_pool.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 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, KssCommandOptException
20
21 import __cmd__
22
23 try:
24     import karesansui
25     from karesansui import __version__
26     from karesansui.lib.virt.virt import KaresansuiVirtConnection
27     from karesansui.lib.utils import load_locale
28     from karesansui.lib.const import STORAGE_POOL_TYPE
29 except ImportError:
30     print >>sys.stderr, "[Error] karesansui package was not found."
31     sys.exit(1)
32
33 _ = load_locale()
34
35 usage = '%prog [options]'
36
37 def getopts():
38     optp = OptionParser(usage=usage, version=__version__)
39     optp.add_option('-n', '--name', dest='name', help=_('Storage pool name'))
40     return optp.parse_args()
41
42 def chkopts(opts):
43     if not opts.name:
44         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
45
46 class StartStorage(KssCommand):
47
48     def process(self):
49         (opts, args) = getopts()
50         chkopts(opts)
51         self.up_progress(10)
52         conn = KaresansuiVirtConnection(readonly=False)
53         
54         try:
55             try:
56                 inactive_storage_pools = conn.list_inactive_storage_pool()
57                 active_storage_pools = conn.list_active_storage_pool()
58
59                 self.up_progress(10)
60
61                 if not (opts.name in active_storage_pools or opts.name in inactive_storage_pools):
62                     raise KssCommandException(
63                         'Specified storage pool does not exist. - pool=%s' % opts.name)
64
65                 if opts.name in active_storage_pools:
66                     raise KssCommandException('Storage pool is already running. - pool=%s' % opts.name)
67
68                 self.up_progress(10)
69                 if conn.start_storage_pool(opts.name) is False:
70                     raise KssCommandException(
71                         'Failed to start storage pool. (libvirt) - pool=%s' % (opts.name))
72
73                 self.up_progress(40)
74
75                 inactive_storage_pools = conn.list_inactive_storage_pool()
76                 active_storage_pools = conn.list_active_storage_pool()
77                 if not (opts.name in active_storage_pools):
78                     raise KssCommandException(
79                         'Could not start the storage pool. - pool=%s' % (opts.name))
80
81                 self.logger.info('Start storage pool. - pool=%s' % (opts.name))
82                 print >>sys.stdout, _('Start storage pool. - pool=%s') % (opts.name)
83                 return True
84             except KssCommandException, e:
85                 raise e
86         finally:
87             conn.close()
88
89 if __name__ == "__main__":
90     target = StartStorage()
91     sys.exit(target.run())