mirror of
https://github.com/mmumshad/ansible-playable.git
synced 2025-03-09 23:38:54 +00:00
Integrate winston logging
This commit is contained in:
parent
fc7aafa5f6
commit
7aeb99b732
3 changed files with 38 additions and 23 deletions
|
@ -61,6 +61,7 @@
|
||||||
"ssh2": "^0.5.0",
|
"ssh2": "^0.5.0",
|
||||||
"ts-loader": "^2.1.0",
|
"ts-loader": "^2.1.0",
|
||||||
"underscore": "^1.8.3",
|
"underscore": "^1.8.3",
|
||||||
|
"winston": "^2.3.1",
|
||||||
"yamljs": "^0.2.10"
|
"yamljs": "^0.2.10"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -6,6 +6,8 @@ const Q = require("q");
|
||||||
|
|
||||||
const all_commands = require('../../config/commands');
|
const all_commands = require('../../config/commands');
|
||||||
|
|
||||||
|
const logger = require('../../components/logger/winston');
|
||||||
|
|
||||||
var local_logPath = 'logs/ansible/execute/';
|
var local_logPath = 'logs/ansible/execute/';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,8 +17,12 @@ var local_logPath = 'logs/ansible/execute/';
|
||||||
* @param errorCallback
|
* @param errorCallback
|
||||||
*/
|
*/
|
||||||
exports.getLogs = function(logfilename,successCallback,errorCallback){
|
exports.getLogs = function(logfilename,successCallback,errorCallback){
|
||||||
var logFile = local_logPath + logfilename;
|
|
||||||
var fs = require('fs');
|
let logFile = local_logPath + logfilename;
|
||||||
|
|
||||||
|
logger.log('info', 'Get Logs - %s', logFile);
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
fs.readFile(logFile, function(err, data){
|
fs.readFile(logFile, function(err, data){
|
||||||
if(err){
|
if(err){
|
||||||
errorCallback(err);
|
errorCallback(err);
|
||||||
|
@ -69,10 +75,10 @@ exports.getAnsibleVersion = function(successCallback,errorCallback, ansibleEngin
|
||||||
null,
|
null,
|
||||||
function(data){
|
function(data){
|
||||||
ansibleVersionResult=data;
|
ansibleVersionResult=data;
|
||||||
console.log("Ansible Verison =" + ansibleVersionResult);
|
logger.info('Ansible Verison = %s', ansibleVersionResult);
|
||||||
ansibleVersionResult = "" + ansibleVersionResult;
|
ansibleVersionResult = "" + ansibleVersionResult;
|
||||||
var version = ansibleVersionResult.replace(/ansible (.*)[^]+/,"$1");
|
var version = ansibleVersionResult.replace(/ansible (.*)[^]+/,"$1");
|
||||||
console.log("Version=" + version);
|
logger.info("Version=" + version);
|
||||||
successCallback(version || ansibleVersionResult);
|
successCallback(version || ansibleVersionResult);
|
||||||
},
|
},
|
||||||
function(data){
|
function(data){
|
||||||
|
@ -122,7 +128,7 @@ exports.getAnsibleCommand = function(project_folder, playbook_name, inventory_fi
|
||||||
command += ' --check ';
|
command += ' --check ';
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Command= " + command);
|
logger.info('command = %s', command);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
};
|
};
|
||||||
|
@ -148,6 +154,8 @@ exports.executeAnsible = function(logfilename, project_folder, playbook_name, in
|
||||||
var time = new Date().getTime();
|
var time = new Date().getTime();
|
||||||
var logFile = local_logPath + logfilename;
|
var logFile = local_logPath + logfilename;
|
||||||
|
|
||||||
|
// Capture output of execution in a separate log file
|
||||||
|
// TODO: Improve
|
||||||
fs.writeFileSync(logFile,"Executing Ansible Playbook \n\n",{'flag':'a'});
|
fs.writeFileSync(logFile,"Executing Ansible Playbook \n\n",{'flag':'a'});
|
||||||
fs.writeFileSync(logFile," Completed \n",{'flag':'a'});
|
fs.writeFileSync(logFile," Completed \n",{'flag':'a'});
|
||||||
|
|
||||||
|
@ -159,16 +167,16 @@ exports.executeAnsible = function(logfilename, project_folder, playbook_name, in
|
||||||
//Calling datacallbcak back as the call is Asynchronous
|
//Calling datacallbcak back as the call is Asynchronous
|
||||||
//Logs are queried to check status
|
//Logs are queried to check status
|
||||||
dataCallback(response);
|
dataCallback(response);
|
||||||
console.log(response);
|
logger.info('%s', response);
|
||||||
//fs.writeFile(logFile,response,{'flag':'a'});
|
//fs.writeFile(logFile,response,{'flag':'a'});
|
||||||
fs.writeFile(logFile,"\n Executing Command =" + command + "\n" + response);
|
fs.writeFile(logFile,"\n Executing Command =" + command + "\n" + response);
|
||||||
},function(response){
|
},function(response){
|
||||||
successCallback(response);
|
successCallback(response);
|
||||||
console.log(response);
|
logger.info('%s', response);
|
||||||
//fs.writeFile(logFile,response,{'flag':'a'});
|
//fs.writeFile(logFile,response,{'flag':'a'});
|
||||||
},function(response){
|
},function(response){
|
||||||
errorCallback(response);
|
errorCallback(response);
|
||||||
console.log(response);
|
logger.error('Ansible Execution error = %s', response);
|
||||||
fs.writeFile(logFile,response,{'flag':'a'});
|
fs.writeFile(logFile,response,{'flag':'a'});
|
||||||
},ansibleEngine, true //addScriptEndString
|
},ansibleEngine, true //addScriptEndString
|
||||||
)
|
)
|
||||||
|
@ -202,7 +210,7 @@ exports.getVars = function(project_folder, inventory_file_name, host_name, dataC
|
||||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Command= " + command);
|
logger.info('Command= %s', command);
|
||||||
|
|
||||||
// Copy helper script to Ansible Engine
|
// Copy helper script to Ansible Engine
|
||||||
scp2_exec.copyFileToScriptEngine(config.paths.ansible_custom_api_local,AnsibleAPILocation,ansibleEngine,function(){
|
scp2_exec.copyFileToScriptEngine(config.paths.ansible_custom_api_local,AnsibleAPILocation,ansibleEngine,function(){
|
||||||
|
@ -233,7 +241,7 @@ exports.getRolesVars = function(project_folder, role_name, dataCallback, success
|
||||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Command= " + command);
|
logger.info('Command= %s', command);
|
||||||
|
|
||||||
scp2_exec.copyFileToScriptEngine(config.paths.ansible_custom_api_local,AnsibleAPILocation,ansibleEngine,function(){
|
scp2_exec.copyFileToScriptEngine(config.paths.ansible_custom_api_local,AnsibleAPILocation,ansibleEngine,function(){
|
||||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||||
|
@ -306,7 +314,7 @@ exports.writePlaybook = function(project_folder,playbook_file_name,playbook_file
|
||||||
|
|
||||||
var playbook_file_path = '' + project_folder + '/' + playbook_file_name + '';
|
var playbook_file_path = '' + project_folder + '/' + playbook_file_name + '';
|
||||||
|
|
||||||
console.log('playbook_file_path=' + playbook_file_path);
|
logger.info('playbook_file_path= %s', playbook_file_path);
|
||||||
|
|
||||||
scp2_exec.createFileOnScriptEngine(playbook_file_contents, playbook_file_path,
|
scp2_exec.createFileOnScriptEngine(playbook_file_contents, playbook_file_path,
|
||||||
function(){
|
function(){
|
||||||
|
@ -526,9 +534,9 @@ exports.searchRolesGithub = function(searchText, successCallback, errorCallback)
|
||||||
options.path = util.format(options.path, searchText);
|
options.path = util.format(options.path, searchText);
|
||||||
|
|
||||||
var req = https.get(options, function(res) {
|
var req = https.get(options, function(res) {
|
||||||
console.log('STATUS: ' + res.statusCode);
|
logger.info('STATUS: ' + res.statusCode);
|
||||||
console.log('HEADERS: ' + JSON.stringify(res.headers));
|
logger.info('HEADERS: ' + JSON.stringify(res.headers));
|
||||||
console.log('DATA: ' + JSON.stringify(res.data));
|
logger.info('DATA: ' + JSON.stringify(res.data));
|
||||||
|
|
||||||
// Buffer the body entirely for processing as a whole.
|
// Buffer the body entirely for processing as a whole.
|
||||||
var bodyChunks = [];
|
var bodyChunks = [];
|
||||||
|
@ -537,16 +545,16 @@ exports.searchRolesGithub = function(searchText, successCallback, errorCallback)
|
||||||
bodyChunks.push(chunk);
|
bodyChunks.push(chunk);
|
||||||
}).on('end', function() {
|
}).on('end', function() {
|
||||||
var body = Buffer.concat(bodyChunks);
|
var body = Buffer.concat(bodyChunks);
|
||||||
console.log('BODY: ' + body);
|
logger.info('BODY: ' + body);
|
||||||
|
|
||||||
var json_results = JSON.parse(body);
|
var json_results = JSON.parse(body);
|
||||||
var results = [];
|
var results = [];
|
||||||
console.log("Search Results = " + json_results.total_count);
|
logger.info("Search Results = " + json_results.total_count);
|
||||||
for(var i=0;i<json_results.total_count;i++){
|
for(var i=0;i<json_results.total_count;i++){
|
||||||
try{
|
try{
|
||||||
results.push({'name':json_results.items[i].name,'description':json_results.items[i].description,'url':json_results.items[i].clone_url,'type':'gitrepo'})
|
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){
|
}catch (e){
|
||||||
console.error(e)
|
logger.error(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -557,7 +565,7 @@ exports.searchRolesGithub = function(searchText, successCallback, errorCallback)
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', function(e) {
|
req.on('error', function(e) {
|
||||||
console.log('ERROR: ' + e.message);
|
logger.error(e.message);
|
||||||
errorCallback(e.message)
|
errorCallback(e.message)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -694,7 +702,7 @@ exports.getTagList = function(project_folder, playbook_name, inventory_file_name
|
||||||
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
command = 'export ANSIBLE_LIBRARY="' + ansibleEngine.customModules + '"; ' + command;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Command = " + command);
|
logger.info("Command = " + command);
|
||||||
|
|
||||||
// Copy temporary script and execute
|
// Copy temporary script and execute
|
||||||
scp2_exec.copyFileToScriptEngine(config.paths.ansible_list_tasks_json_local,config.paths.ansible_list_tasks_json_remote,ansibleEngine,function(response){
|
scp2_exec.copyFileToScriptEngine(config.paths.ansible_list_tasks_json_local,config.paths.ansible_list_tasks_json_remote,ansibleEngine,function(response){
|
||||||
|
@ -714,7 +722,6 @@ exports.getTagList = function(project_folder, playbook_name, inventory_file_name
|
||||||
*/
|
*/
|
||||||
exports.createFile = function(fileAbsolutePath, successCallback, errorCallback, ansibleEngine){
|
exports.createFile = function(fileAbsolutePath, successCallback, errorCallback, ansibleEngine){
|
||||||
|
|
||||||
var projectFolder = ansibleEngine.projectFolder;
|
|
||||||
var command = util.format(all_commands.general.create_file, fileAbsolutePath);
|
var command = util.format(all_commands.general.create_file, fileAbsolutePath);
|
||||||
|
|
||||||
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
ssh2_exec.executeCommand(command,null,successCallback,errorCallback,ansibleEngine);
|
||||||
|
@ -743,9 +750,7 @@ exports.getInventoryList = function(project_folder, successCallback, errorCallba
|
||||||
files = ansiblePlaybookListResults.trim().split('\n');
|
files = ansiblePlaybookListResults.trim().split('\n');
|
||||||
successCallback(files);
|
successCallback(files);
|
||||||
},
|
},
|
||||||
function(data){
|
errorCallback,
|
||||||
errorCallback(data)
|
|
||||||
},
|
|
||||||
ansibleEngine
|
ansibleEngine
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
9
server/components/logger/winston.js
Normal file
9
server/components/logger/winston.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
/**
|
||||||
|
* Created by mannam4 on 7/8/2017.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var winston = require('winston');
|
||||||
|
|
||||||
|
winston.add(winston.transports.File, {filename: './logs/server.log', json: false, colorize: true});
|
||||||
|
|
||||||
|
module.exports = winston;
|
Loading…
Add table
Add a link
Reference in a new issue