Elevate privileges of update command if necessary

This commit is contained in:
Juan Cruz Viotti 2015-04-01 12:46:41 -04:00
parent 19c019076e
commit 2a4ba895e5
3 changed files with 42 additions and 7 deletions

View File

@ -1,8 +1,12 @@
(function() {
var child_process, npm, packageJSON;
var _, child_process, npm, packageJSON, president;
_ = require('lodash');
child_process = require('child_process');
president = require('president');
npm = require('../npm');
packageJSON = require('../../package.json');
@ -13,13 +17,14 @@
help: 'Use this command to update the Resin CLI\n\nThis command outputs information about the update process.\nUse `--quiet` to remove that output.\n\nThe Resin CLI checks for updates once per day.\n\nMajor updates require a manual update with this update command,\nwhile minor updates are applied automatically.\n\nExamples:\n\n $ resin update',
action: function(params, options, done) {
return npm.isUpdated(packageJSON.name, packageJSON.version, function(error, isUpdated) {
var command, onUpdate;
if (error != null) {
return done(error);
}
if (isUpdated) {
return done(new Error('You\'re already running the latest version.'));
}
return child_process.exec("npm install -g " + packageJSON.name, function(error, stdout, stderr) {
onUpdate = function(error, stdout, stderr) {
if (error != null) {
return done(error);
}
@ -28,6 +33,16 @@
}
console.info("Upgraded " + packageJSON.name + ".");
return done();
};
command = "npm install --global " + packageJSON.name;
return child_process.exec(command, function(error, stdout, stderr) {
if (error != null) {
return onUpdate(null, stdout, stderr);
}
if (_.any([error.code === 3, error.code === 'EPERM', error.code === 'ACCES'])) {
return president.execute(command, onUpdate);
}
return done(error);
});
});
}

View File

@ -1,4 +1,6 @@
_ = require('lodash')
child_process = require('child_process')
president = require('president')
npm = require('../npm')
packageJSON = require('../../package.json')
@ -27,13 +29,30 @@ exports.update =
if isUpdated
return done(new Error('You\'re already running the latest version.'))
onUpdate = (error, stdout, stderr) ->
return done(error) if error?
return done(new Error(stderr)) if not _.isEmpty(stderr)
console.info("Upgraded #{packageJSON.name}.")
return done()
command = "npm install --global #{packageJSON.name}"
# Attempting to self update using the NPM API was not considered safe.
# A safer thing to do is to call npm as a child process
# https://github.com/npm/npm/issues/7723
child_process.exec "npm install -g #{packageJSON.name}", (error, stdout, stderr) ->
return done(error) if error?
return done(new Error(stderr)) if not _.isEmpty(stderr)
child_process.exec command, (error, stdout, stderr) ->
return onUpdate(null, stdout, stderr) if error?
if _.any [
# 3 is equivalent to EACCES for NPM
error.code is 3
error.code is 'EPERM'
error.code is 'ACCES'
]
return president.execute(command, onUpdate)
return done(error)
console.info("Upgraded #{packageJSON.name}.")
return done()

View File

@ -60,6 +60,7 @@
"nplugm": "^2.1.0",
"npm": "^2.6.1",
"open": "0.0.5",
"president": "^1.0.0",
"progress-stream": "^0.5.0",
"resin-cli-visuals": "^0.1.0",
"resin-sdk": "^1.2.0",