OSDN Git Service

ignore inactive pools. (ticket#94)
[karesansui/karesansui.git] / bin / autostart_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=_('Domain Name'))
39     optp.add_option('-e', '--enable', dest='enable', action="store_true", help=_('Enable autostart'))
40     optp.add_option('-d', '--disable', dest='disable', action="store_true", help=_('Disable autostart'))
41     return optp.parse_args()
42
43 def chkopts(opts):
44     if not opts.name:
45         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
46     if opts.enable is None and opts.disable is None:
47         raise KssCommandOptException('ERROR: either %s options must be specified.' % '--enable or --disable')
48     if opts.enable is not None and opts.disable is not None:
49         raise KssCommandOptException('ERROR: %s options are conflicted.' % '--enable and --disable')
50
51 class AutostartNetwork(KssCommand):
52
53     def process(self):
54         (opts, args) = getopts()
55         chkopts(opts)
56         self.up_progress(10)
57
58         conn = KaresansuiVirtConnection(readonly=False)
59         try:
60             conn.network.set_network_name(opts.name)
61             flag = None
62             if opts.enable:
63                 flag = True
64             if opts.disable:
65                 flag = False
66             self.up_progress(10)
67             ret = conn.autostart_network(flag)
68             self.up_progress(40)
69         finally:
70             conn.close()
71
72         if ret is False:
73             raise KssCommandException('Failed to set autostart flag. - net=%s flag=%s' % (opts.name,flag))
74
75         self.logger.info('Set autostart flag. - net=%s flag=%s' % (opts.name,flag))
76         print >>sys.stderr, _('Set autostart flag. - net=%s flag=%s') % (opts.name,flag)
77
78         return True
79
80 if __name__ == "__main__":
81     target = AutostartNetwork()
82     sys.exit(target.run())