refactor web interface
This commit is contained in:
63
app.py
63
app.py
@@ -176,8 +176,22 @@ def automated_backup_for_host(host):
|
||||
def automated_backups():
|
||||
with app.app_context():
|
||||
hosts = Host.query.all()
|
||||
now = datetime.now(timezone.utc)
|
||||
for host in hosts:
|
||||
automated_backup_for_host(host)
|
||||
# Pobieramy ustawienia użytkownika
|
||||
settings = UserSettings.query.filter_by(user_id=host.user_id).first()
|
||||
backup_interval = settings.backup_interval if settings and settings.backup_interval else 60
|
||||
|
||||
# Pobieramy ostatni backup dla danego hosta
|
||||
last_backup = Backup.query.filter_by(user_id=host.user_id, host_id=host.id).order_by(Backup.created_at.desc()).first()
|
||||
if last_backup:
|
||||
last_backup_time = last_backup.created_at
|
||||
else:
|
||||
last_backup_time = None
|
||||
|
||||
# Jeśli brak backupu lub minęło wystarczająco czasu, wykonujemy backup
|
||||
if (last_backup_time is None) or ((now - last_backup_time).total_seconds() >= backup_interval * 60):
|
||||
automated_backup_for_host(host)
|
||||
|
||||
def wrap_content_with_comments(content):
|
||||
now_str = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
|
||||
@@ -421,27 +435,44 @@ 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()
|
||||
if request.method == 'POST':
|
||||
user_id = session['user_id']
|
||||
clear_option = request.form.get('clear_option', 'all')
|
||||
linux_clear = request.form.get('linux')
|
||||
mikrotik_clear = request.form.get('mikrotik')
|
||||
hosts = Host.query.filter_by(user_id=user_id).all()
|
||||
default_content = ensure_local_defaults("")
|
||||
for h in hosts:
|
||||
if h.type == 'linux' and linux_clear:
|
||||
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:
|
||||
clear_linux(h, default_content)
|
||||
flash(f'Cleared Linux host: {h.hostname}', 'success')
|
||||
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 Linux host {h.hostname}: {str(e)}', 'danger')
|
||||
elif h.type == 'mikrotik' and mikrotik_clear:
|
||||
try:
|
||||
clear_mikrotik(h)
|
||||
flash(f'Cleared Mikrotik host: {h.hostname}', 'success')
|
||||
except Exception as e:
|
||||
flash(f'Error clearing Mikrotik host {h.hostname}: {str(e)}', 'danger')
|
||||
flash(f'Error clearing host {h.hostname}: {str(e)}', 'danger')
|
||||
return redirect(url_for('clear_server'))
|
||||
return render_template('clear_servers.html')
|
||||
return render_template('clear_servers.html', hosts=hosts)
|
||||
|
||||
# -------------------
|
||||
# ZARZĄDZANIE PLIKAMI HOSTS (WIELOKROTNE PLIKI)
|
||||
@@ -1075,7 +1106,7 @@ def scheduled_deployments():
|
||||
|
||||
scheduler = BackgroundScheduler()
|
||||
scheduler.add_job(func=scheduled_deployments, trigger="interval", minutes=5)
|
||||
scheduler.add_job(func=automated_backups, trigger="interval", minutes=60)
|
||||
scheduler.add_job(func=automated_backups, trigger="interval", minutes=5)
|
||||
scheduler.add_job(func=cleanup_old_backups, trigger="interval", hours=24)
|
||||
|
||||
scheduler.start()
|
||||
|
Reference in New Issue
Block a user