Update server3.py
Browse files- server3.py +117 -82
server3.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
ini_content = {"username":"lixuhs875","key":"642ac670e3e0f7a237b53919592582c5"}
|
| 2 |
frp_url = '119.8.118.149'
|
| 3 |
-
ssh_port = '
|
| 4 |
ssh_url = '47.74.57.131'
|
| 5 |
-
st_url = f'http://{frp_url}:
|
| 6 |
huggingface_name = 'lixuhs875'
|
| 7 |
huggingface_Data_name = 'qilan-st1'
|
| 8 |
configs = [
|
|
@@ -11,24 +11,69 @@ configs = [
|
|
| 11 |
server_addr = 47.237.156.222
|
| 12 |
server_port = 7000
|
| 13 |
|
| 14 |
-
[
|
| 15 |
type = tcp
|
| 16 |
local_ip = 127.0.0.1
|
| 17 |
local_port = 8000
|
| 18 |
-
remote_port =
|
| 19 |
|
| 20 |
-
[
|
| 21 |
type = tcp
|
| 22 |
local_ip = 127.0.0.1
|
| 23 |
local_port = 22
|
| 24 |
-
remote_port =
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
type = tcp
|
| 28 |
local_ip = 127.0.0.1
|
| 29 |
local_port = 8000
|
| 30 |
-
remote_port =
|
| 31 |
-
""", '/content/frp/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
]
|
| 33 |
kaggle_metadata = {
|
| 34 |
"title": "Your Dataset Title",
|
|
@@ -183,73 +228,70 @@ def update_last_uploaded_size(size):
|
|
| 183 |
global last_uploaded_size
|
| 184 |
last_uploaded_size = size
|
| 185 |
|
| 186 |
-
def get_folder_size(folder):
|
| 187 |
-
"""使用 du 命令获取文件夹的总大小(字节数)"""
|
| 188 |
-
result = subprocess.run(['du', '-sb', folder], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 189 |
-
if result.returncode == 0:
|
| 190 |
-
size = result.stdout.split()[0] # 获取大小
|
| 191 |
-
return int(size)
|
| 192 |
-
else:
|
| 193 |
-
print(f"Error calculating size: {result.stderr.decode()}")
|
| 194 |
-
return 0
|
| 195 |
-
|
| 196 |
def compress_folder():
|
| 197 |
global last_uploaded_size # 声明使用全局变量
|
|
|
|
| 198 |
# 清理并创建目录 /a
|
| 199 |
if os.path.exists('/a'):
|
| 200 |
subprocess.run(['rm', '-r', '/a/'])
|
|
|
|
| 201 |
os.makedirs('/a', exist_ok=True)
|
|
|
|
| 202 |
with lock: # 使用锁
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
return
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
except subprocess.CalledProcessError as e:
|
| 225 |
-
print(f"命令失败,返回代码: {e.returncode}")
|
| 226 |
-
return
|
| 227 |
-
|
| 228 |
-
print('压缩完成')
|
| 229 |
-
file_path = "/a/sillytavern.ctk"
|
| 230 |
-
subprocess.run(['mv', zip_file_path, file_path])
|
| 231 |
-
print(f"数据压缩为 {file_path}")
|
| 232 |
-
|
| 233 |
-
file_size = os.path.getsize(file_path) # 获取压缩包的大小
|
| 234 |
-
print('文件大小:', file_size)
|
| 235 |
-
|
| 236 |
-
if file_size < (800 * 1024 ** 2): # 判断压缩后的大小是否小于 800MB
|
| 237 |
-
print("压缩文件小于800MB,不进行上传。")
|
| 238 |
-
return
|
| 239 |
-
|
| 240 |
-
# 执行上传
|
| 241 |
if last_uploaded_size == -1:
|
| 242 |
print("首次运行,正在执行上传...")
|
| 243 |
-
update_last_uploaded_size(file_size)
|
| 244 |
|
| 245 |
with open(metadata_file_path, 'w') as json_fid:
|
| 246 |
json.dump(kaggle_metadata, json_fid)
|
| 247 |
-
|
| 248 |
-
subprocess.run(['kaggle', 'datasets', 'version', '-m', 'new version message', '-p', '/a'])
|
|
|
|
| 249 |
last_uploaded_size = file_size
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
| 253 |
|
| 254 |
with open(metadata_file_path, 'w') as json_fid:
|
| 255 |
json.dump(kaggle_metadata, json_fid)
|
|
@@ -258,14 +300,14 @@ def compress_folder():
|
|
| 258 |
last_uploaded_size = file_size
|
| 259 |
|
| 260 |
else:
|
| 261 |
-
print(f"
|
| 262 |
|
| 263 |
|
| 264 |
def repeat_task():
|
| 265 |
print('------打包线程启动-------------')
|
| 266 |
while True:
|
| 267 |
print('等待打包')
|
| 268 |
-
time.sleep(
|
| 269 |
compress_folder()
|
| 270 |
print('------打包线程结束-------------')
|
| 271 |
# repeat_task()
|
|
@@ -394,11 +436,8 @@ def check_api1():
|
|
| 394 |
retry_count = 3 # 设置重试次数
|
| 395 |
for attempt in range(retry_count):
|
| 396 |
try:
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
# os.system(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no -o ConnectTimeout=30' --timeout=300 root@{ssh_url}:{data_folder}/ {data_folder}/
|
| 400 |
-
")
|
| 401 |
-
# subprocess.run(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/",
|
| 402 |
shell=True, check=True, timeout=300)
|
| 403 |
break # 如果成功,跳出重试循环
|
| 404 |
except subprocess.CalledProcessError as e:
|
|
@@ -410,6 +449,7 @@ def check_api1():
|
|
| 410 |
logging.info("%s 接口返回信息: %s", url, response_json) # 修正日志记录
|
| 411 |
except requests.exceptions.ConnectionError as e:
|
| 412 |
logging.error(f"接口异常1: {e}, 尝试次数: {n}, nn: {nn}")
|
|
|
|
| 413 |
nn += 1
|
| 414 |
if nn > 10:
|
| 415 |
break
|
|
@@ -639,28 +679,23 @@ if os.path.exists(f'{data_folder}/start.sh'):
|
|
| 639 |
replace_info_in_file(f'{data_folder}/public/login.html')
|
| 640 |
# threading.Thread(target=repeat_task, daemon=True).start() #压缩备份
|
| 641 |
# backup_thread = threading.Thread(target=repeat_task, daemon=True)
|
| 642 |
-
backup_thread.start()
|
| 643 |
print("开始衔接")
|
| 644 |
Service_status = True
|
| 645 |
frp(configs, True)
|
| 646 |
-
# frp(config_data2,'/content/frp/frpc2.ini',True)
|
| 647 |
-
# frp(config_data,'/content/frp/frpc.ini',True)
|
| 648 |
-
# frp(config_data3,'/content/frp/frpc3.ini',True)
|
| 649 |
# frp(config_data4,'/content/frp/frpc4.ini',True)
|
| 650 |
# 以线程的形式运行 frp 任务
|
| 651 |
# frp_thread1 = threading.Thread(target=frp, args=(config_data2, '/content/frp/frpc2.ini', True))
|
| 652 |
# frp_thread1.start()
|
| 653 |
-
#
|
| 654 |
-
# frp_thread2.start()
|
| 655 |
-
threading.Thread(target=monitor_port, daemon=True).start()#检测端口状态
|
| 656 |
while True:
|
| 657 |
-
if backup_thread.is_alive():
|
| 658 |
-
|
| 659 |
-
else:
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
time.sleep(5)
|
| 665 |
|
| 666 |
else:
|
|
|
|
| 1 |
ini_content = {"username":"lixuhs875","key":"642ac670e3e0f7a237b53919592582c5"}
|
| 2 |
frp_url = '119.8.118.149'
|
| 3 |
+
ssh_port = '7075'
|
| 4 |
ssh_url = '47.74.57.131'
|
| 5 |
+
st_url = f'http://{frp_url}:7074'
|
| 6 |
huggingface_name = 'lixuhs875'
|
| 7 |
huggingface_Data_name = 'qilan-st1'
|
| 8 |
configs = [
|
|
|
|
| 11 |
server_addr = 47.237.156.222
|
| 12 |
server_port = 7000
|
| 13 |
|
| 14 |
+
[safgha]
|
| 15 |
type = tcp
|
| 16 |
local_ip = 127.0.0.1
|
| 17 |
local_port = 8000
|
| 18 |
+
remote_port = 7076
|
| 19 |
|
| 20 |
+
[safab2f]
|
| 21 |
type = tcp
|
| 22 |
local_ip = 127.0.0.1
|
| 23 |
local_port = 22
|
| 24 |
+
remote_port = 7077
|
| 25 |
+
""", '/content/frp/frpc.ini'),
|
| 26 |
+
(f"""
|
| 27 |
+
[common]
|
| 28 |
+
server_addr = {frp_url}
|
| 29 |
+
server_port = 7000
|
| 30 |
+
token = qilan
|
| 31 |
+
[kkjmk1]
|
| 32 |
+
type = tcp
|
| 33 |
+
local_ip = 127.0.0.1
|
| 34 |
+
local_port = 8000
|
| 35 |
+
remote_port = 7076
|
| 36 |
+
""", '/content/frp/frpc1.ini'),
|
| 37 |
+
("""
|
| 38 |
+
[common]
|
| 39 |
+
server_addr = 8.137.94.137
|
| 40 |
+
server_port = 7000
|
| 41 |
+
[kjas2]
|
| 42 |
type = tcp
|
| 43 |
local_ip = 127.0.0.1
|
| 44 |
local_port = 8000
|
| 45 |
+
remote_port = 7077
|
| 46 |
+
""", '/content/frp/frpc2.ini'),
|
| 47 |
+
("""
|
| 48 |
+
[common]
|
| 49 |
+
server_addr = 120.76.218.32
|
| 50 |
+
server_port = 7000
|
| 51 |
+
[kja1sh3]
|
| 52 |
+
type = tcp
|
| 53 |
+
local_ip = 127.0.0.1
|
| 54 |
+
local_port = 8000
|
| 55 |
+
remote_port = 7074
|
| 56 |
+
""", '/content/frp/frpc3.ini'),
|
| 57 |
+
("""
|
| 58 |
+
[common]
|
| 59 |
+
server_addr = 140.143.15.186
|
| 60 |
+
server_port=7000
|
| 61 |
+
[kkji1mk4]
|
| 62 |
+
type=tcp
|
| 63 |
+
local_ip=127.0.0.1
|
| 64 |
+
local_port=8000
|
| 65 |
+
remote_port=7074
|
| 66 |
+
""", '/content/frp/frpc4.ini'),
|
| 67 |
+
(f"""
|
| 68 |
+
[common]
|
| 69 |
+
server_addr = {ssh_url}
|
| 70 |
+
server_port=7000
|
| 71 |
+
[kkji1mk5]
|
| 72 |
+
type=tcp
|
| 73 |
+
local_ip=127.0.0.1
|
| 74 |
+
local_port=22
|
| 75 |
+
remote_port={ssh_port}
|
| 76 |
+
""", '/content/frp/frpc5.ini')
|
| 77 |
]
|
| 78 |
kaggle_metadata = {
|
| 79 |
"title": "Your Dataset Title",
|
|
|
|
| 228 |
global last_uploaded_size
|
| 229 |
last_uploaded_size = size
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
def compress_folder():
|
| 232 |
global last_uploaded_size # 声明使用全局变量
|
| 233 |
+
|
| 234 |
# 清理并创建目录 /a
|
| 235 |
if os.path.exists('/a'):
|
| 236 |
subprocess.run(['rm', '-r', '/a/'])
|
| 237 |
+
|
| 238 |
os.makedirs('/a', exist_ok=True)
|
| 239 |
+
|
| 240 |
with lock: # 使用锁
|
| 241 |
+
if os.path.exists(zip_file_path):
|
| 242 |
+
print(f"压缩文件 {zip_file_path} 已存在,跳过本次压缩。")
|
| 243 |
+
return
|
| 244 |
+
|
| 245 |
+
print('开始压缩数据')
|
| 246 |
+
#result = subprocess.run(['7z', 'a', '-tzip', '-mx=1','-mmt=2', zip_file_path, data_folder])
|
| 247 |
+
# 7z a -tzip -mx=1 -mmt=2 /a/sillytavern.zip /sillytavern/*
|
| 248 |
+
# tar -cvf - /sillytavern | pigz -p 6 -1 > /a/sillytavern.tar.gz
|
| 249 |
+
try:
|
| 250 |
+
result = subprocess.run(
|
| 251 |
+
f'tar -cvf - {data_folder} | pigz -p 2 -1 > {zip_file_path} 2> /dev/null',
|
| 252 |
+
shell=True,
|
| 253 |
+
check=True,
|
| 254 |
+
stdout=subprocess.DEVNULL, # 将标准输出重定向到 /dev/null
|
| 255 |
+
stderr=subprocess.DEVNULL # 将标准错误也重定向到 /dev/null
|
| 256 |
+
)
|
| 257 |
+
except subprocess.CalledProcessError as e:
|
| 258 |
+
print(f"Command failed with return code {e.returncode}")
|
| 259 |
|
| 260 |
+
print('重命名压缩后的文件')
|
| 261 |
+
file_path = "/a/sillytavern.ctk"
|
| 262 |
+
print(zip_file_path)
|
| 263 |
+
|
| 264 |
+
subprocess.run(['mv', zip_file_path,file_path])
|
| 265 |
+
if result.returncode != 0:
|
| 266 |
+
print("压缩失败,请检查7z命令是否正确")
|
| 267 |
return
|
| 268 |
|
| 269 |
+
print(f"Data compressed into {file_path}")
|
| 270 |
+
|
| 271 |
+
file_size = os.path.getsize(file_path) # 获取压缩包的大小
|
| 272 |
+
print('文件大小:', file_size, '上次文件大小:', last_uploaded_size,file_size * 1024 ** 2)
|
| 273 |
+
|
| 274 |
+
# 判断是否大于800MB (800MB = 800 * 1024 * 1024 字节)
|
| 275 |
+
if file_size < (400 * 1024 ** 2):
|
| 276 |
+
print("文件小于800MB,不进行上传。")
|
| 277 |
+
return
|
| 278 |
+
|
| 279 |
+
if last_uploaded_size == -1 or file_size != last_uploaded_size:
|
| 280 |
+
# 判断是否为首次运行或上次上传大小为0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
if last_uploaded_size == -1:
|
| 282 |
print("首次运行,正在执行上传...")
|
| 283 |
+
update_last_uploaded_size(file_size) # 更新为当前文件大小
|
| 284 |
|
| 285 |
with open(metadata_file_path, 'w') as json_fid:
|
| 286 |
json.dump(kaggle_metadata, json_fid)
|
| 287 |
+
os.system("kaggle datasets version -m 'new version message' -p /a")
|
| 288 |
+
#subprocess.run(['kaggle', 'datasets', 'version', '-m', 'new version message', '-p', '/a'])
|
| 289 |
+
# kaggle datasets version -m 'new version message' -p /a
|
| 290 |
last_uploaded_size = file_size
|
| 291 |
+
|
| 292 |
+
elif file_size > last_uploaded_size:
|
| 293 |
+
print(f"新文件 {zip_file_path} 大于上次上传的大小,正在执行上传...")
|
| 294 |
+
update_last_uploaded_size(file_size) # 更新元数据中的最后一次上传大小
|
| 295 |
|
| 296 |
with open(metadata_file_path, 'w') as json_fid:
|
| 297 |
json.dump(kaggle_metadata, json_fid)
|
|
|
|
| 300 |
last_uploaded_size = file_size
|
| 301 |
|
| 302 |
else:
|
| 303 |
+
print(f"新文件 {zip_file_path} 不大于上次上传的大小,跳过")
|
| 304 |
|
| 305 |
|
| 306 |
def repeat_task():
|
| 307 |
print('------打包线程启动-------------')
|
| 308 |
while True:
|
| 309 |
print('等待打包')
|
| 310 |
+
time.sleep(18000)
|
| 311 |
compress_folder()
|
| 312 |
print('------打包线程结束-------------')
|
| 313 |
# repeat_task()
|
|
|
|
| 436 |
retry_count = 3 # 设置重试次数
|
| 437 |
for attempt in range(retry_count):
|
| 438 |
try:
|
| 439 |
+
|
| 440 |
+
subprocess.run(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/",
|
|
|
|
|
|
|
|
|
|
| 441 |
shell=True, check=True, timeout=300)
|
| 442 |
break # 如果成功,跳出重试循环
|
| 443 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 449 |
logging.info("%s 接口返回信息: %s", url, response_json) # 修正日志记录
|
| 450 |
except requests.exceptions.ConnectionError as e:
|
| 451 |
logging.error(f"接口异常1: {e}, 尝试次数: {n}, nn: {nn}")
|
| 452 |
+
time.sleep(3)
|
| 453 |
nn += 1
|
| 454 |
if nn > 10:
|
| 455 |
break
|
|
|
|
| 679 |
replace_info_in_file(f'{data_folder}/public/login.html')
|
| 680 |
# threading.Thread(target=repeat_task, daemon=True).start() #压缩备份
|
| 681 |
# backup_thread = threading.Thread(target=repeat_task, daemon=True)
|
| 682 |
+
# backup_thread.start()
|
| 683 |
print("开始衔接")
|
| 684 |
Service_status = True
|
| 685 |
frp(configs, True)
|
|
|
|
|
|
|
|
|
|
| 686 |
# frp(config_data4,'/content/frp/frpc4.ini',True)
|
| 687 |
# 以线程的形式运行 frp 任务
|
| 688 |
# frp_thread1 = threading.Thread(target=frp, args=(config_data2, '/content/frp/frpc2.ini', True))
|
| 689 |
# frp_thread1.start()
|
| 690 |
+
# threading.Thread(target=monitor_port, daemon=True).start()#检测端口状态
|
|
|
|
|
|
|
| 691 |
while True:
|
| 692 |
+
# if backup_thread.is_alive():
|
| 693 |
+
# print("备份线程仍在运行...")
|
| 694 |
+
# else:
|
| 695 |
+
# print("备份线程已停止.")
|
| 696 |
+
# backup_thread = threading.Thread(target=repeat_task, daemon=True)
|
| 697 |
+
# backup_thread.start()
|
| 698 |
+
# break
|
| 699 |
time.sleep(5)
|
| 700 |
|
| 701 |
else:
|