Skip to content

Commit

Permalink
CLI Feature added: pm2 pull <name> [commit_id] // goes to commit_id i…
Browse files Browse the repository at this point in the history
…f specified
  • Loading branch information
jshkurti committed Nov 21, 2014
1 parent c0143cc commit 4021902
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
13 changes: 10 additions & 3 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,17 @@ commander.command('kill')
//
// Update repository for a given app
//
commander.command('pull <name>')

commander.command('pull <name> [commit id]')
.description('updates repository for a given app')
.action(function(pm2_name) {
CLI.pullAndRestart(pm2_name);
.action(function(pm2_name, commit_id) {
if (commit_id !== undefined) {
CLI.pullCommitId(
{pm2_name: pm2_name,
commit_id: commit_id});
}
else
CLI.pullAndRestart(pm2_name);
});

//
Expand Down
12 changes: 11 additions & 1 deletion lib/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ CLI.infoInteract = function(cb) {
});
};

var Version = require('./tools/VersionManagement');
var Version = require('./tools/VersionManagement.js');

/**
* CLI method for updating a repository
Expand Down Expand Up @@ -1425,6 +1425,16 @@ CLI.pullAndGracefulReload = function (process_name, cb) {
Version._pull({process_name: process_name, action: 'gracefulReload'}, cb);
}

/**
* CLI method for updating a repository to a specific commit id
* @method pullCommitId
* @param {object} opts
* @return
*/
CLI.pullCommitId = function (opts, cb) {
Version.pullCommitId(opts.pm2_name, opts.commit_id);
}

/**
* CLI method for downgrading a repository to the previous commit (older)
* @method backward
Expand Down
60 changes: 60 additions & 0 deletions lib/tools/VersionManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,66 @@ Methods._pull = function(opts, cb) {
});
};

/**
* CLI method for updating a repository to a specific commit id
* @method pullCommitId
* @param {string} process_name
* @param {string} commit_id
* @return
*/
Methods.pullCommitId = function(process_name, commit_id, cb) {
var reload_type = 'restart';

printOut(cst.PREFIX_MSG + 'Updating repository for process name %s', process_name);

Common.getProcessByName(process_name, function(err, processes) {

if (processes.length === 0) {
printError('No processes with this name: %s', process_name);
return cb ? cb({msg:'Process not found: '+process_name}) : exitCli(cst.ERROR_EXIT);
}

var proc = processes[0];
if (proc.pm2_env.versioning) {
vizion.isUpToDate({folder: proc.pm2_env.versioning.repo_path},
function(err, meta) {
if (err !== null)
return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT);
vizion.revertTo(
{revision: commit_id,
folder: proc.pm2_env.versioning.repo_path},
function(err2, meta2) {
if (!err2 && meta2.success) {
getPostUpdateCmds(proc.pm2_env.versioning.repo_path, process_name,
function (command_list) {
execCommands(proc.pm2_env.versioning.repo_path, command_list, function(err) {
if (err !== null)
{
printError(err);
return cb ? cb({msg:err}) : exitCli(cst.ERROR_EXIT);
}
else {
printOut(cst.PREFIX_MSG + 'Process successfully updated %s', process_name);
printOut(cst.PREFIX_MSG + 'Current commit %s', commit_id);
return CLI[reload_type](process_name, cb);
}
});
});
}
else {
printOut(cst.PREFIX_MSG + 'Already up-to-date or an error occured: %s', process_name);
return cb ? cb(null, {success:meta.success}) : exitCli(cst.SUCCESS_EXIT);
}
});
});
}
else {
printOut(cst.PREFIX_MSG + 'No versioning system found for process %s', process_name);
return cb ? cb(null, {success:false}) : exitCli(cst.SUCCESS_EXIT);
}
});
};

/**
* CLI method for downgrading a repository to the previous commit (older)
* @method backward
Expand Down
4 changes: 4 additions & 0 deletions test/bash/pull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ OUT=`$pm2 jlist | egrep -oh '"ahead":true' | wc -c`
[ $OUT -eq 13 ] || fail "Worker: ahead flag should be true"
success "Worker: ahead flag should be true"

OUT=`$pm2 pull app 83dfc32383a84e146005d8981bcae2c52a5b123b | egrep -oh 'Current commit 83dfc32383a84e146005d8981bcae2c52a5b123b' | wc -c`
[ $OUT -eq 56 ] || fail "Commit ID should be correct"
success "Commit ID should be correct"

$pm2 kill
cd ..
rm -rf ./app-playground

0 comments on commit 4021902

Please sign in to comment.