diff --git a/app.py b/app.py index 2dc62f0..e3fd7a2 100644 --- a/app.py +++ b/app.py @@ -8,10 +8,8 @@ from io import StringIO import socket import ipaddress import difflib - from croniter import croniter from tzlocal import get_localzone - from werkzeug.serving import WSGIRequestHandler WSGIRequestHandler.server_version = "" @@ -46,11 +44,9 @@ class Host(db.Model): auto_backup_enabled = db.Column(db.Boolean, default=True) preferred_hostfile_id = db.Column(db.Integer, db.ForeignKey('host_file.id'), nullable=True) preferred_hostfile = db.relationship('HostFile', foreign_keys=[preferred_hostfile_id]) - - # Funkcja wspolpracy z hosts_daemon - use_daemon = db.Column(db.Boolean, default=False) # <--- NOWE - daemon_url = db.Column(db.String(255), nullable=True) # <--- NOWE - daemon_token = db.Column(db.String(255), nullable=True) # <--- NOWE + use_daemon = db.Column(db.Boolean, default=False) + daemon_url = db.Column(db.String(255), nullable=True) + daemon_token = db.Column(db.String(255), nullable=True) @property def resolved_hostname(self): @@ -65,13 +61,11 @@ class DeployLog(db.Model): details = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref='deploy_logs', lazy=True) - class HostFile(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) title = db.Column(db.String(100), nullable=False, default='Default Hosts') content = db.Column(db.Text, nullable=False) - class UserSettings(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), unique=True, nullable=False) @@ -84,7 +78,6 @@ class UserSettings(db.Model): last_deploy_time = db.Column(db.DateTime, nullable=True) regex_deploy_enabled = db.Column(db.Boolean, default=True) backup_retention_days = db.Column(db.Integer, default=0) - class Backup(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) @@ -93,7 +86,6 @@ class Backup(db.Model): content = db.Column(db.Text, nullable=False) description = db.Column(db.String(255), nullable=True) host = db.relationship('Host', backref='backups', lazy=True) - class RegexHostEntry(db.Model): __tablename__ = 'regex_host_entry' id = db.Column(db.Integer, primary_key=True) @@ -106,7 +98,6 @@ class RegexHostEntry(db.Model): use_gateway_ip = db.Column(db.Boolean, default=False) comment = db.Column(db.String(255), nullable=True) user = db.relationship('User', backref='regex_entries') - class HostFileVersion(db.Model): id = db.Column(db.Integer, primary_key=True) hostfile_id = db.Column(db.Integer, db.ForeignKey('host_file.id'), nullable=False) @@ -214,7 +205,7 @@ def automated_backup_for_host(host): db.session.add(backup) db.session.commit() - log_entry = DeployLog(details=f'[BACKUP] Automatic backup created for host {host.hostname}', + log_entry = DeployLog(details=f'[BACKUP] Automatic backup created for server {host.hostname}', user_id=host.user_id) db.session.add(log_entry) db.session.commit() @@ -222,7 +213,7 @@ def automated_backup_for_host(host): print(f'Automated backup for host {host.hostname} created successfully.') except Exception as e: - print(f'Error creating automated backup for host {host.hostname}: {str(e)}') + print(f'Error creating automated backup for server {host.hostname}: {str(e)}') def automated_backups(): @@ -329,6 +320,7 @@ def cleanup_old_backups(): # ------------------- # LOGOWANIE, REJESTRACJA, ZMIANA HASŁA # ------------------- + @app.route('/') def index(): if 'user_id' in session: @@ -380,8 +372,9 @@ def change_password(): return render_template('change_password.html') # ------------------- -# ZARZĄDZANIE HOSTAMI +# ZARZĄDZANIE SERWERAMI # ------------------- + @app.route('/add_server', methods=['GET', 'POST']) def add_server(): if 'user_id' not in session: @@ -538,7 +531,6 @@ def edit_server(id): # GET -> renderuj z user_hostfiles return render_template('edit_server.html', host=host, user_hostfiles=user_hostfiles) - # ------------------- # TESTOWANIE POŁĄCZENIA SSH DLA HOSTA # ------------------- @@ -799,6 +791,7 @@ def deploy_hosts_file(file_id): # ------------------- # BACKUP # ------------------- + @app.route('/server-backup/', methods=['GET']) def server_backup(host_id): if 'user_id' not in session: @@ -912,8 +905,6 @@ def restore_backup(backup_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: @@ -1112,31 +1103,10 @@ def delete_backup(backup_id): flash('Backup deleted successfully', 'info') return redirect(url_for('backups')) - -# ------------------- -# EDYCJA LOKALNEGO PLIKU HOSTS -# ------------------- -# @app.route('/edit-local-hosts', methods=['GET', 'POST'], endpoint='edit_local_hosts') -# def edit_local_hosts(): -# if 'user_id' not in session: -# return redirect(url_for('login')) -# user_id = session['user_id'] -# hostfile = HostFile.query.filter_by(user_id=user_id, title="Default Hosts").first() -# if not hostfile: -# default_content = "# This is a sample hosts file.\n127.0.0.1 localhost\n" -# hostfile = HostFile(user_id=user_id, title="Default Hosts", content=default_content) -# db.session.add(hostfile) -# db.session.commit() -# if request.method == 'POST': -# new_content = request.form['hosts_content'] -# hostfile.content = new_content -# db.session.commit() -# flash('Local hosts content updated successfully', 'success') -# return render_template('edit_hosts.html', content=hostfile.content) - # ------------------- # DEPLOYMENT DOMYŚLNY DLA UŻYTKOWNIKA # ------------------- + def deploy_user(user_id): user_settings = UserSettings.query.filter_by(user_id=user_id).first() # domyślny plik "Default Hosts" (np. szukasz title=="Default Hosts") @@ -1217,7 +1187,6 @@ def deploy_user(user_id): )) db.session.commit() - def deploy_mikrotik(host, hosts_content): ssh = open_ssh_connection(host) stdin, stdout, stderr = ssh.exec_command("/ip dns static export") @@ -1287,6 +1256,7 @@ def deploy_now(): # ------------------- # DASHBOARD ZE STATYSTYKAMI # ------------------- + @app.route('/dashboard') def dashboard(): if 'user_id' not in session: @@ -1435,6 +1405,11 @@ def update_host_automation(id): flash('Ustawienia automatyzacji zostały zaktualizowane.', 'success') return redirect(url_for('server_list')) + +# ------------------- +# EDYCJA LOKALNEGO PLIKU HOSTS +# ------------------- + @app.route('/edit-local-hosts', methods=['GET', 'POST'], endpoint='edit_local_hosts') def edit_local_hosts(): if 'user_id' not in session: @@ -1468,7 +1443,6 @@ def hostfile_versions(hostfile_id): return redirect(url_for('dashboard')) if request.method == 'POST': - # Masowe usuwanie – lista zaznaczonych wersji selected_ids = request.form.getlist('selected_versions') for version_id in selected_ids: version = HostFileVersion.query.get(version_id) @@ -1542,7 +1516,6 @@ def diff_hostfile_versions(version1_id, version2_id): context=True, numlines=3 ) - # Przekazujemy hostfile_id, zakładając, że obie wersje należą do tego samego pliku hostfile_id = version1.hostfile_id return render_template('diff_versions.html', diff_html=diff_html, hostfile_id=hostfile_id) @@ -1564,7 +1537,6 @@ def restore_hostfile_version(version_id): if not version or version.hostfile.user_id != session['user_id']: flash('Version not found or unauthorized', 'danger') return redirect(url_for('dashboard')) - # Przywróć zawartość wersji do głównego hostfile hostfile = version.hostfile hostfile.content = version.content db.session.commit() @@ -1582,7 +1554,6 @@ def default_hostfile_versions(): return redirect(url_for('edit_local_hosts')) return redirect(url_for('hostfile_versions', hostfile_id=hostfile.id)) - def scheduled_deployments(): with app.app_context(): now = datetime.now(timezone.utc) diff --git a/templates/server_list.html b/templates/server_list.html index ac4206e..6b65de3 100644 --- a/templates/server_list.html +++ b/templates/server_list.html @@ -26,7 +26,7 @@ Port Typ Uwierzytelnianie - Wybrany plik /etc/hosts + Wybrany plik /etc/hosts Auto Deploy Auto Backup Akcje @@ -37,10 +37,28 @@ {{ h.id }} - {{ h.hostname }} + {% if h.use_daemon and h.type == 'linux' and h.daemon_url %} + {% set daemon_str = h.daemon_url.split('://') | last %} + {% set daemon_split = daemon_str.split(':') %} + (Daemon IP: {{ daemon_split[0] }}) + {% else %} + {{ h.hostname }} + {% endif %} + + + {% if h.use_daemon and h.type == 'linux' %} + + {% else %} + {{ h.username }} + {% endif %} + + + {% if h.use_daemon and h.type == 'linux' %} + + {% else %} + {{ h.port }} + {% endif %} - {{ h.username }} - {{ h.port }} {% if h.type == 'linux' %} Linux{% if h.use_daemon %} (Demon){% endif %}