main
黄海 5 months ago
parent 8891710bba
commit 9bfffa3e10

@ -0,0 +1,100 @@
import os
import paramiko
def upload_files(sftp, local_path, remote_path):
# 遍历本地目录
for root, dirs, files in os.walk(local_path):
# 排除 __pycache__ 目录
if '__pycache__' in dirs:
dirs.remove('__pycache__')
# 打印当前目录和文件
print(f"Current directory: {root}")
print(f"Files to upload: {files}")
for file in files:
local_file = os.path.join(root, file)
# 计算相对路径
relative_path = os.path.relpath(local_file, local_path)
# 计算远程文件路径
remote_file = os.path.join(remote_path, relative_path).replace("\\", "/") # 替换反斜杠为正斜杠
# 创建远程目录
remote_dir = os.path.dirname(remote_file)
try:
# 创建所有必要的远程目录
sftp.mkdir(remote_dir)
print(f"Created remote directory: {remote_dir}")
except IOError:
# 目录已存在,忽略错误
print(f"Remote directory already exists: {remote_dir}")
# 上传文件
try:
sftp.put(local_file, remote_file)
print(f"Uploaded: {local_file} to {remote_file}")
except Exception as e:
print(f"Failed to upload {local_file} to {remote_file}: {e}")
def execute_remote_command(ssh, command):
stdin, stdout, stderr = ssh.exec_command(command)
print(stdout.read().decode())
print(stderr.read().decode())
def main():
# SFTP 连接信息
hostname = '10.10.14.250'
port = 22
username = 'root'
password = 'dsideal'
remote_path = '/usr/local/SyncData'
venv_path = '/usr/local/SyncData/venv' # 虚拟环境路径
# 创建 SSH 客户端
transport = paramiko.Transport((hostname, port))
try:
transport.connect(username=username, password=password)
except Exception as e:
print(f"Failed to connect to SFTP server: {e}")
return
# 创建 SFTP 客户端
sftp = paramiko.SFTPClient.from_transport(transport)
try:
# 获取当前目录
local_path = os.getcwd()
print(f"Local path: {local_path}")
# 上传文件
upload_files(sftp, local_path, remote_path)
# 创建 SSH 客户端
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, port, username, password)
# 列出远程目录内容
list_command = f'ls -l {remote_path}'
execute_remote_command(ssh, list_command)
# 检查 requirements.txt 是否存在
check_command = f'ls {remote_path}/requirements.txt'
execute_remote_command(ssh, check_command)
# 创建虚拟环境
create_venv_command = f'python3 -m venv {venv_path}'
execute_remote_command(ssh, create_venv_command)
# 激活虚拟环境并安装依赖
install_command = f'{venv_path}/bin/pip install -r {remote_path}/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple'
execute_remote_command(ssh, install_command)
finally:
# 关闭 SFTP 和 SSH 连接
sftp.close()
transport.close()
ssh.close()
if __name__ == "__main__":
main()

@ -2,5 +2,6 @@ pymysql==1.1.1
clickhouse-driver==0.2.9
tqdm==4.67.1
psutil==7.0.0
python-dateutil==2.8.2
python-dateutil==2.9.0post0
APScheduler==3.10.4
paramiko==3.5.0

Loading…
Cancel
Save