diviner.py 4.2 KB

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