Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switching library to use scp2 npm module, allowing privateKey & passPhra... #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
.tern-port
tmp
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
servers: require('servers').servers(),
cmds_before_deploy: [],
cmds_after_deploy: [],
deploy_path: '~/grunt-plugins/grunt-deploy'
source_path: '.',
deploy_path: '~/grunt-plugins/grunt-deploy'
}
}
/*default_options: {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt": "~0.4.1",
"ssh2": "*",
"moment": "*"
"scp2": "~0.1.4",
"ssh2": "~0.2.13",
"moment": "~2.4.0"
},
"peerDependencies": {
"grunt": "~0.4.1"
},
"keywords": [
"gruntplugin"
]
}
}
14 changes: 14 additions & 0 deletions servers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports.servers = function() {
var fs = require('fs');
//var keyContent = fs.readFileSync( '/home/user/.ssh/id_rsa' ).toString();
return [{
host: 'host',
port: 22,
username: 'username',
password: 'password'

// for privateKey/passPhrase login
//privateKey: keyContent,
//passphrase: 'passphrase'
}];
}
132 changes: 23 additions & 109 deletions tasks/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks

grunt.registerMultiTask('deploy', 'Your task description goes here.', function() {
grunt.registerMultiTask('deploy', 'Task to deploy folders or files via node-scp2.', function() {
var self = this;
var done = self.async();
var Connection = require('ssh2');
var client = require('scp2');
var Connection = require('ssh2');
var moment = require('moment');
var timeStamp = moment().format('YYYYMMDDHHmmssSSS');

Expand Down Expand Up @@ -53,31 +54,32 @@
}
}

console.log('executing cmds before deploy');
execCmds(options.cmds_before_deploy, 0, true, function(){
console.log('cmds before deploy executed');


var createFolder = 'cd ' + options.deploy_path + '/releases && mkdir ' + timeStamp;
var removeCurrent = 'rm -rf ' + options.deploy_path + '/current';
var setCurrent = 'ln -s ' + options.deploy_path + '/releases/' + timeStamp + ' ' + options.deploy_path + '/current';

console.log('start deploy');
exec(createFolder + ' && ' + removeCurrent + ' && ' + setCurrent, false,function(){

var sys = require('sys')
var execLocal = require('child_process').exec;
var child;

child = execLocal("scp -r . " + server.username + "@" + server.host + ":" + options.deploy_path + "/releases/" + timeStamp, function (error, stdout, stderr) {
console.log('end deploy');

console.log('executing cmds after deploy');
execCmds(options.cmds_after_deploy, 0, true, function(){
console.log('cmds after deploy executed');
connection.end();
});
});


server.path = options.deploy_path + '/releases/' + timeStamp;

var startTime = ( new Date() ).getTime();;
client.scp(
(options.source_path || '.'),
server,
function(err) {
var timeDiff = ( new Date() ).getTime() - startTime;
grunt.log.ok('Deployment done in '+(timeDiff / 60000).toFixed(2)+' minutes.');
execCmds(options.cmds_after_deploy, 0, true, function(){
connection.end();
});
}
);
})
})
}
Expand All @@ -94,112 +96,24 @@
options.servers.forEach(function(server){
var c = new Connection();
c.on('connect', function() {
console.log('Connecting to server: ' + server.host);
//console.log('Connecting to server: ' + server.host);
});
c.on('ready', function() {
console.log('Connected to server: ' + server.host);
grunt.log.ok('Connected to server: ' + server.host);
execSingleServer(server,c);
});
c.on('error', function(err) {
console.log("Error on server: " + server.host)
grunt.log.error("Error on server: " + server.host)
console.error(err);
if (err) {throw err;}
});
c.on('close', function(had_error) {
console.log("Closed connection for server: " + server.host);
//console.log("Closed connection for server: " + server.host);
checkCompleted();
});
c.connect(server);
})




/*var c = new Connection();
c.on('connect', function() {
console.log('Connection :: connect');
});
c.on('ready', function() {
console.log('Connection :: ready');
c.exec("~/.nvm/v0.10.6/bin/node --version", function(err, stream) {
if (err) {throw err;}
stream.on('data', function(data, extended) {
console.log(data + '');
});
stream.on('end', function() {
console.log('Stream :: EOF');
});
stream.on('close', function() {
console.log('Stream :: close');
});
stream.on('exit', function(code, signal) {
console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
c.end();
});
});
});
c.on('error', function(err) {
console.log('Connection :: error :: ' + err);
done();
});
c.on('end', function() {
console.log('Connection :: end');
done();
});
c.on('close', function(had_error) {
console.log('Connection :: close');
done();
});
c.connect({
host: '10.211.55.11',
port: 22,
username: 'zhe',
password: 'Jldnm0ci9b#ioo*'
});*/



//console.log("run cmds by ssh");
//console.log("create folder in distination");
//console.log("copy files to distination folder and don't copy the ignore files");
//console.log("run cmds by ssh");




/*// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
punctuation: '.',
separator: ', '
});

// Iterate over all specified file groups.
this.files.forEach(function(f) {
// Concat specified files.
console.log(f.src);
var src = f.src.filter(function(filepath) {
console.log(filepath);
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
}).map(function(filepath) {
// Read file source.
return grunt.file.read(filepath);
}).join(grunt.util.normalizelf(options.separator));

// Handle options.
src += options.punctuation;

// Write the destination file.
grunt.file.write(f.dest, src);

// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
});*/
});

};