OSDN Git Service

Merge branch 'dev' of ssh://raid.local.hde.co.jp/hde/karesansui/karesansui into dev
[karesansui/karesansui.git] / bin / start_network.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 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, KaresansuiVirtException
27     from karesansui.lib.utils import load_locale
28 except ImportError:
29     print >>sys.stderr, "[Error] karesansui package was not found."
30     sys.exit(1)
31
32 _ = load_locale()
33
34 usage = '%prog [options]'
35
36 def getopts():
37     optp = OptionParser(usage=usage, version=__version__)
38     optp.add_option('-n', '--name', dest='name', help=_('Network name'))
39     return optp.parse_args()
40
41 def chkopts(opts):
42     if not opts.name:
43         KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
44
45 class StartNetwork(KssCommand):
46
47     def process(self):
48         (opts, args) = getopts()
49         chkopts(opts)
50         self.up_progress(10)
51
52         conn = KaresansuiVirtConnection(readonly=False)
53
54         try:
55             active_networks = conn.list_active_network()
56             inactive_networks = conn.list_inactive_network()
57             if opts.name in active_networks or opts.name in inactive_networks:
58                 self.up_progress(10)
59                 conn.start_network(opts.name)
60                 self.up_progress(50)
61                 
62                 if opts.name in conn.list_active_network():
63                     self.logger.info('Started network. - net=%s' % (opts.name))
64                     print >>sys.stderr, _('Started network. - net=%s') % (opts.name)
65
66             else:
67                 raise KssCommandException('network not found. - net=%s' % (opts.name))
68
69             return True
70         finally:
71             conn.close()
72
73 if __name__ == "__main__":
74     target = StartNetwork()
75     sys.exit(target.run())