Update server.py
Browse files
server.py
CHANGED
|
@@ -357,7 +357,7 @@ def copy_public_key_to_remote(host, port, user, password):
|
|
| 357 |
generate_ssh_key()
|
| 358 |
os.system('chmod 600 ~/.ssh/id_rsa')
|
| 359 |
# # 复制公钥到远程服务器
|
| 360 |
-
copy_public_key_to_remote(
|
| 361 |
|
| 362 |
def write_rsyncd_conf(path, port=7071):
|
| 363 |
rsyncd_conf_content = f"""
|
|
@@ -385,96 +385,91 @@ def requests_retry_session(retries=3, backoff_factor=0.3, status_forcelist=(500,
|
|
| 385 |
session.mount('https://', adapter)
|
| 386 |
return session
|
| 387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
def check_api1():
|
| 389 |
# 第一次检查 SillyTavern 是否启动
|
| 390 |
-
thread_started = False
|
| 391 |
-
n=0
|
| 392 |
while True:
|
| 393 |
-
n =
|
| 394 |
if not os.path.exists(data_folder):
|
| 395 |
-
|
| 396 |
break
|
| 397 |
try:
|
| 398 |
url = "http://127.0.0.1:8000/v1"
|
| 399 |
response = requests.get(url, timeout=30)
|
| 400 |
|
| 401 |
-
# 检查响应状态码
|
| 402 |
if response.status_code == 403:
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
break # 接口正常,退出循环
|
| 406 |
|
| 407 |
-
|
| 408 |
-
print("SillyTavern未启动,状态码:", response.status_code,n)
|
| 409 |
|
| 410 |
except requests.exceptions.RequestException as e:
|
| 411 |
-
|
| 412 |
|
| 413 |
-
time.sleep(5)
|
| 414 |
|
| 415 |
-
|
| 416 |
Service_status = False
|
| 417 |
-
# 第二次检查接口状态
|
| 418 |
n = 0
|
| 419 |
nn = 0
|
| 420 |
while True:
|
| 421 |
-
n =
|
| 422 |
-
if not os.path.exists(data_folder):
|
| 423 |
-
|
| 424 |
break
|
| 425 |
if Service_status:
|
| 426 |
-
|
| 427 |
break
|
| 428 |
-
url = f"{st_url}/csrf-token"
|
| 429 |
try:
|
| 430 |
response = requests_retry_session().get(url, timeout=30)
|
| 431 |
-
response.raise_for_status()
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
print(url,"接口返回信息:", response_json)
|
| 435 |
if "token" in response_json:
|
| 436 |
-
|
| 437 |
nn = 0
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
|
|
|
|
|
|
|
|
|
| 447 |
else:
|
| 448 |
-
|
| 449 |
except requests.exceptions.ConnectionError as e:
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
if nn == 5:
|
| 454 |
break
|
| 455 |
-
|
| 456 |
except requests.exceptions.Timeout as e:
|
| 457 |
-
|
| 458 |
-
# threading.Thread(target=Frpc, daemon=True).start()
|
| 459 |
break
|
| 460 |
-
|
| 461 |
except requests.exceptions.RequestException as e:
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
break
|
| 465 |
|
| 466 |
-
time.sleep(
|
| 467 |
|
| 468 |
|
| 469 |
def frp(configs, backstage):
|
| 470 |
-
|
| 471 |
print('-----------FRP开始-------------')
|
| 472 |
-
|
| 473 |
-
# 创建 FRP 文件夹并下载 frpc 可执行文件。
|
| 474 |
-
os.system("mkdir -p /content/frp")
|
| 475 |
-
os.system("wget /content/frp/frpc https://huggingface.co/Qilan2/box/resolve/main/frp/frpc -O /content/frp/frpc")
|
| 476 |
-
os.system("chmod +x /content/frp/frpc")
|
| 477 |
-
|
| 478 |
# 遍历所有配置并启动 FRP。
|
| 479 |
for config_data, config_file_path in configs:
|
| 480 |
with open(config_file_path, 'w') as config_file:
|
|
@@ -640,7 +635,7 @@ def SillyTavern():
|
|
| 640 |
if not os.path.exists(f'/{huggingface_Data_name}.zip'):
|
| 641 |
print(f"文件 /{huggingface_Data_name}.zip 不存在,启用实时同步。")
|
| 642 |
generate_ssh_key()
|
| 643 |
-
copy_public_key_to_remote(
|
| 644 |
subprocess.run(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/", shell=True, check=True)
|
| 645 |
# subprocess.run(f"rsync -avz --port=7075 ljj@{frp_url}::qilan {data_folder}/", shell=True, check=True)
|
| 646 |
# sys.exit()
|
|
|
|
| 357 |
generate_ssh_key()
|
| 358 |
os.system('chmod 600 ~/.ssh/id_rsa')
|
| 359 |
# # 复制公钥到远程服务器
|
| 360 |
+
copy_public_key_to_remote(ssh_url, ssh_port, "root", "qilan")
|
| 361 |
|
| 362 |
def write_rsyncd_conf(path, port=7071):
|
| 363 |
rsyncd_conf_content = f"""
|
|
|
|
| 385 |
session.mount('https://', adapter)
|
| 386 |
return session
|
| 387 |
|
| 388 |
+
import os
|
| 389 |
+
import time
|
| 390 |
+
import requests
|
| 391 |
+
import subprocess
|
| 392 |
+
import logging
|
| 393 |
+
|
| 394 |
+
# 配置日志
|
| 395 |
+
logging.basicConfig(level=logging.INFO)
|
| 396 |
+
|
| 397 |
def check_api1():
|
| 398 |
# 第一次检查 SillyTavern 是否启动
|
| 399 |
+
thread_started = False
|
| 400 |
+
n = 0
|
| 401 |
while True:
|
| 402 |
+
n += 1
|
| 403 |
if not os.path.exists(data_folder):
|
| 404 |
+
logging.error(f"/sillytavern/ 文件不存在,结束检测")
|
| 405 |
break
|
| 406 |
try:
|
| 407 |
url = "http://127.0.0.1:8000/v1"
|
| 408 |
response = requests.get(url, timeout=30)
|
| 409 |
|
|
|
|
| 410 |
if response.status_code == 403:
|
| 411 |
+
logging.info("SillyTavern已经启动")
|
| 412 |
+
break
|
|
|
|
| 413 |
|
| 414 |
+
logging.info("SillyTavern未启动,状态码:", response.status_code, n)
|
|
|
|
| 415 |
|
| 416 |
except requests.exceptions.RequestException as e:
|
| 417 |
+
logging.error(f"SillyTavern未启动3: {e}, 尝试次数: {n}")
|
| 418 |
|
| 419 |
+
time.sleep(5)
|
| 420 |
|
| 421 |
+
logging.info('开始检测接口状态')
|
| 422 |
Service_status = False
|
|
|
|
| 423 |
n = 0
|
| 424 |
nn = 0
|
| 425 |
while True:
|
| 426 |
+
n += 1
|
| 427 |
+
if not os.path.exists(data_folder):
|
| 428 |
+
logging.error(f"/sillytavern/ 文件不存在,结束检测")
|
| 429 |
break
|
| 430 |
if Service_status:
|
| 431 |
+
logging.info("结束检测")
|
| 432 |
break
|
| 433 |
+
url = f"{st_url}/csrf-token"
|
| 434 |
try:
|
| 435 |
response = requests_retry_session().get(url, timeout=30)
|
| 436 |
+
response.raise_for_status()
|
| 437 |
+
response_json = response.json()
|
| 438 |
+
logging.info("%s 接口返回信息: %s", url, response_json) # 修正日志记录
|
|
|
|
| 439 |
if "token" in response_json:
|
| 440 |
+
logging.info("检测到 token")
|
| 441 |
nn = 0
|
| 442 |
+
retry_count = 3 # 设置重试次数
|
| 443 |
+
for attempt in range(retry_count):
|
| 444 |
+
try:
|
| 445 |
+
|
| 446 |
+
subprocess.run(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/",
|
| 447 |
+
shell=True, check=True, timeout=600)
|
| 448 |
+
break # 如果成功,跳出重试循环
|
| 449 |
+
except subprocess.CalledProcessError as e:
|
| 450 |
+
logging.error(f"Rsync 失败,返回码: {e.returncode}, 尝试次数: {attempt + 1}")
|
| 451 |
+
if attempt == retry_count - 1:
|
| 452 |
+
logging.error("所有重试均失败,停止尝试。")
|
| 453 |
+
generate_ssh_key() # 处理 SSH 密钥生成
|
| 454 |
else:
|
| 455 |
+
logging.info("%s 接口返回信息: %s", url, response_json) # 修正日志记录
|
| 456 |
except requests.exceptions.ConnectionError as e:
|
| 457 |
+
logging.error(f"接口异常1: {e}, 尝试次数: {n}, nn: {nn}")
|
| 458 |
+
nn += 1
|
| 459 |
+
if nn > 10:
|
|
|
|
| 460 |
break
|
|
|
|
| 461 |
except requests.exceptions.Timeout as e:
|
| 462 |
+
logging.error(f"接口异常2: {e}, 尝试次数: {n}")
|
|
|
|
| 463 |
break
|
|
|
|
| 464 |
except requests.exceptions.RequestException as e:
|
| 465 |
+
logging.error(f"接口异常3: {e}, 尝试次数: {n}")
|
| 466 |
+
break
|
|
|
|
| 467 |
|
| 468 |
+
time.sleep(5)
|
| 469 |
|
| 470 |
|
| 471 |
def frp(configs, backstage):
|
|
|
|
| 472 |
print('-----------FRP开始-------------')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
# 遍历所有配置并启动 FRP。
|
| 474 |
for config_data, config_file_path in configs:
|
| 475 |
with open(config_file_path, 'w') as config_file:
|
|
|
|
| 635 |
if not os.path.exists(f'/{huggingface_Data_name}.zip'):
|
| 636 |
print(f"文件 /{huggingface_Data_name}.zip 不存在,启用实时同步。")
|
| 637 |
generate_ssh_key()
|
| 638 |
+
copy_public_key_to_remote(ssh_url, ssh_port, "root", "qilan")
|
| 639 |
subprocess.run(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/", shell=True, check=True)
|
| 640 |
# subprocess.run(f"rsync -avz --port=7075 ljj@{frp_url}::qilan {data_folder}/", shell=True, check=True)
|
| 641 |
# sys.exit()
|