new_functions_and_fixes #1
@@ -8,12 +8,17 @@ edit_bp = Blueprint('edit', __name__)
|
||||
@requires_auth
|
||||
def edit_haproxy_config():
|
||||
if request.method == 'POST':
|
||||
edited_config = request.form['haproxy_config']
|
||||
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,
|
||||
@@ -21,57 +26,80 @@ def edit_haproxy_config():
|
||||
check_level="danger"
|
||||
)
|
||||
|
||||
def run_check():
|
||||
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
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
out = (result.stdout or '').strip()
|
||||
|
||||
check_output = (result.stdout or '').strip()
|
||||
|
||||
if result.returncode == 0:
|
||||
if not out:
|
||||
out = "Configuration file is valid"
|
||||
level = "success"
|
||||
if "Warning" in out or "Warnings" in out:
|
||||
level = "warning"
|
||||
else:
|
||||
if not out:
|
||||
out = f"Check failed with return code {result.returncode}"
|
||||
level = "danger"
|
||||
|
||||
return result.returncode, out, level
|
||||
|
||||
check_output = ""
|
||||
if not check_output:
|
||||
check_output = "Configuration file is valid"
|
||||
check_level = "success"
|
||||
|
||||
if 'save_check' in request.form:
|
||||
_, check_output, check_level = run_check()
|
||||
if "Warning" in check_output or "Warnings" in check_output:
|
||||
check_level = "warning"
|
||||
check_output = f"⚠ {check_output}"
|
||||
else:
|
||||
check_output = f"✓ {check_output}"
|
||||
|
||||
elif 'save_reload' in request.form:
|
||||
rc, out, level = run_check()
|
||||
check_output, check_level = out, level
|
||||
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)
|
||||
|
||||
if rc == 0:
|
||||
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:
|
||||
supervisor_result = subprocess.run(
|
||||
restart_result = subprocess.run(
|
||||
['pkill', '-f', 'haproxy'],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
if supervisor_result.returncode == 0:
|
||||
check_output += f"\n\nHAProxy Restarted:\n{supervisor_result.stdout}"
|
||||
|
||||
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\nRestart attempt returned {supervisor_result.returncode}:\n"
|
||||
f"{supervisor_result.stdout}"
|
||||
)
|
||||
except Exception as e:
|
||||
check_output += f"\n\nRestart failed: {e}"
|
||||
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',
|
||||
@@ -80,12 +108,19 @@ def edit_haproxy_config():
|
||||
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"
|
||||
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"
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user