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

Commit

Permalink
Remove 'exec after rsync' feature and use ssh gateway 'rsync' command
Browse files Browse the repository at this point in the history
  • Loading branch information
lekkas committed Apr 25, 2016
1 parent 30cf39f commit 3becdba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 62 deletions.
70 changes: 35 additions & 35 deletions build/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ config = require('./config');
*
* - `rsync`
* - `ssh`
* - `nc`
*
* Resin Sync **doesn't support Windows yet**, however it will work
* under Cygwin.
Expand All @@ -60,7 +59,6 @@ config = require('./config');
* $ cat $PWD/resin-sync.yml
* source: src/
* before: 'echo Hello'
* exec: 'python main.py'
* ignore:
* - .git
* - node_modules/
Expand All @@ -74,9 +72,8 @@ config = require('./config');
* @param {String} [options.source=$PWD] - source path
* @param {String[]} [options.ignore] - ignore paths
* @param {String} [options.before] - command to execute before sync
* @param {String} [options.exec] - command to execute after sync (on the device)
* @param {Boolean} [options.progress=true] - display sync progress
* @param {Number} [options.port=80] - ssh port
* @param {Number} [options.port=22] - ssh port
*
* @example
* resinSync.sync('7a4e3dc', {
Expand All @@ -86,10 +83,10 @@ config = require('./config');
*/

exports.sync = function(uuid, options) {
var performSync;
options = _.merge(config.load(), options);
_.defaults(options, {
source: process.cwd()
source: process.cwd(),
port: 22
});
utils.validateObject(options, {
properties: {
Expand All @@ -103,11 +100,6 @@ exports.sync = function(uuid, options) {
type: 'string',
message: 'The before option should be a string'
},
exec: {
description: 'exec',
type: 'string',
message: 'The exec option should be a string'
},
progress: {
description: 'progress',
type: 'boolean',
Expand All @@ -116,36 +108,44 @@ exports.sync = function(uuid, options) {
}
});
console.info("Connecting with: " + uuid);
performSync = function(fullUUID) {
return resin.models.device.isOnline(uuid).tap(function(isOnline) {
if (!isOnline) {
throw new Error('Device is not online');
}
return Promise["try"](function() {
if (options.before != null) {
return shell.runCommand(options.before);
}
}).then(function() {
var command;
command = rsync.getCommand(fullUUID, options);
return shell.runCommand(command);
}).then(function() {
});
}).then(function() {
return Promise.props({
uuid: resin.models.device.get(uuid).get('uuid'),
username: resin.auth.whoami()
});
}).then(function(_arg) {
var username, uuid;
uuid = _arg.uuid, username = _arg.username;
console.log('Stopping application container...');
return resin.models.device.stopApplication(uuid).then(function(containerId) {
var command;
if (options.exec != null) {
console.info('Synced, running command');
command = ssh.getConnectCommand({
uuid: fullUUID,
command: options.exec,
port: options.port
});
return shell.runCommand(command);
} else {
console.info('Synced, restarting device');
return resin.models.device.restart(uuid);
if (containerId == null) {
throw new Error('No stopped application container found');
}
options = _.merge(options, {
username: username,
uuid: uuid,
containerId: containerId
});
command = rsync.getCommand(options);
return shell.runCommand(command)["catch"](function(err) {
return console.log('rsync error: ', err);
});
}).tap(function() {
console.log('Starting application container...');
return resin.models.device.startApplication(uuid);
})["catch"](function(err) {
console.log('Rsync failed: application container could not be restarted', err);
return resin.models.device.startApplication(uuid);
});
};
return resin.models.device.isOnline(uuid).tap(function(isOnline) {
if (!isOnline) {
throw new Error('Device is not online');
}
}).then(function() {
return resin.models.device.get(uuid).get('uuid').then(performSync);
});
};
55 changes: 28 additions & 27 deletions lib/sync.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ config = require('./config')
#
# - `rsync`
# - `ssh`
# - `nc`
#
# Resin Sync **doesn't support Windows yet**, however it will work
# under Cygwin.
Expand All @@ -50,7 +49,6 @@ config = require('./config')
# $ cat $PWD/resin-sync.yml
# source: src/
# before: 'echo Hello'
# exec: 'python main.py'
# ignore:
# - .git
# - node_modules/
Expand All @@ -64,9 +62,8 @@ config = require('./config')
# @param {String} [options.source=$PWD] - source path
# @param {String[]} [options.ignore] - ignore paths
# @param {String} [options.before] - command to execute before sync
# @param {String} [options.exec] - command to execute after sync (on the device)
# @param {Boolean} [options.progress=true] - display sync progress
# @param {Number} [options.port=80] - ssh port
# @param {Number} [options.port=22] - ssh port
#
# @example
# resinSync.sync('7a4e3dc', {
Expand All @@ -79,6 +76,7 @@ exports.sync = (uuid, options) ->

_.defaults options,
source: process.cwd()
port: 22

utils.validateObject options,
properties:
Expand All @@ -90,36 +88,39 @@ exports.sync = (uuid, options) ->
description: 'before'
type: 'string'
message: 'The before option should be a string'
exec:
description: 'exec'
type: 'string'
message: 'The exec option should be a string'
progress:
description: 'progress'
type: 'boolean'
message: 'The progress option should be a boolean'

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

performSync = (fullUUID) ->
Promise.try ->
return shell.runCommand(options.before) if options.before?
.then ->
command = rsync.getCommand(fullUUID, options)
return shell.runCommand(command)
.then ->
if options.exec?
console.info('Synced, running command')
command = ssh.getConnectCommand
uuid: fullUUID
command: options.exec
port: options.port
return shell.runCommand(command)
else
console.info('Synced, restarting device')
return resin.models.device.restart(uuid)

resin.models.device.isOnline(uuid).tap (isOnline) ->
throw new Error('Device is not online') if not isOnline
Promise.try ->
shell.runCommand(options.before) if options.before?
.then ->
return resin.models.device.get(uuid).get('uuid').then(performSync)
Promise.props
uuid: resin.models.device.get(uuid).get('uuid') # get full uuid
username: resin.auth.whoami()
.then ({ uuid, username }) ->
console.log('Stopping application container...')
resin.models.device.stopApplication(uuid)
.then (containerId) ->
if not containerId?
throw new Error('No stopped application container found')

options = _.merge(options, { username, uuid, containerId })
command = rsync.getCommand(options)
shell.runCommand(command)
.catch (err) ->
console.log('rsync error: ', err)
.tap ->
console.log('Starting application container...')
resin.models.device.startApplication(uuid)
.catch (err) ->
# TODO: Supervisor completely removes a stopped container that
# fails to start, so notify the user and run 'startApplication()'
# once again to make sure that a new app container will be started
console.log('Rsync failed: application container could not be restarted', err)
resin.models.device.startApplication(uuid)

0 comments on commit 3becdba

Please sign in to comment.