Skip to content

Commit 10a57fc

Browse files
author
Matthew Fisher
committed
refactor(controller): clean up Release.new
1 parent 71047e5 commit 10a57fc

2 files changed

Lines changed: 30 additions & 35 deletions

File tree

controller/api/models.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -474,51 +474,31 @@ def new(self, user, config=None, build=None, summary=None):
474474
config = self.config
475475
if not build:
476476
build = self.build
477+
# construct fully-qualified build image
478+
if build.sha:
479+
source_image = '{}:{}'.format(build.image, build.sha)
480+
else:
481+
source_image = '{}:{}'.format(build.image, 'latest')
477482
# construct fully-qualified target image
478483
new_version = self.version + 1
479484
tag = 'v{}'.format(new_version)
485+
release_image = '{}:{}'.format(self.app.id, tag)
480486
target_image = '{}:{}/{}'.format(
481487
settings.REGISTRY_HOST, settings.REGISTRY_PORT, self.app.id)
482488
# create new release and auto-increment version
483489
release = Release.objects.create(
484490
owner=user, app=self.app, config=config,
485491
build=build, version=new_version, image=target_image, summary=summary)
486-
# publish release to registry as new docker image
487-
if build.sha:
488-
publish_release(build.image,
489-
build.sha,
490-
config.values,
491-
self.app.id,
492-
tag)
493-
else:
492+
# IOW, this image did not come from the builder
493+
if not build.sha:
494494
# we assume that the image is not present on our registry,
495495
# so shell out a task to pull in the repository
496-
url = urlparse(build.image)
497-
repository_name = url.path[1:]
498-
if url.hostname and url.port:
499-
tasks.import_repository.delay(
500-
'{}://{}:{}'.format(url.scheme, url.hostname, url.port),
501-
repository_name,
502-
self.app.id,
503-
).get()
504-
elif url.hostname:
505-
tasks.import_repository.delay(
506-
'{}://{}'.format(url.scheme, url.hostname),
507-
repository_name,
508-
self.app.id,
509-
).get()
510-
else:
511-
# assume only the repository path is given, which means that
512-
# it's on the public index
513-
tasks.import_repository.delay(settings.PUBLIC_INDEX_URL,
514-
build.image,
515-
self.app.id,
516-
).get()
517-
publish_release(self.app.id,
518-
'latest',
519-
config.values,
520-
self.app.id,
521-
tag)
496+
tasks.import_repository.delay(build.image, self.app.id).get()
497+
# update the source image to the repository we just imported
498+
source_image = '{}:{}'.format(self.app.id, 'latest')
499+
publish_release(source_image,
500+
config.values,
501+
release_image,)
522502
return release
523503

524504
def previous(self):

controller/api/tasks.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import requests
1111
import threading
12+
from urlparse import urlparse
1213

1314
from celery import task
1415
from django.conf import settings
@@ -37,9 +38,23 @@ def deploy_release(app, release):
3738

3839

3940
@task
40-
def import_repository(src_index, src_repository, target_repository):
41+
def import_repository(source, target_repository):
4142
"""Imports an image from a remote into our own private registry"""
4243

44+
url = urlparse(source)
45+
scheme = url.scheme if url.scheme else 'http'
46+
# strip the leading slash
47+
src_repository = url.path[1:]
48+
49+
if url.hostname and url.port:
50+
src_index = '{}://{}:{}'.format(scheme, url.hostname, url.port)
51+
elif url.hostname:
52+
src_index = '{}://{}'.format(scheme, url.hostname)
53+
else:
54+
# assume just the repository name was given, therefore it came from
55+
# the public index
56+
src_index = settings.PUBLIC_INDEX_URL
57+
src_repository = source
4358
data = {
4459
'src_index': src_index,
4560
'src_repository': src_repository,

0 commit comments

Comments
 (0)