diff --git a/.gitignore b/.gitignore index b785247..bf707c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules npm-debug.log +.tern-port tmp diff --git a/Gruntfile.js b/Gruntfile.js index cf8f731..bd23d1d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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: { diff --git a/package.json b/package.json index b969e11..0ba8c13 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,9 @@ "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" @@ -41,4 +42,4 @@ "keywords": [ "gruntplugin" ] -} \ No newline at end of file +} diff --git a/servers.js b/servers.js new file mode 100644 index 0000000..254c693 --- /dev/null +++ b/servers.js @@ -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' + }]; +} diff --git a/tasks/deploy.js b/tasks/deploy.js index f16b6da..da63163 100644 --- a/tasks/deploy.js +++ b/tasks/deploy.js @@ -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'); @@ -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(); + }); + } + ); }) }) } @@ -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.'); - });*/ }); };