mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-24 02:08:36 +00:00
[13.0][IMP] Make cloud_platform fully abstract + update related modules (#244)
This commit is contained in:
committed by
Yannick Vaucher
co-authored by
Yannick Vaucher
parent
8a4e636d94
commit
cd4fec96ee
@@ -7,11 +7,9 @@ import os
|
||||
import re
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
from openerp import api, models, SUPERUSER_ID
|
||||
from openerp.tools.config import config
|
||||
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
@@ -27,19 +25,26 @@ PlatformConfig = namedtuple(
|
||||
)
|
||||
|
||||
|
||||
class FilestoreKind(object):
|
||||
db = 'db'
|
||||
s3 = 's3' # or compatible s3 object storage
|
||||
swift = 'swift'
|
||||
file = 'file'
|
||||
|
||||
|
||||
DefaultConfig = PlatformConfig(filestore=FilestoreKind.db)
|
||||
FilestoreKind = namedtuple(
|
||||
'FilestoreKind',
|
||||
['name', 'location']
|
||||
)
|
||||
|
||||
|
||||
class CloudPlatform(models.AbstractModel):
|
||||
_name = 'cloud.platform'
|
||||
|
||||
@api.model
|
||||
def _default_config(self):
|
||||
return PlatformConfig(self._filestore_kinds()['db'])
|
||||
|
||||
@api.model
|
||||
def _filestore_kinds(self):
|
||||
return {
|
||||
'db': FilestoreKind('db', 'local'),
|
||||
'file': FilestoreKind('file', 'local'),
|
||||
}
|
||||
|
||||
@api.model
|
||||
def _platform_kinds(self):
|
||||
# XXX for backward compatibility, we need this one here, move
|
||||
@@ -66,7 +71,7 @@ class CloudPlatform(models.AbstractModel):
|
||||
None
|
||||
)
|
||||
configs = configs_getter() if configs_getter else {}
|
||||
return configs.get(environment) or DefaultConfig
|
||||
return configs.get(environment) or self._default_config()
|
||||
|
||||
# Due to the addition of the ovh cloud platform
|
||||
# This will be moved to cloud_platform_exoscale on v11
|
||||
@@ -75,130 +80,25 @@ class CloudPlatform(models.AbstractModel):
|
||||
self.install('exoscale')
|
||||
|
||||
@api.model
|
||||
def install(self, platform_kind):
|
||||
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', platform_kind)
|
||||
environment = config['running_env']
|
||||
configs = self._config_by_server_env(platform_kind, environment)
|
||||
params.set_param('ir_attachment.location', configs.filestore)
|
||||
environment_name = self._get_running_env()
|
||||
configs = self._config_by_server_env(platform_kind, environment_name)
|
||||
params.set_param('ir_attachment.location', configs.filestore.name)
|
||||
self.check()
|
||||
if configs.filestore in [FilestoreKind.swift, FilestoreKind.s3]:
|
||||
if configs.filestore.location == 'remote':
|
||||
self.env['ir.attachment'].sudo().force_storage()
|
||||
_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.get('SWIFT_WRITE_CONTAINER', '')
|
||||
if environment_name in ('prod', 'integration', 'labs'):
|
||||
assert container_name, (
|
||||
"SWIFT_WRITE_CONTAINER environment variable is required when "
|
||||
"ir_attachment.location is 'swift'.\n"
|
||||
"Normally, 'swift' is activated on labs, integration "
|
||||
"and production, but should not be used in dev environment"
|
||||
" (or using a dedicated dev bucket, never using the "
|
||||
"integration/prod bucket).\n"
|
||||
"If you don't actually need a bucket, change the"
|
||||
" 'ir_attachment.location' parameter."
|
||||
)
|
||||
prod_container = bool(re.match(r'[a-z0-9-]+-odoo-prod',
|
||||
container_name))
|
||||
if environment_name == 'prod':
|
||||
assert prod_container, (
|
||||
"SWIFT_WRITE_CONTAINER should match '<client>-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 "
|
||||
"'<client>-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()'."
|
||||
)
|
||||
def install(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@api.model
|
||||
def _check_s3(self, environment_name):
|
||||
params = self.env['ir.config_parameter'].sudo()
|
||||
use_s3 = params.get_param('ir_attachment.location') == FilestoreKind.s3
|
||||
if environment_name in ('prod', 'integration'):
|
||||
assert use_s3, (
|
||||
"S3 must be used on production and integration instances. "
|
||||
"It is activated by setting 'ir_attachment.location.' to 's3'."
|
||||
" The 'install_exoscale()' function sets this option "
|
||||
"automatically."
|
||||
)
|
||||
if use_s3:
|
||||
assert os.environ.get('AWS_ACCESS_KEY_ID'), (
|
||||
"AWS_ACCESS_KEY_ID environment variable is required when "
|
||||
"ir_attachment.location is 's3'."
|
||||
)
|
||||
assert os.environ.get('AWS_SECRET_ACCESS_KEY'), (
|
||||
"AWS_SECRET_ACCESS_KEY environment variable is required when "
|
||||
"ir_attachment.location is 's3'."
|
||||
)
|
||||
bucket_name = os.environ.get('AWS_BUCKETNAME', '')
|
||||
if environment_name in ('prod', 'integration', 'labs'):
|
||||
assert bucket_name, (
|
||||
"AWS_BUCKETNAME environment variable is required when "
|
||||
"ir_attachment.location is 's3'.\n"
|
||||
"Normally, 's3' is activated on labs, integration "
|
||||
"and production, but should not be used in dev environment"
|
||||
" (or using a dedicated dev bucket, never using the "
|
||||
"integration/prod bucket).\n"
|
||||
"If you don't actually need a bucket, change the"
|
||||
" 'ir_attachment.location' parameter."
|
||||
)
|
||||
prod_bucket = bool(re.match(r'[a-z-0-9]+-odoo-prod', bucket_name))
|
||||
if environment_name == 'prod':
|
||||
assert prod_bucket, (
|
||||
"AWS_BUCKETNAME should match '<client>-odoo-prod', "
|
||||
"we got: '%s'" % (bucket_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_bucket, (
|
||||
"AWS_BUCKETNAME should not match '<client>-odoo-prod', "
|
||||
"we got: '%s'" % (bucket_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_exoscale()'."
|
||||
)
|
||||
def _check_filestore(self, environment_name):
|
||||
raise NotImplementedError
|
||||
|
||||
@api.model
|
||||
def _check_redis(self, environment_name):
|
||||
@@ -235,14 +135,11 @@ class CloudPlatform(models.AbstractModel):
|
||||
if not kind:
|
||||
_logger.warning(
|
||||
"cloud platform not configured, you should "
|
||||
"probably run 'env['cloud.platform'].install_exoscale()'"
|
||||
"probably run 'env['cloud.platform'].install()'"
|
||||
)
|
||||
return
|
||||
environment_name = config['running_env']
|
||||
if kind == 'exoscale':
|
||||
self._check_s3(environment_name)
|
||||
elif kind == 'ovh':
|
||||
self._check_swift(environment_name)
|
||||
environment_name = self._get_running_env()
|
||||
self._check_filestore(environment_name)
|
||||
self._check_redis(environment_name)
|
||||
|
||||
@api.cr
|
||||
|
||||
Reference in New Issue
Block a user