Qilan2 commited on
Commit
6cfe052
·
verified ·
1 Parent(s): dcae0c8

Update Card_Hub.py

Browse files
Files changed (1) hide show
  1. Card_Hub.py +51 -28
Card_Hub.py CHANGED
@@ -420,6 +420,14 @@ def prepare_database_sync():
420
  """准备数据库同步"""
421
  db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite"
422
 
 
 
 
 
 
 
 
 
423
  # 1. 停止 Node.js 服务
424
  try:
425
  subprocess.run("pkill -f 'node.*server.js'", shell=True)
@@ -430,7 +438,6 @@ def prepare_database_sync():
430
  # 2. 确保数据库文件权限正确
431
  try:
432
  subprocess.run(f"chmod 666 {db_path}", shell=True)
433
- # 确保数据库文件所在目录也有正确权限
434
  subprocess.run(f"chmod 755 {os.path.dirname(db_path)}", shell=True)
435
  except Exception as e:
436
  print(f"修改权限时出错: {e}")
@@ -447,6 +454,15 @@ def sync_database():
447
  db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite"
448
 
449
  try:
 
 
 
 
 
 
 
 
 
450
  # 1. 准备同步
451
  prepare_database_sync()
452
 
@@ -495,35 +511,42 @@ def sync_database():
495
  subprocess.run(f"chmod 666 {db_path}", shell=True)
496
  subprocess.run(f"chown root:root {db_path}", shell=True)
497
  finally:
498
- # 5. 重启服务
499
  try:
500
- os.chdir('/Card_Hub/RoleCardSite')
501
- # 修复权限问题
502
- subprocess.run('chmod +x ./start.sh', shell=True)
503
-
504
- # 修复客户端权限
505
- client_path = './client'
506
- subprocess.run(f'chmod -R 755 {client_path}/node_modules', shell=True)
507
- subprocess.run(f'chmod +x {client_path}/node_modules/.bin/*', shell=True)
508
-
509
- # 特别修复 esbuild 权限
510
- esbuild_path = f'{client_path}/node_modules/@esbuild/linux-x64/bin/esbuild'
511
- if os.path.exists(esbuild_path):
512
- subprocess.run(f'chmod +x {esbuild_path}', shell=True)
513
- subprocess.run(f'chown root:root {esbuild_path}', shell=True)
514
-
515
- # 修复服务器权限
516
- server_path = './server'
517
- subprocess.run(f'chmod -R 755 {server_path}/node_modules', shell=True)
518
- subprocess.run(f'chmod +x {server_path}/node_modules/.bin/*', shell=True)
519
-
520
- # 修复项目文件权限
521
- subprocess.run('chmod -R 755 .', shell=True)
522
-
523
- # 启动服务
524
- subprocess.run('sh ./start.sh', shell=True)
 
 
 
 
 
 
 
525
  except Exception as e:
526
- print(f"重启服务时出错: {e}")
527
 
528
  def frp(configs, backstage):
529
  print('-----------FRP开始-------------')
 
420
  """准备数据库同步"""
421
  db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite"
422
 
423
+ # 0. 检查并安装 sqlite3
424
+ try:
425
+ subprocess.run("which sqlite3", shell=True, check=True)
426
+ except subprocess.CalledProcessError:
427
+ print("安装 sqlite3...")
428
+ subprocess.run("apt-get update", shell=True)
429
+ subprocess.run("apt-get install -y sqlite3", shell=True)
430
+
431
  # 1. 停止 Node.js 服务
432
  try:
433
  subprocess.run("pkill -f 'node.*server.js'", shell=True)
 
438
  # 2. 确保数据库文件权限正确
439
  try:
440
  subprocess.run(f"chmod 666 {db_path}", shell=True)
 
441
  subprocess.run(f"chmod 755 {os.path.dirname(db_path)}", shell=True)
442
  except Exception as e:
443
  print(f"修改权限时出错: {e}")
 
454
  db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite"
455
 
456
  try:
457
+ # 检查源服务器是否在运行
458
+ try:
459
+ response = requests.get(f"{st_url}/api", timeout=5)
460
+ if response.status_code == 200:
461
+ print("源服务器正在运行,等待同步...")
462
+ time.sleep(5) # 等待一段时间确保数据写入完成
463
+ except requests.exceptions.RequestException:
464
+ print("源服务器未运行,继续同步...")
465
+
466
  # 1. 准备同步
467
  prepare_database_sync()
468
 
 
511
  subprocess.run(f"chmod 666 {db_path}", shell=True)
512
  subprocess.run(f"chown root:root {db_path}", shell=True)
513
  finally:
514
+ # 5. 检查是否需要重启服务
515
  try:
516
+ # 检查当前服务是否在运行
517
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
518
+ result = sock.connect_ex(('localhost', 8003))
519
+ if result != 0: # 如果服务没有运行
520
+ print("服务未运行,准备启动...")
521
+ os.chdir('/Card_Hub/RoleCardSite')
522
+ # 修复权限问题
523
+ subprocess.run('chmod +x ./start.sh', shell=True)
524
+
525
+ # 修复客户端权限
526
+ client_path = './client'
527
+ subprocess.run(f'chmod -R 755 {client_path}/node_modules', shell=True)
528
+ subprocess.run(f'chmod +x {client_path}/node_modules/.bin/*', shell=True)
529
+
530
+ # 特别修复 esbuild 权限
531
+ esbuild_path = f'{client_path}/node_modules/@esbuild/linux-x64/bin/esbuild'
532
+ if os.path.exists(esbuild_path):
533
+ subprocess.run(f'chmod +x {esbuild_path}', shell=True)
534
+ subprocess.run(f'chown root:root {esbuild_path}', shell=True)
535
+
536
+ # 修复服务器权限
537
+ server_path = './server'
538
+ subprocess.run(f'chmod -R 755 {server_path}/node_modules', shell=True)
539
+ subprocess.run(f'chmod +x {server_path}/node_modules/.bin/*', shell=True)
540
+
541
+ # 修复项目文件权限
542
+ subprocess.run('chmod -R 755 .', shell=True)
543
+
544
+ # 启动服务
545
+ subprocess.run('sh ./start.sh', shell=True)
546
+ else:
547
+ print("服务正在运行,跳过启动步骤")
548
  except Exception as e:
549
+ print(f"检查服务状态时出错: {e}")
550
 
551
  def frp(configs, backstage):
552
  print('-----------FRP开始-------------')