NextERP-scripts/services/odoo/manager.py

117 lines
5.2 KiB
Python

from utility.services.git.handler import GitHandler
from utility.services.odoo.connection import OdooConnection
import subprocess
class OdooModuleManager:
def __init__(self, config_path="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)
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.")
continue
for module_name in module_names:
print(f"Upgrading module: {module_name} in {instance['name']}")
try:
# Use OdooConnection.execute to upgrade the module
module_ids = self.config.execute(
instance["name"],
"ir.module.module",
"search",
[("name", "=", module_name), ("state", "=", "installed")],
)
if module_ids:
self.config.execute(
instance["name"],
"ir.module.module",
"button_upgrade",
[module_ids],
)
print(
f"Module {module_name} upgraded 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
def restart_service(self, instance_name=None):
"""Restart the Odoo service based on the instance type using SSH with private key."""
for instance in self.config.get_instances():
if instance_name and instance["name"] != instance_name:
continue
print(f"Restarting service for instance: {instance['name']}")
service_type = instance.get("type", "systemctl")
host = instance["host"]
service_name = instance.get("service_name", f"odoo_{instance['name']}")
# Access ssh as a dictionary
ssh_settings = instance.get("ssh", {})
ssh_user = ssh_settings.get("user", "root")
ssh_key_path = ssh_settings.get("key_path")
# ssh_password = ssh_settings.get("password") # Not used with key
if service_type == "systemctl":
if host == "localhost" or host == "127.0.0.1":
cmd = f"sudo systemctl restart {service_name}"
else:
if not ssh_key_path:
cmd = f"ssh -t {ssh_user}@{host} 'sudo systemctl restart {service_name}'"
else:
cmd = f"ssh -i {ssh_key_path} {ssh_user}@{host} 'sudo systemctl restart {service_name}'"
elif service_type == "docker":
container_name = instance.get(
"container_name", f"odoo_{instance['name']}"
)
if host == "localhost" or host == "127.0.0.1":
cmd = f"docker restart {container_name}"
else:
if not ssh_key_path:
raise ValueError(
f"SSH key path not specified for {instance['name']}"
)
cmd = f"ssh -i {ssh_key_path} {ssh_user}@{host} 'docker restart {container_name}'"
else:
print(
f"Unsupported service type '{service_type}' for {instance['name']}"
)
continue
try:
print(f"Executing: {cmd}")
subprocess.run(
cmd, shell=True, check=True, capture_output=True, text=True
)
print(f"Service restarted successfully for {instance['name']}")
except subprocess.CalledProcessError as e:
print(f"Error restarting service for {instance['name']}: {e}")
raise
def update_and_restart(self, instance_name=None):
"""Combine update, upgrade, and restart."""
updated = self.update_and_upgrade(instance_name)
if updated:
self.restart_service(instance_name)
return updated