OSDN Git Service

Merge branch 'dev' of ssh://raid.local.hde.co.jp/hde/karesansui/karesansui into dev
[karesansui/karesansui.git] / bin / autostart_service.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 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 import __cmd__
21
22 try:
23     import karesansui
24     from karesansui import __version__
25     from karesansui.lib.utils import load_locale
26     from karesansui.lib.service.config import ServiceConfigParam
27     from karesansui.lib.service.sysvinit_rh import SysVInit_RH
28     from karesansui.lib.const import SERVICE_XML_FILE
29
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=_('Service name'))
41     optp.add_option('-e', '--enable', dest='enable', action="store_true", help=_('Enable autostart'))
42     optp.add_option('-d', '--disable', dest='disable', action="store_true", help=_('Disable autostart'))
43     return optp.parse_args()
44
45 def chkopts(opts):
46     if opts.name is None:
47         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
48     if opts.enable is None and opts.disable is None:
49         raise KssCommandOptException('ERROR: either %s options must be specified.' % '--enable or --disable')
50     if opts.enable is not None and opts.disable is not None:
51         raise KssCommandOptException('ERROR: %s options are conflicted.' % '--enable and --disable')
52
53 class AutostartService(KssCommand):
54
55     def process(self):
56         (opts, args) = getopts()
57         chkopts(opts)
58         self.up_progress(10)
59         config = ServiceConfigParam(SERVICE_XML_FILE)
60         config.load_xml_config()
61
62         service = config.findby1service(opts.name)
63         self.up_progress(10)
64         sysv = SysVInit_RH(service['system_name'], service['system_command'])
65
66         flag = None
67         if opts.enable:
68             flag = True
69         if opts.disable:
70             flag = False
71
72         #import pdb; pdb.set_trace()
73
74         retval = sysv.onboot(flag)
75         if not(retval is False) and not(retval is True):
76             raise KssCommandException(str(sysv.error_msg))
77
78         self.up_progress(50)
79         if flag is True:
80             message = 'Enable service. - service=%s' % (opts.name)
81         else:
82             message = 'Disable service. - service=%s' % (opts.name)
83
84         self.logger.info(message)
85         print >>sys.stdout, _(message)
86
87         return True
88
89 if __name__ == "__main__":
90     target = AutostartService()
91     sys.exit(target.run())