diviner.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import subprocess
  6. from demeter.core import *
  7. def init():
  8. #import nltk
  9. #nltk.download('punkt', quiet=True)
  10. #nltk.download('averaged_perceptron_tagger', quiet=True)
  11. model = Demeter.model('manage_admin')
  12. model.id = 1
  13. info = model.select(type='fetchone')
  14. if not info:
  15. model.role_id = 1
  16. model.username = 'admin'
  17. model.mobile = '15810090845'
  18. model.password = '123456'
  19. model.insert()
  20. model = Demeter.model('manage_role')
  21. model.id = 1
  22. info = model.select(type='fetchone')
  23. if not info:
  24. model.name = u'管理员'
  25. model.insert()
  26. def get_service_name(script_name):
  27. return 'diviner_' + script_name.replace(".py", "")
  28. def install(script_name):
  29. init()
  30. bin_path = '/usr/local/bin'
  31. target_path = os.path.abspath(script_name)
  32. link_path = os.path.join(bin_path, os.path.splitext(os.path.basename(script_name))[0])
  33. Demeter.echo(f"安装脚本到 {link_path}")
  34. if os.path.exists(link_path):
  35. subprocess.run(['sudo', 'rm', '-f', link_path], check=True)
  36. subprocess.run(['sudo', 'ln', '-s', target_path, link_path], check=True)
  37. subprocess.run(['sudo', 'chmod', '+x', target_path], check=True)
  38. Demeter.echo(f"安装完成,可以直接用命令 `{os.path.basename(script_name).replace('.py','')} start` 启动")
  39. def start(script_name, desc):
  40. template = """[Unit]
  41. Description={desc}
  42. After=network.target
  43. [Service]
  44. ExecStart={python_path} {workdir}/{script}
  45. WorkingDirectory={workdir}
  46. Restart=always
  47. RestartSec=3
  48. User={user}
  49. Environment=PYTHONUNBUFFERED=1
  50. [Install]
  51. WantedBy=multi-user.target
  52. """
  53. workdir = os.getcwd()
  54. python_path = subprocess.getoutput("which python3")
  55. user = subprocess.getoutput("whoami")
  56. service_name = get_service_name(script_name)
  57. service_file = f"/etc/systemd/system/{service_name}.service"
  58. content = template.format(
  59. desc=desc,
  60. python_path=python_path,
  61. workdir=workdir,
  62. script=script_name,
  63. user=user,
  64. )
  65. # 写入临时 service 文件
  66. with open(f"/tmp/{service_name}.service", "w") as f:
  67. f.write(content)
  68. # 移动到 systemd 目录(需 root 权限)
  69. subprocess.run(["sudo", "mv", f"/tmp/{service_name}.service", service_file], check=True)
  70. # 重新加载 systemd,启用并启动服务
  71. subprocess.run(["sudo", "systemctl", "daemon-reload"], check=True)
  72. subprocess.run(["sudo", "systemctl", "enable", service_name], check=True)
  73. subprocess.run(["sudo", "systemctl", "restart", service_name], check=True)
  74. Demeter.echo(f"已安装并启动服务: {service_name}")
  75. def stop(script_name):
  76. service_name = get_service_name(script_name)
  77. Demeter.echo(f"停止服务: {service_name}")
  78. subprocess.run(["sudo", "systemctl", "stop", service_name], check=True)
  79. subprocess.run(["sudo", "systemctl", "disable", service_name], check=True)
  80. def restart(script_name):
  81. service_name = get_service_name(script_name)
  82. Demeter.echo(f"重启服务: {service_name}")
  83. subprocess.run(["sudo", "systemctl", "restart", service_name], check=True)
  84. def main():
  85. services = [
  86. ("front.py", "Diviner Front Service"),
  87. ("admin.py", "Diviner Admin Service"),
  88. ("cron.py", "Diviner Cron Service"),
  89. ]
  90. action = "start"
  91. if len(sys.argv) > 1:
  92. action = sys.argv[1].lower()
  93. if action == "install":
  94. current_script = os.path.basename(sys.argv[0])
  95. if not os.path.exists(current_script):
  96. Demeter.echo(f"当前脚本文件不存在: {current_script}")
  97. sys.exit(1)
  98. install(current_script)
  99. else:
  100. valid_services = [(s, d) for s, d in services if os.path.exists(s)]
  101. for s, d in services:
  102. if not os.path.exists(s):
  103. Demeter.echo(f"脚本不存在: {s}")
  104. if action == "start":
  105. for script, desc in valid_services:
  106. start(script, desc)
  107. elif action == "stop":
  108. for script, _ in valid_services:
  109. stop(script)
  110. elif action == "restart":
  111. for script, _ in valid_services:
  112. restart(script)
  113. else:
  114. Demeter.echo(f"未知动作: {action}")
  115. sys.exit(1)
  116. if __name__ == "__main__":
  117. main()