OSDN Git Service

Merge branch 'dev' of ssh://raid.local.hde.co.jp/hde/karesansui/karesansui into dev
[karesansui/karesansui.git] / bin / delete_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 DeleteNetwork(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 not (opts.name in active_networks or opts.name in inactive_networks):
57                 raise KssCommandException('Could not find the specified network. - net=%s' % (opts.name))
58
59             self.up_progress(10)
60             try:
61                 conn.delete_network(opts.name)
62             except:
63                 raise KssCommandException('Failed to delete network. - net=%s' % (opts.name))
64
65             self.up_progress(20)
66             active_networks = conn.list_active_network()
67             inactive_networks = conn.list_inactive_network()
68             if opts.name in active_networks or opts.name in inactive_networks:
69                 raise KssCommandException('Failed to delete the network. - net=%s' % (opts.name))
70
71             self.logger.info('Deleted network. - net=%s' % (opts.name))
72             print >>sys.stdout, _('Deleted network. - net=%s') % (opts.name)
73             return True
74         finally:
75             conn.close()
76
77 if __name__ == "__main__":
78     target = DeleteNetwork()
79     sys.exit(target.run())