OSDN Git Service

Add validate
[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 re
17 import logging
18 from optparse import OptionParser
19
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
21 import __cmd__
22
23 try:
24     import karesansui
25     from karesansui import __version__
26     from karesansui.lib.utils import load_locale
27     from karesansui.lib.service.config import ServiceConfigParam
28     from karesansui.lib.service.sysvinit_rh import SysVInit_RH
29     from karesansui.lib.const import SERVICE_XML_FILE
30
31 except ImportError:
32     print >>sys.stderr, "[Error] karesansui package was not found."
33     sys.exit(1)
34
35 _ = load_locale()
36
37 usage = '%prog [options]'
38
39 def getopts():
40     optp = OptionParser(usage=usage, version=__version__)
41     optp.add_option('-n', '--name', dest='name', help=_('Service name'), default=None)
42     optp.add_option('-e', '--enable', dest='enable', action="store_true", help=_('Enable autostart'), default=False)
43     optp.add_option('-d', '--disable', dest='disable', action="store_true", help=_('Disable autostart'), default=False)
44     return optp.parse_args()
45
46 def chkopts(opts):
47     reg = re.compile("[^a-zA-Z0-9\./_-]")
48
49     if opts.name:
50         if reg.search(opts.name):
51             raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-n or --name', opts.name))
52     else:
53         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
54
55     if opts.enable is False and opts.disable is False:
56         raise KssCommandOptException('ERROR: either %s options must be specified.' % '--enable or --disable')
57     if opts.enable is True and opts.disable is True:
58         raise KssCommandOptException('ERROR: %s options are conflicted.' % '--enable and --disable')
59
60 class AutostartService(KssCommand):
61
62     def process(self):
63         (opts, args) = getopts()
64         chkopts(opts)
65         self.up_progress(10)
66
67         config = ServiceConfigParam(SERVICE_XML_FILE)
68         config.load_xml_config()
69
70         service = config.findby1service(opts.name)
71         if service is None:
72             raise KssCommandException("Service not found in xml file. service=%s" % (opts.name))
73
74         self.up_progress(10)
75         sysv = SysVInit_RH(service['system_name'], service['system_command'])
76
77         flag = None
78         if opts.enable:
79             flag = True
80         if opts.disable:
81             flag = False
82
83         retval = sysv.onboot(flag)
84         if not(retval is False) and not(retval is True):
85             raise KssCommandException(str(sysv.error_msg))
86
87         self.up_progress(50)
88         if flag is True:
89             message = 'Enable service. - service=%s' % (opts.name)
90         else:
91             message = 'Disable service. - service=%s' % (opts.name)
92
93         self.logger.info(message)
94         print >>sys.stdout, _(message)
95
96         return True
97
98 if __name__ == "__main__":
99     target = AutostartService()
100     sys.exit(target.run())