refactor web interface
This commit is contained in:
99
app.py
99
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/<int:host_id>', 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/<int:backup_id>', methods=['GET'])
|
||||
def view_backup(backup_id):
|
||||
if 'user_id' not in session:
|
||||
|
Reference in New Issue
Block a user