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