Don't try preloading the same build twice in an image.

Connects to #626

Change-Type: patch
This commit is contained in:
Alexis Svinartchouk 2017-08-17 14:58:45 +02:00
parent 6882f4bbe8
commit c6c2f0bedc
4 changed files with 44 additions and 21 deletions

View File

@ -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

View File

@ -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);

View File

@ -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)

View File

@ -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",