Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Check HostOS version before attempting rsync
Browse files Browse the repository at this point in the history
  • Loading branch information
lekkas committed Jun 17, 2016
1 parent c488bbe commit 3d8d076
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
27 changes: 19 additions & 8 deletions build/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
/**
* @module resinSync
*/
var MIN_HOSTOS_RSYNC, Promise, Spinner, chalk, config, ensureHostOSCompatibility, resin, rsync, semver, shell, ssh, utils, _;
var MIN_HOSTOS_RSYNC, Promise, Spinner, chalk, config, ensureHostOSCompatibility, resin, rsync, semver, semverRegExp, shell, ssh, utils, _;

Promise = require('bluebird');

Expand All @@ -44,6 +44,8 @@ semver = require('semver');

MIN_HOSTOS_RSYNC = '1.1.4';

semverRegExp = /[0-9]+\.[0-9]+\.[0-9]+(?:(-|\+)[^\s]+)?/;


/**
* @summary Ensure HostOS compatibility
Expand All @@ -55,8 +57,8 @@ MIN_HOSTOS_RSYNC = '1.1.4';
* HostOS version. Fullfills promise if device is compatible or
* rejects it otherwise. Version checks are based on semver.
*
* @param {String} version - HostOS version to check
* @param {String} minVersion - Minimum accepted version
* @param {String} osRelease - HostOS version as returned from the API (device.os_release field)
* @param {String} minVersion - Minimum accepted HostOS version
* @returns {Promise}
*
* @example
Expand All @@ -67,9 +69,14 @@ MIN_HOSTOS_RSYNC = '1.1.4';
* console.log('Is incompatible')
*/

exports.ensureHostOSCompatibility = ensureHostOSCompatibility = Promise.method(function(version, minVersion) {
exports.ensureHostOSCompatibility = ensureHostOSCompatibility = Promise.method(function(osRelease, minVersion) {
var version, _ref;
version = osRelease != null ? (_ref = osRelease.match(semverRegExp)) != null ? _ref[0] : void 0 : void 0;
if (version == null) {
throw new Error("Could not parse semantic version from HostOS release info: " + osRelease);
}
if (semver.lt(version, minVersion)) {
throw new Error("Incompatible HostOS version: " + version + " - must be >= " + minVersion);
throw new Error("Incompatible HostOS version: " + osRelease + " - must be >= " + minVersion);
}
});

Expand Down Expand Up @@ -142,20 +149,24 @@ exports.sync = function(uuid, options) {
}
});
console.info("Connecting with: " + uuid);
return resin.models.device.isOnline(uuid).tap(function(isOnline) {
return resin.models.device.isOnline(uuid).then(function(isOnline) {
if (!isOnline) {
throw new Error('Device is not online');
}
return resin.models.device.get(uuid);
}).tap(function(device) {
return ensureHostOSCompatibility(device.os_version, MIN_HOSTOS_RSYNC);
}).tap(function(device) {
return Promise["try"](function() {
if (options.before != null) {
return shell.runCommand(options.before, {
cwd: options.source
});
}
});
}).then(function() {
}).then(function(device) {
return Promise.props({
uuid: resin.models.device.get(uuid).get('uuid'),
uuid: device.uuid,
username: resin.auth.whoami()
});
}).then(function(_arg) {
Expand Down
26 changes: 19 additions & 7 deletions lib/sync.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ semver = require('semver')

MIN_HOSTOS_RSYNC = '1.1.4'

# Extract semver from device.os_version, since its format
# can be in a form similar to 'Resin OS 1.1.0 (fido)'
semverRegExp = /[0-9]+\.[0-9]+\.[0-9]+(?:(-|\+)[^\s]+)?/

###*
# @summary Ensure HostOS compatibility
# @function
Expand All @@ -42,8 +46,8 @@ MIN_HOSTOS_RSYNC = '1.1.4'
# HostOS version. Fullfills promise if device is compatible or
# rejects it otherwise. Version checks are based on semver.
#
# @param {String} version - HostOS version to check
# @param {String} minVersion - Minimum accepted version
# @param {String} osRelease - HostOS version as returned from the API (device.os_release field)
# @param {String} minVersion - Minimum accepted HostOS version
# @returns {Promise}
#
# @example
Expand All @@ -53,9 +57,13 @@ MIN_HOSTOS_RSYNC = '1.1.4'
# .catch ->
# console.log('Is incompatible')
###
exports.ensureHostOSCompatibility = ensureHostOSCompatibility = Promise.method (version, minVersion) ->
exports.ensureHostOSCompatibility = ensureHostOSCompatibility = Promise.method (osRelease, minVersion) ->
version = osRelease?.match(semverRegExp)?[0]
if not version?
throw new Error("Could not parse semantic version from HostOS release info: #{osRelease}")

if semver.lt(version, minVersion)
throw new Error("Incompatible HostOS version: #{version} - must be >= #{minVersion}")
throw new Error("Incompatible HostOS version: #{osRelease} - must be >= #{minVersion}")

###*
# @summary Sync your changes with a device
Expand Down Expand Up @@ -122,13 +130,17 @@ exports.sync = (uuid, options) ->

console.info("Connecting with: #{uuid}")

resin.models.device.isOnline(uuid).tap (isOnline) ->
resin.models.device.isOnline(uuid).then (isOnline) ->
throw new Error('Device is not online') if not isOnline
resin.models.device.get(uuid)
.tap (device) ->
ensureHostOSCompatibility(device.os_version, MIN_HOSTOS_RSYNC)
.tap (device) ->
Promise.try ->
shell.runCommand(options.before, cwd: options.source) if options.before?
.then ->
.then (device) ->
Promise.props
uuid: resin.models.device.get(uuid).get('uuid') # get full uuid
uuid: device.uuid # get full uuid
username: resin.auth.whoami()
.then ({ uuid, username }) ->
spinner = new Spinner('Stopping application container...')
Expand Down

0 comments on commit 3d8d076

Please sign in to comment.