Browse Source

[FIX] envs

Gogs 6 years ago
parent
commit
b52a2dee48
4 changed files with 87 additions and 8 deletions
  1. 37 2
      api/resources/odoo_resource.py
  2. 50 4
      api/utils/odoo_api.py
  3. 0 1
      data/.env
  4. 0 1
      odoo_control/settings.py

+ 37 - 2
api/resources/odoo_resource.py

@@ -17,7 +17,8 @@ from api.utils.odoo_api import (
     remove_database_seed,
     create_odoo_container,
     apply_permissions,
-    get_odoo_internal_ip
+    get_odoo_internal_ip,
+    install_module
 )
 from api.utils.jwt_token import get_user
 from api.utils.jwt_authentication import JWTAuthentication
@@ -39,6 +40,7 @@ class OdooResource(Resource):
     def prepend_urls(self):
         return [
             url(r'^(?P<resource_name>%s)/create%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('odoo_create'), name='api_odoo_create'),
+            url(r'^(?P<resource_name>%s)/install_module%s$' % (self._meta.resource_name, trailing_slash), self.wrap_view('install_odoo_module'), name='api_install_odoo_module'),
         ]
     
     '''
@@ -249,4 +251,37 @@ class OdooResource(Resource):
                 'ip': settings.EXTERNAL_IP,
                 'port': port
             }
-        })
+        })
+
+    '''
+    '''
+    def install_odoo_module(self, request, **kwargs):
+        self.method_check(request, allowed='post')
+        
+        err = {'error_message': None}
+        data = None
+
+        try:
+            data = json.loads(request.body)
+        except JSONDecodeError:
+            error('json decode error')
+
+        if not data:
+            err['error_message'] = 'cannot parse request body'
+
+        if data and (not 'system' in data or not 'module' in data):
+            err['error_message'] = 'module and system name is required'
+
+        result = install_module(data.get('system', None), data.get('module', None))
+
+        if 'error' in result:
+            err['error_message'] = result['error']
+
+        return self.create_response(request, {
+            'result': ''
+        })
+
+        
+
+        
+

+ 50 - 4
api/utils/odoo_api.py

@@ -7,10 +7,13 @@ from api.utils.docker_api import (
     get_internal_ip,
     execute_command,
     run_container,
+    start_container,
+    stop_container,
     copy_in
 )
-from api.utils.logger import info
+from api.utils.git_api import clone_repo
 from api.utils.command import execute
+from api.utils.logger import info
 import os
 import socket
 import time
@@ -206,7 +209,50 @@ def get_odoo_internal_ip(name=None):
 '''
 '''
 def install_module(system_name=None, module_name=None):
-    if not system_name or not module_name:
-        return False
+    if not system_name:
+        return {'error': 'system name is required'} 
+
+    if not module_name:
+        return {'error': 'module name is required'}
+
+    # 1. check system path
+    system_path = os.path.join(settings.ODOO_ROOT_PATH, system_name)
+
+    if not os.path.exists(system_path):
+        return {'error': 'system path not exists'}
+
+    # 2. stop system container
+    stopped = stop_container(system_name)
+
+    if not stopped:
+        return {'error': 'cannot stop system'}
+
+    # 3. 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
+    cloned = clone_repo(module_name, os.path.join(system_path, 'custom/addons'))
+
+    if not cloned:
+        return {'error': 'cannot clone repo'}
+
+    # 5. start system container
+    started = start_container(system_name)
+
+    if not started:
+        return {'error': 'cannot start system'}
+
+    return {'result': '%s installed' % module_name}
+
+'''
+'''
+def up():
+    pass
+
+'''
+'''
+def kill():
+    pass

+ 0 - 1
data/.env

@@ -29,4 +29,3 @@ ODOO_DB_PORT = '5432'
 ODOO_DB_USER = 'postgres'
 ODOO_DB_PASSWORD = 'root'
 GIT_PATH = '/opt/gogs/git/gogs-repositories'
-GIT_HOST = 'https://repo.eiru.com.py'

+ 0 - 1
odoo_control/settings.py

@@ -111,4 +111,3 @@ ODOO_DB_PORT = CONFIG('ODOO_DB_PORT', default=5432, cast=int)
 ODOO_DB_USER = CONFIG('ODOO_DB_USER', 'postgres')
 ODOO_DB_PASSWORD = CONFIG('ODOO_DB_PASSWORD', 'root')
 GIT_PATH = CONFIG('GIT_PATH')
-GIT_HOST = CONFIG('GIT_HOST')