diff --git a/app.py b/app.py index 2a0a827..af985fc 100644 --- a/app.py +++ b/app.py @@ -1253,13 +1253,17 @@ def deploy_user(user_id): default_file = HostFile.query.filter_by(user_id=user_id, title="Default Hosts").first() if not default_file: return + regex_lines = "" if user_settings and user_settings.regex_deploy_enabled: regex_lines = generate_regex_hosts(user_id) + hosts = Host.query.filter_by(user_id=user_id).all() for h in hosts: if not h.auto_deploy_enabled: continue + + # Pobranie pliku hosts wybranego dla serwera 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: @@ -1267,31 +1271,39 @@ def deploy_user(user_id): else: chosen_file = default_file - # Jeśli `disable_local_default` jest włączone, pomijamy lokalne ustawienia - final_content = ("" if h.disable_regex_deploy else regex_lines) + \ - ("" if h.disable_local_default else ensure_local_defaults(chosen_file.content, user_id)) + # 🛠 Poprawiona logika final_content: + final_content = chosen_file.content # Zawsze dodajemy podstawowy plik hosts + + # Jeśli regex deploy jest włączony, dodaj regex_lines + if not h.disable_regex_deploy: + final_content = regex_lines + final_content + + # Jeśli local-defaults jest włączone, dodaj je + if not h.disable_local_default: + final_content = ensure_local_defaults(final_content, user_id) try: + # 🖥 Wgrywanie na Mikrotik if h.type == 'mikrotik': wrapped_content = wrap_mikrotik_content(final_content) deploy_mikrotik(h, wrapped_content) log_details = f'[MIKROTIK] Updated {format_host(h)} for user {user_id}' - db.session.add(DeployLog(details=log_details, user_id=user_id)) + + # 🖥 Wgrywanie na Linux Daemon elif h.use_daemon and h.type == 'linux': import requests - adjusted_content = ensure_local_defaults(final_content, user_id) if not h.disable_local_default else final_content - wrapped_content = wrap_content_with_comments(adjusted_content) + wrapped_content = wrap_content_with_comments(final_content) url = h.daemon_url.rstrip('/') + '/hosts' headers = {"Authorization": h.daemon_token} resp = requests.post(url, json={"hosts": wrapped_content}, headers=headers, timeout=10, verify=False) if resp.status_code != 200: raise Exception(f"Daemon POST error: {resp.status_code} - {resp.text}") log_details = f'[LINUX/DAEMON] Updated {format_host(h)} for user {user_id}' - db.session.add(DeployLog(details=log_details, user_id=user_id)) + + # 🖥 Wgrywanie na standardowy Linux przez SSH else: ssh = open_ssh_connection(h) - adjusted_content = ensure_local_defaults(final_content, user_id) if not h.disable_local_default else final_content - wrapped_content = wrap_content_with_comments(adjusted_content) + wrapped_content = wrap_content_with_comments(final_content) with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmpf: tmpf.write(wrapped_content) tmp_file_path = tmpf.name @@ -1301,10 +1313,14 @@ def deploy_user(user_id): ssh.close() os.remove(tmp_file_path) log_details = f'[LINUX] Updated {format_host(h)} for user {user_id}' - db.session.add(DeployLog(details=log_details, user_id=user_id)) + + # Logowanie wdrożenia + db.session.add(DeployLog(details=log_details, user_id=user_id)) db.session.commit() + except Exception as e: - db.session.add(DeployLog(details=f'Failed to update {format_host(h)}: {str(e)} for user {user_id}', user_id=user_id)) + error_log = f'Failed to update {format_host(h)}: {str(e)} for user {user_id}' + db.session.add(DeployLog(details=error_log, user_id=user_id)) db.session.commit()