Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.

Cw 273 cli different process #65

Open
wants to merge 2 commits into
base: develop
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
193 changes: 193 additions & 0 deletions CloudifyCliServiceManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* Created by kinneretzin on 12/8/14.
*/

/**
* Initialize the logger if needed
*/
var logger = require('log4js').getLogger('CloudifyCliServiceManager');
var conf = require('./backend/Conf');
if ( !!conf.log4js ){
logger.info('configuring');
require('log4js').configure(conf.log4js);
}

/**
* Requires
*/
var managers = require('./backend/managers');
var _ = require('lodash');
var services = require('./backend/services');
var async = require('async');

/**
* Process
* read commands to execute
* execute each command, and remove it from the db
*/
findInstallCommands(function(commands){
if (commands == null) {
process.exit();
}

async.each(commands, function(command,callback){
// First remove it from the db so it wont be executed several times
removeCommand(command, function(result){
if (!result || result == null) {
callback(new Error('Cant remove command from DB. aborting'));
} else {
runCommand(command, callback);
}
});
}, function(err){
if (err) {
logger.error("Cannot execute commands. ", err);
}
process.exit();
});
});


/**
* Read install commands from the db
* @param callback
*/
function findInstallCommands(callback) {
managers.db.connect('widgetInstallCommands', function (db, collection, done) {

collection.find({}).toArray(function(err,commands){
if (!!err) {
logger.error('failed reading commands from install executions', err);
if (callback) callback(null);
return;
}
if (!commands) {
logger.error('no widget install execution docs were found in the database');
if (callback) callback(null);
return;
}
if (commands.length == 0) {
logger.info('No widgets install commands to run at the moment');
callback(null);
return;
}

callback(commands);
});
});
}

function removeCommand(command,callback) {
managers.db.connect('widgetInstallCommands', function (db, collection, done) {

collection.remove(command,function(err,result){
if (!!err) {
logger.error('failed remove command from install commands doc', err);
if (callback) callback(null);
return;
}
if (callback) callback(result);
});
});

}

function runCommand(commandObj, callback) {
var command = commandObj.command;
var curryParams = commandObj.curryParams;

services.cloudifyCli.executeCommand(command, function (exErr/*, exResult*/) {
if (!!exErr) {
logger.error('error while running install from cli',exErr);
return;
}

if (curryParams) {
sendEmailAfterInstall( curryParams , function(err){
if (callback) callback(err);
});
} else {
if (callback) callback();
}
// TODO change execution status
});
}

/**
*
*
* if specified on curry params:
* - Send email after installation.
* - update execution model whether email sent successfully or not.
*
*/
function sendEmailAfterInstall(curryParams,callback){
if (!curryParams.widget.socialLogin || !curryParams.widget.socialLogin.handlers || !curryParams.widget.socialLogin.handlers.mandrill || !curryParams.widget.socialLogin.handlers.mandrill.enabled) {
// noop
return;
}

var mandrillConfig = curryParams.widget.socialLogin.handlers.mandrill;
var publicIp = curryParams.nodeModel.machineSshDetails.publicIp;
var link = '<a href="http://"' + publicIp + '> http://' + publicIp + '</a>';

managers.widgetLogins.getWidgetLoginById(curryParams.loginDetailsId, function(err, result) {
if (!!err) {
logger.error('unable to find login details, email send failed', err);
return;
}

if (!result) {
logger.error('result is null for login details find, email send failed');
return;
}

var fullname = result.loginDetails.name + ' ' + result.loginDetails.lastName;

var data = {
'apiKey': mandrillConfig.apiKey,
'template_name': mandrillConfig.templateName,
'template_content': [
{
'name': 'link',
'content': link
},
{
'name': 'name',
'content': fullname
},
{
'name': 'randomValue',
'content': curryParams.nodeModel.randomValue
},
{
'name' : 'publicIp',
'content' : publicIp
}
],
'message': {
'to':[
{
'email':result.loginDetails.email,
'name': fullname,
'type': 'to'
}
]
},
'async':true
};

services.mandrill.sendMandrillTemplate( data,
function(err, result){
if (!!err) {
curryParams.widget.socialLogin.handlers.mandrill.status = err;
if (callback) callback(err);
} else {
curryParams.widget.socialLogin.handlers.mandrill.status = result;
if (callback) callback();
}
});

});
}

51 changes: 41 additions & 10 deletions backend/managers/WidgetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,21 @@ function _runInstallCommand(curryParams, curryCallback) {
logsDir: curryParams.executionLogsPath,
executionId: curryParams.executionObjectId.toHexString()
};
// we want to remove the execution model when the execution is over
services.cloudifyCli.executeCommand(command, function (exErr/*, exResult*/) {
if (!!exErr) {
logger.error('error while running install from cli',exErr);
return;
}

sendEmailAfterInstall( curryParams );
// TODO change execution status
});
if (conf.cloudifyCliService && (!conf.cloudifyCliService.runInline)) {
createInstallCommand(command,curryParams);
} else {
// we want to remove the execution model when the execution is over
services.cloudifyCli.executeCommand(command, function (exErr/*, exResult*/) {
if (!!exErr) {
logger.error('error while running install from cli',exErr);
return;
}

sendEmailAfterInstall( curryParams );
// TODO change execution status
});
}

curryCallback(null, curryParams);
}
Expand Down Expand Up @@ -573,7 +578,11 @@ function _runBootstrapAndInstallCommands(curryParams, curryCallback) {

logger.info('-command', command);

services.cloudifyCli.executeCommand(command);
if (conf.cloudifyCliService && (!conf.cloudifyCliService.runInline)) {
createInstallCommand(command);
} else {
services.cloudifyCli.executeCommand(command);
}

curryCallback(null, curryParams);
}
Expand Down Expand Up @@ -601,6 +610,28 @@ function updateExecution(executionObjectId, data) {
});
}

function createInstallCommand( command, curryParams ) {
managers.db.connect('widgetInstallCommands', function (db, collection, done) {
collection.insert([ {
command: command,
curryParams: curryParams
}],
function (err, createResult) {
if (!!err) {
logger.error('failed creating an install command model', err);
done();
return;
}
if (!createResult) {
logger.error('no widget install command docs created in the database');
done();
return;
}
done();
});
});
}

function _playFinally(err, curryParams) {

if (!!err) {
Expand Down
1 change: 0 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ module.exports = function (config) {
'app/bower_components/ngstorage/ngStorage.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/gs-ui-infra/app/scripts/app.js',
'app/bower_components/gs-ui-infra/app/scripts/filters/i18n.js',
'app/bower_components/gs-ui-infra/app/scripts/services/i18next.js',
Expand Down