Commit cc8b88f0 authored by Piergiorgio Pancino's avatar Piergiorgio Pancino Committed by Benoit Formet
Browse files

config.static: add option to `reload` in order not to raise exception on some

                    situations, specifically when there is a duplicated key in the same
                    file or in another file.
web.configuration.config_app: reload configuration not raising exception in case of
                    some misconfigurations
parent f883082c
......@@ -90,7 +90,7 @@ class WebConfig(object):
with self.__lock:
cfg = static.get_config()
if self.__new_config:
cfg.reload()
cfg.reload(raise_yaml_exc=False)
self.__new_config = False
return cfg
......@@ -524,7 +524,7 @@ def get_item_config(name):
@web_app.route("/config/reload")
def reload_config():
cfg = __config.get_config()
cfg.reload()
cfg.reload(raise_yaml_exc=False)
event.send(server.__name__, "config_changed")
return flask.json.dumps(
dict(message="Configuration fully reloaded!", type="success")
......
......@@ -515,7 +515,7 @@ class Config:
channels.Bus.clear_cache()
self._connection.close()
def reload(self, base_path=None, timeout=3):
def reload(self, base_path=None, timeout=3, raise_yaml_exc=True):
"""
Reloads the configuration from the bliss server.
......@@ -569,6 +569,8 @@ class Config:
exp.note += "----------------\n"
exp.note += "Hint: You can check your configuration with an on-line YAML validator like http://www.yamllint.com/ \n\n"
exp.problem_mark.name = path
if not raise_yaml_exc:
continue
raise exp
# from ruamel.yaml.parser import ParserError
except ruamel.yaml.error.MarkedYAMLError as exp:
......@@ -614,8 +616,14 @@ class Config:
path,
)
raise RuntimeError(_msg)
self._create_index(local_parent)
parents.append(local_parent)
try:
self._create_index(local_parent)
except ValueError:
if not raise_yaml_exc:
continue
raise
else:
parents.append(local_parent)
else:
parents = Node(self, fs_node, path)
try:
......@@ -623,7 +631,12 @@ class Config:
except TypeError:
_msg = "Parsing error3 on %s in '%s'" % (self._connection, path)
raise RuntimeError(_msg)
self._create_index(parents)
try:
self._create_index(parents)
except ValueError:
if not raise_yaml_exc:
continue
raise
if isinstance(fs_node, MutableSequence):
continue
......
......@@ -12,6 +12,7 @@ import os
import ast
import glob
import itertools
import shutil
def test_home(beacon, config_app_port):
......@@ -43,3 +44,38 @@ def test_db_files(beacon, config_app_port):
set([os.path.join(cfg_test_files_path, f) for f in ast.literal_eval(r.text)])
== db_files
)
def test_duplicated_key_on_new_file(beacon, beacon_directory, config_app_port):
# copy the diode.yml to a new file
diode_path = os.path.join(beacon_directory, "diode.yml")
diode1_path = os.path.join(beacon_directory, "diode1.yml")
shutil.copyfile(diode_path, diode1_path)
r = requests.get("http://localhost:%d/config/reload" % config_app_port)
os.remove(diode1_path)
assert r.status_code == 200 # OK
def test_duplicated_key_on_same_file(beacon, beacon_directory, config_app_port):
file_content = """\
-
name: diode_test
plugin: bliss
class: simulation_diode
independent: True
"""
filename = "diode_duplicated_key.yml"
filepath = os.path.join(beacon_directory, filename)
with open(filepath, "w") as f:
f.write(file_content)
with open(filepath, "w") as f:
f.write(file_content)
f.write(file_content) # write twice to raise key error
r = requests.get("http://localhost:%d/config/reload" % config_app_port)
os.remove(filepath)
assert r.status_code == 200 # OK
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment