diff --git a/Dockerfile b/Dockerfile index eb38882..83fffb4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.6.7 +FROM python:3.8 ENV FLASK_APP app.py ENV APP_SETTINGS settings.cfg diff --git a/app.py b/app.py index 44b67e9..166f43e 100644 --- a/app.py +++ b/app.py @@ -88,19 +88,17 @@ def return_code(self): return self._return_code -@app.route('/execute', methods=['POST']) -def execute(): +def _execute(command: str): success = False - req = request.get_json() try: conn = get_conn_through_sentinel() - response = conn.execute_command(*req['command'].split()) + response = conn.execute_command(*command.split()) success = True except (redis.exceptions.ConnectionError, redis.exceptions.ResponseError): try: reload_username_password_from_file_system_if_needed(app) conn = get_conn_through_sentinel() - response = conn.execute_command(*req['command'].split()) + response = conn.execute_command(*command.split()) success = True except Exception as err: response = 'Exception: cannot connect. %s' % str(err) @@ -108,6 +106,13 @@ def execute(): except Exception as err: response = 'Exception: %s' % str(err) app.logger.exception("execute err") + return response, success + + +@app.route('/execute', methods=['POST']) +def execute(): + req = request.get_json() + response, success = _execute(req['command']) return jsonify({ 'response': response, @@ -115,6 +120,26 @@ def execute(): }) +@app.route('/batch_execute', methods=['POST']) +def batch_execute(): + all_succeeded = True + responses = [] + req = request.get_json() + commands = req['commands'] + for command in commands: + response, success = _execute(command) + responses.append({ + 'response': response, + 'success': success, + }) + if not success: + all_succeeded = False + return jsonify({ + 'response': responses, + 'success': all_succeeded + }) + + def reload_username_password_from_file_system_if_needed(app): # It may be that the dynamic password was changed since the config was set if should_read_from_file_system():