Add proper error handling when the config volume is mounted in a read-only state.

This commit is contained in:
Joseph Schorr 2015-04-02 18:54:09 -04:00
parent bcd8a48159
commit 036c8e56e0
3 changed files with 23 additions and 3 deletions

View file

@ -15,6 +15,7 @@ from auth.permissions import SuperUserPermission
from auth.auth_context import get_authenticated_user
from data.database import User
from util.config.configutil import add_enterprise_config_defaults
from util.config.provider import CannotWriteConfigException
from util.config.validator import validate_service_for_config, SSL_FILENAMES
from data.runmigration import run_alembic_migration

View file

@ -4,6 +4,7 @@ import json
from flask import make_response
from app import app
from util.useremails import CannotSendEmailException
from util.config.provider import CannotWriteConfigException
from data import model
logger = logging.getLogger(__name__)
@ -17,3 +18,11 @@ def handle_dme(ex):
def handle_emailexception(ex):
message = 'Could not send email. Please contact an administrator and report this problem.'
return make_response(json.dumps({'message': message}), 400)
@app.errorhandler(CannotWriteConfigException)
def handle_configexception(ex):
message = ('Configuration could not be written to the mounted volume. \n' +
'Please make sure the mounted volume is not read-only and restart ' +
'the setup process. \n\nIssue: %s' % ex)
return make_response(json.dumps({'message': message}), 400)

View file

@ -6,6 +6,10 @@ from StringIO import StringIO
logger = logging.getLogger(__name__)
class CannotWriteConfigException(Exception):
""" Exception raised when the config cannot be written. """
pass
def _import_yaml(config_obj, config_file):
with open(config_file) as f:
c = yaml.safe_load(f)
@ -24,8 +28,11 @@ def _import_yaml(config_obj, config_file):
def _export_yaml(config_obj, config_file):
with open(config_file, 'w') as f:
f.write(yaml.safe_dump(config_obj, encoding='utf-8', allow_unicode=True))
try:
with open(config_file, 'w') as f:
f.write(yaml.safe_dump(config_obj, encoding='utf-8', allow_unicode=True))
except IOError as ioe:
raise CannotWriteConfigException(str(ioe))
class BaseProvider(object):
@ -116,7 +123,10 @@ class FileConfigProvider(BaseProvider):
return open(os.path.join(self.config_volume, filename), mode)
def save_volume_file(self, filename, flask_file):
flask_file.save(os.path.join(self.config_volume, filename))
try:
flask_file.save(os.path.join(self.config_volume, filename))
except IOError as ioe:
raise CannotWriteConfigException(str(ioe))
def requires_restart(self, app_config):
file_config = self.get_yaml()