Add support for loading kolla config from ENV
Implements: blueprint zookeeper Change-Id: Ibabcb5d6d304be9290633338c513f16651d4479a
This commit is contained in:
parent
df0ef8ddb5
commit
2f1c00a467
@ -140,21 +140,38 @@ def set_permissions(data):
|
|||||||
|
|
||||||
|
|
||||||
def load_config():
|
def load_config():
|
||||||
|
def load_from_env():
|
||||||
|
config_raw = os.environ.get("KOLLA_CONFIG")
|
||||||
|
if config_raw is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Attempt to read config
|
||||||
|
try:
|
||||||
|
return json.loads(config_raw)
|
||||||
|
except ValueError:
|
||||||
|
LOG.error('Invalid json for Kolla config')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
def load_from_file():
|
||||||
config_file = '/var/lib/kolla/config_files/config.json'
|
config_file = '/var/lib/kolla/config_files/config.json'
|
||||||
LOG.info('Loading config file at {}'.format(config_file))
|
LOG.info('Loading config file at {}'.format(config_file))
|
||||||
|
|
||||||
# Attempt to read config file
|
# Attempt to read config file
|
||||||
with open(config_file) as f:
|
with open(config_file) as f:
|
||||||
try:
|
try:
|
||||||
config = json.load(f)
|
return json.load(f)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
LOG.error('Invalid json file found at {}'.format(config_file))
|
LOG.error('Invalid json file found at {}'.format(config_file))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
LOG.error('Could not read file {}. Failed with error {}'.format(
|
LOG.error('Could not read file {}. Failed with error {}'
|
||||||
config_file, e))
|
.format(config_file, e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
config = load_from_env()
|
||||||
|
if config is None:
|
||||||
|
config = load_from_file()
|
||||||
|
|
||||||
LOG.info('Validating config file')
|
LOG.info('Validating config file')
|
||||||
validate_config(config)
|
validate_config(config)
|
||||||
|
|
||||||
|
@ -45,3 +45,20 @@ class LoadFromFile(base.BaseTestCase):
|
|||||||
mock.call().__enter__(),
|
mock.call().__enter__(),
|
||||||
mock.call().write(u'/bin/true'),
|
mock.call().write(u'/bin/true'),
|
||||||
mock.call().__exit__(None, None, None)], mo.mock_calls)
|
mock.call().__exit__(None, None, None)], mo.mock_calls)
|
||||||
|
|
||||||
|
|
||||||
|
class LoadFromEnv(base.BaseTestCase):
|
||||||
|
|
||||||
|
def test_load_ok(self):
|
||||||
|
in_config = json.dumps({'command': '/bin/true',
|
||||||
|
'config_files': {}})
|
||||||
|
|
||||||
|
mo = mock.mock_open()
|
||||||
|
with mock.patch.object(set_configs, 'open', mo):
|
||||||
|
with mock.patch.dict('os.environ', {'KOLLA_CONFIG': in_config}):
|
||||||
|
set_configs.load_config()
|
||||||
|
self.assertEqual([mock.call('/run_command', 'w+'),
|
||||||
|
mock.call().__enter__(),
|
||||||
|
mock.call().write(u'/bin/true'),
|
||||||
|
mock.call().__exit__(None, None, None)],
|
||||||
|
mo.mock_calls)
|
||||||
|
Loading…
Reference in New Issue
Block a user