comparison twisted/plugins/libervia_server.py @ 1363:c3dac1e11341

server: options can now be specified with environment variables: environment variable are named `LIBERVIA_` + the option name in uppercase. For instance, `LIBERVIA_PASSPHRASE` can be used to set the passphrase of service profile. Variable are set in this order of priority (lowest to highest priority): - `sat.conf` settings - environment variables - arguments specified at command line
author Goffi <goffi@goffi.org>
date Sun, 15 Nov 2020 16:59:55 +0100
parents 2da573bf3f8b
children df40708c4c76
comparison
equal deleted inserted replaced
1362:45ebeea1bacd 1363:c3dac1e11341
97 # but we don't do the conversion here as Twisted expect str 97 # but we don't do the conversion here as Twisted expect str
98 return value 98 return value
99 99
100 100
101 DATA_DIR_DEFAULT = '' 101 DATA_DIR_DEFAULT = ''
102 # prefix used for environment variables
103 ENV_PREFIX = "LIBERVIA_"
102 # options which are in sat.conf and on command line, 104 # options which are in sat.conf and on command line,
103 # see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html 105 # see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html
104 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _("'http', 'https' or 'both' " 106 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _("'http', 'https' or 'both' "
105 "(to launch both servers).").encode('utf-8'), 107 "(to launch both servers).").encode('utf-8'),
106 coerceConnectionType], 108 coerceConnectionType],
127 ['allow_registration', '', True, _('Allow user to register new ' 129 ['allow_registration', '', True, _('Allow user to register new '
128 'account').encode('utf-8'), coerceBool], 130 'account').encode('utf-8'), coerceBool],
129 ['base_url_ext', '', '', 131 ['base_url_ext', '', '',
130 _('The external URL to use as base URL').encode('utf-8'), 132 _('The external URL to use as base URL').encode('utf-8'),
131 coerceUnicode], 133 coerceUnicode],
132 ['dev_mode', 'D', False, _('Developer mode, automatically reload' 134 ['dev_mode', 'D', False, _('Developer mode, automatically reload '
133 'modified pages').encode('utf-8'), coerceBool], 135 'modified pages').encode('utf-8'), coerceBool],
134 ] 136 ]
135 # Options which are in sat.conf only 137 # Options which are in sat.conf only
136 OPT_PARAMETERS_CFG = [ 138 OPT_PARAMETERS_CFG = [
137 ["empty_password_allowed_warning_dangerous_list", None, "", None], 139 ["empty_password_allowed_warning_dangerous_list", None, "", None],
182 config_parser = configparser.ConfigParser() 184 config_parser = configparser.ConfigParser()
183 config_parser.read(C.CONFIG_FILES) 185 config_parser.read(C.CONFIG_FILES)
184 self.handleDeprecated(config_parser) 186 self.handleDeprecated(config_parser)
185 for param in self.optParameters + OPT_PARAMETERS_CFG: 187 for param in self.optParameters + OPT_PARAMETERS_CFG:
186 name = param[0] 188 name = param[0]
189 env_name = f"{ENV_PREFIX}{name.upper()}"
187 try: 190 try:
188 value = config.getConfig(config_parser, C.CONFIG_SECTION, name, Exception) 191 value = os.getenv(env_name)
189 # if isinstance(value, str): 192 if value is None:
190 # value = value.encode("utf-8") 193 value = config.getConfig(
194 config_parser, C.CONFIG_SECTION, name, Exception)
191 try: 195 try:
192 param[2] = param[4](value) 196 param[2] = param[4](value)
193 except IndexError: # the coerce method is optional 197 except IndexError: # the coerce method is optional
194 param[2] = value 198 param[2] = value
195 except (configparser.NoSectionError, configparser.NoOptionError): 199 except (configparser.NoSectionError, configparser.NoOptionError):