diff --git a/servicesManager.py b/servicesManager.py deleted file mode 100755 index d599aaa..0000000 --- a/servicesManager.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python3 - -import subprocess -import sys -import os -import logging -import colorlog - -# init color log -handler = colorlog.StreamHandler() -handler.setFormatter(colorlog.ColoredFormatter( - '%(log_color)s%(asctime)s - %(levelname)s - %(message)s', - log_colors={ - 'DEBUG': 'cyan', - 'INFO': 'green', - 'WARNING': 'yellow', - 'ERROR': 'red', - 'CRITICAL': 'bold_red', - } -)) - -logger = colorlog.getLogger(__name__) -logger.setLevel(logging.INFO) -logger.addHandler(handler) - -def get_operation(): - while True: - choice = input("请选择操作 (0 停止, 1 重启, q 退出): ").strip().lower() - if choice == 'q': - return 'q' - try: - op = int(choice) - if op in (0, 1): - return op - except ValueError: - pass - logger.warning("无效输入,请输入 0、1 或 q。") - -def manage_service(name, command_template): - logger.info(f"===== {name} 管理 =====") - logger.info("0. 停止") - logger.info("1. 启动") - op = get_operation() - if op == 'q': - return False - action = 'down' if op == 0 else 'up -d' - cmd = command_template.format(action=action) - try: - subprocess.run(cmd, shell=True, check=True) - logger.info(f"{name} {('停止' if op == 0 else '重启')} 完成") - except subprocess.CalledProcessError as e: - logger.error(f"{name} 管理失败: {e}") - return True - -def nginx_manager(): - logger.info("===== Nginx 管理 =====") - logger.info("0. 停止") - logger.info("1. 重启") - op = get_operation() - if op == 'q': - return False - action = 'stop' if op == 0 else 'restart' - cmd = f"sudo systemctl {action} nginx" - try: - subprocess.run(cmd, shell=True, check=True) - logger.info(f"Nginx {('停止' if op == 0 else '重启')} 完成") - except subprocess.CalledProcessError as e: - logger.error(f"Nginx 管理失败: {e}") - return True - -def homepage_manager(): - path = os.path.expanduser("~/web/homepageService") - return manage_service("Homepage", f"cd {path} && docker compose {{action}}") - -def status_manager(): - path = os.path.expanduser("~/web/statusService") - return manage_service("Status", f"cd {path} && docker compose {{action}}") - -def gitea_manager(): - path = os.path.expanduser("~/web/giteaService") - return manage_service("Gitea", f"cd {path} && docker compose {{action}}") - -def main(): - services = { - 0: ("Nginx", nginx_manager), - 1: ("Homepage", homepage_manager), - 2: ("Status", status_manager), - 3: ("Gitea", gitea_manager), - } - - while True: - logger.info("=== 服务管理 ===") - for key, (name, _) in services.items(): - logger.info(f"{key}. {name}") - logger.info("a. 全部") - logger.info("q. 退出") - - choice = input("请选择要管理的服务或 a 全部: ").strip().lower() - if choice == 'q': - logger.info("退出程序") - break - if choice == 'a': - logger.info("=== 批量操作: 全部服务 ===") - logger.info("0. 停止") - logger.info("1. 重启") - op = get_operation() - if op == 'q': - continue - action_nginx = 'stop' if op == 0 else 'restart' - action_others = 'down' if op == 0 else 'restart' - commands = [ - ("Nginx", f"sudo systemctl {action_nginx} nginx"), - ("Homepage", f"cd {os.path.expanduser('~/web/homepageService')} && docker compose {action_others}"), - ("Status", f"cd {os.path.expanduser('~/web/statusService')} && docker compose {action_others}"), - ("Gitea", f"cd {os.path.expanduser('~/web/giteaService')} && docker compose {action_others}"), - ] - for name, cmd in commands: - try: - logger.info(f"{name} 开始 {('停止' if op==0 else '重启')}") - subprocess.run(cmd, shell=True, check=True) - logger.info(f"{name} 完成") - except subprocess.CalledProcessError as e: - logger.error(f"{name} 操作失败: {e}") - continue - - try: - idx = int(choice) - if idx in services: - cont = services[idx][1]() - if cont is False: - continue - else: - logger.warning("无效选项") - except ValueError: - logger.warning("无效输入,请输入数字或 q") - - return 0 - -if __name__ == "__main__": - sys.exit(main()) diff --git a/src/ServicesManager.py b/src/ServicesManager.py index bd09121..1e8ff40 100644 --- a/src/ServicesManager.py +++ b/src/ServicesManager.py @@ -24,7 +24,7 @@ logger.addHandler(handler) def get_operation(): while True: - choice = input("请选择操作 (0 停止, 1 重启, q 退出): ").strip().lower() + choice = input("Please select an operation (0 to stop, 1 to restart, q to quit): ").strip().lower() if choice == 'q': return 'q' try: @@ -33,27 +33,28 @@ def get_operation(): return op except ValueError: pass - logger.warning("无效输入,请输入 0、1 或 q。") + logger.warning("Invalid input, please enter 0, 1, or q.") class Service: - """A template of webservices + """A template of web services Attributes: - tag (str): The tag that mark the service instance use which way to deploy. + tag (str): The tag that marks the service instance to use which way to deploy. name (str): The name of the service instance. dir (str): The configuration & data directory of the service instance. """ - def __init__(self, tag: str, name: str, dir: str): + def __init__(self, tag: str, name: str, dir=None): self.tag = tag self.name = name self.dir = dir def command_gen(self): - """Generate services's management commands according to services's tag + """Generate service management commands according to the service's tag. """ + system_services_command = "sudo systemctl" docker_services_command = f"cd {os.path.expanduser(f'{self.dir}')} && docker compose" if self.tag == "sys": @@ -61,4 +62,41 @@ class Service: elif self.tag == "docker": return docker_services_command else: - raise ValueError("The service tag {self.tag} was not included") + raise ValueError(f"The service tag {self.tag} was not included.") + + def manage_service(self): + """Manage the service based on user input in English. + + """ + + operation = get_operation() + if operation == 'q': + logger.info("User chose to quit the service management.") + return + + command = self.command_gen() + full_command = None # Initialize full_command + + if operation == 0: + # Stop the service + full_command = f"{command} stop {self.name}" + logger.info(f"Stopping service: {self.name}") + elif operation == 1: + # Restart the service + full_command = f"{command} restart {self.name}" + logger.info(f"Restarting service: {self.name}") + else: + logger.warning("Invalid operation, no service management executed.") + return # Exit if the operation is invalid + + if full_command: # Ensure full_command is defined + try: + subprocess.run(full_command, check=True, shell=True) + logger.info(f"Service {self.name} operation completed successfully.") + except subprocess.CalledProcessError as e: + logger.error(f"Failed to manage service {self.name}: {e}") + +# 示例调用 +if __name__ == "__main__": + service = Service(tag="sys", name="nginx") + service.manage_service()