import os import sys import time from pathlib import Path import paramiko HOST = "140.44.44.80" USER = "root" KEY_FILE = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet" LOCAL_SEEDS_DIR = Path(r"C:\Users\Snarf\OneDrive\Desktop\code\SnarfCode\snarfnet-game-seeds") def main(): print(f"Connecting to {USER}@{HOST} with SSH key...") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(HOST, username=USER, key_filename=KEY_FILE, timeout=10) print("Connected successfully!") def run(cmd, check=True): print(f"\n>>> [RUN] {cmd}") stdin, stdout, stderr = ssh.exec_command(cmd) while not stdout.channel.exit_status_ready(): time.sleep(0.5) out = stdout.read().decode() err = stderr.read().decode() rc = stdout.channel.recv_exit_status() if out: print(out.strip()) if err and "warning" not in err.lower(): print(f"[STDERR] {err.strip()}", file=sys.stderr) if check and rc != 0: print(f"[FAIL] Exit code: {rc}", file=sys.stderr) return rc, out, err # Step 1: Configure Persistent DNS in systemd-resolved print("\n--- Step 1: Persist DNS in systemd-resolved ---") dns_conf = """[Resolve] DNS=1.1.1.1 8.8.8.8 FallbackDNS=1.0.0.1 8.8.4.4 Domains=snarfnet.net """ run(f"cat << 'EOF' > /etc/systemd/resolved.conf.d/snarfnet-dns.conf\n{dns_conf}\nEOF") run("systemctl restart systemd-resolved") run("resolvectl dns enp0s31f6 1.1.1.1 8.8.8.8 || true") # Step 2: Expand LVM to use all 1.8TB on sda print("\n--- Step 2: Expand LVM to full 1.8TB disk ---") run("lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv -r || true") run("df -h /") # Step 3: Install Essential Packages print("\n--- Step 3: Apt Update & Install Tools ---") run("DEBIAN_FRONTEND=noninteractive apt-get update") run("DEBIAN_FRONTEND=noninteractive apt-get install -y curl wget git jq ufw nfs-common ethtool net-tools python3 python3-yaml python3-pip apt-transport-https ca-certificates gnupg lsb-release") # Step 4: Configure Wake-on-LAN persistently print("\n--- Step 4: Configure Wake-on-LAN ---") run("ethtool -s enp0s31f6 wol g || true") wol_svc = """[Unit] Description=Enable Wake-on-LAN on enp0s31f6 After=network.target [Service] Type=oneshot ExecStart=/sbin/ethtool -s enp0s31f6 wol g [Install] WantedBy=basic.target """ run(f"cat << 'EOF' > /etc/systemd/system/wol.service\n{wol_svc}\nEOF") run("systemctl daemon-reload && systemctl enable wol.service") # Step 5: Mount Synology NAS Game Backup Share print("\n--- Step 5: Configure Synology NAS NFS Mount ---") run("mkdir -p /mnt/nas/game-backups") run("grep -qs '/mnt/nas/game-backups' /etc/fstab || echo '14.10.10.71:/volume1/docker/game-backups /mnt/nas/game-backups nfs rw,hard,intr,nolock,timeo=14 0 0' >> /etc/fstab") run("mount -a || true") run("df -h /mnt/nas/game-backups") # Step 6: Install Docker & Compose print("\n--- Step 6: Install Docker Engine & Compose ---") rc, out, _ = run("docker --version", check=False) if rc != 0: run("curl -fsSL https://get.docker.com | sh") run("systemctl enable docker && systemctl start docker") run("usermod -aG docker snarf") run("docker --version && docker compose version") # Step 7: Prepare Directory Structure print("\n--- Step 7: Game Server Storage Directories ---") run("mkdir -p /opt/game-servers/{enshrouded,fs25,stationeers,valheim,vrising}/{savegame,saves,worlds,data}") run("mkdir -p /opt/snarfnet-game-seeds") run("chown -R snarf:snarf /opt/game-servers") run("chmod -R 775 /opt/game-servers") # Step 8: Upload snarfnet-game-seeds print("\n--- Step 8: Upload snarfnet-game-seeds repo ---") sftp = ssh.open_sftp() def put_dir(local_path, remote_path): for item in os.listdir(local_path): s = os.path.join(local_path, item) d = f"{remote_path}/{item}" if os.path.isdir(s): try: sftp.mkdir(d) except Exception: pass put_dir(s, d) else: sftp.put(s, d) print(f"Uploaded: {d}") try: sftp.mkdir("/opt/snarfnet-game-seeds") except Exception: pass put_dir(str(LOCAL_SEEDS_DIR), "/opt/snarfnet-game-seeds") sftp.close() run("chown -R snarf:snarf /opt/snarfnet-game-seeds") run("chmod +x /opt/snarfnet-game-seeds/core/manager.py /opt/snarfnet-game-seeds/scripts/*.sh || true") # Step 9: Configure Backup Systemd Timer print("\n--- Step 9: Configure Automated Save Backup Timer ---") backup_svc = """[Unit] Description=SnarfNet Declarative Game Server Save Backup After=network.target docker.service [Service] Type=oneshot WorkingDirectory=/opt/snarfnet-game-seeds ExecStart=/usr/bin/python3 /opt/snarfnet-game-seeds/core/manager.py backup [Install] WantedBy=multi-user.target """ backup_timer = """[Unit] Description=Nightly Game Save Backup Timer (04:00 AM) [Timer] OnCalendar=*-*-* 04:00:00 Persistent=true [Install] WantedBy=timers.target """ run(f"cat << 'EOF' > /etc/systemd/system/gameseed-backup.service\n{backup_svc}\nEOF") run(f"cat << 'EOF' > /etc/systemd/system/gameseed-backup.timer\n{backup_timer}\nEOF") run("systemctl daemon-reload && systemctl enable --now gameseed-backup.timer") # Step 10: Generate Docker Compose print("\n--- Step 10: Generate docker-compose.yml from Seeds ---") run("cd /opt/snarfnet-game-seeds && python3 core/manager.py plan") run("cd /opt/snarfnet-game-seeds && python3 core/manager.py compose") # Step 11: SSH Security Hardening (Disable password login, enforce SSH keys) print("\n--- Step 11: Enforce SSH Key Authentication ---") ssh_dropin = """# SnarfNet Hardening: Enforce SSH Key authentication PasswordAuthentication no KbdInteractiveAuthentication no PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys """ run(f"cat << 'EOF' > /etc/ssh/sshd_config.d/99-snarfnet-keys-only.conf\n{ssh_dropin}\nEOF") run("systemctl restart ssh || systemctl restart sshd") print("SSH PasswordAuthentication disabled; SSH Key authentication enforced!") ssh.close() print("\nšŸŽ‰ Deployment completed successfully!") if __name__ == "__main__": main()