mirror of
https://github.com/mmumshad/ansible-playable.git
synced 2025-03-09 23:38:54 +00:00
Feature - Integrate System API to view logs in UI
This commit is contained in:
parent
1690470269
commit
bf6c8743c5
15 changed files with 353 additions and 26 deletions
|
@ -27,14 +27,6 @@ var routerStub = {
|
|||
patch: sinon.spy(),
|
||||
post: sinon.spy(),
|
||||
delete: sinon.spy(),
|
||||
modules: sinon.spy(),
|
||||
command: sinon.spy(),
|
||||
execute: sinon.spy(),
|
||||
project_files: sinon.spy(),
|
||||
playbook_get: sinon.spy(),
|
||||
playbook_create: sinon.spy(),
|
||||
playbook_delete: sinon.spy(),
|
||||
playbook_list: sinon.spy()
|
||||
};
|
||||
|
||||
// require the index with our stubbed out modules
|
||||
|
|
|
@ -15,6 +15,8 @@ import CustomModule from './custom_module.model';
|
|||
var ssh2_exec = require('../../components/ssh/ssh2_exec');
|
||||
var scp2_exec = require('../../components/scp/scp_exec');
|
||||
|
||||
const logger = require('../../components/logger/logger');
|
||||
|
||||
function respondWithResult(res, statusCode) {
|
||||
statusCode = statusCode || 200;
|
||||
return function(entity) {
|
||||
|
@ -88,20 +90,18 @@ export function index(req, res) {
|
|||
ansibleEngine
|
||||
);
|
||||
|
||||
/*return CustomModule.find().exec()
|
||||
.then(respondWithResult(res))
|
||||
.catch(handleError(res));*/
|
||||
}
|
||||
|
||||
// Gets a single CustomModule or a module_template from DB
|
||||
export function show(req, res) {
|
||||
console.log("Show " + req.params.custom_module);
|
||||
var ansibleEngine = req.body.ansibleEngine;
|
||||
|
||||
if(!ansibleEngine.customModules){
|
||||
return res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
|
||||
}
|
||||
|
||||
logger.info('Show custom module ');
|
||||
|
||||
var command = 'cat "' + ansibleEngine.customModules + '"/' + req.params.custom_module;
|
||||
|
||||
// If request is for module template, return module_template from default path
|
||||
|
|
11
server/api/system/index.js
Normal file
11
server/api/system/index.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var controller = require('./system.controller');
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
router.get('/logs/server', controller.serverLogs);
|
||||
router.get('/logs/api', controller.apiLogs);
|
||||
|
||||
module.exports = router;
|
49
server/api/system/index.spec.js
Normal file
49
server/api/system/index.spec.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
'use strict';
|
||||
|
||||
/* globals sinon, describe, expect, it */
|
||||
|
||||
var proxyquire = require('proxyquire').noPreserveCache();
|
||||
|
||||
var systemCtrlStub = {
|
||||
serverLogs: 'systemCtrl.serverLogs',
|
||||
apiLogs: 'systemCtrl.apiLogs'
|
||||
};
|
||||
|
||||
var routerStub = {
|
||||
get: sinon.spy(),
|
||||
serverLogs: sinon.spy(),
|
||||
apiLogs: sinon.spy()
|
||||
};
|
||||
|
||||
// require the index with our stubbed out modules
|
||||
var systemIndex = proxyquire('./index.js', {
|
||||
express: {
|
||||
Router() {
|
||||
return routerStub;
|
||||
}
|
||||
},
|
||||
'./system.controller': systemCtrlStub
|
||||
});
|
||||
|
||||
describe('System API Router:', function() {
|
||||
it('should return an express router instance', function() {
|
||||
expect(systemIndex).to.equal(routerStub);
|
||||
});
|
||||
|
||||
describe('GET /api/system/logs/server', function() {
|
||||
it('should route to system.controller.serverLogs', function() {
|
||||
expect(routerStub.get
|
||||
.withArgs('/logs/server', 'systemCtrl.serverLogs')
|
||||
).to.have.been.calledOnce;
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/system/logs/api', function() {
|
||||
it('should route to system.controller.apiLogs', function() {
|
||||
expect(routerStub.get
|
||||
.withArgs('/logs/api', 'systemCtrl.apiLogs')
|
||||
).to.have.been.calledOnce;
|
||||
});
|
||||
});
|
||||
|
||||
});
|
90
server/api/system/system.controller.js
Normal file
90
server/api/system/system.controller.js
Normal file
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* Using Rails-like standard naming convention for endpoints.
|
||||
* GET /api/system -> index
|
||||
* POST /api/system -> create
|
||||
* GET /api/system/:id -> show
|
||||
* PUT /api/system/:id -> upsert
|
||||
* PATCH /api/system/:id -> patch
|
||||
* DELETE /api/system/:id -> destroy
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import jsonpatch from 'fast-json-patch';
|
||||
import config from '../../config/environment';
|
||||
|
||||
function respondWithResult(res, statusCode) {
|
||||
statusCode = statusCode || 200;
|
||||
return function(entity) {
|
||||
if(entity) {
|
||||
return res.status(statusCode).json(entity);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
function patchUpdates(patches) {
|
||||
return function(entity) {
|
||||
try {
|
||||
// eslint-disable-next-line prefer-reflect
|
||||
jsonpatch.apply(entity, patches, /*validate*/ true);
|
||||
} catch(err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
|
||||
return entity.save();
|
||||
};
|
||||
}
|
||||
|
||||
function removeEntity(res) {
|
||||
return function(entity) {
|
||||
if(entity) {
|
||||
return entity.remove()
|
||||
.then(() => {
|
||||
res.status(204).end();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function handleEntityNotFound(res) {
|
||||
return function(entity) {
|
||||
if(!entity) {
|
||||
res.status(404).end();
|
||||
return null;
|
||||
}
|
||||
return entity;
|
||||
};
|
||||
}
|
||||
|
||||
function handleError(res, statusCode) {
|
||||
statusCode = statusCode || 500;
|
||||
return function(err) {
|
||||
res.status(statusCode).send(err);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
exports.serverLogs = function(req,res){
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
console.log("Server logs")
|
||||
|
||||
fs.readFile(config.paths.local_server_logfile, function(err, data){
|
||||
if(err)res.status(500).send(err);
|
||||
else res.send(data);
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
exports.apiLogs = function(req,res){
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
fs.readFile(config.paths.local_express_server_logfile, function(err, data){
|
||||
if(err)res.status(500).send(err);
|
||||
else res.send(data);
|
||||
})
|
||||
|
||||
};
|
35
server/api/system/system.events.js
Normal file
35
server/api/system/system.events.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* System model events
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import {EventEmitter} from 'events';
|
||||
var SystemEvents = new EventEmitter();
|
||||
|
||||
// Set max event listeners (0 == unlimited)
|
||||
SystemEvents.setMaxListeners(0);
|
||||
|
||||
// Model events
|
||||
var events = {
|
||||
save: 'save',
|
||||
remove: 'remove'
|
||||
};
|
||||
|
||||
// Register the event emitter to the model events
|
||||
function registerEvents(System) {
|
||||
for(var e in events) {
|
||||
let event = events[e];
|
||||
System.post(e, emitEvent(event));
|
||||
}
|
||||
}
|
||||
|
||||
function emitEvent(event) {
|
||||
return function(doc) {
|
||||
SystemEvents.emit(event + ':' + doc._id, doc);
|
||||
SystemEvents.emit(event, doc);
|
||||
};
|
||||
}
|
||||
|
||||
export {registerEvents};
|
||||
export default SystemEvents;
|
57
server/api/system/system.integration.js
Normal file
57
server/api/system/system.integration.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
'use strict';
|
||||
|
||||
/* globals describe, expect, it, beforeEach, afterEach */
|
||||
|
||||
var app = require('../..');
|
||||
import request from 'supertest';
|
||||
|
||||
var newSystem;
|
||||
|
||||
describe('System API:', function() {
|
||||
describe('GET /api/system/logs/server', function() {
|
||||
var systems;
|
||||
|
||||
beforeEach(function(done) {
|
||||
request(app)
|
||||
.get('/api/system/logs/server')
|
||||
.expect(200)
|
||||
//.expect('Content-Type', /json/)
|
||||
.end((err, res) => {
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
systems = res.text;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond with String containing "Express server listening"', function() {
|
||||
expect(systems).to.contain('Express server listening');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('GET /api/system/logs/api', function() {
|
||||
var systems;
|
||||
|
||||
beforeEach(function(done) {
|
||||
request(app)
|
||||
.get('/api/system/logs/api')
|
||||
.expect(200)
|
||||
//.expect('Content-Type', /json/)
|
||||
.end((err, res) => {
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
console.log(JSON.stringify(res));
|
||||
systems = res.text;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond with String', function() {
|
||||
expect(systems).to.contain('/api/system/logs');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
|
@ -10,6 +10,7 @@ mongoose.Promise = require('bluebird');
|
|||
import config from './config/environment';
|
||||
import http from 'http';
|
||||
import seedDatabaseIfNeeded from './config/seed';
|
||||
const logger = require('./components/logger/logger');
|
||||
|
||||
// Connect to MongoDB
|
||||
mongoose.connect(config.mongo.uri, config.mongo.options);
|
||||
|
@ -27,7 +28,10 @@ require('./routes').default(app);
|
|||
// Start server
|
||||
function startServer() {
|
||||
app.angularFullstack = server.listen(config.port, config.ip, function() {
|
||||
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
|
||||
logger.info('\n');
|
||||
logger.info('---------------------------------------------------------------');
|
||||
logger.info('Express server listening on %d, in %s mode', config.port, app.get('env'));
|
||||
logger.info('---------------------------------------------------------------\n');
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import * as auth from './auth/auth.service';
|
|||
|
||||
export default function(app) {
|
||||
// Insert routes below
|
||||
app.use('/api/system', require('./api/system'));
|
||||
app.use('/api/custom_modules', auth.isAuthenticated(), require('./api/custom_module'));
|
||||
app.use('/api/ansible', auth.isAuthenticated(), require('./api/ansible'));
|
||||
app.use('/api/projects', auth.isAuthenticated(), require('./api/project'));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue