mirror of
https://github.com/mmumshad/ansible-playable.git
synced 2025-03-09 23:38:54 +00:00
Initial Commit
This commit is contained in:
commit
c92f737237
273 changed files with 16964 additions and 0 deletions
658
server/components/ansible/ansible_tool.js
Normal file
658
server/components/ansible/ansible_tool.js
Normal file
|
@ -0,0 +1,658 @@
|
|||
var ssh2_exec = require('../ssh/ssh2_exec');
|
||||
var scp2_exec = require('../scp/scp_exec');
|
||||
var config = require('../../config/environment');
|
||||
|
||||
var local_logPath = 'logs/ansible/execute/'
|
||||
|
||||
exports.getLogs = function(logfilename,successCallback,errorCallback){
|
||||
var logFile = local_logPath + logfilename;
|
||||
var fs = require('fs');
|
||||
fs.readFile(logFile, function(err, data){
|
||||
if(err){
|
||||
errorCallback(err);
|
||||
}else{
|
||||
successCallback(data);
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
exports.getModules = function(dataCallback, successCallback,errorCallback, ansibleEngine){
|
||||
|
||||
var command = 'ansible-doc -l';
|
||||
|
||||
if(ansibleEngine.customModules){
|
||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||
}
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
null,
|
||||
function(data){
|
||||
successCallback(data);
|
||||
},
|
||||
function(data){
|
||||
errorCallback(data)
|
||||
},
|
||||
ansibleEngine
|
||||
)
|
||||
};
|
||||
|
||||
exports.getAnsibleVersion = function(successCallback,errorCallback, ansibleEngine){
|
||||
var command = 'ansible --version';
|
||||
var ansibleVersionResult = "";
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
null,
|
||||
function(data){
|
||||
ansibleVersionResult=data;
|
||||
console.log("Ansible Verison =" + ansibleVersionResult);
|
||||
ansibleVersionResult = "" + ansibleVersionResult;
|
||||
var version = ansibleVersionResult.replace(/ansible (.*)[^]+/,"$1");
|
||||
console.log("Version=" + version);
|
||||
successCallback(version || ansibleVersionResult);
|
||||
},
|
||||
function(data){
|
||||
errorCallback(data)
|
||||
},
|
||||
ansibleEngine
|
||||
)
|
||||
};
|
||||
|
||||
|
||||
exports.executeAnsible = function(logfilename,project_folder, playbook_name, inventory_file_name, tags_joined, limit_to_hosts_joined, verbose,check_mode,dataCallback, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var fs = require('filendir');
|
||||
var time = new Date().getTime();
|
||||
var logFile = local_logPath + logfilename;
|
||||
|
||||
fs.writeFileSync(logFile,"Executing Ansible Playbook \n\n",{'flag':'a'});
|
||||
fs.writeFileSync(logFile," Completed \n",{'flag':'a'});
|
||||
|
||||
// export ANSIBLE_GATHERING=FALSE;
|
||||
var command= 'export ANSIBLE_FORCE_COLOR=true; export ANSIBLE_HOST_KEY_CHECKING=False; cd "' + project_folder + '"; ansible-playbook --vault-password-file ~/.vault_pass.txt "' + playbook_name + '" -i "' + inventory_file_name + '"';
|
||||
|
||||
if(ansibleEngine.customModules){
|
||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||
}
|
||||
|
||||
if(tags_joined)
|
||||
command += ' --tags "' + tags_joined + '"';
|
||||
//command += ' --tags "' + tags.join(",") + '"';
|
||||
|
||||
if(limit_to_hosts_joined)
|
||||
command += ' --limit "' + limit_to_hosts_joined + '"';
|
||||
|
||||
if(verbose === 'verbose_detail'){
|
||||
command += ' -vvv ';
|
||||
}
|
||||
else if(verbose === 'verbose'){
|
||||
command += ' -v ';
|
||||
}
|
||||
|
||||
if(check_mode !== 'No_Check'){
|
||||
command += ' --check ';
|
||||
}
|
||||
|
||||
console.log("Command= " + command);
|
||||
|
||||
//fs.writeFileSync(logFile,"\n Executing Command =" + command + "\n",{'flag':'a'});
|
||||
fs.writeFile(logFile,"\n Executing Command =" + command + "\n");
|
||||
|
||||
ssh2_exec.executeCommand(command,function(response){
|
||||
//Calling datacallbcak back as the call is Asynchronous
|
||||
//Logs are queried to check status
|
||||
dataCallback(response);
|
||||
console.log(response);
|
||||
//fs.writeFile(logFile,response,{'flag':'a'});
|
||||
fs.writeFile(logFile,"\n Executing Command =" + command + "\n" + response);
|
||||
},function(response){
|
||||
successCallback(response);
|
||||
console.log(response);
|
||||
//fs.writeFile(logFile,response,{'flag':'a'});
|
||||
},function(response){
|
||||
errorCallback(response);
|
||||
console.log(response);
|
||||
fs.writeFile(logFile,response,{'flag':'a'});
|
||||
},ansibleEngine, true //addScriptEndString
|
||||
)
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
exports.getVars = function(project_folder, inventory_file_name, host_name, dataCallback, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var fs = require('filendir');
|
||||
|
||||
var AnsibleAPILocation = '/opt/ehc-builder-scripts/ansible_modules/AnsibleAPI.py';
|
||||
|
||||
var command= 'cd "' + project_folder + '"; python "' + AnsibleAPILocation + '" host_vars --inventory_file="' + inventory_file_name + '"';
|
||||
|
||||
if(host_name){
|
||||
command += ' --host_name ' + host_name;
|
||||
}
|
||||
|
||||
if(ansibleEngine.customModules){
|
||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||
}
|
||||
|
||||
console.log("Command= " + command);
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
|
||||
exports.getRolesVars = function(project_folder, role_name, dataCallback, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var AnsibleAPILocation = '/opt/ehc-builder-scripts/ansible_modules/AnsibleAPI.py';
|
||||
|
||||
var project_roles_folder = project_folder + '/roles';
|
||||
var playbook_path = role_name + '/tests/test.yml';
|
||||
var command= 'cd "' + project_roles_folder + '"; python "' + AnsibleAPILocation + '" role_vars --playbook_path="' + playbook_path + '" --vault_password_file ~/.vault_pass.txt';
|
||||
|
||||
if(ansibleEngine.customModules){
|
||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||
}
|
||||
|
||||
console.log("Command= " + command);
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
exports.executeAnsible = function(logfilename,inventory_file_contents,playbook_file_contents,tags,verbose,check_mode,dataCallback, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var fs = require('filendir');
|
||||
var time = new Date().getTime();
|
||||
var logFile = local_logPath + logfilename;
|
||||
|
||||
fs.writeFileSync(logFile,"Executing Ansible Playbook \n\n",{'flag':'a'});
|
||||
|
||||
var inventory_file_name = 'inventory_file_' + time + '.ini';
|
||||
var playbook_file_name = 'playbook_file_' + time + '.yml';
|
||||
|
||||
fs.writeFileSync(logFile,"inventory_file_location - " + inventory_file_name +" \n",{'flag':'a'});
|
||||
fs.writeFileSync(logFile,"playbook_file_location - " + playbook_file_name +" \n\n",{'flag':'a'});
|
||||
|
||||
console.log('inventory_file_name=' + inventory_file_name);
|
||||
|
||||
var inputFilePathOnScriptEngine = config.scriptEngine.inputDirectory + '/inventory/' + inventory_file_name;
|
||||
var playbookFilePathOnScriptEngine = config.scriptEngine.inputDirectory + '/playbooks/' + playbook_file_name;
|
||||
|
||||
fs.writeFileSync(logFile,"Writing inventory file to Script Engine - " + inputFilePathOnScriptEngine +" - ",{'flag':'a'});
|
||||
|
||||
scp2_exec.createFileOnScriptEngine(inventory_file_contents,inputFilePathOnScriptEngine,
|
||||
function(){
|
||||
console.log("Inventory file written");
|
||||
fs.writeFileSync(logFile," Completed \n",{'flag':'a'});
|
||||
fs.writeFileSync(logFile,"Writing playbook file to Script Engine - " + playbookFilePathOnScriptEngine +" - ",{'flag':'a'});
|
||||
scp2_exec.createFileOnScriptEngine(playbook_file_contents,playbookFilePathOnScriptEngine,
|
||||
function(){
|
||||
console.log("Playbook file written");
|
||||
fs.writeFileSync(logFile," Completed \n",{'flag':'a'});
|
||||
|
||||
var command= "export ANSIBLE_HOST_KEY_CHECKING=False; ansible-playbook --vault-password-file ~/.vault_pass.txt " + playbookFilePathOnScriptEngine + " -i " + inputFilePathOnScriptEngine;
|
||||
|
||||
if(ansibleEngine.customModules){
|
||||
command = 'export ANSIBLE_LIBRARY=' + ansibleEngine.customModules + '; ' + command;
|
||||
}
|
||||
|
||||
if(tags)
|
||||
command += ' --tags "' + tags.join(",") + '"';
|
||||
|
||||
if(verbose === 'verbose_detail'){
|
||||
command += ' -vvv ';
|
||||
}
|
||||
else if(verbose === 'verbose'){
|
||||
command += ' -v ';
|
||||
}
|
||||
|
||||
if(check_mode !== 'No_Check'){
|
||||
command += ' --check ';
|
||||
}
|
||||
|
||||
console.log("Command= " + command);
|
||||
|
||||
fs.writeFileSync(logFile,"\n Executing Command =" + command + "\n",{'flag':'a'});
|
||||
|
||||
ssh2_exec.executeCommand(command,function(response){
|
||||
dataCallback(response);
|
||||
console.log(response);
|
||||
fs.writeFile(logFile,response,{'flag':'a'});
|
||||
},function(response){
|
||||
successCallback(response);
|
||||
console.log(response);
|
||||
fs.writeFile(logFile,response,{'flag':'a'});
|
||||
},function(response){
|
||||
errorCallback(response);
|
||||
console.log(response);
|
||||
fs.writeFile(logFile,response,{'flag':'a'});
|
||||
},ansibleEngine)
|
||||
|
||||
},function(err){
|
||||
errorCallback(err);
|
||||
fs.writeFile(logFile," Failed \n",{'flag':'a'});
|
||||
},ansibleEngine);
|
||||
|
||||
},function(err){
|
||||
errorCallback(err);
|
||||
fs.writeFile(logFile," Failed \n",{'flag':'a'});
|
||||
},ansibleEngine);
|
||||
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
|
||||
exports.writeFile = function(file_path,file_contents, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
scp2_exec.createFileOnScriptEngine(file_contents, file_path,
|
||||
function(){
|
||||
successCallback('file written');
|
||||
},function(err){
|
||||
errorCallback(err);
|
||||
},ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
exports.deleteFile = function(file_path,successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var command = 'rm -rf "' + file_path + '"';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine)
|
||||
|
||||
};
|
||||
|
||||
exports.readFile = function(file_path, dataCallback, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var command = 'cat "' + file_path + '"';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine)
|
||||
|
||||
};
|
||||
|
||||
exports.writePlaybook = function(project_folder,playbook_file_name,playbook_file_contents, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var playbook_file_path = '' + project_folder + '/' + playbook_file_name + '';
|
||||
|
||||
console.log('playbook_file_path=' + playbook_file_path);
|
||||
|
||||
scp2_exec.createFileOnScriptEngine(playbook_file_contents, playbook_file_path,
|
||||
function(){
|
||||
console.log('playbook_file written');
|
||||
successCallback('playbook_file written');
|
||||
},function(err){
|
||||
errorCallback(err);
|
||||
},ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
|
||||
exports.readPlaybook = function(project_folder,playbook_file_name, dataCallback, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var playbook_file_path = project_folder + '/' + playbook_file_name;
|
||||
var command = 'cat "' + playbook_file_path + '"';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine)
|
||||
|
||||
};
|
||||
|
||||
|
||||
exports.deletePlaybook = function(project_folder,playbook_file_name, dataCallback, successCallback,errorCallback,ansibleEngine){
|
||||
|
||||
var playbook_file_path = project_folder + '/' + playbook_file_name;
|
||||
var command = 'rm -f "' + playbook_file_path + '"';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine)
|
||||
|
||||
};
|
||||
|
||||
exports.getPlaybookList = function(project_folder, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var playbook_file_path = project_folder + '/';
|
||||
var command = 'ls "' + playbook_file_path + '" | grep .yml';
|
||||
var ansiblePlaybookListResults = "";
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
null,
|
||||
function(data){
|
||||
ansiblePlaybookListResults=data;
|
||||
var files = [];
|
||||
if(ansiblePlaybookListResults)
|
||||
files = ansiblePlaybookListResults.trim().split('\n');
|
||||
successCallback(files);
|
||||
},
|
||||
function(data){
|
||||
errorCallback(data)
|
||||
},
|
||||
ansibleEngine
|
||||
)
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get list of roles from project
|
||||
* @param project_folder - Project root folder
|
||||
* @param successCallback - Success Callback method
|
||||
* @param errorCallback - Error Callback method
|
||||
* @param ansibleEngine - Remote Ansible Engine details
|
||||
*/
|
||||
exports.getRolesList = function(project_folder, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var playbook_file_path = project_folder + '/roles';
|
||||
var command = 'ls "' + playbook_file_path + '"';
|
||||
var ansiblePlaybookListResults = "";
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
null,
|
||||
function(data){
|
||||
ansiblePlaybookListResults=data;
|
||||
var roles = [];
|
||||
if(ansiblePlaybookListResults)
|
||||
roles = ansiblePlaybookListResults.trim().split('\n');
|
||||
successCallback(roles);
|
||||
},
|
||||
function(data){
|
||||
errorCallback(data)
|
||||
},
|
||||
ansibleEngine
|
||||
)
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create Project Folder
|
||||
* @param project_folder - Project folder to create
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine - Remote Ansible Engine details
|
||||
*/
|
||||
exports.createProjectFolder = function(project_folder, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var librarypath = project_folder + '/library';
|
||||
var rolespath = project_folder + '/roles';
|
||||
var command = 'mkdir -p "' + librarypath + '"; mkdir -p "' + rolespath + '"';
|
||||
|
||||
var check_dir_command = '[ ! -d ' + project_folder + ' ]';
|
||||
|
||||
ssh2_exec.executeCommand(check_dir_command,null,function(data){
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
},function(){
|
||||
errorCallback("Directory - " + project_folder +" already exists. Try a different Project Folder path.")
|
||||
},ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Search roles in ansible-galaxy
|
||||
* Use ansible-galaxy search command
|
||||
* @param searchText - Text to search
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine - Remote Ansible Engine details
|
||||
*/
|
||||
exports.searchRolesGalaxy = function(searchText, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var command = 'ansible-galaxy search ' + searchText;
|
||||
console.log('Command = ' + command);
|
||||
ssh2_exec.executeCommand(command,null,function(response){
|
||||
|
||||
console.log("Galaxy Response =" + response);
|
||||
|
||||
if(response.indexOf('No roles match your search.') > -1){
|
||||
return errorCallback('No roles match your search.')
|
||||
}else{
|
||||
var str = response.replace(/[^]+--\n([^]+)/g,'$1');
|
||||
|
||||
var re = /\s+(.*?)\s+(.*)/gm;
|
||||
var m;
|
||||
var results = [];
|
||||
while ((m = re.exec(str)) !== null) {
|
||||
if (m.index === re.lastIndex) {
|
||||
re.lastIndex++;
|
||||
}
|
||||
// View your result using the m-variable.
|
||||
// eg m[0] etc.
|
||||
|
||||
results.push({'name':m[1],'description':m[2],'type':'galaxy'})
|
||||
|
||||
}
|
||||
|
||||
successCallback(results);
|
||||
|
||||
}
|
||||
|
||||
}, errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Search Roles in GitHub
|
||||
* Uses uri https://api.github.com/search/repositories?q=ansible-role-<searchText>
|
||||
* @param searchText
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine
|
||||
*/
|
||||
exports.searchRolesGithub = function(searchText, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var https = require('https');
|
||||
var options = {
|
||||
host: 'api.github.com',
|
||||
path: '/search/repositories?q=ansible-role-' + searchText,
|
||||
headers: {'user-agent': 'node.js'}
|
||||
};
|
||||
|
||||
console.log("path " + '/search/repositories?q=ansible-role' + searchText)
|
||||
|
||||
var req = https.get(options, function(res) {
|
||||
console.log('STATUS: ' + res.statusCode);
|
||||
console.log('HEADERS: ' + JSON.stringify(res.headers));
|
||||
console.log('DATA: ' + JSON.stringify(res.data));
|
||||
|
||||
// Buffer the body entirely for processing as a whole.
|
||||
var bodyChunks = [];
|
||||
res.on('data', function(chunk) {
|
||||
// You can process streamed parts here...
|
||||
bodyChunks.push(chunk);
|
||||
}).on('end', function() {
|
||||
var body = Buffer.concat(bodyChunks);
|
||||
console.log('BODY: ' + body);
|
||||
|
||||
var json_results = JSON.parse(body);
|
||||
var results = [];
|
||||
console.log("Search Results = " + json_results.total_count);
|
||||
for(var i=0;i<json_results.total_count;i++){
|
||||
try{
|
||||
results.push({'name':json_results.items[i].name,'description':json_results.items[i].description,'url':json_results.items[i].clone_url,'type':'gitrepo'})
|
||||
}catch (e){
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
successCallback(results);
|
||||
|
||||
// ...and/or process the entire body here.
|
||||
})
|
||||
});
|
||||
|
||||
req.on('error', function(e) {
|
||||
console.log('ERROR: ' + e.message);
|
||||
errorCallback(e.message)
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates Ansible Role
|
||||
* Uses ansible-galaxy init to create role in projectFolder
|
||||
* @param roleName - Role name to create
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine
|
||||
*/
|
||||
exports.createRole = function(roleName, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var projectFolder = ansibleEngine.projectFolder;
|
||||
var command = 'cd "' + projectFolder + '/roles"; ansible-galaxy init ' + roleName;
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy an existing role
|
||||
* Uses cp -r command to copy existing folder and deletes .git directory inside it.
|
||||
* @param roleName - Name of new role name
|
||||
* @param selectedRoleName - Existing roleName
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine
|
||||
*/
|
||||
exports.copyRole = function(roleName, successCallback, errorCallback, ansibleEngine, selectedRoleName){
|
||||
var projectFolder = ansibleEngine.projectFolder;
|
||||
var command = 'cd "' + projectFolder + '/roles"; cp -r "' + selectedRoleName + '" "' + roleName + '"; rm -rf "' + roleName + '/.git"';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete role folder
|
||||
* Uses rm -rf to delete
|
||||
* @param roleName - role folder name to delete
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine
|
||||
*/
|
||||
exports.deleteRole = function(roleName, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var projectFolder = ansibleEngine.projectFolder;
|
||||
var command = 'rm -rf "' + projectFolder + '/roles/' + roleName + '"';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
exports.importRole = function(roleType, roleNameUri, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var projectFolder = ansibleEngine.projectFolder;
|
||||
var rolesFolder = projectFolder + '/roles';
|
||||
var command = 'cd "' + rolesFolder + '";';
|
||||
|
||||
if(roleType === 'gitrepo'){
|
||||
command += 'git clone ' + roleNameUri;
|
||||
}else if(roleType === 'galaxy'){
|
||||
command += 'ansible-galaxy install ' + roleNameUri + ' -p ' + rolesFolder;
|
||||
}else{
|
||||
return errorCallback('Invalid Type - allowed = gitrepo,galaxy ; given = ' + roleType);
|
||||
}
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get Role Files
|
||||
* @param roleName
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine
|
||||
*/
|
||||
exports.getRoleFiles = function(roleName, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var projectFolder = ansibleEngine.projectFolder;
|
||||
// var command = 'node /opt/node-programs/dirTree.js "' + projectFolder + '/roles/' + roleName + '"';
|
||||
var command = 'cd "' + projectFolder + '/roles/' + roleName + '"; python /opt/ehc-builder-scripts/bin/dir_tree.py';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* GetProjectFiles
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine
|
||||
*/
|
||||
exports.getProjectFiles = function(successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var projectFolder = ansibleEngine.projectFolder;
|
||||
// var command = 'node /opt/node-programs/dirTree.js "' + projectFolder + '/roles/' + roleName + '"';
|
||||
var command = 'cd "' + projectFolder + '"; python /opt/ehc-builder-scripts/bin/dir_tree.py';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get Tag List
|
||||
* @param project_folder - CWD while running the playbook
|
||||
* @param playbook_name - playbook name or relative path
|
||||
* @param inventory_file_name - inventory file name or relative path
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param ansibleEngine
|
||||
*/
|
||||
exports.getTagList = function(project_folder, playbook_name, inventory_file_name, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
//var command = 'cd "' + project_folder + '"; ansible-playbook --vault-password-file ~/.vault_pass.txt "' + playbook_name + '" -i "' + inventory_file_name + '" --list-tags';
|
||||
var command = 'cd "' + project_folder + '"; python2.7 /opt/ehc-builder-scripts/ansible_modules/my_playbook.py --vault-password-file ~/.vault_pass.txt "' + playbook_name + '" -i "' + inventory_file_name + '" --list-hosts --list-tasks-json ';
|
||||
|
||||
if(ansibleEngine.customModules){
|
||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||
}
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
|
||||
exports.createFile = function(fileAbsolutePath, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var projectFolder = ansibleEngine.projectFolder;
|
||||
var command = 'touch "' + fileAbsolutePath + '"';
|
||||
|
||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||
|
||||
};
|
||||
|
||||
exports.getInventoryList = function(project_folder, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var playbook_file_path = project_folder + '/';
|
||||
var command = 'cd "' + playbook_file_path + '" ; ls --ignore="*.*" -p | grep -v /';
|
||||
var ansiblePlaybookListResults = "";
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
null,
|
||||
function(data){
|
||||
ansiblePlaybookListResults=data;
|
||||
var files = [];
|
||||
if(ansiblePlaybookListResults)
|
||||
files = ansiblePlaybookListResults.trim().split('\n');
|
||||
successCallback(files);
|
||||
},
|
||||
function(data){
|
||||
errorCallback(data)
|
||||
},
|
||||
ansibleEngine
|
||||
)
|
||||
|
||||
};
|
||||
|
||||
exports.readInventoryFile = function(project_folder, inventoryName, successCallback, errorCallback, ansibleEngine){
|
||||
|
||||
var playbook_file_path = project_folder + '/';
|
||||
var command = 'cat "' + playbook_file_path + inventoryName + '"';
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
null,
|
||||
successCallback,
|
||||
errorCallback,
|
||||
ansibleEngine
|
||||
)
|
||||
|
||||
};
|
22
server/components/errors/index.js
Normal file
22
server/components/errors/index.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* Error responses
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports[404] = function pageNotFound(req, res) {
|
||||
var viewFilePath = '404';
|
||||
var statusCode = 404;
|
||||
var result = {
|
||||
status: statusCode
|
||||
};
|
||||
|
||||
res.status(result.status);
|
||||
res.render(viewFilePath, {}, function(err, html) {
|
||||
if(err) {
|
||||
return res.status(result.status).json(result);
|
||||
}
|
||||
|
||||
res.send(html);
|
||||
});
|
||||
};
|
75
server/components/pythonParser.js
Normal file
75
server/components/pythonParser.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
* Created by mannam4 on 11/3/2016.
|
||||
*/
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
var dir = require('node-dir');
|
||||
const path = require('path');
|
||||
var options = {
|
||||
match:/vroConfig.py$/,
|
||||
matchDir: ['vro']
|
||||
};
|
||||
|
||||
|
||||
var results = []
|
||||
|
||||
dir.readFiles('C:\\Mumshad Files\\Projects\\EHC Builder\\python scripts\\Code\\source', options , function(err, content, filename, next) {
|
||||
console.log('processing content of file', filename);
|
||||
|
||||
//const regex = /def (.*)\(.*\s+"""[^]+?Description: (.*)[^]+?Parameters:([^]+?)(Returns:|Return:)([^]+?)(Raises:|Raise:|)([^]+?)"""/g;
|
||||
var regex = /def (.*)\(.*\s+"""([^]+?)"""/g;
|
||||
|
||||
var m;
|
||||
|
||||
while ((m = regex.exec(content)) !== null) {
|
||||
// This is necessary to avoid infinite loops with zero-width matches
|
||||
if (m.index === regex.lastIndex) {
|
||||
regex.lastIndex++;
|
||||
}
|
||||
|
||||
// The result can be accessed through the `m`-variable.
|
||||
// m.forEach((match, groupIndex) => {
|
||||
// console.log(`Found match, group ${groupIndex}: ${match}`);
|
||||
//
|
||||
// });
|
||||
|
||||
var methodName = m[1];
|
||||
var docStringComments = '"""' + m[2] + '"""';
|
||||
|
||||
var regex2 = /"""[^]+(Description[^]+?)(Parameters[^]+?)?(Return[^]+?)?(Raise[^]+?)?"""/gm;
|
||||
var description = docStringComments.replace(regex2, '$1');
|
||||
var Parameters = docStringComments.replace(regex2, '$2');
|
||||
var Return = docStringComments.replace(regex2, '$3');
|
||||
var Raise = docStringComments.replace(regex2, '$4');
|
||||
|
||||
var moduleName = path.parse(filename).name
|
||||
var packageName = path.parse(path.parse(filename).dir).name
|
||||
|
||||
// console.log('Package: %s', packageName);
|
||||
// console.log('Module: %s', moduleName);
|
||||
// console.log('Method: %s', methodName);
|
||||
// console.log('Description: %s', description);
|
||||
|
||||
var method = {
|
||||
moduleName: moduleName,
|
||||
methodName: methodName,
|
||||
packageName: packageName,
|
||||
description: description
|
||||
}
|
||||
|
||||
results.push(method)
|
||||
// console.log('Parameters: %s', Parameters);
|
||||
// console.log('Return: %s', Return);
|
||||
// console.log('Raise: %s', Raise);
|
||||
|
||||
}
|
||||
|
||||
next();
|
||||
}, function(){
|
||||
|
||||
console.log(results);
|
||||
|
||||
});
|
||||
|
||||
|
123
server/components/scp/scp_exec.js
Normal file
123
server/components/scp/scp_exec.js
Normal file
|
@ -0,0 +1,123 @@
|
|||
import config from '../../config/environment';
|
||||
//var config = require('../../config/environment/development.js');
|
||||
var client = require('scp2');
|
||||
|
||||
|
||||
exports.copyFileToScriptEngine = function(sourcePath,destinationPath,ansibleEngine){
|
||||
|
||||
var connHost = ansibleEngine.ansibleHost || config.scriptEngine.host;
|
||||
var connUser = ansibleEngine.ansibleHostUser || config.scriptEngine.user;
|
||||
var connHostPassword = ansibleEngine.ansibleHostPassword || config.scriptEngine.password;
|
||||
|
||||
var scriptEngineConfig = {
|
||||
host: connHost,
|
||||
port: 22,
|
||||
username: connUser,
|
||||
tryKeyboard: true
|
||||
};
|
||||
|
||||
if(connHostPassword){
|
||||
scriptEngineConfig.password = connHostPassword;
|
||||
}else{
|
||||
scriptEngineConfig.privateKey = require('fs').readFileSync(config.scriptEngine.privateKey);
|
||||
}
|
||||
|
||||
scriptEngineConfig.destinationPath = destinationPath;
|
||||
var Client = require('scp2').Client;
|
||||
var cl = new Client(scriptEngineConfig);
|
||||
|
||||
cl.on('keyboard-interactive', function(name, instr, lang, prompts, cb) {
|
||||
cb([connHostPassword]);
|
||||
});
|
||||
|
||||
cl.on('error', function(error) {
|
||||
console.log("SCP Connect Error" + error);
|
||||
return error
|
||||
});
|
||||
|
||||
cl.upload(sourcePath,destinationPath,function(err) {
|
||||
if(err){
|
||||
console.error(err)
|
||||
}else{
|
||||
console.log("Successfully uploaded file")
|
||||
cl.close()
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
exports.createFileOnScriptEngine = function(contents,destinationPath,successCallback,errorCallback,ansibleEngine){
|
||||
var Client = require('scp2').Client;
|
||||
var buffer = new Buffer(contents, "utf-8");
|
||||
|
||||
if(!ansibleEngine) ansibleEngine = {};
|
||||
|
||||
var connHost = ansibleEngine.ansibleHost || config.scriptEngine.host;
|
||||
var connUser = ansibleEngine.ansibleHostUser || config.scriptEngine.user;
|
||||
var connHostPassword = ansibleEngine.ansibleHostPassword || config.scriptEngine.password;
|
||||
|
||||
var scriptEngineConfig = {
|
||||
host: connHost,
|
||||
port: 22,
|
||||
username: connUser,
|
||||
tryKeyboard: true
|
||||
};
|
||||
|
||||
if(connHostPassword){
|
||||
scriptEngineConfig.password = connHostPassword;
|
||||
}else{
|
||||
scriptEngineConfig.privateKey = require('fs').readFileSync(config.scriptEngine.privateKey);
|
||||
}
|
||||
|
||||
|
||||
var cl = new Client(scriptEngineConfig);
|
||||
|
||||
cl.on('keyboard-interactive', function(name, instr, lang, prompts, cb) {
|
||||
cb([connHostPassword]);
|
||||
});
|
||||
|
||||
cl.on('error', function(error) {
|
||||
console.log("SCP Connect Error" + error);
|
||||
errorCallback(error);
|
||||
});
|
||||
|
||||
//cl.connect(scriptEngineConfig);
|
||||
|
||||
var dirname = destinationPath.match(/(.*)[\/\\]/)[1]||'';
|
||||
|
||||
console.log("direcname = " + dirname);
|
||||
|
||||
cl.mkdir(dirname,function(err){
|
||||
if(err){
|
||||
errorCallback('Failed to create directory - ' + dirname + ' -' + err)
|
||||
return cl.close()
|
||||
}
|
||||
|
||||
cl.write({
|
||||
destination: destinationPath,
|
||||
content: buffer
|
||||
}, function(err){
|
||||
if(err){
|
||||
console.error(err);
|
||||
errorCallback(err);
|
||||
|
||||
}else{
|
||||
console.log("Success ");
|
||||
successCallback()
|
||||
}
|
||||
cl.close()
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
//exports.copyFileToScriptEngine('scp_exec.js','/tmp/ssh_tezt.js');
|
||||
/*
|
||||
exports.createFileOnScriptEngine('sdfdddddddddsfd','/tmp/testfile.txt', function(response){
|
||||
console.log("Success" + response)
|
||||
}, function(response){
|
||||
console.log("Error" + response)
|
||||
});
|
||||
*/
|
108
server/components/ssh/ssh2_exec.js
Normal file
108
server/components/ssh/ssh2_exec.js
Normal file
|
@ -0,0 +1,108 @@
|
|||
var Client = require('ssh2').Client;
|
||||
|
||||
//var exec = require('ssh-exec');
|
||||
|
||||
var config = require('../../config/environment');
|
||||
|
||||
exports.executeCommand = function(command, dataCallback,completeCallback,errorCallback, ansibleEngine, addScriptEndString){
|
||||
|
||||
/*var fs = require('filendir');
|
||||
var time = new Date().getTime();
|
||||
//var logFile = 'logs/deploy/' + logfilename;
|
||||
var logFile = logfilelocation;*/
|
||||
|
||||
var conn = new Client();
|
||||
|
||||
if(!ansibleEngine) ansibleEngine = {};
|
||||
|
||||
var connHost = ansibleEngine.ansibleHost || config.scriptEngine.host;
|
||||
var connUser = ansibleEngine.ansibleHostUser || config.scriptEngine.user;
|
||||
var connHostPassword = ansibleEngine.ansibleHostPassword || config.scriptEngine.password;
|
||||
|
||||
var scriptEngineConfig = {
|
||||
host: connHost,
|
||||
port: 22,
|
||||
username: connUser,
|
||||
tryKeyboard: true
|
||||
};
|
||||
|
||||
if(connHostPassword){
|
||||
scriptEngineConfig.password = connHostPassword;
|
||||
}else{
|
||||
scriptEngineConfig.privateKey = require('fs').readFileSync(config.scriptEngine.privateKey);
|
||||
}
|
||||
|
||||
//fs.appendFile(logFile,command);
|
||||
//console.log("Writing Command to log file =" + command)
|
||||
/*fs.writeFile(logFile,"\n",{'flag':'a'});*/
|
||||
|
||||
conn.on('keyboard-interactive', function(name, instr, lang, prompts, cb) {
|
||||
cb([connHostPassword]);
|
||||
});
|
||||
|
||||
conn.on('error', function(error) {
|
||||
console.log("SSH Connect Error" + error);
|
||||
errorCallback(error);
|
||||
});
|
||||
|
||||
conn.on('ready', function() {
|
||||
console.log('Client :: ready');
|
||||
console.log('Command :: ' + command);
|
||||
conn.exec(command, function(err, stream) {
|
||||
var callBackSent = false;
|
||||
|
||||
var result_data = "";
|
||||
var error_data = "";
|
||||
var error = false;
|
||||
|
||||
if (err) {
|
||||
console.log("Error=" + err);
|
||||
errorCallback(err);
|
||||
|
||||
}
|
||||
stream.on('close', function(code, signal) {
|
||||
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
|
||||
//completeCallback('Stream :: close :: code: ' + code + ', signal: ' + signal + '\nSCRIPT_FINISHED');
|
||||
if(addScriptEndString){
|
||||
//dataCallback call is what writes to logfile
|
||||
result_data += '\nSCRIPT_FINISHED';
|
||||
dataCallback(result_data);
|
||||
}
|
||||
|
||||
if(code !== 0){
|
||||
errorCallback(error_data)
|
||||
}else{
|
||||
completeCallback(result_data)
|
||||
}
|
||||
conn.end();
|
||||
}).on('data', function(data) {
|
||||
console.log('STDOUT: ' + data);
|
||||
result_data += data;
|
||||
if(dataCallback){
|
||||
//dataCallback(data);
|
||||
dataCallback(result_data);
|
||||
}
|
||||
|
||||
}).stderr.on('data', function(data) {
|
||||
console.log('STDERR: ' + data);
|
||||
error_data += data;
|
||||
error = true;
|
||||
//errorCallback(data);
|
||||
|
||||
});
|
||||
});
|
||||
}).connect(scriptEngineConfig);
|
||||
};
|
||||
|
||||
|
||||
//exports.executeCommand(null,'python3.4 /data/ehc-builder/scripts/vipr/python/ehc-builder/scripts/bin/main.py vro all --inputfile="configure_vmware_vro_Mumshad_Mannambeth_1468092975124.in" --logfile="configure_vmware_vro_Mumshad_Mannambeth_1468092975124"', 'logs/deploy/configure_vmware_vro_Mumshad_Mannambeth_1468092975124.log' )
|
||||
/*
|
||||
|
||||
exports.executeCommand(null,'date','testfile.log',function(response){
|
||||
console.log(response)
|
||||
},function(response){
|
||||
console.log(response)
|
||||
},function(response){
|
||||
console.log(response)
|
||||
})
|
||||
*/
|
116
server/components/upgrade/upgradetool.js
Normal file
116
server/components/upgrade/upgradetool.js
Normal file
|
@ -0,0 +1,116 @@
|
|||
/**
|
||||
* Created by mannam4 on 7/31/2016.
|
||||
*/
|
||||
var ssh2_exec = require('../ssh/ssh2_exec');
|
||||
var config = require('../../config/environment');
|
||||
|
||||
exports.getLogs = function(logfilename,successCallback,errorCallback){
|
||||
|
||||
var logFile = '/opt/ehc-builder-scripts/logs/' + logfilename;
|
||||
var command = 'cat ' +logFile;
|
||||
|
||||
var logFileData = '';
|
||||
|
||||
console.log("Command = " + command);
|
||||
|
||||
var localLogFile = 'logs/upgrade/upgrade.log';
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
function(data){
|
||||
//Partial Data
|
||||
//console.log("Data = "+ data)
|
||||
//logFileData+=data
|
||||
},
|
||||
function(data){
|
||||
//Complete Data
|
||||
//console.log("Data =" + data)
|
||||
if(data)
|
||||
logFileData = (data.toString().replace('Stream :: close :: code: 0, signal: undefined',''));
|
||||
console.log("Success Callback =" + logFileData);
|
||||
successCallback(logFileData)
|
||||
},
|
||||
function(error){
|
||||
//Error Data
|
||||
//console.log("Error =" + error)
|
||||
if(error)
|
||||
logFileData+=error;
|
||||
console.log("Error Callback =" + logFileData);
|
||||
errorCallback(logFileData)
|
||||
}
|
||||
);
|
||||
|
||||
/*var logFile = 'logs/upgrade/' + logfilename;
|
||||
var fs = require('fs');
|
||||
fs.readFile(logFile, function(err, data){
|
||||
if(err){
|
||||
errorCallback(err);
|
||||
}else{
|
||||
successCallback(data);
|
||||
}
|
||||
|
||||
});*/
|
||||
};
|
||||
|
||||
|
||||
exports.upgrade = function(user,upgradeData,logfilename,dataCallback,completeCallback,errorCallback){
|
||||
var command = '/opt/ehc-builder-scripts/bin/ozone_upgrade.sh --force --restart > /opt/ehc-builder-scripts/logs/' + logfilename + " 2> >(sed $'s,.*,\\e[31m&\\e[m,'>&1)";
|
||||
|
||||
var logFile = 'logs/upgrade/' + logfilename;
|
||||
|
||||
var fs = require('filendir');
|
||||
|
||||
fs.writeFile(logFile,command,{'flag':'a'});
|
||||
//return completeCallback(command);
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
function(data){
|
||||
//Partial Data
|
||||
//console.log("Data = "+ data)
|
||||
completeCallback(data)
|
||||
},
|
||||
function(data){
|
||||
//Complete Data
|
||||
//console.log("Data =" + data)
|
||||
completeCallback(data)
|
||||
},
|
||||
function(error){
|
||||
//Error Data
|
||||
//console.log("Error =" + error)
|
||||
errorCallback(error)
|
||||
}
|
||||
)
|
||||
|
||||
};
|
||||
|
||||
|
||||
exports.checkUpdates = function(user,dataCallback,completeCallback,errorCallback){
|
||||
var command = '/opt/ehc-builder-scripts/bin/check_updates.sh';
|
||||
|
||||
var logFile = 'logs/upgrade/check_updates.log';
|
||||
|
||||
var fs = require('filendir');
|
||||
|
||||
fs.writeFile(logFile,command,{'flag':'a'});
|
||||
//return completeCallback(command);
|
||||
|
||||
console.log("Updates command " + command);
|
||||
|
||||
ssh2_exec.executeCommand(command,
|
||||
function(data){
|
||||
//Partial Data
|
||||
//console.log("Data = "+ data)
|
||||
dataCallback(data)
|
||||
},
|
||||
function(data){
|
||||
//Complete Data
|
||||
//console.log("Data =" + data)
|
||||
completeCallback(data)
|
||||
},
|
||||
function(error){
|
||||
//Error Data
|
||||
//console.log("Error =" + error)
|
||||
errorCallback(error)
|
||||
}
|
||||
)
|
||||
|
||||
};
|
31
server/components/utils/dbutility.js
Normal file
31
server/components/utils/dbutility.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
var _ = require('underscore');
|
||||
|
||||
exports.updateDocument = function(doc, SchemaTarget, data) {
|
||||
for (var field in SchemaTarget.schema.paths) {
|
||||
if ((field !== '_id') && (field !== '__v')) {
|
||||
var newValue = getObjValue(field, data);
|
||||
if (newValue !== undefined) {
|
||||
setObjValue(field, doc, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
};
|
||||
|
||||
function getObjValue(field, data) {
|
||||
return _.reduce(field.split("."), function(obj, f) {
|
||||
if(obj) return obj[f];
|
||||
}, data);
|
||||
}
|
||||
|
||||
function setObjValue(field, data, value) {
|
||||
var fieldArr = field.split('.');
|
||||
return _.reduce(fieldArr, function(o, f, i) {
|
||||
if(i == fieldArr.length-1) {
|
||||
o[f] = value;
|
||||
} else {
|
||||
if(!o[f]) o[f] = {};
|
||||
}
|
||||
return o[f];
|
||||
}, data);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue