refactor web interface
This commit is contained in:
parent
cffc8b3124
commit
4b5d7526ff
63
app.py
63
app.py
@ -176,8 +176,22 @@ def automated_backup_for_host(host):
|
|||||||
def automated_backups():
|
def automated_backups():
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
hosts = Host.query.all()
|
hosts = Host.query.all()
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
for host in hosts:
|
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):
|
def wrap_content_with_comments(content):
|
||||||
now_str = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
|
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():
|
def clear_server():
|
||||||
if 'user_id' not in session:
|
if 'user_id' not in session:
|
||||||
return redirect(url_for('login'))
|
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':
|
if request.method == 'POST':
|
||||||
user_id = session['user_id']
|
clear_option = request.form.get('clear_option', 'all')
|
||||||
linux_clear = request.form.get('linux')
|
linux_clear = request.form.get('linux')
|
||||||
mikrotik_clear = request.form.get('mikrotik')
|
mikrotik_clear = request.form.get('mikrotik')
|
||||||
hosts = Host.query.filter_by(user_id=user_id).all()
|
|
||||||
default_content = ensure_local_defaults("")
|
default_content = ensure_local_defaults("")
|
||||||
for h in hosts:
|
if clear_option == 'one':
|
||||||
if h.type == 'linux' and linux_clear:
|
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:
|
try:
|
||||||
clear_linux(h, default_content)
|
if h.type == 'linux' and linux_clear:
|
||||||
flash(f'Cleared Linux host: {h.hostname}', 'success')
|
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:
|
except Exception as e:
|
||||||
flash(f'Error clearing Linux host {h.hostname}: {str(e)}', 'danger')
|
flash(f'Error clearing 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')
|
|
||||||
return redirect(url_for('clear_server'))
|
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)
|
# ZARZĄDZANIE PLIKAMI HOSTS (WIELOKROTNE PLIKI)
|
||||||
@ -1075,7 +1106,7 @@ def scheduled_deployments():
|
|||||||
|
|
||||||
scheduler = BackgroundScheduler()
|
scheduler = BackgroundScheduler()
|
||||||
scheduler.add_job(func=scheduled_deployments, trigger="interval", minutes=5)
|
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.add_job(func=cleanup_old_backups, trigger="interval", hours=24)
|
||||||
|
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
<td>{{ backup.description }}</td>
|
<td>{{ backup.description }}</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{ url_for('view_backup', backup_id=backup.id) }}" class="btn btn-sm btn-info">Podgląd</a>
|
<a href="{{ url_for('view_backup', backup_id=backup.id) }}" class="btn btn-sm btn-info">Podgląd</a>
|
||||||
|
<a href="{{ url_for('restore_backup', backup_id=backup.id) }}" class="btn btn-sm btn-success">Przywróć</a>
|
||||||
<form action="{{ url_for('delete_backup', backup_id=backup.id) }}" method="post" style="display:inline;">
|
<form action="{{ url_for('delete_backup', backup_id=backup.id) }}" method="post" style="display:inline;">
|
||||||
<button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Czy na pewno usunąć backup?');">Usuń</button>
|
<button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Czy na pewno usunąć backup?');">Usuń</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -12,21 +12,67 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="card mb-4">
|
<div class="card mb-4">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
<h2>Wyczyść serwery</h2>
|
<h2>Wyczyść /etc/hosts</h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form method="POST" action="{{ url_for('clear_server') }}">
|
<form method="POST" action="{{ url_for('clear_server') }}">
|
||||||
<div class="form-check mb-3">
|
<div class="mb-3">
|
||||||
<input class="form-check-input" type="checkbox" name="linux" id="linux">
|
<label class="form-label">Opcja czyszczenia:</label>
|
||||||
<label class="form-check-label" for="linux">Wyczyść Linux</label>
|
<div class="form-check">
|
||||||
|
<input class="form-check-input" type="radio" name="clear_option" id="clear_all" value="all" checked>
|
||||||
|
<label class="form-check-label" for="clear_all">Wyczyść dla wszystkich hostów</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-check">
|
||||||
|
<input class="form-check-input" type="radio" name="clear_option" id="clear_one" value="one">
|
||||||
|
<label class="form-check-label" for="clear_one">Wybierz serwer</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-check mb-3">
|
<div class="mb-3" id="host_select_div" style="display: none;">
|
||||||
<input class="form-check-input" type="checkbox" name="mikrotik" id="mikrotik">
|
<label for="host_id" class="form-label">Wybierz serwer:</label>
|
||||||
<label class="form-check-label" for="mikrotik">Wyczyść Mikrotik</label>
|
<select class="form-select" id="host_id" name="host_id">
|
||||||
|
{% for host in hosts %}
|
||||||
|
<option value="{{ host.id }}">{{ host.hostname }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-danger">Wyczyść wybrane</button>
|
<div class="mb-3">
|
||||||
|
<div class="form-check">
|
||||||
|
<input class="form-check-input" type="checkbox" name="linux" id="linux">
|
||||||
|
<label class="form-check-label" for="linux">Wyczyść Linux</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-check">
|
||||||
|
<input class="form-check-input" type="checkbox" name="mikrotik" id="mikrotik">
|
||||||
|
<label class="form-check-label" for="mikrotik">Wyczyść Mikrotik</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-danger">Wyczyść</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="mt-3 text-center">
|
||||||
|
<a href="{{ url_for('dashboard') }}" class="btn btn-secondary">Przejdź do pulpitu</a>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
{% block extra_js %}
|
||||||
|
{{ super() }}
|
||||||
|
<script>
|
||||||
|
// Pokazuj lub ukrywaj rozwijaną listę w zależności od wybranej opcji
|
||||||
|
const clearAllRadio = document.getElementById('clear_all');
|
||||||
|
const clearOneRadio = document.getElementById('clear_one');
|
||||||
|
const hostSelectDiv = document.getElementById('host_select_div');
|
||||||
|
|
||||||
|
function toggleHostSelect() {
|
||||||
|
if (clearOneRadio.checked) {
|
||||||
|
hostSelectDiv.style.display = 'block';
|
||||||
|
} else {
|
||||||
|
hostSelectDiv.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearAllRadio.addEventListener('change', toggleHostSelect);
|
||||||
|
clearOneRadio.addEventListener('change', toggleHostSelect);
|
||||||
|
|
||||||
|
// Inicjalnie ustaw stan przy załadowaniu strony
|
||||||
|
toggleHostSelect();
|
||||||
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user