Merge pull request #627 from resin-io/626-dont-preload-the-same-image-twice

Don't try preloading the same build twice in an image.
This commit is contained in:
Alexis Svinartchouk 2017-08-21 19:26:53 +02:00 committed by GitHub
commit 14c5d938a6
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. All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/). 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 ## [6.5.1] - 2017-08-21
### Fixed ### 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 See the License for the specific language governing permissions and
limitations under the License. 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'); dockerUtils = require('../utils/docker');
@ -174,17 +175,19 @@ module.exports = {
return streamToPromise(buildOutputStream).then(resin.settings.getAll).then(function(settings) { return streamToPromise(buildOutputStream).then(resin.settings.getAll).then(function(settings) {
options.proxy = settings.proxy; options.proxy = settings.proxy;
options.apiHost = settings.apiUrl; options.apiHost = settings.apiUrl;
return preload.getDeviceTypeSlug(docker, options)["catch"](preload.errors.ResinError, expectedError); return preload.getDeviceTypeSlugAndPreloadedBuilds(docker, options)["catch"](preload.errors.ResinError, expectedError);
}).then(function(deviceType) { }).then(function(arg) {
var builds, slug;
slug = arg.slug, builds = arg.builds;
return Promise["try"](function() { return Promise["try"](function() {
if (options.appId) { if (options.appId) {
return preload.getApplication(resin, options.appId)["catch"](errors.ResinApplicationNotFound, expectedError); 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) { }).then(function(application) {
options.application = application; options.application = application;
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"); expectedError("Image device type (" + application.device_type + ") and application device type (" + slug + ") do not match");
} }
return Promise["try"](function() { return Promise["try"](function() {
if (options.commit) { if (options.commit) {
@ -197,14 +200,24 @@ module.exports = {
} }
return selectApplicationCommit(expectedError, resin, form, application.build); return selectApplicationCommit(expectedError, resin, form, application.build);
}).then(function(commit) { }).then(function(commit) {
if (commit !== LATEST) { if (commit === LATEST) {
options.commit = application.commit;
} else {
options.commit = commit; options.commit = commit;
} }
return offerToDisableAutomaticUpdates(Promise, form, resin, application, 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) { }).then(function(info) {
info.stdout.pipe(process.stdout); info.stdout.pipe(process.stdout);

View File

@ -174,23 +174,22 @@ module.exports =
options.apiHost = settings.apiUrl options.apiHost = settings.apiUrl
# Use the preloader docker image to extract the deviceType of the image # 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) .catch(preload.errors.ResinError, expectedError)
.then (deviceType) -> .then ({ slug, builds }) ->
# Use the appId given as --app or show an interactive app selection menu # Use the appId given as --app or show an interactive app selection menu
Promise.try -> Promise.try ->
if options.appId if options.appId
return preload.getApplication(resin, options.appId) return preload.getApplication(resin, options.appId)
.catch(errors.ResinApplicationNotFound, expectedError) .catch(errors.ResinApplicationNotFound, expectedError)
selectApplication(expectedError, resin, form, deviceType) selectApplication(expectedError, resin, form, slug)
.then (application) -> .then (application) ->
options.application = application options.application = application
# Check that the app device type and the image device type match # Check that the app device type and the image device type match
if deviceType != application.device_type if slug != application.device_type
expectedError( 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 # Use the commit given as --commit or show an interactive commit selection menu
@ -203,16 +202,23 @@ module.exports =
.then (commit) -> .then (commit) ->
# No commit specified => use the latest commit # No commit specified => use the latest commit
if commit != LATEST if commit == LATEST
options.commit = application.commit
else
options.commit = commit options.commit = commit
# Propose to disable automatic app updates if the commit is not the latest # Propose to disable automatic app updates if the commit is not the latest
offerToDisableAutomaticUpdates(Promise, form, resin, application, commit) offerToDisableAutomaticUpdates(Promise, form, resin, application, commit)
.then -> .then ->
# All options are ready: preload the image. builds = builds.map (build) ->
preload.run(resin, docker, options) build.slice(-preload.BUILD_HASH_LENGTH)
.catch(preload.errors.ResinError, expectedError) 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) -> .then (info) ->
info.stdout.pipe(process.stdout) info.stdout.pipe(process.stdout)
info.stderr.pipe(process.stderr) info.stderr.pipe(process.stderr)

View File

@ -82,7 +82,7 @@
"resin-doodles": "0.0.1", "resin-doodles": "0.0.1",
"resin-image-fs": "^2.3.0", "resin-image-fs": "^2.3.0",
"resin-image-manager": "^4.1.1", "resin-image-manager": "^4.1.1",
"resin-preload": "^2.0.0", "resin-preload": "^3.0.0",
"resin-sdk-preconfigured": "^6.9.0", "resin-sdk-preconfigured": "^6.9.0",
"resin-settings-client": "^3.6.1", "resin-settings-client": "^3.6.1",
"resin-stream-logger": "^0.0.4", "resin-stream-logger": "^0.0.4",