diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index e73ac8f3..cae729d4 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -132,6 +132,20 @@ class Controller: self._computes = {} self._projects = {} + def check_can_write_config(self): + """ + Check if the controller configuration can be written on disk + + :returns: boolean + """ + + try: + os.makedirs(os.path.dirname(self._config_file), exist_ok=True) + if not os.access(self._config_file, os.W_OK): + raise aiohttp.web.HTTPConflict(text="Change rejected, cannot write to controller configuration file '{}'".format(self._config_file)) + except OSError as e: + raise aiohttp.web.HTTPConflict(text="Change rejected: {}".format(e)) + def save(self): """ Save the controller configuration on disk diff --git a/gns3server/controller/template.py b/gns3server/controller/template.py index 968acd97..5fadc2ca 100644 --- a/gns3server/controller/template.py +++ b/gns3server/controller/template.py @@ -177,9 +177,11 @@ class Template: def update(self, **kwargs): - self._settings.update(kwargs) + from gns3server.controller import Controller controller = Controller.instance() + Controller.instance().check_can_write_config() + self._settings.update(kwargs) controller.notification.controller_emit("template.updated", self.__json__()) controller.save() diff --git a/gns3server/controller/template_manager.py b/gns3server/controller/template_manager.py index ecf36a52..b7c14bcb 100644 --- a/gns3server/controller/template_manager.py +++ b/gns3server/controller/template_manager.py @@ -93,8 +93,10 @@ class TemplateManager: except jsonschema.ValidationError as e: message = "JSON schema error adding template with JSON data '{}': {}".format(settings, e.message) raise aiohttp.web.HTTPBadRequest(text=message) - self._templates[template.id] = template + from . import Controller + Controller.instance().check_can_write_config() + self._templates[template.id] = template Controller.instance().save() Controller.instance().notification.controller_emit("template.created", template.__json__()) return template @@ -123,8 +125,9 @@ class TemplateManager: template = self.get_template(template_id) if template.builtin: raise aiohttp.web.HTTPConflict(text="Template ID {} cannot be deleted because it is a builtin".format(template_id)) - self._templates.pop(template_id) from . import Controller + Controller.instance().check_can_write_config() + self._templates.pop(template_id) Controller.instance().save() Controller.instance().notification.controller_emit("template.deleted", template.__json__())