Mercurial > libervia-web
comparison src/twisted/plugins/libervia_server.py @ 811:b1826adbeeff
server: tls_certificate option:
- renamed ssl_certificate to tls_certificate
- added a method to handle deprecated options
- use C.APP_NAME for config section instead of hardcoded "libervia"
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 20 Dec 2015 19:59:01 +0100 |
parents | 3905bc24eb17 |
children | 6e27604ec95a |
comparison
equal
deleted
inserted
replaced
810:3905bc24eb17 | 811:b1826adbeeff |
---|---|
31 import os.path | 31 import os.path |
32 | 32 |
33 from libervia.server.constants import Const as C | 33 from libervia.server.constants import Const as C |
34 | 34 |
35 from sat.core.i18n import _ | 35 from sat.core.i18n import _ |
36 from sat.tools.config import getConfig | 36 from sat.tools import config |
37 | 37 |
38 from zope.interface import implements | 38 from zope.interface import implements |
39 | 39 |
40 from twisted.python import usage | 40 from twisted.python import usage |
41 from twisted.plugin import IPlugin | 41 from twisted.plugin import IPlugin |
42 from twisted.application.service import IServiceMaker | 42 from twisted.application.service import IServiceMaker |
43 import ConfigParser | |
43 | 44 |
44 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError | 45 |
46 CONFIG_SECTION = C.APP_NAME.lower() | |
45 | 47 |
46 | 48 |
47 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS | 49 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS |
48 allowed_values = ('http', 'https', 'both') | 50 allowed_values = ('http', 'https', 'both') |
49 if value not in allowed_values: | 51 if value not in allowed_values: |
62 DATA_DIR_DEFAULT = '' | 64 DATA_DIR_DEFAULT = '' |
63 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType], | 65 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType], |
64 ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int], | 66 ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int], |
65 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int], | 67 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int], |
66 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int], | 68 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int], |
67 ['ssl_certificate', 'c', 'libervia.pem', _(u'PEM certificate with both private and public parts.').encode('utf-8'), str], | 69 ['tls_certificate', 'c', 'libervia.pem', _(u'TLS certificate public certificate or private key and public certificate combined (PEM format)').encode('utf-8'), str], |
68 ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int], | 70 ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int], |
69 ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int], | 71 ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int], |
70 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str], | 72 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str], |
71 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], | 73 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], |
72 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html | 74 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html |
103 """ | 105 """ |
104 # If we do it the reading later: after the command line options have been parsed, there's no good way to know | 106 # If we do it the reading later: after the command line options have been parsed, there's no good way to know |
105 # if the options values are the hard-coded ones or if they have been passed on the command line. | 107 # if the options values are the hard-coded ones or if they have been passed on the command line. |
106 | 108 |
107 # FIXME: must be refactored + code can be factorised with backend | 109 # FIXME: must be refactored + code can be factorised with backend |
108 config = SafeConfigParser() | 110 config_parser = ConfigParser.SafeConfigParser() |
109 config.read(C.CONFIG_FILES) | 111 config_parser.read(C.CONFIG_FILES) |
112 self.handleDeprecated(config_parser) | |
110 for param in self.optParameters + OPT_PARAMETERS_CFG: | 113 for param in self.optParameters + OPT_PARAMETERS_CFG: |
111 name = param[0] | 114 name = param[0] |
112 try: | 115 try: |
113 value = getConfig(config, 'libervia', name, Exception) | 116 value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception) |
114 try: | 117 try: |
115 param[2] = param[4](value) | 118 param[2] = param[4](value) |
116 except IndexError: # the coerce method is optional | 119 except IndexError: # the coerce method is optional |
117 param[2] = value | 120 param[2] = value |
118 except (NoSectionError, NoOptionError): | 121 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
119 pass | 122 pass |
120 usage.Options.__init__(self) | 123 usage.Options.__init__(self) |
124 | |
125 def handleDeprecated(self, config_parser): | |
126 """display warning and/or change option when a deprecated option if found | |
127 | |
128 param config_parser(ConfigParser): read ConfigParser instance for sat.conf | |
129 """ | |
130 replacements = (('ssl_certificate', 'tls_certificate'),) | |
131 for old, new in replacements: | |
132 try: | |
133 value = config.getConfig(config_parser, CONFIG_SECTION, old, Exception) | |
134 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): | |
135 pass | |
136 else: | |
137 print u"\n/!\\ Use of {old} is deprecated, please use {new} instead\n".format(old=old, new=new) | |
138 config_parser.set(CONFIG_SECTION, new, value) | |
121 | 139 |
122 | 140 |
123 class LiberviaMaker(object): | 141 class LiberviaMaker(object): |
124 implements(IServiceMaker, IPlugin) | 142 implements(IServiceMaker, IPlugin) |
125 | 143 |