From 6caea7e002a486dbb8f27d80d7696c988bb12c39 Mon Sep 17 00:00:00 2001 From: KaySar12 Date: Fri, 4 Apr 2025 10:22:52 +0700 Subject: [PATCH] update utility --- cli.py | 98 ---------------------------------------- cli/__init__.py | 0 cli/module.py | 47 +++++++++++++++++++ cli/service.py | 29 ++++++++++++ config/settings.template | 18 -------- config/settings.yaml | 56 ++++++++++------------- main.py | 29 +++++++++++- 7 files changed, 127 insertions(+), 150 deletions(-) delete mode 100644 cli.py create mode 100644 cli/__init__.py create mode 100644 cli/module.py create mode 100644 cli/service.py diff --git a/cli.py b/cli.py deleted file mode 100644 index b2ed4a7..0000000 --- a/cli.py +++ /dev/null @@ -1,98 +0,0 @@ -import argparse - -import tqdm -from services.odoo.service import OdooServiceManager -from services.odoo.module import OdooModuleManager -import lib.color_log as color_log - - -def service(args): - service = OdooServiceManager(config_path="utility/config/settings.yaml") - match args.action: - case "start": - service.start_service(args.instance) - case "stop": - service.stop_service(args.instance) - case "restart": - service.restart_service(args.instance) - case _: - color_log.Show( - "FAILED", - f"Invalid action '{args.action}' for service management.", - ) - - -def module(args): - module_manager = OdooModuleManager(config_path="utility/config/settings.yaml") - - # If modules are provided in the command line - if args.modules: - color_log.Show( - "INFO", - f"Processing modules: {', '.join(args.modules)} for {args.instance}", - ) - else: - # Fallback if no modules are provided (can use default from instance settings) - color_log.Show( - "INFO", - f"No modules specified. Using default modules for {args.instance}", - ) - args.modules = module_manager.get_modules(args.instance) - # Create a progress bar using tqdm - for module_name in tqdm.tqdm( - args.modules, desc="Processing modules", unit="module" - ): - match args.action: - case "install": - module_manager.install(args.instance, [module_name]) - case "uninstall": - module_manager.uninstall(args.instance, [module_name]) - case "upgrade": - module_manager.upgrade(args.instance, [module_name]) - case _: - color_log.Show( - "FAILED", - f"Invalid action '{args.action}' for module management.", - ) - - -def setup_cli(): - parser = argparse.ArgumentParser(description="Service Manager CLI") - parser.add_argument( - "-v", "--verbose", action="store_true", help="Enable verbose mode" - ) - - subparsers = parser.add_subparsers(dest="command", required=True) - - service_parser = subparsers.add_parser("service", help="Manage instance service") - service_parser.add_argument( - "action", choices=["start", "stop", "restart"], help="Service action" - ) - service_parser.add_argument("instance", type=str, help="Instance Name") - service_parser.set_defaults(func=service) # Fixed: Correct function assignment - - module_parser = subparsers.add_parser("module", help="Manage instance module") - module_parser.add_argument( - "action", choices=["install", "uninstall", "upgrade"], help="Module action" - ) - module_parser.add_argument("instance", type=str, help="Instance Name") - module_parser.add_argument( - "--modules", "-m", nargs="+", help="List of modules to process" - ) - module_parser.set_defaults(func=module) # Fixed: Correct function assignment - - return parser - - -def main(): - parser = setup_cli() - args = parser.parse_args() - - if hasattr(args, "func"): # Ensuring `func` is set - args.func(args) - else: - print("Invalid command. Use --help for more details.") - - -if __name__ == "__main__": - main() diff --git a/cli/__init__.py b/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cli/module.py b/cli/module.py new file mode 100644 index 0000000..07f6994 --- /dev/null +++ b/cli/module.py @@ -0,0 +1,47 @@ +# cli/module.py +import tqdm +from services.odoo.module import OdooModuleManager +import lib.color_log as color_log + + +def setup_cli(subparsers): + module_parser = subparsers.add_parser("module", help="Manage instance module") + module_parser.add_argument( + "action", choices=["install", "uninstall", "upgrade"], help="Module action" + ) + module_parser.add_argument("instance", type=str, help="Instance Name") + module_parser.add_argument( + "--modules", "-m", nargs="+", help="List of modules to process" + ) + module_parser.set_defaults(func=module) + return module_parser + + +def module(args): + module_manager = OdooModuleManager(config_path="utility/config/settings.yaml") + if args.modules: + color_log.Show( + "INFO", + f"Processing modules: {', '.join(args.modules)} for {args.instance}", + ) + else: + color_log.Show( + "INFO", + f"No modules specified. Using default modules for {args.instance}", + ) + args.modules = module_manager.get_modules(args.instance) + for module_name in tqdm.tqdm( + args.modules, desc="Processing modules", unit="module" + ): + match args.action: + case "install": + module_manager.install(args.instance, [module_name]) + case "uninstall": + module_manager.uninstall(args.instance, [module_name]) + case "upgrade": + module_manager.upgrade(args.instance, [module_name]) + case _: + color_log.Show( + "FAILED", + f"Invalid action '{args.action}' for module management.", + ) diff --git a/cli/service.py b/cli/service.py new file mode 100644 index 0000000..e230c97 --- /dev/null +++ b/cli/service.py @@ -0,0 +1,29 @@ +# cli/service.py +from services.odoo.service import OdooServiceManager +import lib.color_log as color_log + + +def setup_cli(subparsers): + service_parser = subparsers.add_parser("service", help="Manage instance service") + service_parser.add_argument( + "action", choices=["start", "stop", "restart"], help="Service action" + ) + service_parser.add_argument("instance", type=str, help="Instance Name") + service_parser.set_defaults(func=service) + return service_parser + + +def service(args): + service = OdooServiceManager(config_path="utility/config/settings.yaml") + match args.action: + case "start": + service.start_service(args.instance) + case "stop": + service.stop_service(args.instance) + case "restart": + service.restart_service(args.instance) + case _: + color_log.Show( + "FAILED", + f"Invalid action '{args.action}' for service management.", + ) diff --git a/config/settings.template b/config/settings.template index 4ba30d3..1757ea2 100644 --- a/config/settings.template +++ b/config/settings.template @@ -13,24 +13,6 @@ odoo_instances: local_path: "/opt/odoo18/addons" ssh: user: root - password: Smartyourlife123@* - key_path: "/root/.ssh/privatessh.key" -odoo_instances: -- name: "ftacpa" - host: "10.1.1.31" - port: 8069 - database: "server" - username: "nextzen" - password: "smartyourlife" - type: "systemctl" - service_name: "odoo18" - git: - repo_url: "https://hoangvv:smartyourlife@git.nextzenos.com/NextERP/Odoo18-FTACPA.git" - branch: "community/demo/ftacpa" - local_path: "/opt/ftacpa/addons" - ssh: - user: root - password: Smartyourlife123@* key_path: "/root/.ssh/privatessh.key" # - name: "server1_test" # host: "server1.example.com" diff --git a/config/settings.yaml b/config/settings.yaml index 777165d..d7f1dfa 100644 --- a/config/settings.yaml +++ b/config/settings.yaml @@ -1,41 +1,33 @@ -odoo_instances: -- name: "server" - host: "10.1.1.34" - port: 8069 - database: "server" - modules: - - "hr_holidays" - - "timesheet" - - "hr" +common: &common username: "nextzen" password: "smartyourlife" type: "systemctl" service_name: "odoo18" - git: + git: &git_config repo_url: "https://hoangvv:smartyourlife@git.nextzenos.com/NextERP/Odoo18-FTACPA.git" branch: "community/demo/ftacpa" local_path: "/opt/ftacpa/addons" - ssh: + ssh: &ssh_config user: root - password: Smartyourlife123@* key_path: "/root/.ssh/privatessh.key" - # - name: "server1_test" - # host: "server1.example.com" - # port: 8069 - # database: "test_db1" - # username: "admin" - # password: "test_password" - # module_names: - # - "your_module" - # type: "docker" - # - name: "server2_prod" - # host: "server2.example.com" - # port: 8070 - # database: "prod_db2" - # username: "admin" - # password: "admin_password" - # module_names: - # - "your_module" - # - "custom_module" - # - "third_module" - # type: "systemctl" + modules: &module_list + - "hr_holidays" + - "timesheet" + - "hr" + +odoo_instances: + - name: "server" + host: "10.1.1.34" + port: 8069 + database: "server" + action: "update" + modules: *module_list + <<: *common # Inherit common settings + + - name: "server2" + host: "10.1.1.34" + port: 8069 + database: "server2" + action: "install" + modules: *module_list + <<: *common # Inherit common settings \ No newline at end of file diff --git a/main.py b/main.py index 2e8782b..ba70d9e 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,29 @@ -import cli +# main.py +import argparse +from cli.service import setup_cli as setup_service_cli +from cli.module import setup_cli as setup_module_cli + + +def setup_cli(): + parser = argparse.ArgumentParser(description="Service Manager CLI") + parser.add_argument( + "-v", "--verbose", action="store_true", help="Enable verbose mode" + ) + subparsers = parser.add_subparsers(dest="command", required=True) + setup_service_cli(subparsers) + setup_module_cli(subparsers) + return parser + + +def main(): + parser = setup_cli() + args = parser.parse_args() + + if hasattr(args, "func"): + args.func(args) + else: + print("Invalid command. Use --help for more details.") + if __name__ == "__main__": - cli.main() \ No newline at end of file + main()