127 lines
5.4 KiB
Python
127 lines
5.4 KiB
Python
from flask import Blueprint, render_template, request
|
|
import subprocess
|
|
from auth.auth_middleware import requires_auth
|
|
|
|
edit_bp = Blueprint('edit', __name__)
|
|
|
|
@edit_bp.route('/edit', methods=['GET', 'POST'])
|
|
@requires_auth
|
|
def edit_haproxy_config():
|
|
if request.method == 'POST':
|
|
edited_config = request.form.get('haproxy_config', '')
|
|
action = request.form.get('action', 'check')
|
|
|
|
print(f"[EDIT] POST action: {action}", flush=True)
|
|
|
|
try:
|
|
with open('/etc/haproxy/haproxy.cfg', 'w') as f:
|
|
f.write(edited_config)
|
|
print(f"[EDIT] Configuration saved successfully", flush=True)
|
|
except Exception as e:
|
|
print(f"[EDIT] Error writing config: {e}", flush=True)
|
|
return render_template(
|
|
'edit.html',
|
|
config_content=edited_config,
|
|
check_output=f"Error writing configuration: {e}",
|
|
check_level="danger"
|
|
)
|
|
|
|
check_output = ""
|
|
check_level = "success"
|
|
|
|
try:
|
|
result = subprocess.run(
|
|
['haproxy', '-c', '-V', '-f', '/etc/haproxy/haproxy.cfg'],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
timeout=10
|
|
)
|
|
|
|
check_output = (result.stdout or '').strip()
|
|
|
|
if result.returncode == 0:
|
|
if not check_output:
|
|
check_output = "Configuration file is valid"
|
|
check_level = "success"
|
|
|
|
if "Warning" in check_output or "Warnings" in check_output:
|
|
check_level = "warning"
|
|
check_output = f"⚠ {check_output}"
|
|
else:
|
|
check_output = f"✓ {check_output}"
|
|
|
|
print(f"[EDIT] Config validation: SUCCESS", flush=True)
|
|
else:
|
|
if not check_output:
|
|
check_output = f"Check failed with return code {result.returncode}"
|
|
check_output = f"✗ {check_output}"
|
|
check_level = "danger"
|
|
print(f"[EDIT] Config validation: FAILED - {check_output}", flush=True)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
check_output = "✗ Configuration check timed out"
|
|
check_level = "danger"
|
|
print(f"[EDIT] Config validation: TIMEOUT", flush=True)
|
|
except Exception as e:
|
|
check_output = f"✗ Error checking config: {e}"
|
|
check_level = "danger"
|
|
print(f"[EDIT] Config validation ERROR: {e}", flush=True)
|
|
|
|
if action == "save" and check_level == "success":
|
|
print(f"[EDIT] Attempting HAProxy restart...", flush=True)
|
|
try:
|
|
restart_result = subprocess.run(
|
|
['pkill', '-f', 'haproxy'],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
timeout=10
|
|
)
|
|
|
|
if restart_result.returncode == 0 or 'No such process' in restart_result.stdout:
|
|
check_output += "\n\n✓ HAProxy restart signal sent successfully"
|
|
check_output += "\n(supervisord will restart the process)"
|
|
print(f"[EDIT] HAProxy restart successful", flush=True)
|
|
else:
|
|
check_output += f"\n\n⚠ Restart returned code {restart_result.returncode}"
|
|
if restart_result.stdout:
|
|
check_output += f"\nOutput: {restart_result.stdout}"
|
|
check_level = "warning"
|
|
print(f"[EDIT] Restart warning: {restart_result.stdout}", flush=True)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
check_output += "\n\n⚠ Restart command timed out"
|
|
check_level = "warning"
|
|
print(f"[EDIT] Restart TIMEOUT", flush=True)
|
|
except Exception as e:
|
|
check_output += f"\n\n⚠ Restart error: {e}"
|
|
check_level = "warning"
|
|
print(f"[EDIT] Restart ERROR: {e}", flush=True)
|
|
|
|
print(f"[EDIT] Returning check_level={check_level}, output length={len(check_output)}", flush=True)
|
|
|
|
return render_template(
|
|
'edit.html',
|
|
config_content=edited_config,
|
|
check_output=check_output,
|
|
check_level=check_level
|
|
)
|
|
|
|
# GET request - load current config
|
|
try:
|
|
with open('/etc/haproxy/haproxy.cfg', 'r') as f:
|
|
config_content = f.read()
|
|
print(f"[EDIT] Config loaded successfully ({len(config_content)} bytes)", flush=True)
|
|
except FileNotFoundError:
|
|
config_content = "# HAProxy configuration file not found\n# Please create /etc/haproxy/haproxy.cfg\n"
|
|
print(f"[EDIT] Config file not found", flush=True)
|
|
except PermissionError:
|
|
config_content = "# Permission denied reading HAProxy configuration file\n"
|
|
print(f"[EDIT] Permission denied reading config", flush=True)
|
|
except Exception as e:
|
|
config_content = f"# Error reading config: {e}\n"
|
|
print(f"[EDIT] Error reading config: {e}", flush=True)
|
|
|
|
return render_template('edit.html', config_content=config_content)
|