OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / restart_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.const import NETWORK_IFCONFIG_COMMAND, NETWORK_BRCTL_COMMAND
28     from karesansui.lib.utils import load_locale
29     from karesansui.lib.utils import execute_command
30 except ImportError:
31     print >>sys.stderr, "[Error] karesansui package was not found."
32     sys.exit(1)
33
34 _ = load_locale()
35
36 usage = '%prog [options]'
37
38 def getopts():
39     optp = OptionParser(usage=usage, version=__version__)
40     optp.add_option('-n', '--name', dest='name', help=_('Network name'))
41     optp.add_option('-f', '--force', dest='force', action="store_true", help=_('Do everything to bring up network'))
42     return optp.parse_args()
43
44 def chkopts(opts):
45     if not opts.name:
46         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
47
48 class RestartNetwork(KssCommand):
49
50     def process(self):
51         (opts, args) = getopts()
52         chkopts(opts)
53         self.up_progress(10)
54
55         conn = KaresansuiVirtConnection(readonly=False)
56         try:
57             active_networks = conn.list_active_network()
58             inactive_networks = conn.list_inactive_network()
59             if not (opts.name in active_networks or opts.name in inactive_networks):
60                 raise KssCommandException('Could not find the specified network. - net=%s' % (opts.name))
61
62             self.up_progress(10)
63             try:
64                 conn.stop_network(opts.name)
65             except KaresansuiVirtException, e:
66                 if opt.force is not True:
67                     raise KssCommandException('Could not stop the specified network. - net=%s' % (opts.name))
68
69             self.up_progress(20)
70
71             try:
72                 conn.start_network(opts.name)
73             except KaresansuiVirtException, e:
74                 if opts.force is not True:
75                     raise KssCommandException('Could not start the specified network. - net=%s' % (opts.name))
76
77                 # try to bring down existing bridge
78                 kvn = conn.search_kvn_networks(opts.name)[0]
79                 try:
80                     bridge_name = kvn.get_info()['bridge']['name']
81                 except KeyError:
82                     pass
83
84                 ret, res = execute_command([NETWORK_IFCONFIG_COMMAND, bridge_name, 'down'])
85                 ret, res = execute_command([NETWORK_BRCTL_COMMAND, 'delbr', bridge_name])
86
87                 # try again
88                 conn.start_network(opts.name)
89
90             self.up_progress(10)
91             if not (opts.name in conn.list_active_network()):
92                 raise KssCommandException('Failed to start network. - net=%s' % (opts.name))
93
94             self.logger.info('Restarted network. - net=%s' % (opts.name))
95             print >>sys.stdout, _('Restarted network. - net=%s') % (opts.name)
96
97             return True
98         finally:
99             conn.close()
100
101 if __name__ == "__main__":
102     target = RestartNetwork()
103     sys.exit(target.run())