Przeglądaj źródła

[IMP] odoo module installation

Gogs 6 lat temu
rodzic
commit
967c2150e6
5 zmienionych plików z 77 dodań i 43 usunięć
  1. 2 2
      api/resources/odoo_resource.py
  2. 18 1
      api/utils/docker_api.py
  3. 20 14
      api/utils/git_api.py
  4. 35 24
      api/utils/odoo_api.py
  5. 2 2
      data/.env

+ 2 - 2
api/resources/odoo_resource.py

@@ -289,7 +289,7 @@ class OdooResource(Resource):
         if 'error' in result:
             err['error_message'] = result['error']
 
-        if len(err) != 0:    
+        if err['error_message']:    
             return self.create_response(request, err)
 
         module_names = ''
@@ -299,5 +299,5 @@ class OdooResource(Resource):
         send_email('El usuario %s ha instalado los siguientes módulos en el sistema %d:\n%s', (user.username, system, module_names), user.username, only_admins=True)
         
         return self.create_response(request, {
-            'success_message': '%s successfully installed' % data.get('module')
+            'success_message': 'modules successfully installed'
         })

+ 18 - 1
api/utils/docker_api.py

@@ -153,7 +153,7 @@ def start_container(id_or_name=None):
         return None
 
     try:
-        container = client.containers.get(id)
+        container = client.containers.get(id_or_name)
         container.start()
 
         time.sleep(1)
@@ -268,3 +268,20 @@ def copy_in(container_id_or_name=None, path=None, tarfile=None):
         return result
     except (NotFound, APIError, IOError):
         return False
+
+'''
+'''
+def container_is_running(id_or_name=None):
+    if not id_or_name:
+        return False
+
+    client = get_docker_client()
+
+    if not client:
+        return False
+
+    try:
+        container = client.containers.get(id_or_name)
+        return (False, True)[container.status == 'running']
+    except (NotFound, APIError):
+        return False

+ 20 - 14
api/utils/git_api.py

@@ -6,6 +6,7 @@ from git.repo.base import Repo
 from git.repo.fun import is_git_dir
 from git.util import join_path
 from git.exc import GitCommandError
+import os
 
 '''
 '''
@@ -52,24 +53,29 @@ def resolve_owner_by_name(name=None):
 '''
 '''
 def clone_repo(name=None, to=None, branch='master'):
-    if not name or not to:
-        return False
-    
-    owner = resolve_owner_by_name(name)
+    return clone_repos([name], to, branch)
 
-    if not owner:
+'''
+'''
+def clone_repos(repo_names=[], to=None, branch='master'):
+    if len(repo_names) == 0:
         return False
 
-    repo_path = '%s.git' % join_path(settings.GIT_PATH, owner, name)
-
-    try:
-        repo = Repo(repo_path)
-        repo.clone(join_path(to, name), branch=branch)
-        repo.close()
+    for user_folder in get_users_folders():
+        for repo_name in repo_names:
+            repo_path = '%s.git' % join_path(settings.GIT_PATH, user_folder, repo_name)
 
-        return True
-    except GitCommandError:
-        return False
+            if not os.path.exists(repo_path):
+                continue
+            
+            try:
+                repo = Repo(repo_path)
+                repo.clone(join_path(to, repo_name), branch=branch)
+                repo.close()
+            except GitCommandError:
+                pass
+
+    return True
 
 '''
 '''

+ 35 - 24
api/utils/odoo_api.py

@@ -9,12 +9,17 @@ from api.utils.docker_api import (
     run_container,
     start_container,
     stop_container,
-    copy_in
+    copy_in,
+    container_is_running
 )
 from api.utils.git_api import clone_repo
 from api.utils.command import execute
 from api.utils.email import send_email
-from api.utils.logger import info
+from api.utils.logger import (
+    info,
+    error,
+    warning
+)
 import os
 import socket
 import time
@@ -215,33 +220,44 @@ def install_modules(system_name=None, module_names=[]):
     if len(module_names) == 0:
         return {'error': 'module names is required'}
 
-     # 1. check system path
+    # 1. check system path
     system_path = os.path.join(settings.ODOO_ROOT_PATH, system_name)
 
     if not os.path.exists(system_path):
+        error('%s path not exists' % system_name)
         return {'error': 'system path not exists'}
+    
+    info('%s path checked' % system_name)
 
-    # # 2. stop system container
-    # stopped = stop_container(system_name)
+    # 2. check system status
+    system_is_up = container_is_running(system_name)
+    info('%s system state detected' % system_name)
+    
+    # 3. stop system container if necesary
+    if system_is_up:
+        stopped = stop_container(system_name)
 
-    # if not stopped:
-    #     return {'error': 'cannot stop system'}
+        if not stopped:
+            error('%s system cannot be stopped' % system_name)
+            return {'error': 'cannot stop system'}
+    
+    info('%s system state successfully passed [state=%s]' % (system_name, system_is_up))
 
     for module_name in module_names:
-        # 3. check module path and remove it
+        # 4. check module path and remove it
         module_path = os.path.join(system_path, 'custom-addons', module_name)
 
         if os.path.exists(module_path):
             execute(['rm', '-Rf', module_path])
 
-        # 4. clone repo
+        # 5. clone repo
         addons_path = os.path.join(system_path, 'custom-addons')
         cloned = clone_repo(module_name, addons_path)
 
         if not cloned:
             return {'error': 'cannot clone repo'}
 
-        # 5. remove git data
+        # 6. remove git data
         git_data_path = os.path.join(module_path, '.git')
 
         if os.path.exists(git_data_path):
@@ -251,26 +267,21 @@ def install_modules(system_name=None, module_names=[]):
             execute(['chmod', '-R', '777', module_path])
         except Exception:
             pass
+        
+        info('%s is installed in %s system' % (module_name, system_name))
 
-    # # 7. start system container
-    # started = start_container(system_name)
+    # 7. start system container
+    if system_is_up:
+        started = start_container(system_name)
 
-    # if not started:
-    #     return {'error': 'cannot start system'}
+        if not started:
+            error('%s system cannot be started' % system_name)
+            return {'error': 'cannot start system'}
 
+    info('all modules requested installed')
     return {'result': '%s installed' % module_name}
 
 '''
 '''
 def install_module(system_name=None, module_name=None):
     return install_modules(system_name, [module_name])
-
-'''
-'''
-def up():
-    pass
-
-'''
-'''
-def kill():
-    pass

+ 2 - 2
data/.env

@@ -26,6 +26,6 @@ ODOO_ADMIN_PASSWORD = 'admin'
 ODOO_DB_CONTAINER = 'postgres'
 ODOO_DB_HOST = '172.20.0.10'
 ODOO_DB_PORT = '5432'
-ODOO_DB_USER = 'postgres'
-ODOO_DB_PASSWORD = 'root'
+ODOO_DB_USER = 'odoo'
+ODOO_DB_PASSWORD = 'odoo'
 GIT_PATH = '/opt/gogs/git/gogs-repositories'