From a42a1a97ba9ce997b4efb8125d5195192c860816 Mon Sep 17 00:00:00 2001 From: Matthew McGinn Date: Fri, 28 Dec 2018 10:47:38 -0500 Subject: [PATCH] Request access to previously pushed release via `balena deploy` This access is used to cross mount the old layers, rather than reuploading the layers each time. Connects-to: #1045 Change-type: minor Signed-off-by: Matthew McGinn --- lib/utils/compose.coffee | 50 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/utils/compose.coffee b/lib/utils/compose.coffee index 34f7765b..50bffad9 100644 --- a/lib/utils/compose.coffee +++ b/lib/utils/compose.coffee @@ -349,13 +349,54 @@ tagServiceImages = (docker, images, serviceImages) -> logs: d.logs props: d.props -authorizePush = (tokenAuthEndpoint, registry, images) -> + +getPreviousRepos = (sdk, docker, logger, appID) -> + sdk.pine.get( + resource: 'release' + options: + $filter: + belongs_to__application: appID + status: 'success' + $select: + [ 'id' ] + $expand: + contains__image: + $expand: 'image' + $orderby: 'id desc' + $top: 1 + ) + .then (release) -> + # grab all images from the latest release, return all image locations in the registry + if release?.length > 0 + images = release[0].contains__image + Promise.map images, (d) -> + imageName = d.image[0].is_stored_at__image_location + docker.getRegistryAndName(imageName) + .then ( registry ) -> + logger.logDebug("Requesting access to previously pushed image repo (#{registry.imageName})") + return registry.imageName + .catch (e) -> + logger.logDebug("Failed to access previously pushed image repo: #{e}") + +authorizePush = (sdk, logger, tokenAuthEndpoint, registry, images, previousRepos) -> _ = require('lodash') - sdk = require('balena-sdk').fromSharedOptions() + + # TODO: https://github.com/balena-io/balena-cli/issues/1070 + maxRepos = 20 if not _.isArray(images) images = [ images ] + if images.length > maxRepos + throw new Error ( + "More than #{maxRepos} containers is currently not supported, see " + + 'https://github.com/balena-io/balena-cli/issues/1070 for more information' + ) + images.push previousRepos... + if images.length + previousRepos?.length > maxRepos + logger.logDebug("Truncating requested repositories to #{maxRepos} by limiting previously pushed repo access") + # at this point, we know we're only truncating access to previously pushed repos + images = images[0...maxRepos] sdk.request.send baseUrl: tokenAuthEndpoint url: '/auth/v1/token' @@ -423,7 +464,10 @@ exports.deployProject = ( tagServiceImages(docker, images, serviceImages) .tap (images) -> logger.logDebug('Authorizing push...') - authorizePush(apiEndpoint, images[0].registry, _.map(images, 'repo')) + sdk = require('balena-sdk').fromSharedOptions() + getPreviousRepos(sdk, docker, logger, appId) + .then (previousRepos) -> + authorizePush(sdk, logger, apiEndpoint, images[0].registry, _.map(images, 'repo'), previousRepos) .then (token) -> logger.logInfo('Pushing images to registry...') pushAndUpdateServiceImages docker, token, images, (serviceImage) ->