new_functions_and_fixes #1
@@ -8,12 +8,17 @@ edit_bp = Blueprint('edit', __name__)
|
|||||||
@requires_auth
|
@requires_auth
|
||||||
def edit_haproxy_config():
|
def edit_haproxy_config():
|
||||||
if request.method == 'POST':
|
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:
|
try:
|
||||||
with open('/etc/haproxy/haproxy.cfg', 'w') as f:
|
with open('/etc/haproxy/haproxy.cfg', 'w') as f:
|
||||||
f.write(edited_config)
|
f.write(edited_config)
|
||||||
|
print(f"[EDIT] Configuration saved successfully", flush=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print(f"[EDIT] Error writing config: {e}", flush=True)
|
||||||
return render_template(
|
return render_template(
|
||||||
'edit.html',
|
'edit.html',
|
||||||
config_content=edited_config,
|
config_content=edited_config,
|
||||||
@@ -21,57 +26,80 @@ def edit_haproxy_config():
|
|||||||
check_level="danger"
|
check_level="danger"
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_check():
|
check_output = ""
|
||||||
|
check_level = "success"
|
||||||
|
|
||||||
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
['haproxy', '-c', '-V', '-f', '/etc/haproxy/haproxy.cfg'],
|
['haproxy', '-c', '-V', '-f', '/etc/haproxy/haproxy.cfg'],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.STDOUT,
|
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 result.returncode == 0:
|
||||||
if not out:
|
if not check_output:
|
||||||
out = "Configuration file is valid"
|
check_output = "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 = ""
|
|
||||||
check_level = "success"
|
check_level = "success"
|
||||||
|
|
||||||
if 'save_check' in request.form:
|
if "Warning" in check_output or "Warnings" in check_output:
|
||||||
_, check_output, check_level = run_check()
|
check_level = "warning"
|
||||||
|
check_output = f"⚠ {check_output}"
|
||||||
|
else:
|
||||||
|
check_output = f"✓ {check_output}"
|
||||||
|
|
||||||
elif 'save_reload' in request.form:
|
print(f"[EDIT] Config validation: SUCCESS", flush=True)
|
||||||
rc, out, level = run_check()
|
else:
|
||||||
check_output, check_level = out, level
|
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:
|
try:
|
||||||
supervisor_result = subprocess.run(
|
restart_result = subprocess.run(
|
||||||
['pkill', '-f', 'haproxy'],
|
['pkill', '-f', 'haproxy'],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.STDOUT,
|
||||||
text=True,
|
text=True,
|
||||||
timeout=10
|
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:
|
else:
|
||||||
check_output += (
|
check_output += f"\n\n⚠ Restart returned code {restart_result.returncode}"
|
||||||
f"\n\nRestart attempt returned {supervisor_result.returncode}:\n"
|
if restart_result.stdout:
|
||||||
f"{supervisor_result.stdout}"
|
check_output += f"\nOutput: {restart_result.stdout}"
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
check_output += f"\n\nRestart failed: {e}"
|
|
||||||
check_level = "warning"
|
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(
|
return render_template(
|
||||||
'edit.html',
|
'edit.html',
|
||||||
@@ -80,12 +108,19 @@ def edit_haproxy_config():
|
|||||||
check_level=check_level
|
check_level=check_level
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# GET request - load current config
|
||||||
try:
|
try:
|
||||||
with open('/etc/haproxy/haproxy.cfg', 'r') as f:
|
with open('/etc/haproxy/haproxy.cfg', 'r') as f:
|
||||||
config_content = f.read()
|
config_content = f.read()
|
||||||
|
print(f"[EDIT] Config loaded successfully ({len(config_content)} bytes)", flush=True)
|
||||||
except FileNotFoundError:
|
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:
|
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)
|
return render_template('edit.html', config_content=config_content)
|
||||||
|
|||||||
Reference in New Issue
Block a user