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.
17 from optparse import OptionParser
19 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
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
31 print >>sys.stderr, "[Error] karesansui package was not found."
36 usage = '%prog [options]'
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()
46 raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
48 class RestartNetwork(KssCommand):
51 (opts, args) = getopts()
55 conn = KaresansuiVirtConnection(readonly=False)
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))
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))
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))
77 # try to bring down existing bridge
78 kvn = conn.search_kvn_networks(opts.name)[0]
80 bridge_name = kvn.get_info()['bridge']['name']
84 ret, res = execute_command([NETWORK_IFCONFIG_COMMAND, bridge_name, 'down'])
85 ret, res = execute_command([NETWORK_BRCTL_COMMAND, 'delbr', bridge_name])
88 conn.start_network(opts.name)
91 if not (opts.name in conn.list_active_network()):
92 raise KssCommandException('Failed to start network. - net=%s' % (opts.name))
94 self.logger.info('Restarted network. - net=%s' % (opts.name))
95 print >>sys.stdout, _('Restarted network. - net=%s') % (opts.name)
101 if __name__ == "__main__":
102 target = RestartNetwork()
103 sys.exit(target.run())