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