77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import paramiko
|
|
import os
|
|
import sys
|
|
|
|
host = "26.0.12.13"
|
|
user = "root"
|
|
password = "root@1234"
|
|
local_dist = r"C:\Users\wangxj\tokenFactory\web\dist"
|
|
remote_dist = "/root/tokenFactory/web/dist"
|
|
|
|
print("Connecting...")
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
ssh.connect(host, username=user, password=password)
|
|
|
|
# Clean old dist files AND rebuild Go
|
|
print("Cleaning old dist and rebuilding...")
|
|
stdin, stdout, stderr = ssh.exec_command(
|
|
"rm -rf /root/tokenFactory/web/dist && mkdir -p /root/tokenFactory/web/dist"
|
|
)
|
|
stdout.read()
|
|
|
|
sftp = ssh.open_sftp()
|
|
|
|
uploaded = 0
|
|
for root, dirs, files in os.walk(local_dist):
|
|
rel_path = os.path.relpath(root, local_dist)
|
|
if rel_path == ".":
|
|
remote_dir = remote_dist
|
|
else:
|
|
remote_dir = remote_dist + "/" + rel_path.replace("\\", "/")
|
|
stdin, stdout, stderr = ssh.exec_command(f"mkdir -p {remote_dir}")
|
|
stdout.read()
|
|
|
|
for f in files:
|
|
local_file = os.path.join(root, f)
|
|
remote_file = (remote_dir + "/" + f).replace("\\", "/")
|
|
try:
|
|
sftp.put(local_file, remote_file)
|
|
uploaded += 1
|
|
if uploaded % 30 == 0:
|
|
print(f" Uploaded {uploaded} files...")
|
|
except Exception as e:
|
|
print(f" FAILED: {f}: {e}")
|
|
|
|
sftp.close()
|
|
print(f"Uploaded {uploaded} files.")
|
|
|
|
# Rebuild Go
|
|
print("Building Go...")
|
|
stdin, stdout, stderr = ssh.exec_command(
|
|
"export PATH=$PATH:/usr/local/go/bin && cd /root/tokenFactory && go build -o tf . 2>&1"
|
|
)
|
|
out = stdout.read().decode()
|
|
err = stderr.read().decode()
|
|
if out.strip():
|
|
print("stdout:", out[-300:].strip())
|
|
if err.strip():
|
|
print("stderr:", err[-300:].strip())
|
|
|
|
# Replace binary file (not in a subdirectory — Docker mounts ./token-factory as a file)
|
|
print("Replacing binary...")
|
|
stdin, stdout, stderr = ssh.exec_command(
|
|
"rm -rf /root/tokenFactory/token-factory && cp /root/tokenFactory/tf /root/tokenFactory/token-factory && ls -la /root/tokenFactory/token-factory"
|
|
)
|
|
print(stdout.read().decode().strip())
|
|
|
|
# Docker restart
|
|
print("Restarting Docker...")
|
|
stdin, stdout, stderr = ssh.exec_command(
|
|
"cd /root/tokenFactory && docker compose down 2>&1 && docker compose up -d 2>&1"
|
|
)
|
|
print(stdout.read().decode()[-500:].strip())
|
|
|
|
ssh.close()
|
|
print("Done!")
|