From 7fc296d73e559ebc40d20667af2ba5d2c2c490ee Mon Sep 17 00:00:00 2001 From: jcoux Date: Mon, 29 Oct 2018 11:45:54 +0100 Subject: [PATCH] Backport OVH modules from 10.0 --- .../{__manifest__.py => __openerp__.py} | 2 +- attachment_swift/models/ir_attachment.py | 2 +- attachment_swift/tests/test_mock_swift_api.py | 2 +- .../tests/test_with_swift_store.py | 2 +- .../{__manifest__.py => __openerp__.py} | 2 +- .../models/ir_attachment.py | 13 ++- cloud_platform/__openerp__.py | 2 +- cloud_platform/models/cloud_platform.py | 96 +++++++++++++++++-- cloud_platform/songs.py | 4 + .../{__manifest__.py => __openerp__.py} | 2 +- cloud_platform_ovh/models/cloud_platform.py | 8 +- requirements.txt | 2 + 12 files changed, 116 insertions(+), 21 deletions(-) rename attachment_swift/{__manifest__.py => __openerp__.py} (95%) rename base_attachment_object_storage/{__manifest__.py => __openerp__.py} (94%) rename cloud_platform_ovh/{__manifest__.py => __openerp__.py} (94%) diff --git a/attachment_swift/__manifest__.py b/attachment_swift/__openerp__.py similarity index 95% rename from attachment_swift/__manifest__.py rename to attachment_swift/__openerp__.py index 1a63b4b..5ff01cf 100644 --- a/attachment_swift/__manifest__.py +++ b/attachment_swift/__openerp__.py @@ -5,7 +5,7 @@ {'name': 'Attachments on Swift storage', 'summary': 'Store assets and attachments on a Swift compatible object store', - 'version': '10.0.1.1.0', + 'version': '9.0.1.1.0', 'author': 'Camptocamp,Odoo Community Association (OCA)', 'license': 'AGPL-3', 'category': 'Knowledge Management', diff --git a/attachment_swift/models/ir_attachment.py b/attachment_swift/models/ir_attachment.py index f57cacb..e307a8a 100644 --- a/attachment_swift/models/ir_attachment.py +++ b/attachment_swift/models/ir_attachment.py @@ -8,7 +8,7 @@ import logging import os from ..swift_uri import SwiftUri -from odoo import api, exceptions, models, _ +from openerp import api, exceptions, models, _ _logger = logging.getLogger(__name__) diff --git a/attachment_swift/tests/test_mock_swift_api.py b/attachment_swift/tests/test_mock_swift_api.py index 7c093f9..72b088c 100644 --- a/attachment_swift/tests/test_mock_swift_api.py +++ b/attachment_swift/tests/test_mock_swift_api.py @@ -5,7 +5,7 @@ import os from mock import patch -from odoo.addons.base.tests.test_ir_attachment import TestIrAttachment +from openerp.addons.base.tests.test_ir_attachment import TestIrAttachment from ..swift_uri import SwiftUri diff --git a/attachment_swift/tests/test_with_swift_store.py b/attachment_swift/tests/test_with_swift_store.py index 61c44fd..dcfd54d 100644 --- a/attachment_swift/tests/test_with_swift_store.py +++ b/attachment_swift/tests/test_with_swift_store.py @@ -2,7 +2,7 @@ # Copyright 2017 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) -from odoo.addons.base.tests.test_ir_attachment import TestIrAttachment +from openerp.addons.base.tests.test_ir_attachment import TestIrAttachment from ..swift_uri import SwiftUri from swiftclient.exceptions import ClientException diff --git a/base_attachment_object_storage/__manifest__.py b/base_attachment_object_storage/__openerp__.py similarity index 94% rename from base_attachment_object_storage/__manifest__.py rename to base_attachment_object_storage/__openerp__.py index 2ea0bf9..29d5cd8 100644 --- a/base_attachment_object_storage/__manifest__.py +++ b/base_attachment_object_storage/__openerp__.py @@ -5,7 +5,7 @@ {'name': 'Base Attachment Object Store', 'summary': 'Base module for the implementation of external object store.', - 'version': '10.0.1.2.0', + 'version': '9.0.1.2.0', 'author': 'Camptocamp,Odoo Community Association (OCA)', 'license': 'AGPL-3', 'category': 'Knowledge Management', diff --git a/base_attachment_object_storage/models/ir_attachment.py b/base_attachment_object_storage/models/ir_attachment.py index 319e2bf..8ece688 100644 --- a/base_attachment_object_storage/models/ir_attachment.py +++ b/base_attachment_object_storage/models/ir_attachment.py @@ -9,7 +9,8 @@ import psycopg2 import odoo from contextlib import closing, contextmanager -from odoo import api, exceptions, models, _ +from openerp import api, exceptions, models, _ +from openerp import SUPERUSER_ID _logger = logging.getLogger(__name__) @@ -40,10 +41,14 @@ class IrAttachment(models.Model): _local_fields = ('image_small', 'image_medium', 'web_icon_data') @api.cr - def _register_hook(self): - super(IrAttachment, self)._register_hook() + def _register_hook(self, cr): + super(IrAttachment, self)._register_hook(cr) # ignore if we are not using an object storage - if self._storage() not in self._get_stores(): + # Use directly SUPERUSER_ID + # because the uid parameter is required + # in function _storage and + # the SUPERUSER_ID is used directly instead of use the uid parameter. + if self._storage(cr, SUPERUSER_ID) not in self._get_stores(): return curframe = inspect.currentframe() calframe = inspect.getouterframes(curframe, 2) diff --git a/cloud_platform/__openerp__.py b/cloud_platform/__openerp__.py index 26b0b1e..72f89eb 100644 --- a/cloud_platform/__openerp__.py +++ b/cloud_platform/__openerp__.py @@ -10,7 +10,7 @@ 'license': 'AGPL-3', 'category': 'Extra Tools', 'depends': [ - 'attachment_s3', + 'base_attachment_object_storage', 'session_redis', 'monitoring_status', 'logging_json', diff --git a/cloud_platform/models/cloud_platform.py b/cloud_platform/models/cloud_platform.py index 602d5ee..56c2648 100644 --- a/cloud_platform/models/cloud_platform.py +++ b/cloud_platform/models/cloud_platform.py @@ -30,6 +30,7 @@ PlatformConfig = namedtuple( class FilestoreKind(object): db = 'db' s3 = 's3' # or compatible s3 object storage + swift = 'swift' file = 'file' @@ -37,26 +38,104 @@ class CloudPlatform(models.AbstractModel): _name = 'cloud.platform' @api.model - def _config_by_server_env(self, environment): + def _platform_kinds(self): + # XXX for backward compatibility, we need this one here, move + # it in cloud_platform_exoscale in V11 + return ['exoscale'] + + # XXX for backward compatibility, we need this one here, move + # it in cloud_platform_exoscale in V11 + @api.model + def _config_by_server_env_for_exoscale(self): configs = { 'prod': PlatformConfig(filestore=FilestoreKind.s3), 'integration': PlatformConfig(filestore=FilestoreKind.s3), 'test': PlatformConfig(filestore=FilestoreKind.db), 'dev': PlatformConfig(filestore=FilestoreKind.db), } - return configs.get(environment) or configs['dev'] + return configs + @api.model + def _config_by_server_env(self, platform_kind, environment): + configs_getter = getattr( + self, + '_config_by_server_env_for_%s' % platform_kind, + None + ) + configs = configs_getter() if configs_getter else {} + return configs.get(environment) or FilestoreKind.db + + # Due to the addition of the ovh cloud platform + # This will be moved to cloud_platform_exoscale on v11 @api.model def install_exoscale(self): + self.install('exoscale') + + @api.model + def install(self, platform_kind): + assert platform_kind in self._platform_kinds() params = self.env['ir.config_parameter'].sudo() - params.set_param('cloud.platform.kind', 'exoscale') + params.set_param('cloud.platform.kind', platform_kind) environment = config['running_env'] - configs = self._config_by_server_env(environment) + configs = self._config_by_server_env(platform_kind, environment) params.set_param('ir_attachment.location', configs.filestore) self.check() - if configs.filestore == FilestoreKind.s3: + if configs.filestore in [FilestoreKind.swift, FilestoreKind.s3]: self.env['ir.attachment'].sudo().force_storage() - _logger.info('cloud platform configured for exoscale') + _logger.info('cloud platform configured for {}'.format(platform_kind)) + + @api.model + def _check_swift(self, environment_name): + params = self.env['ir.config_parameter'].sudo() + use_swift = (params.get_param('ir_attachment.location') == + FilestoreKind.swift) + if environment_name in ('prod', 'integration'): + assert use_swift, ( + "Swift must be used on production and integration instances. " + "It is activated, setting 'ir_attachment.location.' to 'swift'" + " The 'install_exoscale()' function sets this option " + "automatically." + ) + if use_swift: + assert os.environ.get('SWIFT_AUTH_URL'), ( + "SWIFT_AUTH_URL environment variable is required when " + "ir_attachment.location is 'swift'." + ) + assert os.environ.get('SWIFT_ACCOUNT'), ( + "SWIFT_ACCOUNT environment variable is required when " + "ir_attachment.location is 'swift'." + ) + assert os.environ.get('SWIFT_PASSWORD'), ( + "SWIFT_PASSWORD environment variable is required when " + "ir_attachment.location is 'swift'." + ) + container_name = os.environ['SWIFT_WRITE_CONTAINER'] + if environment_name in ('integration', 'prod'): + assert container_name, ( + "SWIFT_WRITE_CONTAINER must not be empty for prod " + "and integration" + ) + prod_container = bool(re.match(r'[a-z]+-odoo-prod', + container_name)) + if environment_name == 'prod': + assert prod_container, ( + "SWIFT_WRITE_CONTAINER should match '-odoo-prod', " + "we got: '%s'" % (container_name,) + ) + else: + # if we are using the prod bucket on another instance + # such as an integration, we must be sure to be in read only! + assert not prod_container, ( + "SWIFT_WRITE_CONTAINER should not match " + "'-odoo-prod', we got: '%s'" % (container_name,) + ) + elif environment_name == 'test': + # store in DB so we don't have files local to the host + assert params.get_param('ir_attachment.location') == 'db', ( + "In test instances, files must be stored in the database with " + "'ir_attachment.location' set to 'db'. This is " + "automatically set by the function 'install_ovh()'." + ) @api.model def _check_s3(self, environment_name): @@ -148,7 +227,10 @@ class CloudPlatform(models.AbstractModel): ) return environment_name = config['running_env'] - self._check_s3(environment_name) + if kind == 'exoscale': + self._check_s3(environment_name) + elif kind == 'ovh': + self._check_swift(environment_name) self._check_redis(environment_name) @api.cr diff --git a/cloud_platform/songs.py b/cloud_platform/songs.py index d8ef9b5..277c7dd 100644 --- a/cloud_platform/songs.py +++ b/cloud_platform/songs.py @@ -3,3 +3,7 @@ def install_exoscale(ctx): ctx.env['cloud.platform'].install_exoscale() + + +def install_ovh(ctx): + ctx.env['cloud.platform'].install('ovh') diff --git a/cloud_platform_ovh/__manifest__.py b/cloud_platform_ovh/__openerp__.py similarity index 94% rename from cloud_platform_ovh/__manifest__.py rename to cloud_platform_ovh/__openerp__.py index 7436ebe..415e3a5 100644 --- a/cloud_platform_ovh/__manifest__.py +++ b/cloud_platform_ovh/__openerp__.py @@ -5,7 +5,7 @@ {'name': 'Cloud Platform OVH', 'summary': 'Addons required for the Camptocamp Cloud Platform on OVH', - 'version': '10.0.1.1.0', + 'version': '9.0.1.1.0', 'author': 'Camptocamp,Odoo Community Association (OCA)', 'license': 'AGPL-3', 'category': 'Extra Tools', diff --git a/cloud_platform_ovh/models/cloud_platform.py b/cloud_platform_ovh/models/cloud_platform.py index bb00f0f..9645da2 100644 --- a/cloud_platform_ovh/models/cloud_platform.py +++ b/cloud_platform_ovh/models/cloud_platform.py @@ -4,13 +4,15 @@ import logging -from odoo import api, models +from openerp import api, models _logger = logging.getLogger(__name__) try: - from odoo.addons.cloud_platform.models.cloud_platform import FilestoreKind - from odoo.addons.cloud_platform.models.cloud_platform import PlatformConfig + from openerp.addons.cloud_platform.models.cloud_platform \ + import FilestoreKind + from openerp.addons.cloud_platform.models.cloud_platform \ + import PlatformConfig except ImportError: FilestoreKind = None PlatformConfig = None diff --git a/requirements.txt b/requirements.txt index 5dfb4b8..e3c77a7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,5 @@ boto==2.42.0 redis==2.10.5 python-json-logger==0.1.5 statsd==3.2.1 +python-swiftclient==3.4.0 +python-keystoneclient==3.13.0