OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / stop_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
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         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
44
45 class StopNetwork(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         try:
54             active_networks = conn.list_active_network()
55             inactive_networks = conn.list_inactive_network()
56             if opts.name in active_networks or opts.name in inactive_networks:
57                 self.up_progress(10)
58                 conn.stop_network(opts.name)
59                 self.up_progress(50)
60
61                 if not opts.name in conn.list_active_network():
62                     self.logger.info('Stopped network. - net=%s' % (opts.name))
63                     print >>sys.stderr, _('Stopped network. - net=%s') % (opts.name)
64
65             else:
66                 raise KssCommandException('Network not found. - net=%s' % (opts.name))
67
68             return True
69         finally:
70             conn.close()
71
72 if __name__ == "__main__":
73     target = StopNetwork()
74     sys.exit(target.run())