|
/** |
|
* Created by udit on 19/3/15. |
|
*/ |
|
|
|
var argv = require( 'yargs' ).argv; |
|
var gulp = require( 'gulp' ); |
|
var fs = require( 'fs.extra' ); |
|
var rsync = require( 'gulp-rsync' ); |
|
var gulpSSH = require( 'gulp-ssh' ); |
|
var chalk = require( 'chalk' ); |
|
|
|
var deployJSON = require( './deploy.json' ); |
|
|
|
var deployConfig; |
|
var sshConnect; |
|
var releasesDirecoryBase; |
|
var timestamp; |
|
var releasesDirecory; |
|
|
|
gulp.task( 'setupSSH', function() { |
|
|
|
if ( argv.staging ) { |
|
// Staging |
|
deployConfig = deployJSON.staging; |
|
} else if( argv.production ) { |
|
//Production |
|
deployConfig = deployJSON.production; |
|
} else { |
|
// Custom Environment |
|
console.log( chalk.bold.red( '"staging" or "production" environment is not passed. USE : gulp <command> --<environment> If your custom environment is configured then you need to provide its support. Comming Soon !' ) ); |
|
process.exit(); |
|
} |
|
|
|
var homePath = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE |
|
var privateKey = homePath + '/.ssh/id_rsa'; |
|
|
|
// Setup SSH object for remote commands |
|
sshConnect = gulpSSH( { |
|
ignoreErrors: false, |
|
sshConfig: { |
|
host: deployConfig.hostname, |
|
port: deployConfig.port, |
|
username: deployConfig.username, |
|
privateKey: fs.readFileSync( privateKey ) |
|
} |
|
} ); |
|
|
|
// decide releaseDirectoryBase for rsync |
|
releasesDirecoryBase = ( deployConfig.releasesDirectory ) ? deployConfig.releasesDirectory : deployConfig.destination + '../releases/'; |
|
|
|
var releaseDirCheckCommand = 'mkdir -p ' + releasesDirecoryBase; |
|
return sshConnect.shell( [ releaseDirCheckCommand ] ) |
|
.pipe( gulp.dest('./') ) |
|
.on( 'end', function() { console.log( chalk.green( "SSH setup Complete !" ) ); console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log' ).toString() ); } ); |
|
|
|
} ); |
|
|
|
gulp.task( 'upload', [ 'setupSSH' ], function() { |
|
|
|
var now = new Date(); |
|
var month = "" + (now.getMonth() + 1); |
|
var day = "" + now.getDate(); |
|
var hours = "" + now.getHours(); |
|
var minutes = "" + now.getMinutes(); |
|
var seconds = "" + now.getSeconds(); |
|
|
|
// Calculate Timestamp |
|
timestamp = now.getFullYear() |
|
+ ( month.length < 2 ? "0" + month : month ) |
|
+ ( day.length < 2 ? "0" + day : day ) |
|
+ ( hours.length < 2 ? "0" + hours : hours ) |
|
+ ( minutes.length < 2 ? "0" + minutes : minutes ) |
|
+ ( seconds.length < 2 ? "0" + seconds : seconds ); |
|
|
|
// generate releaseDirectory |
|
releasesDirecory = releasesDirecoryBase + timestamp + "/"; |
|
|
|
// Setup local rsync config |
|
var rsyncLocalConfig = {}; |
|
rsyncLocalConfig['destination'] = releasesDirecory; |
|
rsyncLocalConfig['root'] = deployConfig.root; |
|
rsyncLocalConfig['hostname'] = deployConfig.hostname; |
|
rsyncLocalConfig['username'] = deployConfig.username; |
|
rsyncLocalConfig['incremental'] = true; |
|
rsyncLocalConfig['progress'] = true; |
|
rsyncLocalConfig['relative'] = true; |
|
rsyncLocalConfig['emptyDirectory'] = true; |
|
rsyncLocalConfig['recursive'] = true; |
|
rsyncLocalConfig['recursiveclean'] = false; |
|
rsyncLocalConfig['exclude'] = [ 'node_modules/**', '.sass-cache/**', '/vendor/**' ]; |
|
|
|
return gulp.src( [ deployConfig.root+'/**', '!' + deployConfig.root + '/node_modules/**', '!' + deployConfig.root + '/.sass-cache/**', '!' + deployConfig.root + '/vendor/**' ] ) |
|
// Copy files to releases folder on the server. |
|
.pipe( rsync( rsyncLocalConfig ) ) |
|
.on( 'end', function() { console.log( chalk.green( 'Upload Complete !' ) ); } ); |
|
} ); |
|
|
|
gulp.task( 'setcurrent', [ 'upload' ], function() { |
|
|
|
// setup resetCurrentVersionCommand on the server |
|
var resetCurrentVersionCommand = 'cd ' + releasesDirecoryBase + ' && rm -rf current && rm -rf .currentTimeStamp && ln -s ' + releasesDirecory + ' current && echo "' + timestamp + '" >> .currentTimeStamp'; |
|
|
|
return sshConnect.shell( [ resetCurrentVersionCommand ] ) |
|
.pipe( gulp.dest( './' ) ) |
|
.on( 'end', function() { console.log( chalk.green( "Current version setup Complete !" ) ); console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log' ).toString() ); } ); |
|
} ); |
|
|
|
gulp.task( 'symlink', [ 'setcurrent' ], function() { |
|
|
|
var symlinkCommand = 'mkdir -p ' + deployConfig.destination + ' && rm -rf ' + deployConfig.destination + deployConfig.themeName +' && cd ' + deployConfig.destination + ' && ln -s ' + releasesDirecoryBase + 'current ' + deployConfig.themeName; |
|
|
|
return sshConnect.shell( [ symlinkCommand ] ) |
|
.pipe( gulp.dest( './' ) ) |
|
.on( 'end', function() { console.log( chalk.green( "Symlink Complete !" ) ); console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log' ).toString() ); } ); |
|
} ); |
|
|
|
gulp.task( 'deploy', [ 'setcurrent', 'symlink', 'upload', 'setupSSH' ] ); |
|
|
|
gulp.task( 'releases', [ 'setupSSH' ], function() { |
|
|
|
var showReleasesCommand = 'echo "\nList of Releases:\n" && ls -lsa ' + releasesDirecoryBase + ' && echo "\nCurrent Revision:\n" && cat ' + releasesDirecoryBase + '.currentTimeStamp && echo "\nTheme Directory Status:\n" && ls -lsa ' + deployConfig.destination + deployConfig.themeName; |
|
return sshConnect.shell( [ showReleasesCommand ] ) |
|
.pipe( gulp.dest( './' ) ) |
|
.on( 'end', function() { console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log' ).toString() ); } ); |
|
} ); |
|
|
|
gulp.task( 'rollback', [ 'setupSSH' ], function() { |
|
|
|
if ( ! argv.revision ) { |
|
console.log( chalk.bold.red( 'You need to pass valid revision to be able to rollback ! USE : gulp rollback --<environment> --revision=<timestamp> !' ) ); |
|
process.exit(); |
|
} |
|
|
|
releasesDirecory = releasesDirecoryBase + argv.revision + "/"; |
|
|
|
var rollbackCommand = '[ -d "' + releasesDirecory + '" ] && ( cd ' + releasesDirecoryBase + ' && rm -rf current && rm -rf .currentTimeStamp && ln -s ' + releasesDirecory + ' current && echo "' + argv.revision + '" >> .currentTimeStamp && mkdir -p ' + deployConfig.destination + ' && rm -rf ' + deployConfig.destination + deployConfig.themeName +' && cd ' + deployConfig.destination + ' && ln -s ' + releasesDirecoryBase + 'current ' + deployConfig.themeName + ' && echo "Success : Rollback done successfully !" ) || echo "Error: Revision "' + argv.revision + '" does not exists. Please use gulp releases to view available releases."' |
|
|
|
return sshConnect.shell( [ rollbackCommand ] ) |
|
.pipe( gulp.dest( './' ) ) |
|
.on( 'end', function() { console.log( chalk.green( "Output:" ) ); console.log( fs.readFileSync('./gulp-ssh.shell.log' ).toString() ); } ); |
|
} ); |