fixy z listy todo

This commit is contained in:
Mateusz Gruszczyński
2025-03-09 00:03:29 +01:00
parent 2904209332
commit a5c59f8a64
5 changed files with 71 additions and 31 deletions

53
app.py
View File

@@ -46,6 +46,7 @@ class Host(db.Model):
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)
disable_regex_deploy = db.Column(db.Boolean, default=False)
@property
def resolved_hostname(self):
try:
@@ -53,6 +54,18 @@ class Host(db.Model):
except Exception:
return self.hostname
@property
def resolved_daemon(self):
if self.use_daemon and self.daemon_url:
try:
daemon_str = self.daemon_url.split("://")[-1]
daemon_ip = daemon_str.split(":")[0]
return socket.gethostbyaddr(daemon_ip)[0]
except Exception:
return daemon_ip
return self.resolved_hostname
class DeployLog(db.Model):
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.DateTime, default=db.func.current_timestamp())
@@ -86,6 +99,7 @@ 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)
@@ -736,6 +750,8 @@ def delete_hosts_file(file_id):
# -------------------
# WDROŻENIE WYBRANEGO PLIKU HOSTS NA WYBRANE SERWERY
# -------------------
import socket
@app.route('/deploy-hosts-file/<int:file_id>', methods=['GET', 'POST'])
def deploy_hosts_file(file_id):
if 'user_id' not in session:
@@ -746,6 +762,17 @@ def deploy_hosts_file(file_id):
return redirect(url_for('list_hosts_files'))
hosts = Host.query.filter_by(user_id=session['user_id']).all()
# Dla hostów z demonem obliczamy IP i rozwiązaną nazwę z IP
for host in hosts:
if host.use_daemon and host.type == 'linux' and host.daemon_url:
daemon_str = host.daemon_url.split("://")[-1]
daemon_ip = daemon_str.split(":")[0]
host.daemon_ip = daemon_ip
try:
host.resolved_daemon = socket.gethostbyaddr(daemon_ip)[0]
except Exception:
host.resolved_daemon = daemon_ip # jeśli nie uda się rozwiązać, pozostaw IP
if request.method == 'POST':
selected_host_ids = request.form.getlist('hosts')
for host in hosts:
@@ -764,11 +791,10 @@ def deploy_hosts_file(file_id):
if resp.status_code != 200:
raise Exception(f"Daemon POST error: {resp.status_code} - {resp.text}")
db.session.add(DeployLog(
details=f'[LINUX/DAEMON] Deployed file "{file.title}" to {host.hostname} for user {session["user_id"]}',
details=f'[LINUX/DAEMON] Deployed file "{file.title}" to {host.daemon_ip} - {host.resolved_daemon} for user {session["user_id"]}',
user_id=session['user_id']
))
elif host.type == 'mikrotik':
# Mikrotik
wrapped_mikro = wrap_mikrotik_content(file.content)
deploy_mikrotik(host, wrapped_mikro)
db.session.add(DeployLog(
@@ -776,7 +802,6 @@ def deploy_hosts_file(file_id):
user_id=session['user_id']
))
else:
# Standard Linux (SSH)
ssh = open_ssh_connection(host)
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmpf:
tmpf.write(wrapped_content)
@@ -790,7 +815,6 @@ def deploy_hosts_file(file_id):
details=f'[LINUX] 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:
@@ -1176,13 +1200,11 @@ def delete_backup(backup_id):
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")
default_file = HostFile.query.filter_by(user_id=user_id, title="Default Hosts").first()
if not default_file:
# jeśli nie ma w ogóle, nic nie robimy
return
# ewentualnie regex
# Pobieramy regex tylko raz globalnie
regex_lines = ""
if user_settings and user_settings.regex_deploy_enabled:
regex_lines = generate_regex_hosts(user_id)
@@ -1192,17 +1214,15 @@ def deploy_user(user_id):
if not h.auto_deploy_enabled:
continue
# Który plik wgrywać?
if h.preferred_hostfile_id:
chosen_file = HostFile.query.filter_by(id=h.preferred_hostfile_id, user_id=user_id).first()
if not chosen_file:
# fallback do default
chosen_file = default_file
else:
chosen_file = default_file
# final_content
final_content = regex_lines + chosen_file.content
# Dołączamy regex_lines tylko, jeśli dla hosta nie wyłączono tej opcji
final_content = ("" if h.disable_regex_deploy else regex_lines) + chosen_file.content
try:
if h.type == 'mikrotik':
@@ -1213,7 +1233,6 @@ def deploy_user(user_id):
user_id=user_id
))
elif h.use_daemon and h.type == 'linux':
# Demon
import requests
adjusted_content = ensure_local_defaults(final_content)
wrapped_content = wrap_content_with_comments(adjusted_content)
@@ -1223,12 +1242,13 @@ def deploy_user(user_id):
if resp.status_code != 200:
raise Exception(f"Daemon POST error: {resp.status_code} - {resp.text}")
daemon_str = h.daemon_url.split("://")[-1]
daemon_ip = daemon_str.split(":")[0]
db.session.add(DeployLog(
details=f'[LINUX/DAEMON] Updated {h.hostname} for user {user_id}',
details=f'[LINUX/DAEMON] Updated {h.hostname} (Daemon IP: {daemon_ip}) for user {user_id}',
user_id=user_id
))
else:
# standard linux - SSH
ssh = open_ssh_connection(h)
adjusted_content = ensure_local_defaults(final_content)
wrapped_content = wrap_content_with_comments(adjusted_content)
@@ -1250,10 +1270,10 @@ def deploy_user(user_id):
db.session.add(DeployLog(
details=f'Failed to update {h.hostname}: {str(e)} for user {user_id}',
user_id=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")
@@ -1468,11 +1488,12 @@ def update_host_automation(id):
host.auto_deploy_enabled = enabled
elif setting == 'auto_backup':
host.auto_backup_enabled = enabled
elif setting == 'disable_regex':
host.disable_regex_deploy = enabled
db.session.commit()
flash('Ustawienia automatyzacji zostały zaktualizowane.', 'success')
return redirect(url_for('server_list'))
# -------------------
# EDYCJA LOKALNEGO PLIKU HOSTS
# -------------------