redactor
This commit is contained in:
@@ -9,6 +9,7 @@ edit_bp = Blueprint('edit', __name__)
|
||||
def edit_haproxy_config():
|
||||
if request.method == 'POST':
|
||||
edited_config = request.form['haproxy_config']
|
||||
|
||||
try:
|
||||
with open('/etc/haproxy/haproxy.cfg', 'w') as f:
|
||||
f.write(edited_config)
|
||||
@@ -16,7 +17,8 @@ def edit_haproxy_config():
|
||||
return render_template(
|
||||
'edit.html',
|
||||
config_content=edited_config,
|
||||
check_output=f"Error writing configuration: {e}"
|
||||
check_output=f"Error writing configuration: {e}",
|
||||
check_level="danger"
|
||||
)
|
||||
|
||||
def run_check():
|
||||
@@ -27,25 +29,31 @@ def edit_haproxy_config():
|
||||
text=True
|
||||
)
|
||||
out = (result.stdout or '').strip()
|
||||
|
||||
if result.returncode == 0:
|
||||
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:
|
||||
if not out:
|
||||
out = f"Check failed with return code {result.returncode}"
|
||||
return result.returncode, out
|
||||
level = "danger"
|
||||
|
||||
return result.returncode, out, level
|
||||
|
||||
check_output = ""
|
||||
check_level = "success"
|
||||
|
||||
if 'save_check' in request.form:
|
||||
rc, out = run_check()
|
||||
check_output = out
|
||||
_, check_output, check_level = run_check()
|
||||
|
||||
elif 'save_reload' in request.form:
|
||||
rc, out = run_check()
|
||||
check_output = out
|
||||
rc, out, level = run_check()
|
||||
check_output, check_level = out, level
|
||||
|
||||
if rc == 0:
|
||||
reload_output = ""
|
||||
try:
|
||||
supervisor_result = subprocess.run(
|
||||
['pkill', '-f', 'haproxy'],
|
||||
@@ -55,14 +63,22 @@ def edit_haproxy_config():
|
||||
timeout=10
|
||||
)
|
||||
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:
|
||||
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:
|
||||
reload_output = f"\n\nRestart failed: {e}"
|
||||
check_output += reload_output
|
||||
check_output += f"\n\nRestart failed: {e}"
|
||||
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:
|
||||
with open('/etc/haproxy/haproxy.cfg', 'r') as f:
|
||||
|
||||
@@ -21,16 +21,11 @@
|
||||
</div>
|
||||
</form>
|
||||
{% if check_output %}
|
||||
<div class="mt-3">
|
||||
{% if 'Fatal errors' in check_output or 'error detected while parsing an' in check_output %}
|
||||
<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 class="alert alert-{{ check_level|default('success') }}" role="alert">
|
||||
<pre class="mb-0">{{ check_output }}</pre>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user