redactor
This commit is contained in:
17
app.py
17
app.py
@@ -20,12 +20,10 @@ app = Flask(
|
||||
template_folder=os.path.join(BASE_DIR, 'templates')
|
||||
)
|
||||
|
||||
# Uniwersalne ścieżki - sprawdzaj obie
|
||||
CONFIG_DIR_DOCKER = '/etc/haproxy-configurator'
|
||||
CONFIG_DIR_LOCAL = './config'
|
||||
CONFIG_DIR_ENV = os.environ.get('CONFIG_DIR', None)
|
||||
|
||||
# Określ która ścieżka istnieje
|
||||
if CONFIG_DIR_ENV and os.path.exists(CONFIG_DIR_ENV):
|
||||
CONFIG_DIR = CONFIG_DIR_ENV
|
||||
elif os.path.exists(CONFIG_DIR_DOCKER):
|
||||
@@ -33,16 +31,14 @@ elif os.path.exists(CONFIG_DIR_DOCKER):
|
||||
elif os.path.exists(CONFIG_DIR_LOCAL):
|
||||
CONFIG_DIR = CONFIG_DIR_LOCAL
|
||||
else:
|
||||
CONFIG_DIR = CONFIG_DIR_DOCKER # Fallback
|
||||
CONFIG_DIR = CONFIG_DIR_DOCKER
|
||||
|
||||
AUTH_CFG = os.path.join(CONFIG_DIR, 'auth', 'auth.cfg')
|
||||
SSL_INI = os.path.join(CONFIG_DIR, 'ssl.ini')
|
||||
|
||||
# Create directories
|
||||
os.makedirs(os.path.dirname(AUTH_CFG), exist_ok=True)
|
||||
os.makedirs(os.path.dirname(SSL_INI), exist_ok=True)
|
||||
|
||||
# Load basic auth credentials
|
||||
BASIC_AUTH_USERNAME = "admin"
|
||||
BASIC_AUTH_PASSWORD = "admin"
|
||||
|
||||
@@ -60,17 +56,11 @@ except Exception as e:
|
||||
BASIC_AUTH_USERNAME = "admin"
|
||||
BASIC_AUTH_PASSWORD = "admin"
|
||||
|
||||
|
||||
# Register blueprints
|
||||
app.register_blueprint(main_bp)
|
||||
app.register_blueprint(edit_bp)
|
||||
|
||||
|
||||
# Setup authentication
|
||||
setup_auth(app)
|
||||
|
||||
|
||||
# SSL Configuration - Z ERROR HANDLINGIEM
|
||||
certificate_path = None
|
||||
private_key_path = None
|
||||
ssl_context = None
|
||||
@@ -79,7 +69,6 @@ try:
|
||||
config2 = configparser.ConfigParser()
|
||||
config2.read(SSL_INI)
|
||||
|
||||
# WAŻNE: has_section check PRZED .get()
|
||||
if config2.has_section('ssl'):
|
||||
certificate_path = config2.get('ssl', 'certificate_path')
|
||||
private_key_path = config2.get('ssl', 'private_key_path')
|
||||
@@ -87,7 +76,6 @@ try:
|
||||
print(f"[APP] ✗ No [ssl] section in {SSL_INI}", flush=True)
|
||||
sys.exit(1)
|
||||
|
||||
# Sprawdź czy pliki istnieją
|
||||
if not os.path.exists(certificate_path):
|
||||
print(f"[APP] ✗ Certificate not found: {certificate_path}", flush=True)
|
||||
sys.exit(1)
|
||||
@@ -105,20 +93,17 @@ except Exception as e:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# Statistics Route
|
||||
@app.route('/statistics')
|
||||
def display_haproxy_stats():
|
||||
haproxy_stats = fetch_haproxy_stats()
|
||||
parsed_stats = parse_haproxy_stats(haproxy_stats)
|
||||
return render_template('statistics.html', stats=parsed_stats)
|
||||
|
||||
# Logs Route
|
||||
@app.route('/logs')
|
||||
def display_logs():
|
||||
log_file_path = '/var/log/haproxy.log'
|
||||
parsed_entries = parse_log_file(log_file_path)
|
||||
return render_template('logs.html', entries=parsed_entries)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='::', port=5000, ssl_context=ssl_context, debug=True)
|
||||
@@ -2,50 +2,45 @@ 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['haproxy_config']
|
||||
# Save the edited config to the haproxy.cfg file
|
||||
with open('/etc/haproxy/haproxy.cfg', 'w') as f:
|
||||
f.write(edited_config)
|
||||
|
||||
check_output = ""
|
||||
|
||||
if 'save_check' in request.form:
|
||||
# Run haproxy -c -V -f to check the configuration
|
||||
check_result = subprocess.run(['haproxy', '-c', '-V', '-f', '/etc/haproxy/haproxy.cfg'], capture_output=True, text=True)
|
||||
check_output = check_result.stdout
|
||||
|
||||
# Check if there was an error, and if so, append it to the output
|
||||
if check_result.returncode != 0:
|
||||
error_message = check_result.stderr
|
||||
check_output += f"\n\nError occurred:\n{error_message}"
|
||||
|
||||
if check_result.returncode == 0:
|
||||
error_message = check_result.stderr
|
||||
check_output += f"\n\nConfiguration OK. You can restart."
|
||||
|
||||
elif 'save_reload' in request.form:
|
||||
# Run haproxy -c -V -f to check the configuration
|
||||
check_result = subprocess.run(['haproxy', '-c', '-V', '-f', '/etc/haproxy/haproxy.cfg'], capture_output=True, text=True)
|
||||
check_output = check_result.stdout
|
||||
|
||||
# Check if there was an error, and if so, append it to the output
|
||||
if check_result.returncode != 0:
|
||||
error_message = check_result.stderr
|
||||
check_output += f"\n\nError occurred:\n{error_message}"
|
||||
else:
|
||||
# Try to reload HAProxy - support both Docker (supervisor) and systemd
|
||||
reload_success = False
|
||||
reload_output = ""
|
||||
|
||||
# Method 1: Supervisor with sudo (Docker)
|
||||
try:
|
||||
supervisor_result = subprocess.run(['pkill', '-f', 'haproxy'], capture_output=True, text=True, timeout=10)
|
||||
if supervisor_result.returncode == 0:
|
||||
reload_output = f"\n\n✓ HAProxy Restarted via Supervisor:\n{supervisor_result.stdout}"
|
||||
reload_output = f"\n\n HAProxy Restarted via Supervisor:\n{supervisor_result.stdout}"
|
||||
reload_success = True
|
||||
print(f"[HAPROXY] Supervisor restart successful", flush=True)
|
||||
except Exception as e:
|
||||
@@ -54,7 +49,6 @@ def edit_haproxy_config():
|
||||
|
||||
return render_template('edit.html', config_content=edited_config, check_output=check_output)
|
||||
|
||||
# GET request - Read the current contents of haproxy.cfg
|
||||
try:
|
||||
with open('/etc/haproxy/haproxy.cfg', 'r') as f:
|
||||
config_content = f.read()
|
||||
|
||||
@@ -45,6 +45,6 @@
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="alert alert-info"><i class="bi bi-info-circle me-1"></i>Brak danych.</div>
|
||||
<div class="alert alert-info"><i class="bi bi-info-circle me-1"></i>No data.</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user