Update server6.py
Browse files- server6.py +47 -52
server6.py
CHANGED
|
@@ -157,35 +157,6 @@ def kill_frpc_processes():
|
|
| 157 |
print(f"结束frpc进程时出错: {e}")
|
| 158 |
kill_frpc_processes()
|
| 159 |
|
| 160 |
-
|
| 161 |
-
# Oneapi中转
|
| 162 |
-
def Oneapi():
|
| 163 |
-
print('-----------Oneapi中转开始-------------')
|
| 164 |
-
# 删除旧的 one-api 文件夹(如果存在)
|
| 165 |
-
os.system("rm -rf /one/one-api") # 使用 -f 参数以避免错误
|
| 166 |
-
os.system("mkdir -p /one") # 创建目录
|
| 167 |
-
# 下载 one-api 文件
|
| 168 |
-
url = "https://github.com/songquanpeng/one-api/releases/download/v0.6.8/one-api"
|
| 169 |
-
os.system(f"wget {url} -O /one/one-api") # 正确下载并重命名为 /one/one-api
|
| 170 |
-
# 修改文件权限,使其可执行
|
| 171 |
-
os.system("chmod +x /one/one-api")
|
| 172 |
-
# 设置环境变量
|
| 173 |
-
os.environ["THEME"] = "berry"
|
| 174 |
-
os.environ["TIKTOKEN_CACHE_DIR"] = os.getcwd() # 使用Python的os.getcwd()获取当前工作目录
|
| 175 |
-
# 设置MySQL数据库的环境变量
|
| 176 |
-
db_user = "oneapi"
|
| 177 |
-
db_password = "epBjK5ZKysCe6yr3"
|
| 178 |
-
db_host = "106.52.51.116"
|
| 179 |
-
db_port = "3306"
|
| 180 |
-
db_name = "oneapi"
|
| 181 |
-
dsn = f"{db_user}:{db_password}@tcp({db_host}:{db_port})/{db_name}"
|
| 182 |
-
os.environ["SQL_DSN"] = dsn
|
| 183 |
-
command = ['/one/one-api', '--port', '3000']
|
| 184 |
-
process = subprocess.Popen(command) # 启动程序并将其放到后台执行
|
| 185 |
-
print(f"OneAPI程序已在后台启动,进程ID为:{process.pid}")
|
| 186 |
-
print('-----------Oneapi中转结束-------------')
|
| 187 |
-
# Oneapi()
|
| 188 |
-
|
| 189 |
# SSH
|
| 190 |
def run_command(command):
|
| 191 |
try:
|
|
@@ -668,30 +639,54 @@ def SillyTavern():
|
|
| 668 |
if not download_success or not os.path.exists(f'/{huggingface_Data_name}.zip'):
|
| 669 |
print(f"下载 {huggingface_Data_name}.zip 失败 {max_retries} 次,切换到rsync同步")
|
| 670 |
os.system("mkdir -p /root/SillyTavern")
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
# 添加进度显示
|
| 678 |
-
rsync_cmd = f"rsync -avz --progress --stats --timeout=600 -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/"
|
| 679 |
-
print(f"执行命令: {rsync_cmd}")
|
| 680 |
-
subprocess.run(rsync_cmd, shell=True, check=True)
|
| 681 |
-
print("rsync同步完成")
|
| 682 |
-
except subprocess.CalledProcessError as e:
|
| 683 |
-
print(f"rsync同步失败: {e.returncode}")
|
| 684 |
-
if e.stdout:
|
| 685 |
-
print(f"输出: {e.stdout}")
|
| 686 |
-
if e.stderr:
|
| 687 |
-
print(f"错误: {e.stderr}")
|
| 688 |
-
print("尝试再次生成SSH密钥并重试...")
|
| 689 |
-
generate_ssh_key()
|
| 690 |
try:
|
| 691 |
-
|
| 692 |
-
print("
|
| 693 |
-
|
| 694 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
else:
|
| 696 |
# 如果文件存在,可以继续执行后续代码
|
| 697 |
print(f"文件 /{huggingface_Data_name}.zip 存在,继续执行程序。")
|
|
|
|
| 157 |
print(f"结束frpc进程时出错: {e}")
|
| 158 |
kill_frpc_processes()
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
# SSH
|
| 161 |
def run_command(command):
|
| 162 |
try:
|
|
|
|
| 639 |
if not download_success or not os.path.exists(f'/{huggingface_Data_name}.zip'):
|
| 640 |
print(f"下载 {huggingface_Data_name}.zip 失败 {max_retries} 次,切换到rsync同步")
|
| 641 |
os.system("mkdir -p /root/SillyTavern")
|
| 642 |
+
|
| 643 |
+
# 添加rsync重试次数计数器
|
| 644 |
+
rsync_retry_count = 0
|
| 645 |
+
max_rsync_retries = 10
|
| 646 |
+
|
| 647 |
+
while rsync_retry_count < max_rsync_retries:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
try:
|
| 649 |
+
print(f"rsync同步尝试 {rsync_retry_count + 1}/{max_rsync_retries}")
|
| 650 |
+
print("开始生成SSH密钥...")
|
| 651 |
+
generate_ssh_key()
|
| 652 |
+
print("复制SSH密钥到远程服务器...")
|
| 653 |
+
copy_public_key_to_remote(ssh_url, ssh_port, "root", "qilan")
|
| 654 |
+
print(f"开始从 {ssh_url}:{ssh_port} 同步数据到 {data_folder}")
|
| 655 |
+
# 添加进度显示
|
| 656 |
+
rsync_cmd = f"rsync -avz --progress --stats --timeout=600 -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/"
|
| 657 |
+
print(f"执行命令: {rsync_cmd}")
|
| 658 |
+
|
| 659 |
+
# 设置超时
|
| 660 |
+
result = subprocess.run(rsync_cmd, shell=True, check=True, timeout=1800) # 30分钟超时
|
| 661 |
+
print("rsync同步完成")
|
| 662 |
+
break # 同步成功,跳出循环
|
| 663 |
+
|
| 664 |
+
except subprocess.TimeoutExpired:
|
| 665 |
+
print(f"rsync同步超时,这是第 {rsync_retry_count + 1} 次尝试")
|
| 666 |
+
rsync_retry_count += 1
|
| 667 |
+
|
| 668 |
+
except subprocess.CalledProcessError as e:
|
| 669 |
+
print(f"rsync同步失败: 返回码 {e.returncode}")
|
| 670 |
+
if hasattr(e, 'stdout') and e.stdout:
|
| 671 |
+
print(f"输出: {e.stdout}")
|
| 672 |
+
if hasattr(e, 'stderr') and e.stderr:
|
| 673 |
+
print(f"错误: {e.stderr}")
|
| 674 |
+
|
| 675 |
+
rsync_retry_count += 1
|
| 676 |
+
|
| 677 |
+
except Exception as e:
|
| 678 |
+
print(f"rsync同步过程中发生未知错误: {str(e)}")
|
| 679 |
+
rsync_retry_count += 1
|
| 680 |
+
|
| 681 |
+
# 检查是否达到最大重试次数
|
| 682 |
+
if rsync_retry_count >= max_rsync_retries:
|
| 683 |
+
print(f"rsync同步失败达到最大重试次数 {max_rsync_retries},结束程序")
|
| 684 |
+
sys.exit(1) # 使用非零退出码表示错误
|
| 685 |
+
|
| 686 |
+
# 等待一段时间后重试
|
| 687 |
+
wait_time = 30
|
| 688 |
+
print(f"等待 {wait_time} 秒后重试...")
|
| 689 |
+
time.sleep(wait_time)
|
| 690 |
else:
|
| 691 |
# 如果文件存在,可以继续执行后续代码
|
| 692 |
print(f"文件 /{huggingface_Data_name}.zip 存在,继续执行程序。")
|