This commit is contained in:
vrenaville
2022-05-20 14:06:41 +02:00
parent 18d07353d9
commit 126b3de74b
+83 -80
View File
@@ -19,99 +19,95 @@ _logger = logging.getLogger(__name__)
def is_true(strval): def is_true(strval):
return bool(strtobool(strval or '0'.lower())) return bool(strtobool(strval or "0".lower()))
PlatformConfig = namedtuple( PlatformConfig = namedtuple("PlatformConfig", "filestore")
'PlatformConfig',
'filestore'
)
class FilestoreKind(object): class FilestoreKind(object):
db = 'db' db = "db"
s3 = 's3' # or compatible s3 object storage s3 = "s3" # or compatible s3 object storage
swift = 'swift' swift = "swift"
file = 'file' file = "file"
azure = "azure"
class CloudPlatform(osv.osv_abstract): class CloudPlatform(osv.osv_abstract):
_name = 'cloud.platform' _name = "cloud.platform"
def _platform_kinds(self): def _platform_kinds(self):
# XXX for backward compatibility, we need this one here, move # XXX for backward compatibility, we need this one here, move
# it in cloud_platform_exoscale in V11 # it in cloud_platform_exoscale in V11
return ['exoscale'] return ["exoscale"]
def _filestore_kinds(self): def _filestore_kinds(self):
# XXX for backward compatibility, we need this one here, move # XXX for backward compatibility, we need this one here, move
# it in cloud_platform_exoscale in V11 # it in cloud_platform_exoscale in V11
return ['exoscale'] return ["exoscale"]
# XXX for backward compatibility, we need this one here, move # XXX for backward compatibility, we need this one here, move
# it in cloud_platform_exoscale in V11 # it in cloud_platform_exoscale in V11
def _config_by_server_env_for_exoscale(self): def _config_by_server_env_for_exoscale(self):
configs = { configs = {
'prod': PlatformConfig(filestore=FilestoreKind.s3), "prod": PlatformConfig(filestore=FilestoreKind.s3),
'integration': PlatformConfig(filestore=FilestoreKind.s3), "integration": PlatformConfig(filestore=FilestoreKind.s3),
'test': PlatformConfig(filestore=FilestoreKind.db), "test": PlatformConfig(filestore=FilestoreKind.db),
'dev': PlatformConfig(filestore=FilestoreKind.db), "dev": PlatformConfig(filestore=FilestoreKind.db),
} }
return configs return configs
def _config_by_server_env(self, platform_kind, environment): def _config_by_server_env(self, platform_kind, environment):
configs_getter = getattr( configs_getter = getattr(
self, self, "_config_by_server_env_for_%s" % platform_kind, None
'_config_by_server_env_for_%s' % platform_kind,
None
) )
configs = configs_getter() if configs_getter else {} configs = configs_getter() if configs_getter else {}
return configs.get(environment) or FilestoreKind.db return configs.get(environment) or FilestoreKind.db
def _get_running_env(self): def _get_running_env(self):
environment_name = config['running_env'] environment_name = config["running_env"]
if environment_name.startswith('labs'): if environment_name.startswith("labs"):
# We allow to have environments such as 'labs-logistics' # We allow to have environments such as 'labs-logistics'
# or 'labs-finance', in order to have the matching ribbon. # or 'labs-finance', in order to have the matching ribbon.
environment_name = 'labs' environment_name = "labs"
return environment_name return environment_name
# Due to the addition of the ovh cloud platform # Due to the addition of the ovh cloud platform
# This will be moved to cloud_platform_exoscale on v11 # This will be moved to cloud_platform_exoscale on v11
def install_exoscale(self, cr, uid, context=None): def install_exoscale(self, cr, uid, context=None):
self.install(cr, uid, 'exoscale', context) self.install(cr, uid, "exoscale", context)
def install(self, cr, uid, platform_kind, context=None): def install(self, cr, uid, platform_kind, context=None):
assert platform_kind in self._platform_kinds() assert platform_kind in self._platform_kinds()
params = self.pool.get('ir.config_parameter') params = self.pool.get("ir.config_parameter")
params.set_param( params.set_param(
cr, SUPERUSER_ID, cr, SUPERUSER_ID, "cloud.platform.kind", platform_kind, context=context
'cloud.platform.kind', platform_kind,
context=context
) )
environment_name = self._get_running_env() environment_name = self._get_running_env()
configs = self._config_by_server_env(platform_kind, environment_name) configs = self._config_by_server_env(platform_kind, environment_name)
params.set_param( params.set_param(
cr, SUPERUSER_ID, cr,
'ir_attachment.location', configs.filestore, SUPERUSER_ID,
context=context "ir_attachment.location",
configs.filestore,
context=context,
) )
self.check(cr, uid, context) self.check(cr, uid, context)
if configs.filestore in [FilestoreKind.swift, FilestoreKind.s3]: if configs.filestore in [FilestoreKind.swift, FilestoreKind.s3]:
self.pool.get('ir.attachment').force_storage( self.pool.get("ir.attachment").force_storage(
cr, SUPERUSER_ID, context=context cr, SUPERUSER_ID, context=context
) )
_logger.info('cloud platform configured for {}'.format(platform_kind)) _logger.info("cloud platform configured for {}".format(platform_kind))
def _check_swift(self, cr, uid, environment_name, context=None): def _check_swift(self, cr, uid, environment_name, context=None):
params = self.pool.get('ir.config_parameter') params = self.pool.get("ir.config_parameter")
use_swift = ( use_swift = (
params.get_param( params.get_param(
cr, SUPERUSER_ID, 'ir_attachment.location', context=context cr, SUPERUSER_ID, "ir_attachment.location", context=context
) == FilestoreKind.swift )
== FilestoreKind.swift
) )
if environment_name in ('prod', 'integration'): if environment_name in ("prod", "integration"):
# Labs instances use swift or s3 by default, but we don't want # Labs instances use swift or s3 by default, but we don't want
# to enforce it in case we want to test something with a different # to enforce it in case we want to test something with a different
# storage. At your own risks! # storage. At your own risks!
@@ -122,19 +118,19 @@ class CloudPlatform(osv.osv_abstract):
"automatically." "automatically."
) )
if use_swift: if use_swift:
assert os.environ.get('SWIFT_AUTH_URL'), ( assert os.environ.get("SWIFT_AUTH_URL"), (
"SWIFT_AUTH_URL environment variable is required when " "SWIFT_AUTH_URL environment variable is required when "
"ir_attachment.location is 'swift'." "ir_attachment.location is 'swift'."
) )
assert os.environ.get('SWIFT_ACCOUNT'), ( assert os.environ.get("SWIFT_ACCOUNT"), (
"SWIFT_ACCOUNT environment variable is required when " "SWIFT_ACCOUNT environment variable is required when "
"ir_attachment.location is 'swift'." "ir_attachment.location is 'swift'."
) )
assert os.environ.get('SWIFT_PASSWORD'), ( assert os.environ.get("SWIFT_PASSWORD"), (
"SWIFT_PASSWORD environment variable is required when " "SWIFT_PASSWORD environment variable is required when "
"ir_attachment.location is 'swift'." "ir_attachment.location is 'swift'."
) )
container_name = os.environ.get('SWIFT_WRITE_CONTAINER') container_name = os.environ.get("SWIFT_WRITE_CONTAINER")
assert container_name, ( assert container_name, (
"SWIFT_WRITE_CONTAINER environment variable is required when " "SWIFT_WRITE_CONTAINER environment variable is required when "
"ir_attachment.location is 'swift'.\n" "ir_attachment.location is 'swift'.\n"
@@ -145,9 +141,8 @@ class CloudPlatform(osv.osv_abstract):
"If you don't actually need a bucket, change the" "If you don't actually need a bucket, change the"
" 'ir_attachment.location' parameter." " 'ir_attachment.location' parameter."
) )
prod_container = bool(re.match(r'[a-z0-9-]+-odoo-prod', prod_container = bool(re.match(r"[a-z0-9-]+-odoo-prod", container_name))
container_name)) if environment_name == "prod":
if environment_name == 'prod':
assert prod_container, ( assert prod_container, (
"SWIFT_WRITE_CONTAINER should match '<client>-odoo-prod', " "SWIFT_WRITE_CONTAINER should match '<client>-odoo-prod', "
"we got: '%s'" % (container_name,) "we got: '%s'" % (container_name,)
@@ -159,21 +154,28 @@ class CloudPlatform(osv.osv_abstract):
"SWIFT_WRITE_CONTAINER should not match " "SWIFT_WRITE_CONTAINER should not match "
"'<client>-odoo-prod', we got: '%s'" % (container_name,) "'<client>-odoo-prod', we got: '%s'" % (container_name,)
) )
elif environment_name == 'test': elif environment_name == "test":
# store in DB so we don't have files local to the host # store in DB so we don't have files local to the host
assert params.get_param(cr, SUPERUSER_ID, 'ir_attachment.location', assert (
context=context) == 'db', ( params.get_param(
cr, SUPERUSER_ID, "ir_attachment.location", context=context
)
== "db"
), (
"In test instances, files must be stored in the database with " "In test instances, files must be stored in the database with "
"'ir_attachment.location' set to 'db'. This is " "'ir_attachment.location' set to 'db'. This is "
"automatically set by the function 'install_ovh()'." "automatically set by the function 'install_ovh()'."
) )
def _check_s3(self, cr, uid, environment_name, context=None): def _check_s3(self, cr, uid, environment_name, context=None):
params = self.pool.get('ir.config_parameter') params = self.pool.get("ir.config_parameter")
use_s3 = params.get_param( use_s3 = (
cr, SUPERUSER_ID, 'ir_attachment.location', context=context params.get_param(
) == FilestoreKind.s3 cr, SUPERUSER_ID, "ir_attachment.location", context=context
if environment_name in ('prod', 'integration'): )
== FilestoreKind.s3
)
if environment_name in ("prod", "integration"):
# Labs instances use swift or s3 by default, but we don't want # Labs instances use swift or s3 by default, but we don't want
# to enforce it in case we want to test something with a different # to enforce it in case we want to test something with a different
# storage. At your own risks! # storage. At your own risks!
@@ -184,15 +186,15 @@ class CloudPlatform(osv.osv_abstract):
"automatically." "automatically."
) )
if use_s3: if use_s3:
assert os.environ.get('AWS_ACCESS_KEY_ID'), ( assert os.environ.get("AWS_ACCESS_KEY_ID"), (
"AWS_ACCESS_KEY_ID environment variable is required when " "AWS_ACCESS_KEY_ID environment variable is required when "
"ir_attachment.location is 's3'." "ir_attachment.location is 's3'."
) )
assert os.environ.get('AWS_SECRET_ACCESS_KEY'), ( assert os.environ.get("AWS_SECRET_ACCESS_KEY"), (
"AWS_SECRET_ACCESS_KEY environment variable is required when " "AWS_SECRET_ACCESS_KEY environment variable is required when "
"ir_attachment.location is 's3'." "ir_attachment.location is 's3'."
) )
bucket_name = os.environ.get('AWS_BUCKETNAME') bucket_name = os.environ.get("AWS_BUCKETNAME")
assert bucket_name, ( assert bucket_name, (
"AWS_BUCKETNAME environment variable is required when " "AWS_BUCKETNAME environment variable is required when "
"ir_attachment.location is 's3'.\n" "ir_attachment.location is 's3'.\n"
@@ -203,8 +205,8 @@ class CloudPlatform(osv.osv_abstract):
"If you don't actually need a bucket, change the" "If you don't actually need a bucket, change the"
" 'ir_attachment.location' parameter." " 'ir_attachment.location' parameter."
) )
prod_bucket = bool(re.match(r'[a-z-0-9]+-odoo-prod', bucket_name)) prod_bucket = bool(re.match(r"[a-z-0-9]+-odoo-prod", bucket_name))
if environment_name == 'prod': if environment_name == "prod":
assert prod_bucket, ( assert prod_bucket, (
"AWS_BUCKETNAME should match '<client>-odoo-prod', " "AWS_BUCKETNAME should match '<client>-odoo-prod', "
"we got: '%s'" % (bucket_name,) "we got: '%s'" % (bucket_name,)
@@ -217,17 +219,21 @@ class CloudPlatform(osv.osv_abstract):
"we got: '%s'" % (bucket_name,) "we got: '%s'" % (bucket_name,)
) )
elif environment_name == 'test': elif environment_name == "test":
# store in DB so we don't have files local to the host # store in DB so we don't have files local to the host
assert params.get_param(cr, SUPERUSER_ID, 'ir_attachment.location', assert (
context=context) == 'db', ( params.get_param(
cr, SUPERUSER_ID, "ir_attachment.location", context=context
)
== "db"
), (
"In test instances, files must be stored in the database with " "In test instances, files must be stored in the database with "
"'ir_attachment.location' set to 'db'. This is " "'ir_attachment.location' set to 'db'. This is "
"automatically set by the function 'install_exoscale()'." "automatically set by the function 'install_exoscale()'."
) )
def _check_azure(self, cr, uid, environment_name, context=None): def _check_azure(self, cr, uid, environment_name, context=None):
params = self.env["ir.config_parameter"].sudo() params = self.pool.get("ir.config_parameter")
use_azure = params.get_param("ir_attachment.location") == AZURE_STORE_KIND.name use_azure = params.get_param("ir_attachment.location") == AZURE_STORE_KIND.name
if environment_name in ("prod", "integration"): if environment_name in ("prod", "integration"):
# Labs instances use azure by default, but we don't want # Labs instances use azure by default, but we don't want
@@ -305,38 +311,35 @@ class CloudPlatform(osv.osv_abstract):
"automatically set by the function 'install()'." "automatically set by the function 'install()'."
) )
def _check_redis(self, cr, uid, environment_name, context=None): def _check_redis(self, cr, uid, environment_name, context=None):
if environment_name in ('prod', 'integration', 'labs', 'test'): if environment_name in ("prod", "integration", "labs", "test"):
assert is_true(os.environ.get('ODOO_SESSION_REDIS')), ( assert is_true(os.environ.get("ODOO_SESSION_REDIS")), (
"Redis must be activated on prod, integration, labs," "Redis must be activated on prod, integration, labs,"
" test instances. This is done by setting ODOO_SESSION_REDIS=1." " test instances. This is done by setting ODOO_SESSION_REDIS=1."
) )
assert (os.environ.get('ODOO_SESSION_REDIS_HOST') or assert os.environ.get("ODOO_SESSION_REDIS_HOST") or os.environ.get(
os.environ.get('ODOO_SESSION_REDIS_SENTINEL_HOST')), ( "ODOO_SESSION_REDIS_SENTINEL_HOST"
), (
"ODOO_SESSION_REDIS_HOST or ODOO_SESSION_REDIS_SENTINEL_HOST " "ODOO_SESSION_REDIS_HOST or ODOO_SESSION_REDIS_SENTINEL_HOST "
"environment variable is required to connect on Redis" "environment variable is required to connect on Redis"
) )
assert os.environ.get('ODOO_SESSION_REDIS_PREFIX'), ( assert os.environ.get("ODOO_SESSION_REDIS_PREFIX"), (
"ODOO_SESSION_REDIS_PREFIX environment variable is required " "ODOO_SESSION_REDIS_PREFIX environment variable is required "
"to store sessions on Redis" "to store sessions on Redis"
) )
prefix = os.environ['ODOO_SESSION_REDIS_PREFIX'] prefix = os.environ["ODOO_SESSION_REDIS_PREFIX"]
assert re.match(r'^[a-z-0-9]+-odoo-[a-z-0-9]+$', prefix), ( assert re.match(r"^[a-z-0-9]+-odoo-[a-z-0-9]+$", prefix), (
"ODOO_SESSION_REDIS_PREFIX must match '<client>-odoo-<env>'" "ODOO_SESSION_REDIS_PREFIX must match '<client>-odoo-<env>'"
", we got: '%s'" % (prefix,) ", we got: '%s'" % (prefix,)
) )
def check(self, cr, uid, context=None): def check(self, cr, uid, context=None):
if is_true(os.environ.get('ODOO_CLOUD_PLATFORM_UNSAFE')): if is_true(os.environ.get("ODOO_CLOUD_PLATFORM_UNSAFE")):
_logger.warning( _logger.warning("cloud platform checks disabled, this is not safe")
"cloud platform checks disabled, this is not safe"
)
return return
params = self.pool.get('ir.config_parameter') params = self.pool.get("ir.config_parameter")
kind = params.get_param(cr, SUPERUSER_ID, kind = params.get_param(cr, SUPERUSER_ID, "cloud.platform.kind", context=None)
'cloud.platform.kind', context=None)
if not kind: if not kind:
_logger.warning( _logger.warning(
"cloud platform not configured, you should " "cloud platform not configured, you should "
@@ -344,14 +347,14 @@ class CloudPlatform(osv.osv_abstract):
) )
return return
environment_name = self._get_running_env() environment_name = self._get_running_env()
if kind == 'exoscale': if kind == "exoscale":
self._check_s3(cr, uid, environment_name, context) self._check_s3(cr, uid, environment_name, context)
elif kind == 'ovh': elif kind == "ovh":
self._check_swift(cr, uid, environment_name, context) self._check_swift(cr, uid, environment_name, context)
elif kind == 'azure': elif kind == "azure":
self._check_azure(cr, uid, environment_name, context) self._check_azure(cr, uid, environment_name, context)
self._check_redis(cr, uid, environment_name, context) self._check_redis(cr, uid, environment_name, context)
def _register_hook(self, cr): def _register_hook(self, cr):
super(CloudPlatform, self)._register_hook(cr) super(CloudPlatform, self)._register_hook(cr)
self.pool.get('cloud.platform').check(cr, SUPERUSER_ID) self.pool.get("cloud.platform").check(cr, SUPERUSER_ID)