diff --git a/cli.py b/cli.py index bf88711..d2889f4 100644 --- a/cli.py +++ b/cli.py @@ -3,7 +3,7 @@ from services.odoo.service import OdooServiceManager def service(args): - service = OdooServiceManager() + service = OdooServiceManager(config_path="utility/config/settings.yaml") match args.action: case "start": service.start_service(args.service) @@ -25,10 +25,11 @@ def setup_cli(): dest="command", required=True, help="Available commands" ) - start_parser = subparsers.add_parser("service", help="Start a service") - start_parser.add_argument("action", type=str, help="start/stop/restart") - start_parser.add_argument("service", type=str, help="Name of the service") - start_parser.set_defaults(func=service) + service_parser = subparsers.add_parser("service", help="Manage instance service") + service_parser.add_argument("action", type=str, help="start/stop/restart") + service_parser.add_argument("service", type=str, help="Name of the service") + service_parser.set_defaults(func=service) + return parser diff --git a/services/odoo/module.py b/services/odoo/module.py index 27eec41..7e943c1 100644 --- a/services/odoo/module.py +++ b/services/odoo/module.py @@ -4,32 +4,34 @@ import subprocess class OdooModuleManager: - def __init__(self, config_path="config/settings.yaml"): + def __init__(self, config_path: str = "config/settings.yaml"): self.config = OdooConnection(config_path) - # Use OdooConnection instead of Config directly - self.git = GitHandler( - repo_url=self.config.config.get("git", "repo_url"), - local_path=self.config.config.get("git", "local_path"), - branch=self.config.config.get("git", "branch", "main"), - ) - def update_and_upgrade(self, instance_name=None): - """Update and upgrade multiple modules for the specified instance(s).""" - self.config.connect(instance_name) # Connect to the target instance(s) + def _manage_module(self, action: str, instance_name: str = None) -> None: + """Generic method to install, uninstall, or upgrade modules.""" + if action not in ["install", "uninstall", "upgrade"]: + raise ValueError(f"Invalid action: {action}") + + self.config.connect(instance_name) + for instance in self.config.get_instances(): if instance_name and instance["name"] != instance_name: continue + print(f"Processing instance: {instance['name']}") module_names = instance.get("module_names", []) if not module_names: - print(f"No modules specified for {instance['name']}, skipping upgrade.") + print( + f"No modules specified for {instance['name']}, skipping {action}." + ) continue for module_name in module_names: - print(f"Upgrading module: {module_name} in {instance['name']}") + print( + f"{action.capitalize()}ing module: {module_name} in {instance['name']}" + ) try: - # Use OdooConnection.execute to upgrade the module module_ids = self.config.execute( instance["name"], "ir.module.module", @@ -37,20 +39,33 @@ class OdooModuleManager: [("name", "=", module_name), ("state", "=", "installed")], ) if module_ids: + button_action = f"button_immediate_{action}" self.config.execute( instance["name"], "ir.module.module", - "button_upgrade", + button_action, [module_ids], ) print( - f"Module {module_name} upgraded successfully in {instance['name']}" + f"Module {module_name} {action}ed successfully in {instance['name']}" ) else: print( f"Module {module_name} not found or not installed in {instance['name']}" ) except Exception as e: - print(f"Failed to upgrade {module_name} in {instance['name']}: {e}") - # Continue with other modules - return True + print( + f"Failed to {action} {module_name} in {instance['name']}: {e}" + ) + + def install(self, instance_name: str = None) -> None: + """Install multiple modules for the specified instance(s).""" + self._manage_module("install", instance_name) + + def uninstall(self, instance_name: str = None) -> None: + """Uninstall multiple modules for the specified instance(s).""" + self._manage_module("uninstall", instance_name) + + def upgrade(self, instance_name: str = None) -> None: + """Upgrade multiple modules for the specified instance(s).""" + self._manage_module("upgrade", instance_name) diff --git a/services/odoo/service.py b/services/odoo/service.py index f10a270..3c76358 100644 --- a/services/odoo/service.py +++ b/services/odoo/service.py @@ -30,7 +30,7 @@ class OdooServiceManager: local_host = host in ["localhost", "127.0.0.1"] if service_type == "systemctl": - cmd = f"sudo systemctl {action} {service_name}" + cmd = f"systemctl {action} {service_name}" elif service_type == "docker": container_name = instance.get("container_name", f"odoo_{instance['name']}") cmd = f"docker {action} {container_name}" @@ -52,7 +52,7 @@ class OdooServiceManager: :param action: "stop" or "restart" :param instance_name: Specific instance name, or None for all instances. """ - if action not in ["stop", "restart"]: + if action not in ["stop", "restart","start"]: raise ValueError("Action must be 'stop' or 'restart'.") for instance in self.config.get_instances():