84a1f578509c35f66547833fbfdafc6597eea637
1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
24 """Global configuration managed by environment variables."""
29 from collections
import namedtuple
33 from osm_policy_module
.core
.singleton
import Singleton
35 log
= logging
.getLogger(__name__
)
38 class BadConfigError(Exception):
39 """Configuration exception."""
44 class CfgParam(namedtuple('CfgParam', ['key', 'default', 'data_type'])):
45 """Configuration parameter definition."""
47 def value(self
, data
):
48 """Convert a string to the parameter type."""
50 return self
.data_type(data
)
51 except (ValueError, TypeError):
53 'Invalid value "%s" for configuration parameter "%s"' % (
59 """Configuration object."""
62 CfgParam('OSMPOL_MESSAGE_DRIVER', "kafka", six
.text_type
),
63 CfgParam('OSMPOL_MESSAGE_HOST', "localhost", six
.text_type
),
64 CfgParam('OSMPOL_MESSAGE_PORT', 9092, int),
65 CfgParam('OSMPOL_DATABASE_DRIVER', "mongo", six
.text_type
),
66 CfgParam('OSMPOL_DATABASE_HOST', "mongo", six
.text_type
),
67 CfgParam('OSMPOL_DATABASE_PORT', 27017, int),
68 CfgParam('OSMPOL_SQL_DATABASE_URI', "sqlite:///mon_sqlite.db", six
.text_type
),
69 CfgParam('OSMPOL_LOG_LEVEL', "INFO", six
.text_type
),
70 CfgParam('OSMPOL_KAFKA_LOG_LEVEL', "WARN", six
.text_type
),
73 _config_dict
= {cfg
.key
: cfg
for cfg
in _configuration
}
74 _config_keys
= _config_dict
.keys()
77 """Set the default values."""
78 for cfg
in self
._configuration
:
79 setattr(self
, cfg
.key
, cfg
.default
)
82 def read_environ(self
):
83 """Check the appropriate environment variables and update defaults."""
84 for key
in self
._config
_keys
:
86 val
= self
._config
_dict
[key
].data_type(os
.environ
[key
])
87 setattr(self
, key
, val
)
88 except KeyError as exc
:
89 log
.debug("Environment variable not present: %s", exc
)