Skip to content
Snippets Groups Projects
Commit b18b73a1 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Common - Settings:

- Corrected methods wait_for_environment_variables and find_missing_environment_variables
parent 7e314a8c
No related branches found
No related tags found
1 merge request!142Release TeraFlowSDN 2.1
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
# limitations under the License. # limitations under the License.
import logging, os, time import logging, os, time
from typing import List from typing import Dict, List
from common.Constants import ( from common.Constants import (
DEFAULT_GRPC_BIND_ADDRESS, DEFAULT_GRPC_GRACE_PERIOD, DEFAULT_GRPC_MAX_WORKERS, DEFAULT_HTTP_BIND_ADDRESS, DEFAULT_GRPC_BIND_ADDRESS, DEFAULT_GRPC_GRACE_PERIOD, DEFAULT_GRPC_MAX_WORKERS, DEFAULT_HTTP_BIND_ADDRESS,
DEFAULT_LOG_LEVEL, DEFAULT_METRICS_PORT, DEFAULT_SERVICE_GRPC_PORTS, DEFAULT_SERVICE_HTTP_BASEURLS, DEFAULT_LOG_LEVEL, DEFAULT_METRICS_PORT, DEFAULT_SERVICE_GRPC_PORTS, DEFAULT_SERVICE_HTTP_BASEURLS,
...@@ -37,23 +37,24 @@ ENVVAR_SUFIX_SERVICE_HOST = 'SERVICE_HOST' ...@@ -37,23 +37,24 @@ ENVVAR_SUFIX_SERVICE_HOST = 'SERVICE_HOST'
ENVVAR_SUFIX_SERVICE_PORT_GRPC = 'SERVICE_PORT_GRPC' ENVVAR_SUFIX_SERVICE_PORT_GRPC = 'SERVICE_PORT_GRPC'
ENVVAR_SUFIX_SERVICE_PORT_HTTP = 'SERVICE_PORT_HTTP' ENVVAR_SUFIX_SERVICE_PORT_HTTP = 'SERVICE_PORT_HTTP'
def find_missing_environment_variables( def find_environment_variables(
required_environment_variables : List[str] = [] environment_variable_names : List[str] = []
) -> List[str]: ) -> Dict[str, str]:
if ENVVAR_KUBERNETES_PORT in os.environ: environment_variable : Dict[str, str] = dict()
missing_variables = set(required_environment_variables).difference(set(os.environ.keys())) for name in environment_variable_names:
else: if name not in os.environ: continue
# We're not running in Kubernetes, nothing to wait for environment_variable[name] = os.environ[name]
missing_variables = required_environment_variables return environment_variable
return missing_variables
def wait_for_environment_variables( def wait_for_environment_variables(
required_environment_variables : List[str] = [], wait_delay_seconds : float = DEFAULT_RESTART_DELAY required_environment_variables : List[str] = [], wait_delay_seconds : float = DEFAULT_RESTART_DELAY
): ) -> None:
missing_variables = find_missing_environment_variables(required_environment_variables) if ENVVAR_KUBERNETES_PORT not in os.environ: return # Not running in Kubernetes
if len(missing_variables) == 0: return # We have all environment variables defined found = find_environment_variables(required_environment_variables)
msg = 'Variables({:s}) are missing in Environment({:s}), restarting in {:f} seconds...' missing = set(required_environment_variables).difference(set(found.keys()))
LOGGER.error(msg.format(str(missing_variables), str(os.environ), wait_delay_seconds)) if len(missing) == 0: return # We have all environment variables defined
MSG = 'Variables({:s}) are missing in Environment({:s}), restarting in {:f} seconds...'
LOGGER.error(MSG.format(str(missing), str(os.environ), wait_delay_seconds))
time.sleep(wait_delay_seconds) time.sleep(wait_delay_seconds)
raise Exception('Restarting...') # pylint: disable=broad-exception-raised raise Exception('Restarting...') # pylint: disable=broad-exception-raised
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment