OSDN Git Service

Merge branch 'dev' of ssh://raid.local.hde.co.jp/hde/karesansui/karesansui into dev
[karesansui/karesansui.git] / bin / apply_lighttpdconf.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 os.path
16 import sys
17 import logging
18 from optparse import OptionParser
19
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
21
22 import __cmd__
23
24 try:
25     import karesansui
26     from karesansui import __version__
27     from karesansui.lib.utils import load_locale, copy_file, remove_file
28     from karesansui.lib.const import LIGHTTPD_CONF_TEMP_DIR,\
29         LIGHTTPD_PORT_CONFIG, LIGHTTPD_ACCESS_CONFIG, LIGHTTPD_SSL_CONFIG
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     opts = OptionParser(usage=usage, version=__version__)
41     opts.add_option('-d', '--dest', dest='dest', help=('copy destination directory'))
42     opts.add_option('-p', '--port', dest='tmp_port_conf', help=('port.conf temporary file path'))
43     opts.add_option('-s', '--ssl', dest='tmp_ssl_conf', help=('ssl.conf temporary file path'))
44     opts.add_option('-a', '--access', dest='tmp_access_conf', help=('access.conf temporary file path'))
45     return opts.parse_args()
46
47 def chkopts(opts):
48     # option check
49     if not opts.dest:
50         raise KssCommandOptException('ERROR: -d or --dest option is required.')
51     elif not opts.tmp_port_conf:
52         raise KssCommandOptException('ERROR: -p or --port option is required.')
53     elif not opts.tmp_ssl_conf:
54         raise KssCommandOptException('ERROR: -s or --ssl option is required.')
55     elif not opts.tmp_access_conf:
56         raise KssCommandOptException('ERROR: -a or --access option is required.')
57
58     # exist check
59     if os.path.isdir(opts.dest) is False:
60         raise KssCommandOptException('ERROR: Not directory dest=%s' % opts.dest)
61     elif os.path.isfile(opts.tmp_port_conf) is False:
62         raise KssCommandOptException('ERROR: Not exist temporary file tmp_port_conf=%s' % opts.tmp_port_conf)
63     elif os.path.isfile(opts.tmp_ssl_conf) is False:
64         raise KssCommandOptException('ERROR: Not exist temporary file tmp_ssl_conf=%s' % opts.tmp_ssl_conf)
65     elif os.path.isfile(opts.tmp_access_conf) is False:
66         raise KssCommandOptException('ERROR: Not exist temporary file tmp_access_conf=%s' % opts.tmp_access_conf)
67
68 class LighttpdConfigFile(KssCommand):
69
70     def process(self):
71         (opts, args) = getopts()
72         chkopts(opts)
73         opts.dest = opts.dest.rstrip('/')
74         self.up_progress(10)
75
76         tmp_configfiles = {
77             LIGHTTPD_PORT_CONFIG : opts.tmp_port_conf,
78             LIGHTTPD_SSL_CONFIG : opts.tmp_ssl_conf,
79             LIGHTTPD_ACCESS_CONFIG : opts.tmp_access_conf
80         }
81         self.up_progress(10)
82
83         for srcfile in tmp_configfiles.values():
84             if os.path.isfile(srcfile) is False:
85                 raise KssCommandException(
86                     'Temporary config file is not found. -path=%s' % srcfile)
87
88         self.up_progress(20)
89         try:
90             for dest, src in tmp_configfiles.items():
91                 copy_file(src, opts.dest + '/' + dest)
92                 remove_file(src)
93
94             self.up_progress(40)
95         except:
96             raise KssCommandException('Failed to copy config file -src=%s dest=%s' % (src, dest))
97
98         self.logger.info('Applied lighttpd settings.')
99         print >>sys.stdout, _('Applied lighttpd settings.')
100
101         return True
102
103 if __name__ == "__main__":
104     target = LighttpdConfigFile()
105     sys.exit(target.run())