1
0
Fork 0
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:
Mumshad Mannambeth 2017-06-07 13:36:44 -04:00
commit c92f737237
273 changed files with 16964 additions and 0 deletions

6
server/.eslintrc Normal file
View file

@ -0,0 +1,6 @@
{
"extends": "../.eslintrc",
"env": {
"node": true
}
}

View file

@ -0,0 +1,814 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/ansible -> index
* POST /api/ansible -> create
* GET /api/ansible/:id -> show
* PUT /api/ansible/:id -> upsert
* PATCH /api/ansible/:id -> patch
* DELETE /api/ansible/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import Ansible from './ansible.model';
var ssh2_exec = require('../../components/ssh/ssh2_exec');
var ansibleTool = require('../../components/ansible/ansible_tool');
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);
};
}
// Creates a new Ansible in the DB
export function command(req, res) {
var command = req.body.command;
var ansibleEngine = req.body.ansibleEngine;
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
)
}
// Creates a new Ansible in the DB
export function modules(req, res) {
var ansibleEngine = req.body.ansibleEngine;
ansibleTool.getModules(function(data){
res.write(data)
},
function(data){
res.write(data);
res.end()
},
function(data){
res.write(data)
},
ansibleEngine
);
}
// Gets a single Deploy from the DB
export function getLogs(req, res) {
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(function(entity){
console.log("Getting logs " + entity.logfile);
ansibleTool.getLogs(entity.logfile,
function(successData){
return res.send(successData);
},
function(errorData){
return res.status(500).send(errorData)
}
);
return null;
})
.catch(handleError(res));
}
// Executes Ansible Play book in the backend
export function execute(req, res) {
//var inventory_file_contents = req.body.inventory_file_contents;
//var playbook_file_contents = req.body.playbook_file_contents;
var playbook_name = req.body.selectedPlaybook;
var inventory_file_name = req.body.inventory_file_name;
var tags = req.body.tags;
var limit_to_hosts = req.body.limit_to_hosts;
var verbose = req.body.verbose;
var check_mode = req.body.check_mode;
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
console.log("Check_Mode=" + check_mode);
var time = new Date().getTime();
var logfilename = 'execution_' + time;
var tags_joined = tags;
if(typeof tags === 'object')tags_joined = tags.join(',');
var limit_to_hosts_joined = limit_to_hosts;
if(typeof limit_to_hosts === 'object')limit_to_hosts_joined = limit_to_hosts.join(',');
var ansibleObject = {
logfile: logfilename,
tags: tags_joined,
limit_to_hosts: limit_to_hosts,
verbose: verbose,
host: req.body.host,
check_mode: check_mode,
selectedPlaybook: req.body.selectedPlaybook,
selectedPlay: req.body.selectedPlay,
executionType: req.body.executionType,
executionName: req.body.executionName,
executionTime: time
};
var resultSent = false;
ansibleTool.executeAnsible(logfilename, project_folder, playbook_name, inventory_file_name, tags_joined, limit_to_hosts_joined, verbose,check_mode,
function(data){
//res.write(data)
if(!resultSent){
resultSent = true;
return Ansible.create(ansibleObject)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
},
function(data){
//res.write(data);
//res.end()
if(!resultSent){
resultSent = true;
return Ansible.create(ansibleObject)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
},
function(data){
//res.write(data)
if(!resultSent){
resultSent = true;
res.status(500).send(data)
}
},
ansibleEngine
);
}
/**
* List playbook tags
* ansible-playbook playbook.yml -i inventory --list-tags
* @param req
* @param res
*/
export function playbook_tags_list(req, res) {
var playbook_name = req.body.selectedPlaybook;
var inventory_file_name = req.body.inventory_file_name;
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getTagList(project_folder, playbook_name, inventory_file_name,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function playbook_create(req, res) {
var playbook_file_contents = req.body.playbookFileContents;
var ansibleEngine = req.body.ansibleEngine;
var play_book_name = req.body.playbookName;
var project_folder = ansibleEngine.projectFolder;
play_book_name = play_book_name.replace(project_folder,'');
console.log("Playbook name = " + play_book_name);
var resultSent = false;
ansibleTool.writePlaybook(project_folder,play_book_name,playbook_file_contents,
function(data){
//res.write(data);
//res.end()
if(!resultSent){
resultSent = true;
res.send(data)
}
},
function(data){
//res.write(data)
if(!resultSent){
resultSent = true;
res.status(500).send(data)
}
},
ansibleEngine
);
}
export function playbook_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var play_book_name = req.body.playbookName;
var project_folder = ansibleEngine.projectFolder;
var resultSent = false;
ansibleTool.deletePlaybook(project_folder,play_book_name,
function(data){
res.write(data)
},
function(data){
if(!resultSent){
resultSent = true;
res.write(data);
res.end();
}
},
function(data){
if(!resultSent){
resultSent = true;
res.status(500);
res.write(data);
res.end();
}
},
ansibleEngine
);
}
export function playbook_get(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var play_book_name = req.body.playbookName;
var project_folder = ansibleEngine.projectFolder;
var resultSent = false;
ansibleTool.readPlaybook(project_folder,play_book_name,
function(data){
res.write(data)
},
function(data){
if(!resultSent){
resultSent = true;
res.write(data);
res.end();
}
},
function(data){
if(!resultSent){
resultSent = true;
res.status(500);
res.write(data);
res.end();
}
},
ansibleEngine
);
}
export function playbook_list(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getPlaybookList(project_folder,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_list(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getRolesList(project_folder,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function inventory_list(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getInventoryList(project_folder,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function inventory_get(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
var inventoryName = req.body.inventoryName;
ansibleTool.readInventoryFile(project_folder,inventoryName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function inventory_create(req, res) {
var inventoryFileContents = req.body.inventoryFileContents;
var ansibleEngine = req.body.ansibleEngine;
var inventoryName = req.body.inventoryName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/' + inventoryName;
ansibleTool.writeFile(file_path,inventoryFileContents,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function inventory_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var inventoryName = req.body.inventoryName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/' + inventoryName;
ansibleTool.deleteFile(file_path,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function update_groups_vars_file(req, res) {
var groupVarsContents = req.body.groupVarsContents;
var ansibleEngine = req.body.ansibleEngine;
var groupName = req.body.groupName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/group_vars/' + groupName;
ansibleTool.writeFile(file_path, groupVarsContents,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function get_groups_vars_file(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var groupName = req.body.groupName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/group_vars/' + groupName;
ansibleTool.readFile(file_path,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function update_hosts_vars_file(req, res) {
var hostVarsContents = req.body.hostVarsContents;
var ansibleEngine = req.body.ansibleEngine;
var hostName = req.body.hostName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/host_vars/' + hostName;
ansibleTool.writeFile(file_path, hostVarsContents,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function get_hosts_vars_file(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var hostName = req.body.hostName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/host_vars/' + hostName;
ansibleTool.readFile(file_path,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
/**
* Get variables for a host using Python AnsibleAPI
* @param req
* @param res
*/
export function get_hosts_vars(req,res){
var ansibleEngine = req.body.ansibleEngine;
var host_name = req.body.hostName;
var project_folder = ansibleEngine.projectFolder;
var inventory_file_name = req.body.inventoryFileName;
console.log('hostName=' + host_name)
ansibleTool.getVars(project_folder,inventory_file_name,host_name,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine)
}
/**
* Get variables for a role using Python AnsibleAPI
* @param req
* @param res
*/
export function get_roles_vars(req,res){
var ansibleEngine = req.body.ansibleEngine;
var role_name = req.body.roleName;
var project_folder = ansibleEngine.projectFolder;
console.log('roleName=' + role_name);
ansibleTool.getRolesVars(project_folder,role_name,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine)
}
export function roles_search_galaxy(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var searchText = req.body.searchText;
ansibleTool.searchRolesGalaxy(searchText,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_search_github(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var searchText = req.body.searchText;
ansibleTool.searchRolesGithub(searchText,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
/**
* Create/Copy Role
* Create a new role if selectedRoleName is null
* Copy existing role if selectedRoleName is not null
* @param req
* @param res
*/
export function roles_create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleName = req.body.roleName;
var selectedRoleName = req.body.selectedRoleName;
var createRoleFunction = ansibleTool.createRole;
if(selectedRoleName)
createRoleFunction = ansibleTool.copyRole;
createRoleFunction(roleName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine,
selectedRoleName
);
}
export function roles_import(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleType = req.body.roleType;
var roleNameUri = req.body.roleNameUri;
ansibleTool.importRole(roleType,roleNameUri,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleName = req.body.roleName;
ansibleTool.deleteRole(roleName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_files(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleName = req.body.roleName;
ansibleTool.getRoleFiles(roleName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function project_files(req, res) {
var ansibleEngine = req.body.ansibleEngine;
ansibleTool.getProjectFiles(
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function file_create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var fileAbsolutePath = req.body.fileAbsolutePath;
ansibleTool.createFile(fileAbsolutePath,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function file_update(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var fileAbsolutePath = req.body.fileAbsolutePath;
var fileContents = req.body.fileContents;
ansibleTool.writeFile(fileAbsolutePath,fileContents,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function file_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var fileAbsolutePath = req.body.fileAbsolutePath;
ansibleTool.deleteFile(fileAbsolutePath,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
// Gets a list of Ansibles
export function index(req, res) {
return Ansible.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single Ansible from the DB
export function show(req, res) {
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new Ansible in the DB
export function create(req, res) {
return Ansible.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
// Upserts the given Ansible in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Ansible.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing Ansible in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a Ansible from the DB
export function destroy(req, res) {
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* Ansible model events
*/
'use strict';
import {EventEmitter} from 'events';
var AnsibleEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
AnsibleEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(Ansible) {
for(var e in events) {
let event = events[e];
Ansible.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
AnsibleEvents.emit(event + ':' + doc._id, doc);
AnsibleEvents.emit(event, doc);
};
}
export {registerEvents};
export default AnsibleEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newAnsible;
describe('Ansible API:', function() {
describe('GET /api/ansible', function() {
var ansibles;
beforeEach(function(done) {
request(app)
.get('/api/ansible')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansibles = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(ansibles).to.be.instanceOf(Array);
});
});
describe('POST /api/ansible', function() {
beforeEach(function(done) {
request(app)
.post('/api/ansible')
.send({
name: 'New Ansible',
info: 'This is the brand new ansible!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newAnsible = res.body;
done();
});
});
it('should respond with the newly created ansible', function() {
expect(newAnsible.name).to.equal('New Ansible');
expect(newAnsible.info).to.equal('This is the brand new ansible!!!');
});
});
describe('GET /api/ansible/:id', function() {
var ansible;
beforeEach(function(done) {
request(app)
.get(`/api/ansible/${newAnsible._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansible = res.body;
done();
});
});
afterEach(function() {
ansible = {};
});
it('should respond with the requested ansible', function() {
expect(ansible.name).to.equal('New Ansible');
expect(ansible.info).to.equal('This is the brand new ansible!!!');
});
});
describe('PUT /api/ansible/:id', function() {
var updatedAnsible;
beforeEach(function(done) {
request(app)
.put(`/api/ansible/${newAnsible._id}`)
.send({
name: 'Updated Ansible',
info: 'This is the updated ansible!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedAnsible = res.body;
done();
});
});
afterEach(function() {
updatedAnsible = {};
});
it('should respond with the updated ansible', function() {
expect(updatedAnsible.name).to.equal('Updated Ansible');
expect(updatedAnsible.info).to.equal('This is the updated ansible!!!');
});
it('should respond with the updated ansible on a subsequent GET', function(done) {
request(app)
.get(`/api/ansible/${newAnsible._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let ansible = res.body;
expect(ansible.name).to.equal('Updated Ansible');
expect(ansible.info).to.equal('This is the updated ansible!!!');
done();
});
});
});
describe('PATCH /api/ansible/:id', function() {
var patchedAnsible;
beforeEach(function(done) {
request(app)
.patch(`/api/ansible/${newAnsible._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Ansible' },
{ op: 'replace', path: '/info', value: 'This is the patched ansible!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedAnsible = res.body;
done();
});
});
afterEach(function() {
patchedAnsible = {};
});
it('should respond with the patched ansible', function() {
expect(patchedAnsible.name).to.equal('Patched Ansible');
expect(patchedAnsible.info).to.equal('This is the patched ansible!!!');
});
});
describe('DELETE /api/ansible/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/ansible/${newAnsible._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when ansible does not exist', function(done) {
request(app)
.delete(`/api/ansible/${newAnsible._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,24 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './ansible.events';
var AnsibleSchema = new mongoose.Schema({
name: String,
info: String,
active: Boolean,
logfile: String,
tags: String,
limit_to_hosts: String,
host: String,
verbose: String,
check_mode: Boolean,
selectedPlaybook: String,
selectedPlay: String,
executionType: String,
executionName: String,
executionTime: Date
});
registerEvents(AnsibleSchema);
export default mongoose.model('Ansible', AnsibleSchema);

View file

@ -0,0 +1,59 @@
'use strict';
var express = require('express');
var controller = require('./ansible.controller');
var router = express.Router();
router.get('/', controller.index);
router.post('/modules', controller.modules);
router.post('/command', controller.command);
router.post('/execute', controller.execute);
router.post('/project/files', controller.project_files);
router.post('/playbook/get', controller.playbook_get);
router.post('/playbook/create', controller.playbook_create);
router.post('/playbook/delete', controller.playbook_delete);
router.post('/playbook/list', controller.playbook_list);
router.post('/roles/create', controller.roles_create);
router.post('/roles/list', controller.roles_list);
router.post('/roles/search/galaxy', controller.roles_search_galaxy);
router.post('/roles/search/github', controller.roles_search_github);
router.post('/roles/delete', controller.roles_delete);
router.post('/roles/files', controller.roles_files);
router.post('/roles/import', controller.roles_import);
router.post('/tags/list', controller.playbook_tags_list);
router.post('/files/create', controller.file_create);
router.post('/files/update', controller.file_update);
router.post('/files/delete', controller.file_delete);
router.post('/inventory/list', controller.inventory_list);
router.post('/inventory/get', controller.inventory_get);
router.post('/inventory/create', controller.inventory_create);
router.post('/inventory/delete', controller.inventory_delete);
router.post('/vars_file/groups/update', controller.update_groups_vars_file);
router.post('/vars_file/groups/get', controller.get_groups_vars_file);
router.post('/vars_file/hosts/update', controller.update_hosts_vars_file);
router.post('/vars_file/hosts/get', controller.get_hosts_vars_file);
router.post('/vars/hosts/get', controller.get_hosts_vars);
router.post('/vars/roles/get', controller.get_roles_vars);
router.get('/logs/:id', controller.getLogs);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var ansibleCtrlStub = {
index: 'ansibleCtrl.index',
show: 'ansibleCtrl.show',
create: 'ansibleCtrl.create',
upsert: 'ansibleCtrl.upsert',
patch: 'ansibleCtrl.patch',
destroy: 'ansibleCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var ansibleIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./ansible.controller': ansibleCtrlStub
});
describe('Ansible API Router:', function() {
it('should return an express router instance', function() {
expect(ansibleIndex).to.equal(routerStub);
});
describe('GET /api/ansible', function() {
it('should route to ansible.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'ansibleCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/ansible/:id', function() {
it('should route to ansible.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'ansibleCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/ansible', function() {
it('should route to ansible.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'ansibleCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/ansible/:id', function() {
it('should route to ansible.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'ansibleCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/ansible/:id', function() {
it('should route to ansible.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'ansibleCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/ansible/:id', function() {
it('should route to ansible.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'ansibleCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,220 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/custom_modules -> index
* POST /api/custom_modules -> create
* GET /api/custom_modules/:id -> show
* PUT /api/custom_modules/:id -> upsert
* PATCH /api/custom_modules/:id -> patch
* DELETE /api/custom_modules/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import CustomModule from './custom_module.model';
var ssh2_exec = require('../../components/ssh/ssh2_exec');
var scp2_exec = require('../../components/scp/scp_exec');
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);
};
}
// Gets a list of CustomModules
export function index(req, res) {
var ansibleEngine = req.body.ansibleEngine;
if(!ansibleEngine.customModules){
return res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
var command = 'ls "' + ansibleEngine.customModules + '"';
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
/*return CustomModule.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));*/
}
// Gets a single CustomModule from the DB
export function show(req, res) {
console.log("Show " + req.params.custom_module);
var ansibleEngine = req.body.ansibleEngine;
if(!ansibleEngine.customModules){
res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
var command = 'cat "' + ansibleEngine.customModules + '"/' + req.params.custom_module;
if(req.params.custom_module === 'template.py'){
command = 'cat ' + '/opt/ehc-builder-scripts/ansible_modules/template.py';
}
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
/*return CustomModule.findById(req.params.custom_module).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));*/
}
// Test Module
export function testModule(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var moduleArgs = req.body.moduleArgs;
if(!ansibleEngine.customModules){
res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
var command = '/opt/ansible/ansible-devel/hacking/test-module -m "' + ansibleEngine.customModules + '/' + req.params.custom_module + "\" -a '" + JSON.stringify(moduleArgs) + "'";
console.log("Command=" + command);
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
/*return CustomModule.findById(req.params.custom_module).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));*/
}
// Creates a new CustomModule in the DB
export function create(req, res) {
console.log("Create");
var custom_module_name = req.params.custom_module;
var custom_module_code = req.body.custom_module_code;
var ansibleEngine = req.body.ansibleEngine;
if(!ansibleEngine.customModules){
res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
console.log("Custom module name " + "\"" + ansibleEngine.customModules + '/' + custom_module_name + "\"")
scp2_exec.createFileOnScriptEngine(custom_module_code,ansibleEngine.customModules + '/' + custom_module_name,
function(){
res.send("Saved")
},function(err){
res.status(500).send("Failed to create file on target")
},
ansibleEngine
);
/*return CustomModule.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));*/
}
// Upserts the given CustomModule in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return CustomModule.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing CustomModule in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return CustomModule.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a CustomModule from the DB
export function destroy(req, res) {
return CustomModule.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* CustomModule model events
*/
'use strict';
import {EventEmitter} from 'events';
var CustomModuleEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
CustomModuleEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(CustomModule) {
for(var e in events) {
let event = events[e];
CustomModule.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
CustomModuleEvents.emit(event + ':' + doc._id, doc);
CustomModuleEvents.emit(event, doc);
};
}
export {registerEvents};
export default CustomModuleEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newCustomModule;
describe('CustomModule API:', function() {
describe('GET /api/custom_modules', function() {
var customModules;
beforeEach(function(done) {
request(app)
.get('/api/custom_modules')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
customModules = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(customModules).to.be.instanceOf(Array);
});
});
describe('POST /api/custom_modules', function() {
beforeEach(function(done) {
request(app)
.post('/api/custom_modules')
.send({
name: 'New CustomModule',
info: 'This is the brand new customModule!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newCustomModule = res.body;
done();
});
});
it('should respond with the newly created customModule', function() {
expect(newCustomModule.name).to.equal('New CustomModule');
expect(newCustomModule.info).to.equal('This is the brand new customModule!!!');
});
});
describe('GET /api/custom_modules/:id', function() {
var customModule;
beforeEach(function(done) {
request(app)
.get(`/api/custom_modules/${newCustomModule._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
customModule = res.body;
done();
});
});
afterEach(function() {
customModule = {};
});
it('should respond with the requested customModule', function() {
expect(customModule.name).to.equal('New CustomModule');
expect(customModule.info).to.equal('This is the brand new customModule!!!');
});
});
describe('PUT /api/custom_modules/:id', function() {
var updatedCustomModule;
beforeEach(function(done) {
request(app)
.put(`/api/custom_modules/${newCustomModule._id}`)
.send({
name: 'Updated CustomModule',
info: 'This is the updated customModule!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedCustomModule = res.body;
done();
});
});
afterEach(function() {
updatedCustomModule = {};
});
it('should respond with the updated customModule', function() {
expect(updatedCustomModule.name).to.equal('Updated CustomModule');
expect(updatedCustomModule.info).to.equal('This is the updated customModule!!!');
});
it('should respond with the updated customModule on a subsequent GET', function(done) {
request(app)
.get(`/api/custom_modules/${newCustomModule._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let customModule = res.body;
expect(customModule.name).to.equal('Updated CustomModule');
expect(customModule.info).to.equal('This is the updated customModule!!!');
done();
});
});
});
describe('PATCH /api/custom_modules/:id', function() {
var patchedCustomModule;
beforeEach(function(done) {
request(app)
.patch(`/api/custom_modules/${newCustomModule._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched CustomModule' },
{ op: 'replace', path: '/info', value: 'This is the patched customModule!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedCustomModule = res.body;
done();
});
});
afterEach(function() {
patchedCustomModule = {};
});
it('should respond with the patched customModule', function() {
expect(patchedCustomModule.name).to.equal('Patched CustomModule');
expect(patchedCustomModule.info).to.equal('This is the patched customModule!!!');
});
});
describe('DELETE /api/custom_modules/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/custom_modules/${newCustomModule._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when customModule does not exist', function(done) {
request(app)
.delete(`/api/custom_modules/${newCustomModule._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,13 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './custom_module.events';
var CustomModuleSchema = new mongoose.Schema({
name: String,
info: String,
active: Boolean
});
registerEvents(CustomModuleSchema);
export default mongoose.model('CustomModule', CustomModuleSchema);

View file

@ -0,0 +1,16 @@
'use strict';
var express = require('express');
var controller = require('./custom_module.controller');
var router = express.Router();
router.post('/query', controller.index);
router.post('/:custom_module/test', controller.testModule);
router.post('/:custom_module/get', controller.show);
router.post('/:custom_module', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var customModuleCtrlStub = {
index: 'customModuleCtrl.index',
show: 'customModuleCtrl.show',
create: 'customModuleCtrl.create',
upsert: 'customModuleCtrl.upsert',
patch: 'customModuleCtrl.patch',
destroy: 'customModuleCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var customModuleIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./custom_module.controller': customModuleCtrlStub
});
describe('CustomModule API Router:', function() {
it('should return an express router instance', function() {
expect(customModuleIndex).to.equal(routerStub);
});
describe('GET /api/custom_modules', function() {
it('should route to customModule.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'customModuleCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/custom_modules/:id', function() {
it('should route to customModule.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'customModuleCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/custom_modules', function() {
it('should route to customModule.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'customModuleCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/custom_modules/:id', function() {
it('should route to customModule.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'customModuleCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/custom_modules/:id', function() {
it('should route to customModule.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'customModuleCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/custom_modules/:id', function() {
it('should route to customModule.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'customModuleCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,15 @@
'use strict';
var express = require('express');
var controller = require('./project.controller');
var router = express.Router();
router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var projectCtrlStub = {
index: 'projectCtrl.index',
show: 'projectCtrl.show',
create: 'projectCtrl.create',
upsert: 'projectCtrl.upsert',
patch: 'projectCtrl.patch',
destroy: 'projectCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var projectIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./project.controller': projectCtrlStub
});
describe('Project API Router:', function() {
it('should return an express router instance', function() {
expect(projectIndex).to.equal(routerStub);
});
describe('GET /api/projects', function() {
it('should route to project.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'projectCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/projects/:id', function() {
it('should route to project.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'projectCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/projects', function() {
it('should route to project.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'projectCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/projects/:id', function() {
it('should route to project.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'projectCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/projects/:id', function() {
it('should route to project.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'projectCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/projects/:id', function() {
it('should route to project.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'projectCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,152 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/projects -> index
* POST /api/projects -> create
* GET /api/projects/:id -> show
* PUT /api/projects/:id -> upsert
* PATCH /api/projects/:id -> patch
* DELETE /api/projects/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import Project from './project.model';
var ansibleTool = require('../../components/ansible/ansible_tool');
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);
};
}
// Gets a list of Projects
export function index(req, res) {
console.log("Getting projects list");
return Project.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single Project from the DB
export function show(req, res) {
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new Project in the DB
export function create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
console.log("Ansible Engine " + JSON.stringify(ansibleEngine));
if(ansibleEngine.ansibleHost){
ansibleTool.getAnsibleVersion(
function(version){
req.body.ansibleVersion = version;
ansibleTool.createProjectFolder(ansibleEngine.projectFolder,
function(){
return Project.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
},
function(data){
res.status(500).send(data)
}, ansibleEngine);
//res.write(data);
//res.end()
},
function(data){
res.status(500).send("" + data);
},ansibleEngine
)
}else{
return Project.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
}
// Upserts the given Project in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Project.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing Project in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a Project from the DB
export function destroy(req, res) {
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* Project model events
*/
'use strict';
import {EventEmitter} from 'events';
var ProjectEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
ProjectEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(Project) {
for(var e in events) {
let event = events[e];
Project.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
ProjectEvents.emit(event + ':' + doc._id, doc);
ProjectEvents.emit(event, doc);
};
}
export {registerEvents};
export default ProjectEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newProject;
describe('Project API:', function() {
describe('GET /api/projects', function() {
var projects;
beforeEach(function(done) {
request(app)
.get('/api/projects')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
projects = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(projects).to.be.instanceOf(Array);
});
});
describe('POST /api/projects', function() {
beforeEach(function(done) {
request(app)
.post('/api/projects')
.send({
name: 'New Project',
info: 'This is the brand new project!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newProject = res.body;
done();
});
});
it('should respond with the newly created project', function() {
expect(newProject.name).to.equal('New Project');
expect(newProject.info).to.equal('This is the brand new project!!!');
});
});
describe('GET /api/projects/:id', function() {
var project;
beforeEach(function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
project = res.body;
done();
});
});
afterEach(function() {
project = {};
});
it('should respond with the requested project', function() {
expect(project.name).to.equal('New Project');
expect(project.info).to.equal('This is the brand new project!!!');
});
});
describe('PUT /api/projects/:id', function() {
var updatedProject;
beforeEach(function(done) {
request(app)
.put(`/api/projects/${newProject._id}`)
.send({
name: 'Updated Project',
info: 'This is the updated project!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedProject = res.body;
done();
});
});
afterEach(function() {
updatedProject = {};
});
it('should respond with the updated project', function() {
expect(updatedProject.name).to.equal('Updated Project');
expect(updatedProject.info).to.equal('This is the updated project!!!');
});
it('should respond with the updated project on a subsequent GET', function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let project = res.body;
expect(project.name).to.equal('Updated Project');
expect(project.info).to.equal('This is the updated project!!!');
done();
});
});
});
describe('PATCH /api/projects/:id', function() {
var patchedProject;
beforeEach(function(done) {
request(app)
.patch(`/api/projects/${newProject._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Project' },
{ op: 'replace', path: '/info', value: 'This is the patched project!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedProject = res.body;
done();
});
});
afterEach(function() {
patchedProject = {};
});
it('should respond with the patched project', function() {
expect(patchedProject.name).to.equal('Patched Project');
expect(patchedProject.info).to.equal('This is the patched project!!!');
});
});
describe('DELETE /api/projects/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when project does not exist', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,22 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './project.events';
var ProjectSchema = new mongoose.Schema({
name: String,
ansibleEngine: {},
ansibleVersion : String,
creationTime: Date,
info: String,
active: Boolean,
ansible_data: String, //YAML Format
ansible_data_json: {}, //JSON Format
inventory_data: String, //YAML Format
inventory_data_json: {}, //JSON Format
roles_data: String, //YAML Format
roles_data_json: {} //JSON Format
});
registerEvents(ProjectSchema);
export default mongoose.model('Project', ProjectSchema);

15
server/api/thing/index.js Normal file
View file

@ -0,0 +1,15 @@
'use strict';
var express = require('express');
var controller = require('./thing.controller');
var router = express.Router();
router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var thingCtrlStub = {
index: 'thingCtrl.index',
show: 'thingCtrl.show',
create: 'thingCtrl.create',
upsert: 'thingCtrl.upsert',
patch: 'thingCtrl.patch',
destroy: 'thingCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var thingIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./thing.controller': thingCtrlStub
});
describe('Thing API Router:', function() {
it('should return an express router instance', function() {
expect(thingIndex).to.equal(routerStub);
});
describe('GET /api/things', function() {
it('should route to thing.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'thingCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/things/:id', function() {
it('should route to thing.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'thingCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/things', function() {
it('should route to thing.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'thingCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/things/:id', function() {
it('should route to thing.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'thingCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/things/:id', function() {
it('should route to thing.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'thingCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/things/:id', function() {
it('should route to thing.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'thingCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,118 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/things -> index
* POST /api/things -> create
* GET /api/things/:id -> show
* PUT /api/things/:id -> upsert
* PATCH /api/things/:id -> patch
* DELETE /api/things/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import Thing from './thing.model';
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);
};
}
// Gets a list of Things
export function index(req, res) {
return Thing.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single Thing from the DB
export function show(req, res) {
return Thing.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new Thing in the DB
export function create(req, res) {
return Thing.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
// Upserts the given Thing in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Thing.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing Thing in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Thing.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a Thing from the DB
export function destroy(req, res) {
return Thing.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* Thing model events
*/
'use strict';
import {EventEmitter} from 'events';
var ThingEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
ThingEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(Thing) {
for(var e in events) {
let event = events[e];
Thing.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
ThingEvents.emit(`${event}:${doc._id}`, doc);
ThingEvents.emit(event, doc);
};
}
export {registerEvents};
export default ThingEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newThing;
describe('Thing API:', function() {
describe('GET /api/things', function() {
var things;
beforeEach(function(done) {
request(app)
.get('/api/things')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
things = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(things).to.be.instanceOf(Array);
});
});
describe('POST /api/things', function() {
beforeEach(function(done) {
request(app)
.post('/api/things')
.send({
name: 'New Thing',
info: 'This is the brand new thing!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newThing = res.body;
done();
});
});
it('should respond with the newly created thing', function() {
expect(newThing.name).to.equal('New Thing');
expect(newThing.info).to.equal('This is the brand new thing!!!');
});
});
describe('GET /api/things/:id', function() {
var thing;
beforeEach(function(done) {
request(app)
.get(`/api/things/${newThing._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
thing = res.body;
done();
});
});
afterEach(function() {
thing = {};
});
it('should respond with the requested thing', function() {
expect(thing.name).to.equal('New Thing');
expect(thing.info).to.equal('This is the brand new thing!!!');
});
});
describe('PUT /api/things/:id', function() {
var updatedThing;
beforeEach(function(done) {
request(app)
.put(`/api/things/${newThing._id}`)
.send({
name: 'Updated Thing',
info: 'This is the updated thing!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedThing = res.body;
done();
});
});
afterEach(function() {
updatedThing = {};
});
it('should respond with the updated thing', function() {
expect(updatedThing.name).to.equal('Updated Thing');
expect(updatedThing.info).to.equal('This is the updated thing!!!');
});
it('should respond with the updated thing on a subsequent GET', function(done) {
request(app)
.get(`/api/things/${newThing._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let thing = res.body;
expect(thing.name).to.equal('Updated Thing');
expect(thing.info).to.equal('This is the updated thing!!!');
done();
});
});
});
describe('PATCH /api/things/:id', function() {
var patchedThing;
beforeEach(function(done) {
request(app)
.patch(`/api/things/${newThing._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Thing' },
{ op: 'replace', path: '/info', value: 'This is the patched thing!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedThing = res.body;
done();
});
});
afterEach(function() {
patchedThing = {};
});
it('should respond with the patched thing', function() {
expect(patchedThing.name).to.equal('Patched Thing');
expect(patchedThing.info).to.equal('This is the patched thing!!!');
});
});
describe('DELETE /api/things/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/things/${newThing._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when thing does not exist', function(done) {
request(app)
.delete(`/api/things/${newThing._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,13 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './thing.events';
var ThingSchema = new mongoose.Schema({
name: String,
info: String,
active: Boolean
});
registerEvents(ThingSchema);
export default mongoose.model('Thing', ThingSchema);

16
server/api/user/index.js Normal file
View file

@ -0,0 +1,16 @@
'use strict';
import {Router} from 'express';
import * as controller from './user.controller';
import * as auth from '../../auth/auth.service';
var router = new Router();
router.get('/', auth.hasRole('admin'), controller.index);
router.delete('/:id', auth.hasRole('admin'), controller.destroy);
router.get('/me', auth.isAuthenticated(), controller.me);
router.put('/:id/password', auth.isAuthenticated(), controller.changePassword);
router.get('/:id', auth.isAuthenticated(), controller.show);
router.post('/', controller.create);
module.exports = router;

View file

@ -0,0 +1,95 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var userCtrlStub = {
index: 'userCtrl.index',
destroy: 'userCtrl.destroy',
me: 'userCtrl.me',
changePassword: 'userCtrl.changePassword',
show: 'userCtrl.show',
create: 'userCtrl.create'
};
var authServiceStub = {
isAuthenticated() {
return 'authService.isAuthenticated';
},
hasRole(role) {
return `authService.hasRole.${role}`;
}
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var userIndex = proxyquire('./index', {
express: {
Router() {
return routerStub;
}
},
'./user.controller': userCtrlStub,
'../../auth/auth.service': authServiceStub
});
describe('User API Router:', function() {
it('should return an express router instance', function() {
expect(userIndex).to.equal(routerStub);
});
describe('GET /api/users', function() {
it('should verify admin role and route to user.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/users/:id', function() {
it('should verify admin role and route to user.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy')
).to.have.been.calledOnce;
});
});
describe('GET /api/users/me', function() {
it('should be authenticated and route to user.controller.me', function() {
expect(routerStub.get
.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me')
).to.have.been.calledOnce;
});
});
describe('PUT /api/users/:id/password', function() {
it('should be authenticated and route to user.controller.changePassword', function() {
expect(routerStub.put
.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword')
).to.have.been.calledOnce;
});
});
describe('GET /api/users/:id', function() {
it('should be authenticated and route to user.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/users', function() {
it('should route to user.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'userCtrl.create')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,122 @@
'use strict';
import User from './user.model';
import config from '../../config/environment';
import jwt from 'jsonwebtoken';
function validationError(res, statusCode) {
statusCode = statusCode || 422;
return function(err) {
return res.status(statusCode).json(err);
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
return res.status(statusCode).send(err);
};
}
/**
* Get list of users
* restriction: 'admin'
*/
export function index(req, res) {
return User.find({}, '-salt -password').exec()
.then(users => {
res.status(200).json(users);
})
.catch(handleError(res));
}
/**
* Creates a new user
*/
export function create(req, res) {
var newUser = new User(req.body);
newUser.provider = 'local';
newUser.role = 'user';
newUser.save()
.then(function(user) {
var token = jwt.sign({ _id: user._id }, config.secrets.session, {
expiresIn: 60 * 60 * 5
});
res.json({ token });
})
.catch(validationError(res));
}
/**
* Get a single user
*/
export function show(req, res, next) {
var userId = req.params.id;
return User.findById(userId).exec()
.then(user => {
if(!user) {
return res.status(404).end();
}
res.json(user.profile);
})
.catch(err => next(err));
}
/**
* Deletes a user
* restriction: 'admin'
*/
export function destroy(req, res) {
return User.findByIdAndRemove(req.params.id).exec()
.then(function() {
res.status(204).end();
})
.catch(handleError(res));
}
/**
* Change a users password
*/
export function changePassword(req, res) {
var userId = req.user._id;
var oldPass = String(req.body.oldPassword);
var newPass = String(req.body.newPassword);
return User.findById(userId).exec()
.then(user => {
if(user.authenticate(oldPass)) {
user.password = newPass;
return user.save()
.then(() => {
res.status(204).end();
})
.catch(validationError(res));
} else {
return res.status(403).end();
}
});
}
/**
* Get my info
*/
export function me(req, res, next) {
var userId = req.user._id;
return User.findOne({ _id: userId }, '-salt -password').exec()
.then(user => { // don't ever give out the password or salt
if(!user) {
return res.status(401).end();
}
res.json(user);
})
.catch(err => next(err));
}
/**
* Authentication callback
*/
export function authCallback(req, res) {
res.redirect('/');
}

View file

@ -0,0 +1,35 @@
/**
* User model events
*/
'use strict';
import {EventEmitter} from 'events';
var UserEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
UserEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(User) {
for(var e in events) {
let event = events[e];
User.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
UserEvents.emit(`${event}:${doc._id}`, doc);
UserEvents.emit(event, doc);
};
}
export {registerEvents};
export default UserEvents;

View file

@ -0,0 +1,67 @@
'use strict';
/* globals describe, expect, it, before, after, beforeEach, afterEach */
import app from '../..';
import User from './user.model';
import request from 'supertest';
describe('User API:', function() {
var user;
// Clear users before testing
before(function() {
return User.remove().then(function() {
user = new User({
name: 'Fake User',
email: 'test@example.com',
password: 'password'
});
return user.save();
});
});
// Clear users after testing
after(function() {
return User.remove();
});
describe('GET /api/users/me', function() {
var token;
before(function(done) {
request(app)
.post('/auth/local')
.send({
email: 'test@example.com',
password: 'password'
})
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
token = res.body.token;
done();
});
});
it('should respond with a user profile when authenticated', function(done) {
request(app)
.get('/api/users/me')
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
expect(res.body._id.toString()).to.equal(user._id.toString());
done();
});
});
it('should respond with a 401 when not authenticated', function(done) {
request(app)
.get('/api/users/me')
.expect(401)
.end(done);
});
});
});

View file

@ -0,0 +1,258 @@
'use strict';
/*eslint no-invalid-this:0*/
import crypto from 'crypto';
mongoose.Promise = require('bluebird');
import mongoose, {Schema} from 'mongoose';
import {registerEvents} from './user.events';
const authTypes = ['github', 'twitter', 'facebook', 'google'];
var UserSchema = new Schema({
name: String,
email: {
type: String,
lowercase: true,
required() {
if(authTypes.indexOf(this.provider) === -1) {
return true;
} else {
return false;
}
}
},
role: {
type: String,
default: 'user'
},
password: {
type: String,
required() {
if(authTypes.indexOf(this.provider) === -1) {
return true;
} else {
return false;
}
}
},
provider: String,
salt: String,
facebook: {},
google: {},
github: {}
});
/**
* Virtuals
*/
// Public profile information
UserSchema
.virtual('profile')
.get(function() {
return {
name: this.name,
role: this.role
};
});
// Non-sensitive info we'll be putting in the token
UserSchema
.virtual('token')
.get(function() {
return {
_id: this._id,
role: this.role
};
});
/**
* Validations
*/
// Validate empty email
UserSchema
.path('email')
.validate(function(email) {
if(authTypes.indexOf(this.provider) !== -1) {
return true;
}
return email.length;
}, 'Email cannot be blank');
// Validate empty password
UserSchema
.path('password')
.validate(function(password) {
if(authTypes.indexOf(this.provider) !== -1) {
return true;
}
return password.length;
}, 'Password cannot be blank');
// Validate email is not taken
UserSchema
.path('email')
.validate(function(value) {
if(authTypes.indexOf(this.provider) !== -1) {
return true;
}
return this.constructor.findOne({ email: value }).exec()
.then(user => {
if(user) {
if(this.id === user.id) {
return true;
}
return false;
}
return true;
})
.catch(function(err) {
throw err;
});
}, 'The specified email address is already in use.');
var validatePresenceOf = function(value) {
return value && value.length;
};
/**
* Pre-save hook
*/
UserSchema
.pre('save', function(next) {
// Handle new/update passwords
if(!this.isModified('password')) {
return next();
}
if(!validatePresenceOf(this.password)) {
if(authTypes.indexOf(this.provider) === -1) {
return next(new Error('Invalid password'));
} else {
return next();
}
}
// Make salt with a callback
this.makeSalt((saltErr, salt) => {
if(saltErr) {
return next(saltErr);
}
this.salt = salt;
this.encryptPassword(this.password, (encryptErr, hashedPassword) => {
if(encryptErr) {
return next(encryptErr);
}
this.password = hashedPassword;
return next();
});
});
});
/**
* Methods
*/
UserSchema.methods = {
/**
* Authenticate - check if the passwords are the same
*
* @param {String} password
* @param {Function} callback
* @return {Boolean}
* @api public
*/
authenticate(password, callback) {
if(!callback) {
return this.password === this.encryptPassword(password);
}
this.encryptPassword(password, (err, pwdGen) => {
if(err) {
return callback(err);
}
if(this.password === pwdGen) {
return callback(null, true);
} else {
return callback(null, false);
}
});
},
/**
* Make salt
*
* @param {Number} [byteSize] - Optional salt byte size, default to 16
* @param {Function} callback
* @return {String}
* @api public
*/
makeSalt(...args) {
let byteSize;
let callback;
let defaultByteSize = 16;
if(typeof args[0] === 'function') {
callback = args[0];
byteSize = defaultByteSize;
} else if(typeof args[1] === 'function') {
callback = args[1];
} else {
throw new Error('Missing Callback');
}
if(!byteSize) {
byteSize = defaultByteSize;
}
return crypto.randomBytes(byteSize, (err, salt) => {
if(err) {
return callback(err);
} else {
return callback(null, salt.toString('base64'));
}
});
},
/**
* Encrypt password
*
* @param {String} password
* @param {Function} callback
* @return {String}
* @api public
*/
encryptPassword(password, callback) {
if(!password || !this.salt) {
if(!callback) {
return null;
} else {
return callback('Missing password or salt');
}
}
var defaultIterations = 10000;
var defaultKeyLength = 64;
var salt = new Buffer(this.salt, 'base64');
if(!callback) {
// eslint-disable-next-line no-sync
return crypto.pbkdf2Sync(password, salt, defaultIterations,
defaultKeyLength, 'sha1')
.toString('base64');
}
return crypto.pbkdf2(password, salt, defaultIterations, defaultKeyLength,
'sha1', (err, key) => {
if(err) {
return callback(err);
} else {
return callback(null, key.toString('base64'));
}
});
}
};
registerEvents(UserSchema);
export default mongoose.model('User', UserSchema);

View file

@ -0,0 +1,164 @@
'use strict';
import app from '../..';
import User from './user.model';
var user;
var genUser = function() {
user = new User({
provider: 'local',
name: 'Fake User',
email: 'test@example.com',
password: 'password'
});
return user;
};
describe('User Model', function() {
before(function() {
// Clear users before testing
return User.remove();
});
beforeEach(function() {
genUser();
});
afterEach(function() {
return User.remove();
});
it('should begin with no users', function() {
return expect(User.find({}).exec()).to
.eventually.have.length(0);
});
it('should fail when saving a duplicate user', function() {
return expect(user.save()
.then(function() {
var userDup = genUser();
return userDup.save();
})).to.be.rejected;
});
describe('#email', function() {
it('should fail when saving with a blank email', function() {
user.email = '';
return expect(user.save()).to.be.rejected;
});
it('should fail when saving with a null email', function() {
user.email = null;
return expect(user.save()).to.be.rejected;
});
it('should fail when saving without an email', function() {
user.email = undefined;
return expect(user.save()).to.be.rejected;
});
describe('given user provider is google', function() {
beforeEach(function() {
user.provider = 'google';
});
it('should succeed when saving without an email', function() {
user.email = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is facebook', function() {
beforeEach(function() {
user.provider = 'facebook';
});
it('should succeed when saving without an email', function() {
user.email = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without an email', function() {
user.email = null;
return expect(user.save()).to.be.fulfilled;
});
});
});
describe('#password', function() {
it('should fail when saving with a blank password', function() {
user.password = '';
return expect(user.save()).to.be.rejected;
});
it('should fail when saving with a null password', function() {
user.password = null;
return expect(user.save()).to.be.rejected;
});
it('should fail when saving without a password', function() {
user.password = undefined;
return expect(user.save()).to.be.rejected;
});
describe('given the user has been previously saved', function() {
beforeEach(function() {
return user.save();
});
it('should authenticate user if valid', function() {
expect(user.authenticate('password')).to.be.true;
});
it('should not authenticate user if invalid', function() {
expect(user.authenticate('blah')).to.not.be.true;
});
it('should remain the same hash unless the password is updated', function() {
user.name = 'Test User';
return expect(user.save()
.then(function(u) {
return u.authenticate('password');
})).to.eventually.be.true;
});
});
describe('given user provider is google', function() {
beforeEach(function() {
user.provider = 'google';
});
it('should succeed when saving without a password', function() {
user.password = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is facebook', function() {
beforeEach(function() {
user.provider = 'facebook';
});
it('should succeed when saving without a password', function() {
user.password = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without a password', function() {
user.password = null;
return expect(user.save()).to.be.fulfilled;
});
});
});
});

38
server/app.js Normal file
View file

@ -0,0 +1,38 @@
/**
* Main application file
*/
'use strict';
import express from 'express';
import mongoose from 'mongoose';
mongoose.Promise = require('bluebird');
import config from './config/environment';
import http from 'http';
import seedDatabaseIfNeeded from './config/seed';
// Connect to MongoDB
mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', function(err) {
console.error(`MongoDB connection error: ${err}`);
process.exit(-1); // eslint-disable-line no-process-exit
});
// Setup server
var app = express();
var server = http.createServer(app);
require('./config/express').default(app);
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'));
});
}
seedDatabaseIfNeeded();
setImmediate(startServer);
// Expose app
exports = module.exports = app;

View file

@ -0,0 +1,82 @@
'use strict';
import config from '../config/environment';
import jwt from 'jsonwebtoken';
import expressJwt from 'express-jwt';
import compose from 'composable-middleware';
import User from '../api/user/user.model';
var validateJwt = expressJwt({
secret: config.secrets.session
});
/**
* Attaches the user object to the request if authenticated
* Otherwise returns 403
*/
export function isAuthenticated() {
return compose()
// Validate jwt
.use(function(req, res, next) {
// allow access_token to be passed through query parameter as well
if(req.query && req.query.hasOwnProperty('access_token')) {
req.headers.authorization = `Bearer ${req.query.access_token}`;
}
// IE11 forgets to set Authorization header sometimes. Pull from cookie instead.
if(req.query && typeof req.headers.authorization === 'undefined') {
req.headers.authorization = `Bearer ${req.cookies.token}`;
}
validateJwt(req, res, next);
})
// Attach user to request
.use(function(req, res, next) {
User.findById(req.user._id).exec()
.then(user => {
if(!user) {
return res.status(401).end();
}
req.user = user;
next();
})
.catch(err => next(err));
});
}
/**
* Checks if the user role meets the minimum requirements of the route
*/
export function hasRole(roleRequired) {
if(!roleRequired) {
throw new Error('Required role needs to be set');
}
return compose()
.use(isAuthenticated())
.use(function meetsRequirements(req, res, next) {
if(config.userRoles.indexOf(req.user.role) >= config.userRoles.indexOf(roleRequired)) {
return next();
} else {
return res.status(403).send('Forbidden');
}
});
}
/**
* Returns a jwt token signed by the app secret
*/
export function signToken(id, role) {
return jwt.sign({ _id: id, role }, config.secrets.session, {
expiresIn: 60 * 60 * 5
});
}
/**
* Set token cookie directly for oAuth strategies
*/
export function setTokenCookie(req, res) {
if(!req.user) {
return res.status(404).send('It looks like you aren\'t logged in, please try again.');
}
var token = signToken(req.user._id, req.user.role);
res.cookie('token', token);
res.redirect('/');
}

View file

@ -0,0 +1,20 @@
'use strict';
import express from 'express';
import passport from 'passport';
import {setTokenCookie} from '../auth.service';
var router = express.Router();
router
.get('/', passport.authenticate('facebook', {
scope: ['email', 'user_about_me'],
failureRedirect: '/signup',
session: false
}))
.get('/callback', passport.authenticate('facebook', {
failureRedirect: '/signup',
session: false
}), setTokenCookie);
export default router;

View file

@ -0,0 +1,34 @@
import passport from 'passport';
import {Strategy as FacebookStrategy} from 'passport-facebook';
export function setup(User, config) {
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callbackURL,
profileFields: [
'displayName',
'emails'
]
},
function(accessToken, refreshToken, profile, done) {
User.findOne({'facebook.id': profile.id}).exec()
.then(user => {
if(user) {
return done(null, user);
}
user = new User({
name: profile.displayName,
email: profile.emails[0].value,
role: 'user',
provider: 'facebook',
facebook: profile._json
});
user.save()
.then(savedUser => done(null, savedUser))
.catch(err => done(err));
})
.catch(err => done(err));
}));
}

View file

@ -0,0 +1,23 @@
'use strict';
import express from 'express';
import passport from 'passport';
import {setTokenCookie} from '../auth.service';
var router = express.Router();
router
.get('/', passport.authenticate('google', {
failureRedirect: '/signup',
scope: [
'profile',
'email'
],
session: false
}))
.get('/callback', passport.authenticate('google', {
failureRedirect: '/signup',
session: false
}), setTokenCookie);
export default router;

View file

@ -0,0 +1,31 @@
import passport from 'passport';
import {Strategy as GoogleStrategy} from 'passport-google-oauth20';
export function setup(User, config) {
passport.use(new GoogleStrategy({
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.google.callbackURL
},
function(accessToken, refreshToken, profile, done) {
User.findOne({'google.id': profile.id}).exec()
.then(user => {
if(user) {
return done(null, user);
}
user = new User({
name: profile.displayName,
email: profile.emails[0].value,
role: 'user',
username: profile.emails[0].value.split('@')[0],
provider: 'google',
google: profile._json
});
user.save()
.then(savedUser => done(null, savedUser))
.catch(err => done(err));
})
.catch(err => done(err));
}));
}

17
server/auth/index.js Normal file
View file

@ -0,0 +1,17 @@
'use strict';
import express from 'express';
import config from '../config/environment';
import User from '../api/user/user.model';
// Passport Configuration
require('./local/passport').setup(User, config);
require('./facebook/passport').setup(User, config);
require('./google/passport').setup(User, config);
var router = express.Router();
router.use('/local', require('./local').default);
router.use('/facebook', require('./facebook').default);
router.use('/google', require('./google').default);
export default router;

View file

@ -0,0 +1,24 @@
'use strict';
import express from 'express';
import passport from 'passport';
import {signToken} from '../auth.service';
var router = express.Router();
router.post('/', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
var error = err || info;
if(error) {
return res.status(401).json(error);
}
if(!user) {
return res.status(404).json({message: 'Something went wrong, please try again.'});
}
var token = signToken(user._id, user.role);
res.json({ token });
})(req, res, next);
});
export default router;

View file

@ -0,0 +1,35 @@
import passport from 'passport';
import {Strategy as LocalStrategy} from 'passport-local';
function localAuthenticate(User, email, password, done) {
User.findOne({
email: email.toLowerCase()
}).exec()
.then(user => {
if(!user) {
return done(null, false, {
message: 'This email is not registered.'
});
}
user.authenticate(password, function(authError, authenticated) {
if(authError) {
return done(authError);
}
if(!authenticated) {
return done(null, false, { message: 'This password is not correct.' });
} else {
return done(null, user);
}
});
})
.catch(err => done(err));
}
export function setup(User/*, config*/) {
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password' // this is the virtual field on the model
}, function(email, password, done) {
return localAuthenticate(User, email, password, done);
}));
}

View 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
)
};

View 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);
});
};

View 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);
});

View 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)
});
*/

View 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)
})
*/

View 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)
}
)
};

View 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);
}

View file

@ -0,0 +1,16 @@
'use strict';
/*eslint no-process-env:0*/
// Development specific configuration
// ==================================
module.exports = {
// MongoDB connection options
mongo: {
uri: 'mongodb://db/app2-dev'
},
// Seed database on startup
seedDB: true
};

View file

@ -0,0 +1,66 @@
'use strict';
/*eslint no-process-env:0*/
import path from 'path';
import _ from 'lodash';
/*function requiredProcessEnv(name) {
if(!process.env[name]) {
throw new Error('You must set the ' + name + ' environment variable');
}
return process.env[name];
}*/
// All configurations will extend these options
// ============================================
var all = {
env: process.env.NODE_ENV,
// Root path of server
root: path.normalize(`${__dirname}/../../..`),
// Browser-sync port
browserSyncPort: process.env.BROWSER_SYNC_PORT || 3000,
// Server port
port: process.env.PORT || 9000,
// Server IP
ip: process.env.IP || '0.0.0.0',
// Should we populate the DB with sample data?
seedDB: false,
// Secret for session, you will want to change this and make it an environment variable
secrets: {
session: 'app2-secret'
},
// MongoDB connection options
mongo: {
options: {
db: {
safe: true
}
}
},
facebook: {
clientID: process.env.FACEBOOK_ID || 'id',
clientSecret: process.env.FACEBOOK_SECRET || 'secret',
callbackURL: `${process.env.DOMAIN || ''}/auth/facebook/callback`
},
google: {
clientID: process.env.GOOGLE_ID || 'id',
clientSecret: process.env.GOOGLE_SECRET || 'secret',
callbackURL: `${process.env.DOMAIN || ''}/auth/google/callback`
}
};
// Export the config object based on the NODE_ENV
// ==============================================
module.exports = _.merge(
all,
require('./shared'),
require(`./${process.env.NODE_ENV}.js`) || {});

View file

@ -0,0 +1,24 @@
'use strict';
/*eslint no-process-env:0*/
// Production specific configuration
// =================================
module.exports = {
// Server IP
ip: process.env.OPENSHIFT_NODEJS_IP
|| process.env.ip
|| undefined,
// Server port
port: process.env.OPENSHIFT_NODEJS_PORT
|| process.env.PORT
|| 8080,
// MongoDB connection options
mongo: {
uri: process.env.MONGODB_URI
|| process.env.MONGOHQ_URL
|| process.env.OPENSHIFT_MONGODB_DB_URL + process.env.OPENSHIFT_APP_NAME
|| 'mongodb://localhost/app2'
}
};

View file

@ -0,0 +1,11 @@
'use strict';
exports = module.exports = {
// List of user roles
userRoles: ['guest', 'user', 'admin'],
'scriptEngine' : {
'host' : 'localhost',
'user' : 'root',
'password' : 'P@ssw0rd@123'
}
};

View file

@ -0,0 +1,21 @@
'use strict';
/*eslint no-process-env:0*/
// Test specific configuration
// ===========================
module.exports = {
// MongoDB connection options
mongo: {
uri: 'mongodb://localhost/app2-test'
},
sequelize: {
uri: 'sqlite://',
options: {
logging: false,
storage: 'test.sqlite',
define: {
timestamps: false
}
}
}
};

133
server/config/express.js Normal file
View file

@ -0,0 +1,133 @@
/**
* Express configuration
*/
'use strict';
import express from 'express';
import favicon from 'serve-favicon';
import morgan from 'morgan';
import shrinkRay from 'shrink-ray';
import bodyParser from 'body-parser';
import methodOverride from 'method-override';
import cookieParser from 'cookie-parser';
import errorHandler from 'errorhandler';
import path from 'path';
import lusca from 'lusca';
import config from './environment';
import passport from 'passport';
import session from 'express-session';
import connectMongo from 'connect-mongo';
import mongoose from 'mongoose';
var MongoStore = connectMongo(session);
export default function(app) {
var env = app.get('env');
if(env === 'development' || env === 'test') {
app.use(express.static(path.join(config.root, '.tmp')));
}
if(env === 'production') {
app.use(favicon(path.join(config.root, 'client', 'favicon.ico')));
}
app.set('appPath', path.join(config.root, 'client'));
app.use(express.static(app.get('appPath')));
app.use(morgan('dev'));
app.set('views', `${config.root}/server/views`);
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(shrinkRay());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
// Persist sessions with MongoStore / sequelizeStore
// We need to enable sessions for passport-twitter because it's an
// oauth 1.0 strategy, and Lusca depends on sessions
app.use(session({
secret: config.secrets.session,
saveUninitialized: true,
resave: false,
store: new MongoStore({
mongooseConnection: mongoose.connection,
db: 'app2'
})
}));
/**
* Lusca - express server security
* https://github.com/krakenjs/lusca
*/
if(env !== 'test' && !process.env.SAUCE_USERNAME) {
app.use(lusca({
csrf: {
angular: true
},
xframe: 'SAMEORIGIN',
hsts: {
maxAge: 31536000, //1 year, in seconds
includeSubDomains: true,
preload: true
},
xssProtection: true
}));
}
if(env === 'development') {
const webpackDevMiddleware = require('webpack-dev-middleware');
const stripAnsi = require('strip-ansi');
const webpack = require('webpack');
const makeWebpackConfig = require('../../webpack.make');
const webpackConfig = makeWebpackConfig({ DEV: true });
const compiler = webpack(webpackConfig);
const browserSync = require('browser-sync').create();
/**
* Run Browsersync and use middleware for Hot Module Replacement
*/
browserSync.init({
open: false,
logFileChanges: false,
proxy: `localhost:${config.port}`,
ws: true,
middleware: [
webpackDevMiddleware(compiler, {
noInfo: false,
stats: {
colors: true,
timings: true,
chunks: false
}
})
],
port: config.browserSyncPort,
plugins: ['bs-fullscreen-message']
});
/**
* Reload all devices when bundle is complete
* or send a fullscreen error message to the browser instead
*/
compiler.plugin('done', function(stats) {
console.log('webpack done hook');
if(stats.hasErrors() || stats.hasWarnings()) {
return browserSync.sockets.emit('fullscreen:message', {
title: 'Webpack Error:',
body: stripAnsi(stats.toString()),
timeout: 100000
});
}
browserSync.reload();
});
}
if(env === 'development' || env === 'test') {
app.use(errorHandler()); // Error handler - has to be last
}
}

View file

@ -0,0 +1,20 @@
'use strict';
// Use local.env.js for environment variables that will be set when the server starts locally.
// Use for your api keys, secrets, etc. This file should not be tracked by git.
//
// You will need to set these on the server you deploy to.
module.exports = {
DOMAIN: 'http://localhost:9000',
SESSION_SECRET: 'app2-secret',
FACEBOOK_ID: 'app-id',
FACEBOOK_SECRET: 'secret',
GOOGLE_ID: 'app-id',
GOOGLE_SECRET: 'secret',
// Control debug level for modules using visionmedia/debug
DEBUG: ''
};

66
server/config/seed.js Normal file
View file

@ -0,0 +1,66 @@
/**
* Populate DB with sample data on server start
* to disable, edit config/environment/index.js, and set `seedDB: false`
*/
'use strict';
import Thing from '../api/thing/thing.model';
import User from '../api/user/user.model';
import config from './environment/';
export default function seedDatabaseIfNeeded() {
if(config.seedDB) {
Thing.find({}).remove()
.then(() => {
let thing = Thing.create({
name: 'Development Tools',
info: 'Integration with popular tools such as Webpack, Gulp, Babel, TypeScript, Karma, '
+ 'Mocha, ESLint, Node Inspector, Livereload, Protractor, Pug, '
+ 'Stylus, Sass, and Less.'
}, {
name: 'Server and Client integration',
info: 'Built with a powerful and fun stack: MongoDB, Express, '
+ 'AngularJS, and Node.'
}, {
name: 'Smart Build System',
info: 'Build system ignores `spec` files, allowing you to keep '
+ 'tests alongside code. Automatic injection of scripts and '
+ 'styles into your index.html'
}, {
name: 'Modular Structure',
info: 'Best practice client and server structures allow for more '
+ 'code reusability and maximum scalability'
}, {
name: 'Optimized Build',
info: 'Build process packs up your templates as a single JavaScript '
+ 'payload, minifies your scripts/css/images, and rewrites asset '
+ 'names for caching.'
}, {
name: 'Deployment Ready',
info: 'Easily deploy your app to Heroku or Openshift with the heroku '
+ 'and openshift subgenerators'
});
return thing;
})
.then(() => console.log('finished populating things'))
.catch(err => console.log('error populating things', err));
User.find({}).remove()
.then(() => {
User.create({
provider: 'local',
name: 'Test User',
email: 'test@example.com',
password: 'test'
}, {
provider: 'local',
role: 'admin',
name: 'Admin',
email: 'admin@example.com',
password: 'admin'
})
.then(() => console.log('finished populating users'))
.catch(err => console.log('error populating users', err));
});
}
}

12
server/index.js Normal file
View file

@ -0,0 +1,12 @@
'use strict';
// Set default node environment to development
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
if(env === 'development' || env === 'test') {
// Register the Babel require hook
require('babel-register');
}
// Export the application
exports = module.exports = require('./app');

29
server/routes.js Normal file
View file

@ -0,0 +1,29 @@
/**
* Main application routes
*/
'use strict';
import errors from './components/errors';
import path from 'path';
export default function(app) {
// Insert routes below
app.use('/api/custom_modules', require('./api/custom_module'));
app.use('/api/ansible', require('./api/ansible'));
app.use('/api/projects', require('./api/project'));
app.use('/api/things', require('./api/thing'));
app.use('/api/users', require('./api/user'));
app.use('/auth', require('./auth').default);
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get((req, res) => {
res.sendFile(path.resolve(`${app.get('appPath')}/index.html`));
});
}

157
server/views/404.html Normal file
View file

@ -0,0 +1,157 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Page Not Found :(</title>
<style>
::-moz-selection {
background: #b3d4fc;
text-shadow: none;
}
::selection {
background: #b3d4fc;
text-shadow: none;
}
html {
padding: 30px 10px;
font-size: 20px;
line-height: 1.4;
color: #737373;
background: #f0f0f0;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
html,
input {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
body {
max-width: 500px;
_width: 500px;
padding: 30px 20px 50px;
border: 1px solid #b3b3b3;
border-radius: 4px;
margin: 0 auto;
box-shadow: 0 1px 10px #a7a7a7, inset 0 1px 0 #fff;
background: #fcfcfc;
}
h1 {
margin: 0 10px;
font-size: 50px;
text-align: center;
}
h1 span {
color: #bbb;
}
h3 {
margin: 1.5em 0 0.5em;
}
p {
margin: 1em 0;
}
ul {
padding: 0 0 0 40px;
margin: 1em 0;
}
.container {
max-width: 380px;
_width: 380px;
margin: 0 auto;
}
/* google search */
#goog-fixurl ul {
list-style: none;
padding: 0;
margin: 0;
}
#goog-fixurl form {
margin: 0;
}
#goog-wm-qt,
#goog-wm-sb {
border: 1px solid #bbb;
font-size: 16px;
line-height: normal;
vertical-align: top;
color: #444;
border-radius: 2px;
}
#goog-wm-qt {
width: 220px;
height: 20px;
padding: 5px;
margin: 5px 10px 0 0;
box-shadow: inset 0 1px 1px #ccc;
}
#goog-wm-sb {
display: inline-block;
height: 32px;
padding: 0 10px;
margin: 5px 0 0;
white-space: nowrap;
cursor: pointer;
background-color: #f5f5f5;
background-image: -webkit-linear-gradient(rgba(255,255,255,0), #f1f1f1);
background-image: -moz-linear-gradient(rgba(255,255,255,0), #f1f1f1);
background-image: -ms-linear-gradient(rgba(255,255,255,0), #f1f1f1);
background-image: -o-linear-gradient(rgba(255,255,255,0), #f1f1f1);
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
*overflow: visible;
*display: inline;
*zoom: 1;
}
#goog-wm-sb:hover,
#goog-wm-sb:focus {
border-color: #aaa;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
background-color: #f8f8f8;
}
#goog-wm-qt:hover,
#goog-wm-qt:focus {
border-color: #105cb6;
outline: 0;
color: #222;
}
input::-moz-focus-inner {
padding: 0;
border: 0;
}
</style>
</head>
<body>
<div class="container">
<h1>Not found <span>:(</span></h1>
<p>Sorry, but the page you were trying to view does not exist.</p>
<p>It looks like this was the result of either:</p>
<ul>
<li>a mistyped address</li>
<li>an out-of-date link</li>
</ul>
<script>
var GOOG_FIXURL_LANG = (navigator.language || '').slice(0,2),GOOG_FIXURL_SITE = location.host;
</script>
<script src="//linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script>
</div>
</body>
</html>