From c6c2f0bedc7c739b6a080888799e6b921a2b87c2 Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Thu, 17 Aug 2017 14:58:45 +0200 Subject: [PATCH] Don't try preloading the same build twice in an image. Connects to #626 Change-Type: patch --- CHANGELOG.md | 4 ++++ build/actions/preload.js | 31 ++++++++++++++++++++++--------- lib/actions/preload.coffee | 28 +++++++++++++++++----------- package.json | 2 +- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3185c78a..c06926a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +### Changed + + - Preload will do nothing if you try to preload a build that is already preloaded in the image. + ## [6.5.1] - 2017-08-21 ### Fixed diff --git a/build/actions/preload.js b/build/actions/preload.js index 2195c738..d1ca086e 100644 --- a/build/actions/preload.js +++ b/build/actions/preload.js @@ -15,7 +15,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -var LATEST, dockerUtils, getApplicationsWithSuccessfulBuilds, offerToDisableAutomaticUpdates, selectApplication, selectApplicationCommit; +var LATEST, dockerUtils, getApplicationsWithSuccessfulBuilds, offerToDisableAutomaticUpdates, selectApplication, selectApplicationCommit, + indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; dockerUtils = require('../utils/docker'); @@ -174,17 +175,19 @@ module.exports = { return streamToPromise(buildOutputStream).then(resin.settings.getAll).then(function(settings) { options.proxy = settings.proxy; options.apiHost = settings.apiUrl; - return preload.getDeviceTypeSlug(docker, options)["catch"](preload.errors.ResinError, expectedError); - }).then(function(deviceType) { + return preload.getDeviceTypeSlugAndPreloadedBuilds(docker, options)["catch"](preload.errors.ResinError, expectedError); + }).then(function(arg) { + var builds, slug; + slug = arg.slug, builds = arg.builds; return Promise["try"](function() { if (options.appId) { return preload.getApplication(resin, options.appId)["catch"](errors.ResinApplicationNotFound, expectedError); } - return selectApplication(expectedError, resin, form, deviceType); + return selectApplication(expectedError, resin, form, slug); }).then(function(application) { options.application = application; - if (deviceType !== application.device_type) { - expectedError("Image device type (" + application.device_type + ") and application device type (" + deviceType + ") do not match"); + if (slug !== application.device_type) { + expectedError("Image device type (" + application.device_type + ") and application device type (" + slug + ") do not match"); } return Promise["try"](function() { if (options.commit) { @@ -197,14 +200,24 @@ module.exports = { } return selectApplicationCommit(expectedError, resin, form, application.build); }).then(function(commit) { - if (commit !== LATEST) { + if (commit === LATEST) { + options.commit = application.commit; + } else { options.commit = commit; } return offerToDisableAutomaticUpdates(Promise, form, resin, application, commit); }); + }).then(function() { + var ref; + builds = builds.map(function(build) { + return build.slice(-preload.BUILD_HASH_LENGTH); + }); + if (ref = options.commit, indexOf.call(builds, ref) >= 0) { + console.log('This build is already preloaded in this image.'); + process.exit(0); + } + return preload.run(resin, docker, options)["catch"](preload.errors.ResinError, expectedError); }); - }).then(function() { - return preload.run(resin, docker, options)["catch"](preload.errors.ResinError, expectedError); }); }).then(function(info) { info.stdout.pipe(process.stdout); diff --git a/lib/actions/preload.coffee b/lib/actions/preload.coffee index 72c7243a..3dfa6d80 100644 --- a/lib/actions/preload.coffee +++ b/lib/actions/preload.coffee @@ -174,23 +174,22 @@ module.exports = options.apiHost = settings.apiUrl # Use the preloader docker image to extract the deviceType of the image - preload.getDeviceTypeSlug(docker, options) + preload.getDeviceTypeSlugAndPreloadedBuilds(docker, options) .catch(preload.errors.ResinError, expectedError) - .then (deviceType) -> - + .then ({ slug, builds }) -> # Use the appId given as --app or show an interactive app selection menu Promise.try -> if options.appId return preload.getApplication(resin, options.appId) .catch(errors.ResinApplicationNotFound, expectedError) - selectApplication(expectedError, resin, form, deviceType) + selectApplication(expectedError, resin, form, slug) .then (application) -> options.application = application # Check that the app device type and the image device type match - if deviceType != application.device_type + if slug != application.device_type expectedError( - "Image device type (#{application.device_type}) and application device type (#{deviceType}) do not match" + "Image device type (#{application.device_type}) and application device type (#{slug}) do not match" ) # Use the commit given as --commit or show an interactive commit selection menu @@ -203,16 +202,23 @@ module.exports = .then (commit) -> # No commit specified => use the latest commit - if commit != LATEST + if commit == LATEST + options.commit = application.commit + else options.commit = commit # Propose to disable automatic app updates if the commit is not the latest offerToDisableAutomaticUpdates(Promise, form, resin, application, commit) - .then -> + .then -> - # All options are ready: preload the image. - preload.run(resin, docker, options) - .catch(preload.errors.ResinError, expectedError) + builds = builds.map (build) -> + build.slice(-preload.BUILD_HASH_LENGTH) + if options.commit in builds + console.log('This build is already preloaded in this image.') + process.exit(0) + # All options are ready: preload the image. + preload.run(resin, docker, options) + .catch(preload.errors.ResinError, expectedError) .then (info) -> info.stdout.pipe(process.stdout) info.stderr.pipe(process.stderr) diff --git a/package.json b/package.json index fed95358..d24d15d5 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "resin-doodles": "0.0.1", "resin-image-fs": "^2.3.0", "resin-image-manager": "^4.1.1", - "resin-preload": "^2.0.0", + "resin-preload": "^3.0.0", "resin-sdk-preconfigured": "^6.9.0", "resin-settings-client": "^3.6.1", "resin-stream-logger": "^0.0.4",