From c6f865bbb817905590db26b1f99135b08a6e9071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Tue, 25 Feb 2025 10:53:13 +0100 Subject: [PATCH] refactor web interface --- app.py | 99 ++++++++++++++++----------- templates/clear_servers.html | 111 +++++++++++++++---------------- templates/deploy_hosts_file.html | 43 ++++++++---- 3 files changed, 146 insertions(+), 107 deletions(-) diff --git a/app.py b/app.py index 30eefd3..0e1d943 100644 --- a/app.py +++ b/app.py @@ -435,45 +435,51 @@ def test_server_connection(id): def clear_server(): if 'user_id' not in session: return redirect(url_for('login')) - # Pobierz listę hostów dla bieżącego użytkownika + hosts = Host.query.filter_by(user_id=session['user_id']).all() + return render_template('clear_servers.html', hosts=hosts) + +@app.route('/clear-single-server/', methods=['POST']) +def clear_single_server(host_id): + if 'user_id' not in session: + return redirect(url_for('login')) + host = db.session.get(Host, host_id) + if not host or host.user_id != session['user_id']: + flash('Host not found or unauthorized', 'danger') + return redirect(url_for('clear_servers')) + default_content = ensure_local_defaults("") + try: + if host.type == 'linux': + clear_linux(host, default_content) + elif host.type == 'mikrotik': + clear_mikrotik(host) + flash(f'Cleared host: {host.hostname}', 'success') + except Exception as e: + flash(f'Error clearing host {host.hostname}: {str(e)}', 'danger') + return redirect(url_for('clear_all_server')) + +@app.route('/clear-all-server', methods=['GET', 'POST']) +def clear_all_server(): + if 'user_id' not in session: + return redirect(url_for('login')) hosts = Host.query.filter_by(user_id=session['user_id']).all() if request.method == 'POST': - clear_option = request.form.get('clear_option', 'all') linux_clear = request.form.get('linux') mikrotik_clear = request.form.get('mikrotik') default_content = ensure_local_defaults("") - if clear_option == 'one': - host_id = request.form.get('host_id') - if host_id: - host = db.session.get(Host, int(host_id)) - if host and host.user_id == session['user_id']: - try: - if host.type == 'linux' and linux_clear: - clear_linux(host, default_content) - elif host.type == 'mikrotik' and mikrotik_clear: - clear_mikrotik(host) - flash(f'Cleared host: {host.hostname}', 'success') - except Exception as e: - flash(f'Error clearing host {host.hostname}: {str(e)}', 'danger') - else: - flash("Wybrany host nie został znaleziony lub nie masz do niego dostępu", "danger") - else: - flash("Nie wybrano hosta", "danger") - else: - # Opcja "all": przetwórz wszystkie hosty - for h in hosts: - try: - if h.type == 'linux' and linux_clear: - clear_linux(h, default_content) - flash(f'Cleared Linux host: {h.hostname}', 'success') - elif h.type == 'mikrotik' and mikrotik_clear: - clear_mikrotik(h) - flash(f'Cleared Mikrotik host: {h.hostname}', 'success') - except Exception as e: - flash(f'Error clearing host {h.hostname}: {str(e)}', 'danger') - return redirect(url_for('clear_server')) + for h in hosts: + try: + if h.type == 'linux' and linux_clear: + clear_linux(h, default_content) + flash(f'Cleared Linux host: {h.hostname}', 'success') + elif h.type == 'mikrotik' and mikrotik_clear: + clear_mikrotik(h) + flash(f'Cleared Mikrotik host: {h.hostname}', 'success') + except Exception as e: + flash(f'Error clearing host {h.hostname}: {str(e)}', 'danger') + return redirect(url_for('clear_all_server')) return render_template('clear_servers.html', hosts=hosts) + # ------------------- # ZARZĄDZANIE PLIKAMI HOSTS (WIELOKROTNE PLIKI) # ------------------- @@ -546,7 +552,8 @@ def deploy_hosts_file(file_id): try: if host.type == 'linux': ssh = open_ssh_connection(host) - adjusted_content = ensure_local_defaults(hosts_content) + # Używamy file.content, nie hosts_content + adjusted_content = ensure_local_defaults(file.content) wrapped_content = wrap_content_with_comments(adjusted_content) with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmpf: tmpf.write(wrapped_content) @@ -556,11 +563,17 @@ def deploy_hosts_file(file_id): sftp.close() ssh.close() os.remove(tmp_file_path) - db.session.add(DeployLog(details=f'[LINUX] Deployed file "{file.title}" to {host.hostname} for user {session["user_id"]}')) + db.session.add(DeployLog( + details=f'[LINUX] Deployed file "{file.title}" to {host.hostname} for user {session["user_id"]}', + user_id=session['user_id'] + )) elif host.type == 'mikrotik': wrapped_content = wrap_mikrotik_content(file.content) deploy_mikrotik(host, wrapped_content) - db.session.add(DeployLog(details=f'[MIKROTIK] Deployed file "{file.title}" to {host.hostname} for user {session["user_id"]}')) + db.session.add(DeployLog( + details=f'[MIKROTIK] Deployed file "{file.title}" to {host.hostname} for user {session["user_id"]}', + user_id=session['user_id'] + )) db.session.commit() flash(f'Deployed file "{file.title}" to {host.hostname}', 'success') except Exception as e: @@ -630,11 +643,19 @@ def restore_backup(backup_id): try: if host.type == 'mikrotik': ssh = open_ssh_connection(host) + # Usuń istniejące wpisy ssh.exec_command("/ip dns static remove [find]") + import time + time.sleep(1) + # Przygotuj jedno polecenie, które dodaje wszystkie wpisy + commands = [] for line in backup.content.splitlines(): line = line.strip() if line.startswith("add "): - ssh.exec_command(line) + commands.append("/ip dns static " + line) + full_command = " ; ".join(commands) + #print("[DEBUG] Full command sent to Mikrotik:", full_command) + ssh.exec_command(full_command) ssh.close() flash(f'Backup restored to mikrotik host {host.hostname} successfully.', 'success') else: @@ -643,6 +664,7 @@ def restore_backup(backup_id): with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmpf: tmpf.write(backup.content) tmp_file_path = tmpf.name + #print(f"[DEBUG] Tymczasowy plik: {tmp_file_path} zawiera: {backup.content}") sftp.put(tmp_file_path, '/etc/hosts') sftp.close() ssh.close() @@ -659,9 +681,12 @@ def restore_backup(backup_id): else: hostfile.content = backup.content db.session.commit() - flash('Backup restored to default configuration successfully.', 'success') + deploy_user(session['user_id']) + flash('Backup restored to default configuration and deployed successfully.', 'success') return redirect(url_for('backups')) + + @app.route('/view-backup/', methods=['GET']) def view_backup(backup_id): if 'user_id' not in session: diff --git a/templates/clear_servers.html b/templates/clear_servers.html index 07ba0b8..8428a55 100644 --- a/templates/clear_servers.html +++ b/templates/clear_servers.html @@ -1,8 +1,11 @@ {% extends "base.html" %} -{% block title %}Wyczyść Server - /etc/hosts Manager{% endblock %} +{% block title %}Czyszczenie /etc/hosts - /etc/hosts Manager{% endblock %} {% block extra_css %} {{ super() }} {% endblock %} {% block content %} -
-
-

Wyczyść /etc/hosts

-
-
-
-
- -
- - +
+
+
+

Grupowe czyszczenie /etc/hosts

+
+
+ +
+ +
+ + +
+
+ + +
-
- - -
-
- -
-
- - -
-
- - -
-
- - + + +
-
- Przejdź do pulpitu + +
+
+
+

Czyszczenie pojedynczego serwera

+
+
+
+
+ + +
+ +
+
+
{% endblock %} {% block extra_js %} {{ super() }} {% endblock %} diff --git a/templates/deploy_hosts_file.html b/templates/deploy_hosts_file.html index 8b9c258..82895a8 100644 --- a/templates/deploy_hosts_file.html +++ b/templates/deploy_hosts_file.html @@ -1,17 +1,32 @@ {% extends "base.html" %} {% block title %}Deploy Hosts File - /etc/hosts Manager{% endblock %} -{% block content %} -

Deploy Hosts File: "{{ file.title }}"

-
-
- - {% for host in hosts %} -
- - -
- {% endfor %} -
- -
+{% block extra_css %} + {{ super() }} + +{% endblock %} +{% block content %} +
+
+

Wgraj plik hosts: "{{ file.title }}"

+
+
+
+
+ + {% for host in hosts %} +
+ + +
+ {% endfor %} +
+ +
+
+
{% endblock %}