This commit is contained in:
Mateusz Gruszczyński
2025-11-01 22:58:41 +01:00
parent b4b9edbc14
commit 2cca3128fe
2 changed files with 32 additions and 21 deletions

View File

@@ -9,6 +9,7 @@ edit_bp = Blueprint('edit', __name__)
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['haproxy_config']
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)
@@ -16,7 +17,8 @@ def edit_haproxy_config():
return render_template( return render_template(
'edit.html', 'edit.html',
config_content=edited_config, config_content=edited_config,
check_output=f"Error writing configuration: {e}" check_output=f"Error writing configuration: {e}",
check_level="danger"
) )
def run_check(): def run_check():
@@ -27,25 +29,31 @@ def edit_haproxy_config():
text=True text=True
) )
out = (result.stdout or '').strip() out = (result.stdout or '').strip()
if result.returncode == 0: if result.returncode == 0:
if not out: if not out:
out = "Configuration file is valid" out = "Configuration file is valid"
level = "success"
if "Warning" in out or "Warnings" in out:
level = "warning"
else: else:
if not out: if not out:
out = f"Check failed with return code {result.returncode}" out = f"Check failed with return code {result.returncode}"
return result.returncode, out level = "danger"
return result.returncode, out, level
check_output = "" check_output = ""
check_level = "success"
if 'save_check' in request.form: if 'save_check' in request.form:
rc, out = run_check() _, check_output, check_level = run_check()
check_output = out
elif 'save_reload' in request.form: elif 'save_reload' in request.form:
rc, out = run_check() rc, out, level = run_check()
check_output = out check_output, check_level = out, level
if rc == 0: if rc == 0:
reload_output = ""
try: try:
supervisor_result = subprocess.run( supervisor_result = subprocess.run(
['pkill', '-f', 'haproxy'], ['pkill', '-f', 'haproxy'],
@@ -55,14 +63,22 @@ def edit_haproxy_config():
timeout=10 timeout=10
) )
if supervisor_result.returncode == 0: if supervisor_result.returncode == 0:
reload_output = f"\n\nHAProxy Restarted:\n{supervisor_result.stdout}" check_output += f"\n\nHAProxy Restarted:\n{supervisor_result.stdout}"
else: else:
reload_output = f"\n\nRestart attempt returned {supervisor_result.returncode}:\n{supervisor_result.stdout}" check_output += (
f"\n\nRestart attempt returned {supervisor_result.returncode}:\n"
f"{supervisor_result.stdout}"
)
except Exception as e: except Exception as e:
reload_output = f"\n\nRestart failed: {e}" check_output += f"\n\nRestart failed: {e}"
check_output += reload_output check_level = "warning"
return render_template('edit.html', config_content=edited_config, check_output=check_output) return render_template(
'edit.html',
config_content=edited_config,
check_output=check_output,
check_level=check_level
)
try: try:
with open('/etc/haproxy/haproxy.cfg', 'r') as f: with open('/etc/haproxy/haproxy.cfg', 'r') as f:

View File

@@ -21,16 +21,11 @@
</div> </div>
</form> </form>
{% if check_output %} {% if check_output %}
<div class="mt-3"> <div class="alert alert-{{ check_level|default('success') }}" role="alert">
{% if 'Fatal errors' in check_output or 'error detected while parsing an' in check_output %} <pre class="mb-0">{{ check_output }}</pre>
<div class="alert alert-danger" role="alert"><pre class="mb-0">{{ check_output }}</pre></div>
{% elif 'Warnings' in check_output %}
<div class="alert alert-warning" role="alert"><pre class="mb-0">{{ check_output }}</pre></div>
{% else %}
<div class="alert alert-success" role="alert"><pre class="mb-0">{{ check_output }}</pre></div>
{% endif %}
</div> </div>
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}