Skip to content

Commit

Permalink
Merge branch 'hotfix/v0.1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Jun 12, 2013
2 parents 5e1b264 + 7b2bde9 commit 27467bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-deployments",
"description": "Deploy (push/pull) MYSQL databases using Grunt",
"version": "0.1.4",
"version": "0.1.5",
"homepage": "https://github.com/getdave/grunt-deployments",
"author": {
"name": "David Smith",
Expand Down
74 changes: 40 additions & 34 deletions tasks/deployments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ var shell = require('shelljs');

module.exports = function(grunt) {

// GLOBALS
var local_options = grunt.config.get('deployments').local,
task_options = grunt.config.get('deployments')['options'];



/**
* DB PUSH
* pushes local database to remote database
*/
grunt.registerTask('db_push', 'Push to Database', function() {

// Options
var task_options = grunt.config.get('deployments')['options'];

// Get the target from the CLI args
var target = grunt.option('target');

Expand All @@ -31,12 +33,12 @@ module.exports = function(grunt) {

// Grab the options from the shared "deployments" config options
var target_options = grunt.config.get('deployments')[target];

var local_options = grunt.config.get('deployments').local;

// Generate required backup directories and paths
var local_backup_paths = generate_backup_paths("local");
var target_backup_paths = generate_backup_paths(target);
var local_backup_paths = generate_backup_paths("local", task_options);
var target_backup_paths = generate_backup_paths(target, task_options);


grunt.log.subhead("Pushing database from 'Local' to '" + target_options.title + "'");

Expand All @@ -54,7 +56,7 @@ module.exports = function(grunt) {
db_import(target_options, local_backup_paths.file);

grunt.log.subhead("Operations completed");
});
});


/**
Expand All @@ -63,21 +65,25 @@ module.exports = function(grunt) {
*/
grunt.registerTask('db_pull', 'Pull from Database', function() {

// Options
var task_options = grunt.config.get('deployments')['options'];

// Get the target from the CLI args
var target = grunt.option('target');

if ( typeof target === "undefined" || typeof grunt.config.get('deployments')[target] === "undefined") {
grunt.fail.warn("Invalid target provided. I cannot pull a database from nowhere! Please checked your configuration and provide a valid target.", 6);
}



// Grab the options from the shared "deployments" config options
var target_options = grunt.config.get('deployments')[target];

var local_options = grunt.config.get('deployments').local;

// Generate required backup directories and paths
var local_backup_paths = generate_backup_paths("local");
var target_backup_paths = generate_backup_paths(target);
var local_backup_paths = generate_backup_paths("local", task_options);
var target_backup_paths = generate_backup_paths(target, task_options);

// Start execution
grunt.log.subhead("Pulling database from '" + target_options.title + "' into Local");
Expand All @@ -86,7 +92,7 @@ module.exports = function(grunt) {
db_dump(target_options, target_backup_paths );

db_replace(target_options.url,local_options.url,target_backup_paths.file);

// Backup Local DB
db_dump(local_options, local_backup_paths);

Expand All @@ -95,18 +101,18 @@ module.exports = function(grunt) {

grunt.log.subhead("Operations completed");

});
});



function generate_backup_paths(target) {
function generate_backup_paths(target, task_options) {

var rtn = [];

var backups_dir = task_options['backups_dir'] || "backups";

// Create suitable backup directory paths
rtn['dir'] = grunt.template.process(tpls.backup_path, {
rtn['dir'] = grunt.template.process(tpls.backup_path, {
data: {
backups_dir: backups_dir,
env: target,
Expand All @@ -130,7 +136,7 @@ module.exports = function(grunt) {
var cmd;

// 1) Create cmd string from Lo-Dash template
var tpl_mysql = grunt.template.process(tpls.mysql, {
var tpl_mysql = grunt.template.process(tpls.mysql, {
data: {
host: config.host,
user: config.user,
Expand All @@ -144,9 +150,9 @@ module.exports = function(grunt) {
// 2) Test whether target MYSQL DB is local or whether requires remote access via SSH
if (typeof config.ssh_host === "undefined") { // it's a local connection
grunt.log.writeln("Importing into local database");
cmd = tpl_mysql + " < " + src;
cmd = tpl_mysql + " < " + src;
} else { // it's a remote connection
var tpl_ssh = grunt.template.process(tpls.ssh, {
var tpl_ssh = grunt.template.process(tpls.ssh, {
data: {
host: config.ssh_host
}
Expand All @@ -157,7 +163,7 @@ module.exports = function(grunt) {
cmd = tpl_ssh + " '" + tpl_mysql + "' < " + src;
}

// Execute cmd
// Execute cmd
shell.exec(cmd);

grunt.log.oklns("Database imported succesfully");
Expand All @@ -171,12 +177,12 @@ module.exports = function(grunt) {
function db_dump(config, output_paths) {

var cmd;

grunt.file.mkdir(output_paths.dir);


// 2) Compile MYSQL cmd via Lo-Dash template string
var tpl_mysqldump = grunt.template.process(tpls.mysqldump, {
var tpl_mysqldump = grunt.template.process(tpls.mysqldump, {
data: {
user: config.user,
pass: config.pass,
Expand All @@ -189,9 +195,9 @@ module.exports = function(grunt) {
if (typeof config.ssh_host === "undefined") { // it's a local connection
grunt.log.writeln("Creating DUMP of local database");
cmd = tpl_mysqldump;

} else { // it's a remote connection
var tpl_ssh = grunt.template.process(tpls.ssh, {
var tpl_ssh = grunt.template.process(tpls.ssh, {
data: {
host: config.ssh_host
}
Expand All @@ -208,24 +214,24 @@ module.exports = function(grunt) {
grunt.file.write( output_paths.file, output );

grunt.log.oklns("Database DUMP succesfully exported to: " + output_paths.file);

}


function db_replace(search,replace,output_file) {
var cmd = grunt.template.process(tpls.search_replace, {

var cmd = grunt.template.process(tpls.search_replace, {
data: {
search: search,
replace: replace,
path: output_file
path: output_file
}
});

grunt.log.writeln("Replacing '" + search + "' with '" + replace + "' in the database.");
// Execute cmd
shell.exec(cmd);
grunt.log.oklns("Database references succesfully updated.");
// Execute cmd
shell.exec(cmd);
grunt.log.oklns("Database references succesfully updated.");
}


Expand All @@ -248,8 +254,8 @@ module.exports = function(grunt) {

ssh: "ssh <%= host %>",
};



};

Expand Down

0 comments on commit 27467bc

Please sign in to comment.