applications: declare an app to services on version generation (#74659)

This commit is contained in:
Lauréline Guérin 2023-02-20 14:23:23 +01:00
parent c2e6ad7214
commit 3bb0a0c3ca
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 40 additions and 15 deletions

View File

@ -241,7 +241,7 @@ class Version(models.Model):
def __repr__(self):
return '<Version %s>' % self.application.slug
def create_bundle(self):
def create_bundle(self, job=None):
app = self.application
elements = app.scandeps()
tar_io = io.BytesIO()
@ -289,9 +289,23 @@ class Version(models.Model):
self.bundle.save('%s.tar' % app.slug, content=ContentFile(tar_io.getvalue()))
self.save()
bundle_content = self.bundle.read()
self.do_something_with_bundle(bundle_content, 'declare', job=job)
def deploy(self, job=None):
bundle_content = self.bundle.read()
self.deploy_roles(bundle_content)
self.do_something_with_bundle(bundle_content, 'deploy', job=job)
self.application.refresh_elements(cache_only=True)
def do_something_with_bundle(self, bundle_content, action, job=None):
if action == 'deploy':
target_url = 'api/export-import/bundle-import/'
exception_message = _('Failed to deploy module %s (%s)')
elif action == 'declare':
target_url = 'api/export-import/bundle-declare/'
exception_message = _('Failed to declare elements for module %s (%s)')
for service_id, services in getattr(settings, 'KNOWN_SERVICES', {}).items():
if service_id not in Application.SUPPORTED_MODULES:
continue
@ -301,12 +315,10 @@ class Version(models.Model):
continue
if service_objects[service['url']].secondary:
continue
url = urllib.parse.urljoin(service['url'], 'api/export-import/bundle-import/')
url = urllib.parse.urljoin(service['url'], target_url)
response = requests.put(url, data=bundle_content)
if not response.ok:
raise DeploymentError(
_('Failed to deploy module %s (%s)') % (service_id, response.status_code)
)
raise DeploymentError(exception_message % (service_id, response.status_code))
if not job:
continue
try:
@ -319,7 +331,6 @@ class Version(models.Model):
job.progression_urls[service_id] = {}
job.progression_urls[service_id][service['title']] = response_json['url']
job.save()
self.application.refresh_elements(cache_only=True)
def get_authentic_service(self):
for service_id, services in getattr(settings, 'KNOWN_SERVICES', {}).items():
@ -396,7 +407,7 @@ class AsyncJob(models.Model):
if self.action == 'scandeps':
self.application.scandeps()
elif self.action == 'create_bundle':
self.version.create_bundle()
self.version.create_bundle(self)
elif self.action == 'deploy':
self.version.deploy(self)
except ApplicationError as e:

View File

@ -386,14 +386,28 @@ def test_create_application(app, admin_user, settings, analyze):
assert versions.count('2.0') == 1
assert resp.text.count('Creating application bundle') == 4
# non editable app
application.editable = False
application.save()
app.get('/applications/manifest/test/metadata/', status=404)
app.get('/applications/manifest/test/scandeps/', status=404)
app.get('/applications/manifest/test/generate/', status=404)
app.get('/applications/manifest/test/add/forms/', status=404)
app.get('/applications/manifest/test/delete/%s/' % application.relation_set.first().pk, status=404)
def response_content(url, request):
if url.path == '/api/export-import/bundle-declare/':
return {'status_code': 500}
return mocked_http(url, request)
resp = app.get('/applications/manifest/test/generate/')
with HTTMock(response_content):
with pytest.raises(DeploymentError) as e:
resp.form.submit()
assert str(e.value) == 'Failed to declare elements for module wcs (500)'
job = AsyncJob.objects.latest('pk')
assert job.status == 'failed'
assert job.exception == 'Failed to declare elements for module wcs (500)'
# non editable app
application.editable = False
application.save()
app.get('/applications/manifest/test/metadata/', status=404)
app.get('/applications/manifest/test/scandeps/', status=404)
app.get('/applications/manifest/test/generate/', status=404)
app.get('/applications/manifest/test/add/forms/', status=404)
app.get('/applications/manifest/test/delete/%s/' % application.relation_set.first().pk, status=404)
def test_manifest_ordering(app, admin_user, settings):