applications: declare an app to services on version generation (#74659)
This commit is contained in:
parent
c2e6ad7214
commit
3bb0a0c3ca
|
@ -241,7 +241,7 @@ class Version(models.Model):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Version %s>' % self.application.slug
|
return '<Version %s>' % self.application.slug
|
||||||
|
|
||||||
def create_bundle(self):
|
def create_bundle(self, job=None):
|
||||||
app = self.application
|
app = self.application
|
||||||
elements = app.scandeps()
|
elements = app.scandeps()
|
||||||
tar_io = io.BytesIO()
|
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.bundle.save('%s.tar' % app.slug, content=ContentFile(tar_io.getvalue()))
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
bundle_content = self.bundle.read()
|
||||||
|
self.do_something_with_bundle(bundle_content, 'declare', job=job)
|
||||||
|
|
||||||
def deploy(self, job=None):
|
def deploy(self, job=None):
|
||||||
bundle_content = self.bundle.read()
|
bundle_content = self.bundle.read()
|
||||||
self.deploy_roles(bundle_content)
|
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():
|
for service_id, services in getattr(settings, 'KNOWN_SERVICES', {}).items():
|
||||||
if service_id not in Application.SUPPORTED_MODULES:
|
if service_id not in Application.SUPPORTED_MODULES:
|
||||||
continue
|
continue
|
||||||
|
@ -301,12 +315,10 @@ class Version(models.Model):
|
||||||
continue
|
continue
|
||||||
if service_objects[service['url']].secondary:
|
if service_objects[service['url']].secondary:
|
||||||
continue
|
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)
|
response = requests.put(url, data=bundle_content)
|
||||||
if not response.ok:
|
if not response.ok:
|
||||||
raise DeploymentError(
|
raise DeploymentError(exception_message % (service_id, response.status_code))
|
||||||
_('Failed to deploy module %s (%s)') % (service_id, response.status_code)
|
|
||||||
)
|
|
||||||
if not job:
|
if not job:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
|
@ -319,7 +331,6 @@ class Version(models.Model):
|
||||||
job.progression_urls[service_id] = {}
|
job.progression_urls[service_id] = {}
|
||||||
job.progression_urls[service_id][service['title']] = response_json['url']
|
job.progression_urls[service_id][service['title']] = response_json['url']
|
||||||
job.save()
|
job.save()
|
||||||
self.application.refresh_elements(cache_only=True)
|
|
||||||
|
|
||||||
def get_authentic_service(self):
|
def get_authentic_service(self):
|
||||||
for service_id, services in getattr(settings, 'KNOWN_SERVICES', {}).items():
|
for service_id, services in getattr(settings, 'KNOWN_SERVICES', {}).items():
|
||||||
|
@ -396,7 +407,7 @@ class AsyncJob(models.Model):
|
||||||
if self.action == 'scandeps':
|
if self.action == 'scandeps':
|
||||||
self.application.scandeps()
|
self.application.scandeps()
|
||||||
elif self.action == 'create_bundle':
|
elif self.action == 'create_bundle':
|
||||||
self.version.create_bundle()
|
self.version.create_bundle(self)
|
||||||
elif self.action == 'deploy':
|
elif self.action == 'deploy':
|
||||||
self.version.deploy(self)
|
self.version.deploy(self)
|
||||||
except ApplicationError as e:
|
except ApplicationError as e:
|
||||||
|
|
|
@ -386,14 +386,28 @@ def test_create_application(app, admin_user, settings, analyze):
|
||||||
assert versions.count('2.0') == 1
|
assert versions.count('2.0') == 1
|
||||||
assert resp.text.count('Creating application bundle') == 4
|
assert resp.text.count('Creating application bundle') == 4
|
||||||
|
|
||||||
# non editable app
|
def response_content(url, request):
|
||||||
application.editable = False
|
if url.path == '/api/export-import/bundle-declare/':
|
||||||
application.save()
|
return {'status_code': 500}
|
||||||
app.get('/applications/manifest/test/metadata/', status=404)
|
return mocked_http(url, request)
|
||||||
app.get('/applications/manifest/test/scandeps/', status=404)
|
|
||||||
app.get('/applications/manifest/test/generate/', status=404)
|
resp = app.get('/applications/manifest/test/generate/')
|
||||||
app.get('/applications/manifest/test/add/forms/', status=404)
|
with HTTMock(response_content):
|
||||||
app.get('/applications/manifest/test/delete/%s/' % application.relation_set.first().pk, status=404)
|
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):
|
def test_manifest_ordering(app, admin_user, settings):
|
||||||
|
|
Loading…
Reference in New Issue