From bf35de5c59a5c0ecea9a1f009dc2016d4b713819 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 18 Nov 2019 09:51:35 +0100 Subject: [PATCH 1/2] Add support of 'labs' RUNNING_ENV in cloud_platform The labs env can be anything starting by 'labs', such as 'labs-logistics', 'labs-finance', ... * At install, s3/swift is set as default storage * However, unlike prod/integration, the storage is not forced to be an object storage * Redis is required * When the storage is set on s3/swift, then the bucket name is mandatory (otherwise, there is no place where to create the files...) The redis prefix regex match is relaxed: anything starting by a project name, then '-odoo-', then any combination of letters, digits, and dashes is accepted (so a prefix my-project9-odoo-labs-web3 is valid). --- cloud_platform/models/cloud_platform.py | 57 +++++++++++++-------- cloud_platform_ovh/models/cloud_platform.py | 1 + 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/cloud_platform/models/cloud_platform.py b/cloud_platform/models/cloud_platform.py index f038f3d7..75276d06 100644 --- a/cloud_platform/models/cloud_platform.py +++ b/cloud_platform/models/cloud_platform.py @@ -63,6 +63,14 @@ def _config_by_server_env(self, platform_kind, environment): configs = configs_getter() if configs_getter else {} return configs.get(environment) or FilestoreKind.db + def _get_running_env(self): + environment_name = config['running_env'] + if environment_name.startswith('labs'): + # We allow to have environments such as 'labs-logistics' + # or 'labs-finance', in order to have the matching ribbon. + environment_name = 'labs' + return environment_name + # Due to the addition of the ovh cloud platform # This will be moved to cloud_platform_exoscale on v11 def install_exoscale(self, cr, uid, context=None): @@ -76,8 +84,8 @@ def install(self, cr, uid, platform_kind, context=None): 'cloud.platform.kind', platform_kind, context=context ) - environment = config['running_env'] - configs = self._config_by_server_env(platform_kind, environment) + environment_name = self._get_running_env() + configs = self._config_by_server_env(platform_kind, environment_name) params.set_param( cr, SUPERUSER_ID, 'ir_attachment.location', configs.filestore, @@ -98,6 +106,9 @@ def _check_swift(self, cr, uid, environment_name, context=None): ) == FilestoreKind.swift ) if environment_name in ('prod', 'integration'): + # 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 + # storage. At your own risks! assert use_swift, ( "Swift must be used on production and integration instances. " "It is activated, setting 'ir_attachment.location.' to 'swift'" @@ -117,13 +128,16 @@ def _check_swift(self, cr, uid, environment_name, context=None): "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-z0-9_-]+-odoo-prod', + container_name = os.environ.get('SWIFT_WRITE_CONTAINER') + 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)." + ) + prod_container = bool(re.match(r'[a-z0-9-]+-odoo-prod', container_name)) if environment_name == 'prod': assert prod_container, ( @@ -152,6 +166,9 @@ def _check_s3(self, cr, uid, environment_name, context=None): cr, SUPERUSER_ID, 'ir_attachment.location', context=context ) == FilestoreKind.s3 if environment_name in ('prod', 'integration'): + # 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 + # storage. At your own risks! assert use_s3, ( "S3 must be used on production and integration instances. " "It is activated by setting 'ir_attachment.location.' to 's3'." @@ -167,16 +184,16 @@ def _check_s3(self, cr, uid, environment_name, context=None): "AWS_SECRET_ACCESS_KEY environment variable is required when " "ir_attachment.location is 's3'." ) - assert os.environ.get('AWS_BUCKETNAME'), ( + bucket_name = os.environ.get('AWS_BUCKETNAME') + assert bucket_name, ( "AWS_BUCKETNAME environment variable is required when " "ir_attachment.location is 's3'.\n" - "Normally, 's3' is activated on integration and production, " - "but should not be used in dev environment (or at least " - "not with a dev bucket, but never the " + "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)." ) - bucket_name = os.environ['AWS_BUCKETNAME'] - prod_bucket = bool(re.match(r'[a-z0-9_-]+-odoo-prod', bucket_name)) + 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 '-odoo-prod', " @@ -200,10 +217,10 @@ def _check_s3(self, cr, uid, environment_name, context=None): ) def _check_redis(self, cr, uid, environment_name, context=None): - if environment_name in ('prod', 'integration', 'test'): + if environment_name in ('prod', 'integration', 'labs', 'test'): assert is_true(os.environ.get('ODOO_SESSION_REDIS')), ( - "Redis must be activated on prod, integration, test instances." - "This is done by setting ODOO_SESSION_REDIS=1." + "Redis must be activated on prod, integration, labs," + " test instances. This is done by setting ODOO_SESSION_REDIS=1." ) assert (os.environ.get('ODOO_SESSION_REDIS_HOST') or os.environ.get('ODOO_SESSION_REDIS_SENTINEL_HOST')), ( @@ -216,7 +233,7 @@ def _check_redis(self, cr, uid, environment_name, context=None): ) prefix = os.environ['ODOO_SESSION_REDIS_PREFIX'] - assert re.match(r'[a-z0-9_-]+-odoo-[a-z]+', prefix), ( + assert re.match(r'^[a-z-0-9]+-odoo-[a-z-0-9]+$', prefix), ( "ODOO_SESSION_REDIS_PREFIX must match '-odoo-'" ", we got: '%s'" % (prefix,) ) @@ -236,7 +253,7 @@ def check(self, cr, uid, context=None): "probably run 'env['cloud.platform'].install_exoscale()'" ) return - environment_name = config['running_env'] + environment_name = self._get_running_env() if kind == 'exoscale': self._check_s3(cr, uid, environment_name, context) elif kind == 'ovh': diff --git a/cloud_platform_ovh/models/cloud_platform.py b/cloud_platform_ovh/models/cloud_platform.py index fed682d4..78e02b12 100644 --- a/cloud_platform_ovh/models/cloud_platform.py +++ b/cloud_platform_ovh/models/cloud_platform.py @@ -31,6 +31,7 @@ def _config_by_server_env_for_ovh(self): configs = { 'prod': PlatformConfig(filestore=FilestoreKind.swift), 'integration': PlatformConfig(filestore=FilestoreKind.swift), + 'labs': PlatformConfig(filestore=FilestoreKind.swift), 'test': PlatformConfig(filestore=FilestoreKind.db), 'dev': PlatformConfig(filestore=FilestoreKind.db), } From c35300a99d4f2c37d257ad7406b6231897eed8cf Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 18 Nov 2019 10:29:23 +0100 Subject: [PATCH 2/2] Add an advice in error message --- cloud_platform/models/cloud_platform.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cloud_platform/models/cloud_platform.py b/cloud_platform/models/cloud_platform.py index 75276d06..06de6d3e 100644 --- a/cloud_platform/models/cloud_platform.py +++ b/cloud_platform/models/cloud_platform.py @@ -135,7 +135,9 @@ def _check_swift(self, cr, uid, environment_name, context=None): "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)." + "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)) @@ -191,7 +193,9 @@ def _check_s3(self, cr, uid, environment_name, context=None): "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)." + "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':