import paramiko, sys
sys.stdout.reconfigure(encoding="utf-8")

UNRAID_HOST = "192.168.1.76"
UNRAID_USER = "root"
UNRAID_PASS = "7309208104"
APP_SRC = r"U:\PythonProject\Janssen\EmailsImport\DockerCustomApp\app.py"
APP_DST = "/mnt/user/appdata/msgreceiver/app.py"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(UNRAID_HOST, username=UNRAID_USER, password=UNRAID_PASS)

sftp = ssh.open_sftp()
sftp.put(APP_SRC, APP_DST)
sftp.close()
print(f"Nahráno: {APP_DST}")

cmds = [
    "cd /mnt/user/appdata/msgreceiver && docker build -t msgreceiver . 2>&1 | tail -5",
    "docker stop msgreceiver && docker rm msgreceiver",
    "docker run -d --name msgreceiver -p 8765:8765 -v /mnt/user/JNJEMAILS:/msgs --restart unless-stopped msgreceiver",
    "docker ps --filter name=msgreceiver --format '{{.Names}} {{.Status}}'",
]
for cmd in cmds:
    _, out, err = ssh.exec_command(cmd)
    output = out.read().decode() + err.read().decode()
    print(f"$ {cmd[:60]}\n{output.strip()}\n")

ssh.close()
