|
| 1 | +from flask import Flask, render_template_string |
| 2 | +from flask_socketio import SocketIO |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import configparser |
| 6 | + |
| 7 | +app = Flask(__name__) |
| 8 | +socketio = SocketIO(app) |
| 9 | + |
| 10 | +env = os.environ.copy() |
| 11 | +env["PYTHONUNBUFFERED"] = "1" |
| 12 | +env["ANSIBLE_FORCE_COLOR"] = "1" |
| 13 | + |
| 14 | +def read_file_to_string(file_path): |
| 15 | + """ |
| 16 | + Reads the file and returns it as a string. |
| 17 | + |
| 18 | + :param file_path: Path to the file |
| 19 | + :return: The contents of the file as a string |
| 20 | + """ |
| 21 | + try: |
| 22 | + with open(file_path, 'r') as file: |
| 23 | + content = file.read() |
| 24 | + return content |
| 25 | + except FileNotFoundError: |
| 26 | + print(f"Error: File not found at {file_path}") |
| 27 | + return "" |
| 28 | + except Exception as e: |
| 29 | + print(f"An error occurred: {e}") |
| 30 | + return "" |
| 31 | + |
| 32 | +@app.route("/") |
| 33 | +def index(): |
| 34 | + return render_template_string(read_file_to_string("index.html")) |
| 35 | + |
| 36 | +# Global process variable |
| 37 | +globals()["process"] = None |
| 38 | + |
| 39 | +def run_ansible_playbook(playbook_path): |
| 40 | + playbook_cmd = ["ansible-playbook", playbook_path] |
| 41 | + process = subprocess.Popen(playbook_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, cwd=ansible_folder) |
| 42 | + for line in process.stdout: |
| 43 | + socketio.emit("playbook_output", {"output": line}) |
| 44 | + socketio.sleep(0) |
| 45 | + process.wait() |
| 46 | + |
| 47 | +@socketio.on("original_cluster") |
| 48 | +def handle_original_cluster(): |
| 49 | + run_ansible_playbook(ansible_folder + "/1_original_cluster.yml") |
| 50 | + |
| 51 | +@socketio.on("sample_data") |
| 52 | +def handle_sample_data(): |
| 53 | + run_ansible_playbook(ansible_folder + "/2_restore_snapshot.yml") |
| 54 | + |
| 55 | +@socketio.on("start_stress") |
| 56 | +def handle_start_stress(): |
| 57 | + run_ansible_playbook(ansible_folder + "/3_stress.yml") |
| 58 | + |
| 59 | +@socketio.on("scale_out") |
| 60 | +def handle_scale_out(): |
| 61 | + run_ansible_playbook(ansible_folder + "/4_scale_out.yml") |
| 62 | + |
| 63 | +@socketio.on("stop_stress") |
| 64 | +def handle_stop_stress(): |
| 65 | + run_ansible_playbook(ansible_folder + "/5_stop_stress.yml") |
| 66 | + |
| 67 | +@socketio.on("scale_in") |
| 68 | +def handle_scale_in(): |
| 69 | + run_ansible_playbook(ansible_folder + "/6_scale_in.yml") |
| 70 | + |
| 71 | + |
| 72 | +def parse_ansible_inventory(inventory_file): |
| 73 | + config = configparser.ConfigParser(allow_no_value=True) |
| 74 | + config.optionxform = str # Preserve case sensitivity |
| 75 | + |
| 76 | + # Read the inventory file |
| 77 | + config.read(inventory_file) |
| 78 | + |
| 79 | + inventory = {} |
| 80 | + for section in config.sections(): |
| 81 | + hosts = {} |
| 82 | + for item in config.items(section): |
| 83 | + hostname, *variables = item[0].split() |
| 84 | + host_vars = dict(var.split('=') for var in variables) |
| 85 | + hosts[hostname] = host_vars |
| 86 | + inventory[section] = hosts |
| 87 | + |
| 88 | + return inventory |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + program_cwd = os.path.dirname(os.path.abspath(__file__)) |
| 92 | + ansible_folder = os.path.join(program_cwd, "ansible") |
| 93 | + |
| 94 | + socketio.run(app, host='0.0.0.0', port=5000, allow_unsafe_werkzeug=True) |
| 95 | + |
0 commit comments