def get(self, session=None): """ Reload Flexget config """ log.info('Reloading config from disk.') try: self.manager.load_config(output_to_console=False) except YAMLError as e: if hasattr(e, 'problem') and hasattr( e, 'context_mark') and hasattr(e, 'problem_mark'): error = {} if e.problem is not None: error.update({'reason': e.problem}) if e.context_mark is not None: error.update({ 'line': e.context_mark.line, 'column': e.context_mark.column }) if e.problem_mark is not None: error.update({ 'line': e.problem_mark.line, 'column': e.problem_mark.column }) raise ApiError(message='Invalid YAML syntax', payload=error) except ValueError as e: errors = [] for er in e.errors: errors.append({ 'error': er.message, 'config_path': er.json_pointer }) raise ApiError('Error loading config: %s' % e.args[0], payload={'errors': errors}) log.info('Config successfully reloaded from disk.') return {}
def post(self, session=None): """ Update config """ data = request.json try: raw_config = base64.b64decode(data['raw_config']) except TypeError: return { 'status': 'error', 'message': 'payload was not a valid base64 encoded string' }, 500 try: config = yaml.safe_load(raw_config) except YAMLError as e: if hasattr(e, 'problem') and hasattr( e, 'context_mark') and hasattr(e, 'problem_mark'): error = {} if e.problem is not None: error.update({'reason': e.problem}) if e.context_mark is not None: error.update({ 'line': e.context_mark.line, 'column': e.context_mark.column }) if e.problem_mark is not None: error.update({ 'line': e.problem_mark.line, 'column': e.problem_mark.column }) raise ApiError(message='Invalid YAML syntax', payload=error) try: self.manager.validate_config(config) except ValueError as e: errors = [] for er in e.errors: errors.append({ 'error': er.message, 'config_path': er.json_pointer }) raise ApiError(message='Error loading config: %s' % e.args[0], payload={'errors': errors}) with open(self.manager.config_path, 'w', encoding='utf-8') as f: f.write(str(raw_config.replace('\r\n', '\n'))) return { 'status': 'success', 'message': 'config successfully updated to file' }
def get(self, session=None): """ Reload Flexget config """ log.info('Reloading config from disk.') try: self.manager.load_config() except ValueError as e: raise ApiError('Error loading config: %s' % e.args[0]) log.info('Config successfully reloaded from disk.') return {}
def get(self, session=None): """ Cache remote resources """ args = cached_parser.parse_args() url = args.get('url') force = args.get('force') try: file_path, mime_type = cached_resource(url, self.manager.config_base, force=force) except RequestException as e: raise BadRequest('Request Error: {}'.format(e.args[0])) except OSError as e: raise ApiError('Error: {}'.format(str(e))) return send_file(file_path, mimetype=mime_type)
def post(self, session=None): """ Update config """ data = request.json try: raw_config = base64.b64decode(data['raw_config']) except (TypeError, binascii.Error): raise BadRequest( message='payload was not a valid base64 encoded string') try: config = yaml.safe_load(raw_config) except YAMLError as e: if hasattr(e, 'problem') and hasattr( e, 'context_mark') and hasattr(e, 'problem_mark'): error = {} if e.problem is not None: error.update({'reason': e.problem}) if e.context_mark is not None: error.update({ 'line': e.context_mark.line, 'column': e.context_mark.column }) if e.problem_mark is not None: error.update({ 'line': e.problem_mark.line, 'column': e.problem_mark.column }) raise BadRequest(message='Invalid YAML syntax', payload=error) try: backup_path = self.manager.update_config(config) except ValueError as e: errors = [] for er in e.errors: errors.append({ 'error': er.message, 'config_path': er.json_pointer }) raise BadRequest(message='Error loading config: %s' % e.args[0], payload={'errors': errors}) try: self.manager.backup_config() except Exception as e: raise ApiError( message= 'Failed to create config backup, config updated but NOT written to file', payload={'reason': str(e)}) try: with open(self.manager.config_path, 'w', encoding='utf-8') as f: f.write(raw_config.decode('utf-8').replace('\r\n', '\n')) except Exception as e: raise ApiError( message= 'Failed to write new config to file, please load from backup', payload={ 'reason': str(e), 'backup_path': backup_path }) return { 'status': 'success', 'message': 'new config loaded and successfully updated to file' }