This commit is contained in:
KaySar12 2025-04-03 17:05:42 +07:00
parent 8ac2ca2ee8
commit b249bc7149
3 changed files with 41 additions and 25 deletions

11
cli.py
View File

@ -3,7 +3,7 @@ from services.odoo.service import OdooServiceManager
def service(args): def service(args):
service = OdooServiceManager() service = OdooServiceManager(config_path="utility/config/settings.yaml")
match args.action: match args.action:
case "start": case "start":
service.start_service(args.service) service.start_service(args.service)
@ -25,10 +25,11 @@ def setup_cli():
dest="command", required=True, help="Available commands" dest="command", required=True, help="Available commands"
) )
start_parser = subparsers.add_parser("service", help="Start a service") service_parser = subparsers.add_parser("service", help="Manage instance service")
start_parser.add_argument("action", type=str, help="start/stop/restart") service_parser.add_argument("action", type=str, help="start/stop/restart")
start_parser.add_argument("service", type=str, help="Name of the service") service_parser.add_argument("service", type=str, help="Name of the service")
start_parser.set_defaults(func=service) service_parser.set_defaults(func=service)
return parser return parser

View File

@ -4,32 +4,34 @@ import subprocess
class OdooModuleManager: class OdooModuleManager:
def __init__(self, config_path="config/settings.yaml"): def __init__(self, config_path: str = "config/settings.yaml"):
self.config = OdooConnection(config_path) 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): def _manage_module(self, action: str, instance_name: str = None) -> None:
"""Update and upgrade multiple modules for the specified instance(s).""" """Generic method to install, uninstall, or upgrade modules."""
self.config.connect(instance_name) # Connect to the target instance(s) 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(): for instance in self.config.get_instances():
if instance_name and instance["name"] != instance_name: if instance_name and instance["name"] != instance_name:
continue continue
print(f"Processing instance: {instance['name']}") print(f"Processing instance: {instance['name']}")
module_names = instance.get("module_names", []) module_names = instance.get("module_names", [])
if not 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 continue
for module_name in module_names: 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: try:
# Use OdooConnection.execute to upgrade the module
module_ids = self.config.execute( module_ids = self.config.execute(
instance["name"], instance["name"],
"ir.module.module", "ir.module.module",
@ -37,20 +39,33 @@ class OdooModuleManager:
[("name", "=", module_name), ("state", "=", "installed")], [("name", "=", module_name), ("state", "=", "installed")],
) )
if module_ids: if module_ids:
button_action = f"button_immediate_{action}"
self.config.execute( self.config.execute(
instance["name"], instance["name"],
"ir.module.module", "ir.module.module",
"button_upgrade", button_action,
[module_ids], [module_ids],
) )
print( print(
f"Module {module_name} upgraded successfully in {instance['name']}" f"Module {module_name} {action}ed successfully in {instance['name']}"
) )
else: else:
print( print(
f"Module {module_name} not found or not installed in {instance['name']}" f"Module {module_name} not found or not installed in {instance['name']}"
) )
except Exception as e: except Exception as e:
print(f"Failed to upgrade {module_name} in {instance['name']}: {e}") print(
# Continue with other modules f"Failed to {action} {module_name} in {instance['name']}: {e}"
return True )
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)

View File

@ -30,7 +30,7 @@ class OdooServiceManager:
local_host = host in ["localhost", "127.0.0.1"] local_host = host in ["localhost", "127.0.0.1"]
if service_type == "systemctl": if service_type == "systemctl":
cmd = f"sudo systemctl {action} {service_name}" cmd = f"systemctl {action} {service_name}"
elif service_type == "docker": elif service_type == "docker":
container_name = instance.get("container_name", f"odoo_{instance['name']}") container_name = instance.get("container_name", f"odoo_{instance['name']}")
cmd = f"docker {action} {container_name}" cmd = f"docker {action} {container_name}"
@ -52,7 +52,7 @@ class OdooServiceManager:
:param action: "stop" or "restart" :param action: "stop" or "restart"
:param instance_name: Specific instance name, or None for all instances. :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'.") raise ValueError("Action must be 'stop' or 'restart'.")
for instance in self.config.get_instances(): for instance in self.config.get_instances():