Merge pull request #9 from keedya/adopt_eslint_openstack

Adopt eslint openstack
This commit is contained in:
Andre Keedy 2016-01-12 15:25:22 -05:00
commit b79a6b7469
14 changed files with 527 additions and 420 deletions

25
Shovel/.eslintrc.js Normal file
View File

@ -0,0 +1,25 @@
module.exports = {
"rules": {
"indent": [
2,
4
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
]
},
"env": {
"es6": true,
"browser": true
},
"extends": "openstack"
};

View File

@ -1,8 +1,6 @@
// Copyright 2015, EMC, Inc. // Copyright 2015, EMC, Inc.
/*eslint-env node*/
'use strict';
var url = require('url');
var monorail = require('./../lib/api/monorail/monorail'); var monorail = require('./../lib/api/monorail/monorail');
var ironic = require('./../lib/api/openstack/ironic'); var ironic = require('./../lib/api/openstack/ironic');
var config = require('./../config.json'); var config = require('./../config.json');
@ -21,12 +19,13 @@ var glanceConfig = config.glance;
* @apiDescription get shovel information * @apiDescription get shovel information
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.infoGet = function infoGet(req, res, next) { module.exports.infoGet = function infoGet(req, res) {
'use strict';
var info = { var info = {
name: 'shovel', name: 'shovel',
description: 'onrack-ironic agent', description: 'rackHD-ironic agent',
appversion: config.appver, appversion: config.shovel.appver,
apiversion: config.apiver apiversion: config.shovel.apiver
}; };
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(info)); res.end(JSON.stringify(info));
@ -37,7 +36,8 @@ module.exports.infoGet = function infoGet(req, res, next) {
* @apiDescription get ironic drivers * @apiDescription get ironic drivers
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.driversGet = function driversGet(req, res, next) { module.exports.driversGet = function driversGet(req, res) {
'use strict';
return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username, return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username,
ironicConfig.os_password). ironicConfig.os_password).
then(function (token) { then(function (token) {
@ -60,7 +60,8 @@ module.exports.driversGet = function driversGet(req, res, next) {
* @apiDescription get ironic nodes * @apiDescription get ironic nodes
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.ironicnodesGet = function ironicnodesGet(req, res, next) { module.exports.ironicnodesGet = function ironicnodesGet(req, res) {
'use strict';
return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username, return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username,
ironicConfig.os_password). ironicConfig.os_password).
then(function (token) { then(function (token) {
@ -83,7 +84,8 @@ module.exports.ironicnodesGet = function ironicnodesGet(req, res, next) {
* @apiDescription get ironic chassis * @apiDescription get ironic chassis
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.ironicchassisGet = function ironicchassisGet(req, res, next) { module.exports.ironicchassisGet = function ironicchassisGet(req, res) {
'use strict';
return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username, return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username,
ironicConfig.os_password). ironicConfig.os_password).
then(function (token) { then(function (token) {
@ -106,7 +108,8 @@ module.exports.ironicchassisGet = function ironicchassisGet(req, res, next) {
* @apiDescription get ironic node * @apiDescription get ironic node
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.ironicnodeGet = function ironicnodeGet(req, res, next) { module.exports.ironicnodeGet = function ironicnodeGet(req, res) {
'use strict';
return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username, return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username,
ironicConfig.os_password). ironicConfig.os_password).
then(function (token) { then(function (token) {
@ -129,7 +132,8 @@ module.exports.ironicnodeGet = function ironicnodeGet(req, res, next) {
* @apiDescription patch ironic node info * @apiDescription patch ironic node info
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.ironicnodePatch = function ironicnodePatch(req, res, next) { module.exports.ironicnodePatch = function ironicnodePatch(req, res) {
'use strict';
return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username, return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username,
ironicConfig.os_password). ironicConfig.os_password).
then(function (token) { then(function (token) {
@ -153,7 +157,8 @@ module.exports.ironicnodePatch = function ironicnodePatch(req, res, next) {
* @apiDescription get catalogs * @apiDescription get catalogs
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.catalogsGet = function catalogsGet(req, res, next) { module.exports.catalogsGet = function catalogsGet(req, res) {
'use strict';
return monorail.request_catalogs_get(req.swagger.params.identifier.value). return monorail.request_catalogs_get(req.swagger.params.identifier.value).
then(function (catalogs) { then(function (catalogs) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
@ -171,7 +176,8 @@ module.exports.catalogsGet = function catalogsGet(req, res, next) {
* @apiDescription get catalogs by source * @apiDescription get catalogs by source
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.catalogsbysourceGet = function catalogsbysourceGet(req, res, next) { module.exports.catalogsbysourceGet = function catalogsbysourceGet(req, res) {
'use strict';
return monorail.get_catalog_data_by_source(req.swagger.params.identifier.value, return monorail.get_catalog_data_by_source(req.swagger.params.identifier.value,
req.swagger.params.source.value). req.swagger.params.source.value).
then(function (catalogs) { then(function (catalogs) {
@ -190,7 +196,8 @@ module.exports.catalogsbysourceGet = function catalogsbysourceGet(req, res, next
* @apiDescription get specific node by id * @apiDescription get specific node by id
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.nodeGet = function nodeGet(req, res, next) { module.exports.nodeGet = function nodeGet(req, res) {
'use strict';
return monorail.request_node_get(req.swagger.params.identifier.value). return monorail.request_node_get(req.swagger.params.identifier.value).
then(function (node) { then(function (node) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
@ -208,7 +215,8 @@ module.exports.nodeGet = function nodeGet(req, res, next) {
* @apiDescription get list of monorail nodes * @apiDescription get list of monorail nodes
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.nodesGet = function nodesGet(req, res, next) { module.exports.nodesGet = function nodesGet(req, res) {
'use strict';
return monorail.request_nodes_get(). return monorail.request_nodes_get().
then(function (nodes) { then(function (nodes) {
Promise.filter(JSON.parse(nodes), function (node) { Promise.filter(JSON.parse(nodes), function (node) {
@ -231,19 +239,24 @@ module.exports.nodesGet = function nodesGet(req, res, next) {
* @apiDescription get specific node by id * @apiDescription get specific node by id
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.getSeldata = function getSeldata(req, res, next) { module.exports.getSeldata = function getSeldata(req, res,next) {
'use strict';
return monorail.request_poller_get(req.swagger.params.identifier.value). return monorail.request_poller_get(req.swagger.params.identifier.value).
then(function (pollers) { then(function (pollers) {
pollers = JSON.parse(pollers); pollers = JSON.parse(pollers);
for (var i in pollers) { return Promise.filter(pollers, function (poller) {
if (pollers[i]['config']['command'] === 'sel') { return poller.config.command === 'sel';
return monorail.request_poller_data_get(pollers[i]['id']). })
then(function (data) { .then(function (sel) {
if (sel.length > 0) {
return monorail.request_poller_data_get(sel[0].id)
.then(function (data) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(data); res.end(data);
}); });
} }
} next();
});
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
@ -257,46 +270,58 @@ module.exports.getSeldata = function getSeldata(req, res, next) {
* @apiDescription register a node in Ironic * @apiDescription register a node in Ironic
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.registerpost = function registerpost(req, res, next) { module.exports.registerpost = function registerpost(req, res) {
var info = {}; 'use strict';
var node = {}; var localGb, ironicToken, ironicNode, extra, port, propreties, node, info, userEntry;
var propreties = {}; //init
var local_gb; extra = port = propreties = node = info = {};
var extra = {}; userEntry = req.body;
var port = {} if (userEntry.driver === 'pxe_ipmitool') {
var ironicToken; info = {
var ironic_node; 'ipmi_address': userEntry.ipmihost,
var onrack_node; 'ipmi_username': userEntry.ipmiuser,
var user_entry = req.body; 'ipmi_password': userEntry.ipmipass,
if (user_entry.driver == 'pxe_ipmitool') { 'deploy_kernel': userEntry.kernel,
info = { 'ipmi_address': user_entry.ipmihost, 'ipmi_username': user_entry.ipmiuser, 'ipmi_password': user_entry.ipmipass, 'deploy_kernel': user_entry.kernel, 'deploy_ramdisk': user_entry.ramdisk }; 'deploy_ramdisk': userEntry.ramdisk
} };
else if (user_entry.driver == 'pxe_ssh') { } else if (userEntry.driver === 'pxe_ssh') {
info = { 'ssh_address': user_entry.sshhost, 'ssh_username': user_entry.sshuser, 'ssh_password': user_entry.sshpass, 'ssh_port': user_entry.sshport, 'deploy_kernel': user_entry.kernel, 'deploy_ramdisk': user_entry.ramdisk }; info = {
} 'ssh_address': userEntry.sshhost,
else { 'ssh_username': userEntry.sshuser,
'ssh_password': userEntry.sshpass,
'ssh_port': userEntry.sshport,
'deploy_kernel': userEntry.kernel,
'deploy_ramdisk': userEntry.ramdisk
};
} else {
info = {}; info = {};
} }
/* Fill in the extra meta data with some failover and event data */ /* Fill in the extra meta data with some failover and event data */
extra = { 'nodeid': user_entry.uuid, 'name': user_entry.name, 'lsevents': { 'time': 0 }, 'eventcnt': 0, 'timer': {} }; extra = {
if (typeof user_entry.failovernode !== 'undefined') { 'nodeid': userEntry.uuid,
extra['failover'] = user_entry.failovernode; 'name': userEntry.name,
'lsevents': { 'time': 0 },
'eventcnt': 0,
'timer': {}
};
if (typeof userEntry.failovernode !== 'undefined') {
extra.failover = userEntry.failovernode;
} }
if (typeof user_entry.eventre !== 'undefined') { if (typeof userEntry.eventre !== 'undefined') {
extra['eventre'] = user_entry.eventre; extra.eventre = userEntry.eventre;
} }
local_gb = 0.0; localGb = 0.0;
return monorail.request_node_get(user_entry.uuid). return monorail.request_node_get(userEntry.uuid).
then(function (result) { then(function (result) {
if (!JSON.parse(result).name) { if (!JSON.parse(result).name) {
var error = { error_message: { message: 'failed to find required node in RackHD' } }; var error = { error_message: { message: 'failed to find required node in RackHD' } };
logger.error(err); logger.error(error);
throw error; throw error;
} }
onrack_node = JSON.parse(result); ironicNode = JSON.parse(result);
return monorail.nodeDiskSize(onrack_node) return monorail.nodeDiskSize(ironicNode)
.catch(function (err) { .catch(function (err) {
var error = { error_message: { message: 'failed to get compute node Disk Size' } }; var error = { error_message: { message: 'failed to get compute node Disk Size' } };
logger.error(err); logger.error(err);
@ -304,32 +329,39 @@ module.exports.registerpost = function registerpost(req, res, next) {
}); });
}).then(function (localDisk) { }).then(function (localDisk) {
local_gb = localDisk; localGb = localDisk;
return monorail.get_node_memory_cpu(onrack_node) return monorail.getNodeMemoryCpu(ironicNode)
.catch(function (err) { .catch(function (err) {
var error = { error_message: { message: 'failed to get compute node memory size' } }; var error = { error_message: { message: 'failed to get compute node memory size' } };
logger.error(err); logger.error(err);
throw error; throw error;
}); });
}).then(function (dmiData) { }).then(function (dmiData) {
if (local_gb == 0 || dmiData.cpus == 0 || dmiData.memory == 0) { if (localGb === 0 || dmiData.cpus === 0 || dmiData.memory === 0) {
var error = { error_message: { message: 'failed to get compute node data', nodeDisk: local_gb, memorySize: dmiData.memory, cpuCount: dmiData.cpus } }; var error = {
throw (error); error_message: {
message: 'failed to get compute node data',
nodeDisk: localGb,
memorySize: dmiData.memory,
cpuCount: dmiData.cpus
}
};
throw error;
} }
propreties = { propreties = {
'cpus': dmiData.cpus, 'cpus': dmiData.cpus,
'memory_mb': dmiData.memory, 'memory_mb': dmiData.memory,
'local_gb': local_gb 'local_gb': localGb
}; };
node = { node = {
'name': user_entry.uuid, 'name': userEntry.uuid,
'driver': user_entry.driver, 'driver': userEntry.driver,
'driver_info': info, 'driver_info': info,
'properties': propreties, 'properties': propreties,
'extra': extra 'extra': extra
}; };
return (keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username, return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username,
ironicConfig.os_password)); ironicConfig.os_password);
}). }).
then(function (token) { then(function (token) {
ironicToken = JSON.parse(token).access.token.id; ironicToken = JSON.parse(token).access.token.id;
@ -338,20 +370,20 @@ module.exports.registerpost = function registerpost(req, res, next) {
then(function (ret) { then(function (ret) {
logger.debug('\r\ncreate node:\r\n' + ret); logger.debug('\r\ncreate node:\r\n' + ret);
if (ret && JSON.parse(ret).error_message) { if (ret && JSON.parse(ret).error_message) {
throw (JSON.parse(ret)); throw JSON.parse(ret);
} }
ironic_node = JSON.parse(ret); ironicNode = JSON.parse(ret);
port = { 'address': user_entry.port, 'node_uuid': ironic_node.uuid }; port = { 'address': userEntry.port, 'node_uuid': ironicNode.uuid };
return ironic.create_port(ironicToken, JSON.stringify(port)); return ironic.createPort(ironicToken, JSON.stringify(port));
}). }).
then(function (create_port) { then(function (createPort) {
logger.info('\r\nCreate port:\r\n' + JSON.stringify(create_port)); logger.info('\r\nCreate port:\r\n' + JSON.stringify(createPort));
return ironic.set_power_state(ironicToken, ironic_node.uuid, "on"); return ironic.set_power_state(ironicToken, ironicNode.uuid, 'on');
}). }).
then(function (pwr_state) { then(function (pwrState) {
logger.info('\r\npwr_state: on'); logger.info('\r\npwrState: on');
if (pwr_state && JSON.parse(pwr_state).error_message) { if (pwrState && JSON.parse(pwrState).error_message) {
throw (JSON.parse(pwr_state)); throw JSON.parse(pwrState);
} }
}).then(function () { }).then(function () {
var timer = {}; var timer = {};
@ -361,20 +393,20 @@ module.exports.registerpost = function registerpost(req, res, next) {
timer.timeInterval = 15000; timer.timeInterval = 15000;
timer.isDone = true; timer.isDone = true;
var data = [{ 'path': '/extra/timer', 'value': timer, 'op': 'replace' }]; var data = [{ 'path': '/extra/timer', 'value': timer, 'op': 'replace' }];
return ironic.patch_node(ironicToken, ironic_node.uuid, JSON.stringify(data)); return ironic.patch_node(ironicToken, ironicNode.uuid, JSON.stringify(data));
}). }).
then(function (result) { then(function (result) {
logger.info('\r\patched node:\r\n' + result); logger.info('\r\patched node:\r\n' + result);
}). }).
then(function () { then(function () {
_.each(onrack_node.identifiers, function (mac) { _.each(ironicNode.identifiers, function (mac) {
return monorail.request_whitelist_set(mac) return monorail.request_whitelist_set(mac)
.then(function (whitelist) { .then(function (whitelist) {
logger.info('\r\nmonorail whitelist:\r\n' + JSON.stringify(whitelist)); logger.info('\r\nmonorail whitelist:\r\n' + JSON.stringify(whitelist));
}); });
}); });
}) })
.then(function (whitelist) { .then(function () {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
var success = { var success = {
result: 'success' result: 'success'
@ -392,21 +424,22 @@ module.exports.registerpost = function registerpost(req, res, next) {
* @apiDescription unregister a node from Ironic * @apiDescription unregister a node from Ironic
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.unregisterdel = function unregisterdel(req, res, next) { module.exports.unregisterdel = function unregisterdel(req, res) {
'use strict';
var ironicToken; var ironicToken;
//TODO allow using name or ironic node uuid
return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username, return keystone.authenticatePassword(ironicConfig.os_tenant_name, ironicConfig.os_username,
ironicConfig.os_password). ironicConfig.os_password).
then(function (token) { then(function (token) {
ironicToken = JSON.parse(token).access.token.id; ironicToken = JSON.parse(token).access.token.id;
return ironic.delete_node(ironicToken, req.swagger.params.identifier.value); return ironic.delete_node(ironicToken, req.swagger.params.identifier.value);
}) })
.then(function (del_node) { .then(function (delNode) {
if (del_node && JSON.parse(del_node).error_message) { if (delNode && JSON.parse(delNode).error_message) {
throw (del_node); throw delNode;
} } else {
else { logger.info('ironicNode: ' +
logger.info('ironicNode: ' + req.swagger.params.identifier.value + ' is been deleted susccessfully'); req.swagger.params.identifier.value +
' is been deleted susccessfully');
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
var success = { var success = {
result: 'success' result: 'success'
@ -433,14 +466,14 @@ module.exports.unregisterdel = function unregisterdel(req, res, next) {
* @apiDescription modify shovel config.json file and restart the server * @apiDescription modify shovel config.json file and restart the server
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.configsetmono = function configsetmono(req, res, next) { module.exports.configsetmono = function configsetmono(req, res) {
'use strict';
res.setHeader('content-type', 'text/plain'); res.setHeader('content-type', 'text/plain');
if (setConfig('monorail', req.body)) { if (setConfig('monorail', req.body)) {
res.end('success'); res.end('success');
} } else {
else {
res.end('failed to update monorail config'); res.end('failed to update monorail config');
}; }
}; };
/* /*
@ -448,14 +481,14 @@ module.exports.configsetmono = function configsetmono(req, res, next) {
* @apiDescription modify shovel config.json file and restart the server * @apiDescription modify shovel config.json file and restart the server
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.configsetkeystone = function configsetkeystone(req, res, next) { module.exports.configsetkeystone = function configsetkeystone(req, res) {
'use strict';
res.setHeader('content-type', 'text/plain'); res.setHeader('content-type', 'text/plain');
if (setConfig('keystone', req.body)) { if (setConfig('keystone', req.body)) {
res.end('success'); res.end('success');
} } else {
else {
res.end('failed to update keystone config'); res.end('failed to update keystone config');
}; }
}; };
/* /*
@ -463,25 +496,24 @@ module.exports.configsetkeystone = function configsetkeystone(req, res, next) {
* @apiDescription modify shovel config.json file and restart the server * @apiDescription modify shovel config.json file and restart the server
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.configsetironic = function configsetironic(req, res, next) { module.exports.configsetironic = function configsetironic(req, res) {
'use strict';
res.setHeader('content-type', 'text/plain'); res.setHeader('content-type', 'text/plain');
if (req.body.hasOwnProperty('os_password')) { if (req.body.hasOwnProperty('os_password')) {
var password = req.body.os_password; var password = req.body.os_password;
//replace password with encrypted value //replace password with encrypted value
try { try {
req.body.os_password = encryption.encrypt(password); req.body.os_password = encryption.encrypt(password);
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
res.end('failed to update ironic config'); res.end('failed to update ironic config');
} }
} }
if (setConfig('ironic', req.body)) { if (setConfig('ironic', req.body)) {
res.end('success'); res.end('success');
} } else {
else {
res.end('failed to update ironic config'); res.end('failed to update ironic config');
}; }
}; };
/* /*
@ -489,25 +521,24 @@ module.exports.configsetironic = function configsetironic(req, res, next) {
* @apiDescription modify shovel config.json file and restart the server * @apiDescription modify shovel config.json file and restart the server
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.configsetglance = function configsetglance(req, res, next) { module.exports.configsetglance = function configsetglance(req, res) {
'use strict';
res.setHeader('content-type', 'text/plain'); res.setHeader('content-type', 'text/plain');
if (req.body.hasOwnProperty('os_password')) { if (req.body.hasOwnProperty('os_password')) {
var password = req.body.os_password; var password = req.body.os_password;
//replace password with encrypted value //replace password with encrypted value
try { try {
req.body.os_password = encryption.encrypt(password); req.body.os_password = encryption.encrypt(password);
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
res.end('failed to update ironic config'); res.end('failed to update ironic config');
} }
} }
if (setConfig('glance', req.body)) { if (setConfig('glance', req.body)) {
res.end('success'); res.end('success');
} } else {
else {
res.end('failed to update glance config'); res.end('failed to update glance config');
}; }
}; };
/* /*
@ -515,32 +546,32 @@ module.exports.configsetglance = function configsetglance(req, res, next) {
* @apiDescription modify shovel config.json file and restart the server * @apiDescription modify shovel config.json file and restart the server
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.configset = function configset(req, res, next) { module.exports.configset = function configset(req, res) {
'use strict';
res.setHeader('content-type', 'text/plain'); res.setHeader('content-type', 'text/plain');
if (setConfig('shovel', req.body) == true) { if (setConfig('shovel', req.body) === true) {
res.end('success'); res.end('success');
} } else {
else {
res.end('failed to update shovel config'); res.end('failed to update shovel config');
}; }
}; };
function setConfig(keyValue, entry) { function setConfig(keyValue, entry) {
'use strict';
var filename = 'config.json'; var filename = 'config.json';
jsonfile.readFile(filename, function (err, output) { jsonfile.readFile(filename, function (error, output) {
try { try {
var content = (keyValue == null) ? output : output[keyValue]; var content = keyValue === null ? output : output[keyValue];
var filteredList = _.pick(content, Object.keys(entry)); var filteredList = _.pick(content, Object.keys(entry));
_.each(Object.keys(filteredList), function (key) { _.each(Object.keys(filteredList), function (key) {
content[key] = entry[key]; content[key] = entry[key];
}); });
output[keyValue] = content; output[keyValue] = content;
jsonfile.writeFile(filename, output, { spaces: 2 }, function (err) { jsonfile.writeFile(filename, output, { spaces: 2 }, function () {
logger.debug(content); logger.debug(content);
}); });
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
return false; return false;
} }
@ -553,25 +584,25 @@ function setConfig(keyValue, entry) {
* @apiDescription get shovel config.json file and restart the server * @apiDescription get shovel config.json file and restart the server
* @apiVersion 1.1.0 * @apiVersion 1.1.0
*/ */
module.exports.configget = function configget(req, res, next) { module.exports.configget = function configget(req, res) {
'use strict';
var filename = 'config.json'; var filename = 'config.json';
jsonfile.readFile(filename, function (err, content) { jsonfile.readFile(filename, function (error,content) {
try { try {
delete content['key']; delete content.key;
if (content.ironic.hasOwnProperty("os_password")) { if (content.ironic.hasOwnProperty('os_password')) {
content.ironic.os_password = '[REDACTED]'; content.ironic.os_password = '[REDACTED]';
} }
if (content.glance.hasOwnProperty("os_password")) { if (content.glance.hasOwnProperty('os_password')) {
content.glance.os_password = '[REDACTED]'; content.glance.os_password = '[REDACTED]';
} }
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(content)); res.end(JSON.stringify(content));
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
res.setHeader('content-type', 'text/plain'); res.setHeader('content-type', 'text/plain');
res.end('failed to get config'); res.end('failed to get config');
}; }
}); });
}; };
@ -579,7 +610,8 @@ module.exports.configget = function configget(req, res, next) {
* @api {get} /api/1.1/glance/images / GET / * @api {get} /api/1.1/glance/images / GET /
* @apiDescription get glance images * @apiDescription get glance images
*/ */
module.exports.imagesGet = function imagesGet(req, res, next) { module.exports.imagesGet = function imagesGet(req, res) {
'use strict';
return keystone.authenticatePassword(glanceConfig.os_tenant_name, glanceConfig.os_username, return keystone.authenticatePassword(glanceConfig.os_tenant_name, glanceConfig.os_username,
glanceConfig.os_password). glanceConfig.os_password).
then(function (token) { then(function (token) {

View File

@ -1,6 +1,11 @@
// Copyright 2015, EMC, Inc.
/*eslint-env node*/
/* http client */ /* http client */
var HttpClient = { var HttpClient = {
Get: function( msg, output ) { Get: function (msg, output) {
'use strict';
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
@ -9,28 +14,26 @@ var HttpClient = {
method: 'GET', method: 'GET',
headers: {} headers: {}
}; };
if (Buffer.byteLength(msg.token)) {
if( Buffer.byteLength(msg.token) )
{
options.headers['X-Auth-Token'] = msg.token; options.headers['X-Auth-Token'] = msg.token;
}; }
cb = function( response ) { var cb = function (response) {
var body = ''; var body = '';
response.on('data', function(chunk) { response.on('data', function (chunk) {
body += chunk; body += chunk;
}); });
response.on('error', function (err) { response.on('error', function (err) {
var errorMessage = { errorMessage: { hostname: msg.host, message: err } }; var errorMessage = { errorMessage: { hostname: msg.host, message: err } };
output(errorMessage); output(errorMessage);
}); });
response.on('end', function() { response.on('end', function () {
output(null, body); output(null, body);
}); });
}; };
request = http.request(options, cb); var request = http.request(options, cb);
request.on('error', function(e) { request.on('error', function (e) {
var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage); output(errorMessage);
}); });
@ -38,55 +41,56 @@ var HttpClient = {
request.end(); request.end();
}, },
Post: function (msg, output) { Post: function (msg, output) {
'use strict';
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
path: msg.path, path: msg.path,
port: msg.port, port: msg.port,
method: 'POST', method: 'POST',
headers: { 'Content-type': 'application/json', headers: {
'Content-type': 'application/json',
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Length': Buffer.byteLength(msg.data), 'Content-Length': Buffer.byteLength(msg.data),
'User-Agent': 'shovel-client' } 'User-Agent': 'shovel-client'
}
}; };
/*Update the request header with special fields*/ /*Update the request header with special fields*/
if( Buffer.byteLength(msg.token) ) if (Buffer.byteLength(msg.token)) {
{
options.headers['X-Auth-Token'] = msg.token; options.headers['X-Auth-Token'] = msg.token;
}; }
if( Buffer.byteLength(JSON.stringify(msg.api)) ) if (Buffer.byteLength(JSON.stringify(msg.api))) {
{
options.headers[msg.api.name] = msg.api.version; options.headers[msg.api.name] = msg.api.version;
}; }
cb = function( response ) { var cb = function (response) {
var body = ''; var body = '';
response.on('data', function(chunk) { response.on('data', function (chunk) {
body += chunk; body += chunk;
}); });
response.on('error', function (err) { response.on('error', function (e) {
var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage); output(errorMessage);
}); });
response.on('end', function() { response.on('end', function () {
output(null,body); output(null, body);
}); });
}; };
request = http.request(options, cb); var request = http.request(options, cb);
request.on('error', function(e) { request.on('error', function (e) {
var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage); output(errorMessage);
}); });
if( Buffer.byteLength(msg.data) ) if (Buffer.byteLength(msg.data)) {
{
request.write(msg.data); request.write(msg.data);
}; }
request.end(); request.end();
}, },
Delete: function( msg, output ) { Delete: function (msg, output) {
'use strict';
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
@ -96,64 +100,17 @@ var HttpClient = {
headers: {} headers: {}
}; };
if( Buffer.byteLength(msg.token) ) if (Buffer.byteLength(msg.token)) {
{
options.headers['X-Auth-Token'] = msg.token; options.headers['X-Auth-Token'] = msg.token;
}; }
if( Buffer.byteLength(JSON.stringify(msg.api)) ) if (Buffer.byteLength(JSON.stringify(msg.api))) {
{
options.headers[msg.api.name] = msg.api.version; options.headers[msg.api.name] = msg.api.version;
}; }
cb = function( response ) { var cb = function (response) {
var body = ''; var body = '';
response.on('data', function(chunk) { response.on('data', function (chunk) {
body += chunk;
});
response.on('error', function (err) {
var errorMessage = { errorMessage: { hostname: msg.host, message: err } };
output(errorMessage);
});
response.on('end', function() {
output(null,body);
});
};
request = http.request(options, cb);
request.on('error', function(e) {
var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage);
});
request.end();
},
Put: function( msg, output ) {
var http = require('http');
var options = {
hostname: msg.host,
path: msg.path,
port: msg.port,
method: 'PUT',
headers: { 'Content-type': 'application/json',
'Accept': 'application/json',
'Content-Length': Buffer.byteLength(msg.data),
'User-Agent': 'shovel-client' }
};
if( Buffer.byteLength(msg.token) )
{
options.headers['X-Auth-Token'] = msg.token;
};
if( Buffer.byteLength(JSON.stringify(msg.api)) )
{
options.headers[msg.api.name] = msg.api.version;
};
cb = function( response ) {
var body = '';
response.on('data', function(chunk) {
body += chunk; body += chunk;
}); });
response.on('error', function (err) { response.on('error', function (err) {
@ -165,8 +122,54 @@ var HttpClient = {
}); });
}; };
request = http.request(options, cb); var request = http.request(options, cb);
request.on('error', function(e) { request.on('error', function (e) {
var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage);
});
request.end();
},
Put: function (msg, output) {
'use strict';
var http = require('http');
var options = {
hostname: msg.host,
path: msg.path,
port: msg.port,
method: 'PUT',
headers: {
'Content-type': 'application/json',
'Accept': 'application/json',
'Content-Length': Buffer.byteLength(msg.data),
'User-Agent': 'shovel-client'
}
};
if (Buffer.byteLength(msg.token)) {
options.headers['X-Auth-Token'] = msg.token;
}
if (Buffer.byteLength(JSON.stringify(msg.api))) {
options.headers[msg.api.name] = msg.api.version;
}
var cb = function (response) {
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('error', function (err) {
var errorMessage = { errorMessage: { hostname: msg.host, message: err } };
output(errorMessage);
});
response.on('end', function () {
output(null, body);
});
};
var request = http.request(options, cb);
request.on('error', function (e) {
var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage); output(errorMessage);
}); });
@ -175,31 +178,32 @@ var HttpClient = {
request.end(); request.end();
}, },
Patch: function (msg, output) { Patch: function (msg, output) {
'use strict';
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
path: msg.path, path: msg.path,
port: msg.port, port: msg.port,
method: 'PATCH', method: 'PATCH',
headers: { 'Content-type': 'application/json', headers: {
'Content-type': 'application/json',
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Length': Buffer.byteLength(msg.data), 'Content-Length': Buffer.byteLength(msg.data),
'User-Agent': 'shovel-client' } 'User-Agent': 'shovel-client'
}
}; };
/*Update the request header with special fields*/ /*Update the request header with special fields*/
if( Buffer.byteLength(msg.token) ) if (Buffer.byteLength(msg.token)) {
{
options.headers['X-Auth-Token'] = msg.token; options.headers['X-Auth-Token'] = msg.token;
}; }
if( Buffer.byteLength(JSON.stringify(msg.api)) ) if (Buffer.byteLength(JSON.stringify(msg.api))) {
{
options.headers[msg.api.name] = msg.api.version; options.headers[msg.api.name] = msg.api.version;
}; }
cb = function( response ) { var cb = function (response) {
var body = ''; var body = '';
response.on('data', function(chunk) { response.on('data', function (chunk) {
body += chunk; body += chunk;
}); });
response.on('error', function (err) { response.on('error', function (err) {
@ -211,18 +215,17 @@ var HttpClient = {
}); });
}; };
request = http.request(options, cb); var request = http.request(options, cb);
request.on('error', function(e) { request.on('error', function (e) {
var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage); output(errorMessage);
}); });
if( Buffer.byteLength(msg.data) ) if (Buffer.byteLength(msg.data)) {
{
request.write(msg.data); request.write(msg.data);
}; }
request.end(); request.end();
}, }
}; };
module.exports = Object.create(HttpClient); module.exports = Object.create(HttpClient);

View File

@ -1,3 +1,6 @@
// Copyright 2015, EMC, Inc.
/*eslint-env node*/
var config = require('./../../../config.json'); var config = require('./../../../config.json');
var client = require('./../client'); var client = require('./../client');
var Promise = require('bluebird'); var Promise = require('bluebird');
@ -16,41 +19,50 @@ var request = {
/* /*
* Monorail wrapper functions * Monorail wrapper functions
*/ */
var MonorailWrapper = { var MonorailWrapper = {
request_nodes_get: function (ret) { request_nodes_get: function () {
'use strict';
request.path = pfx + '/nodes'; request.path = pfx + '/nodes';
return client.GetAsync(request); return client.GetAsync(request);
}, },
request_node_get: function (identifier, ret) { request_node_get: function (identifier) {
'use strict';
request.path = pfx + '/nodes/' + identifier; request.path = pfx + '/nodes/' + identifier;
return client.GetAsync(request); return client.GetAsync(request);
}, },
request_whitelist_set: function (hwaddr, ret) { request_whitelist_set: function (hwaddr) {
'use strict';
request.path = pfx + '/nodes/' + hwaddr + '/dhcp/whitelist'; request.path = pfx + '/nodes/' + hwaddr + '/dhcp/whitelist';
return client.PostAsync(request); return client.PostAsync(request);
}, },
request_whitelist_del: function (hwaddr, ret) { request_whitelist_del: function (hwaddr) {
'use strict';
request.path = pfx + '/nodes/' + hwaddr + '/dhcp/whitelist'; request.path = pfx + '/nodes/' + hwaddr + '/dhcp/whitelist';
return client.DeleteAsync(request); return client.DeleteAsync(request);
}, },
request_catalogs_get: function (hwaddr, ret) { request_catalogs_get: function (hwaddr) {
'use strict';
request.path = pfx + '/nodes/' + hwaddr + '/catalogs'; request.path = pfx + '/nodes/' + hwaddr + '/catalogs';
return client.GetAsync(request); return client.GetAsync(request);
}, },
get_catalog_data_by_source: function (hwaddr, source, ret) { get_catalog_data_by_source: function (hwaddr, source) {
'use strict';
request.path = pfx + '/nodes/' + hwaddr + '/catalogs/' + source; request.path = pfx + '/nodes/' + hwaddr + '/catalogs/' + source;
return client.GetAsync(request); return client.GetAsync(request);
}, },
request_poller_get: function (identifier, ret) { request_poller_get: function (identifier) {
'use strict';
request.path = pfx + '/nodes/' + identifier + '/pollers'; request.path = pfx + '/nodes/' + identifier + '/pollers';
return client.GetAsync(request); return client.GetAsync(request);
}, },
request_poller_data_get: function (identifier, ret) { request_poller_data_get: function (identifier) {
'use strict';
request.path = pfx + '/pollers/' + identifier + '/data/current'; request.path = pfx + '/pollers/' + identifier + '/data/current';
return client.GetAsync(request); return client.GetAsync(request);
}, },
lookupCatalog: function lookupCatalog(node) { lookupCatalog: function lookupCatalog(node) {
'use strict';
var self = this; var self = this;
return self.get_catalog_data_by_source(node.id, 'dmi') return self.get_catalog_data_by_source(node.id, 'dmi')
.then(function (dmi) { .then(function (dmi) {
@ -59,7 +71,7 @@ var MonorailWrapper = {
} }
}) })
.then(function () { .then(function () {
return self.get_catalog_data_by_source(node.id, 'lsscsi') return self.get_catalog_data_by_source(node.id, 'lsscsi');
}) })
.then(function (lsscsi) { .then(function (lsscsi) {
if (!_.has(JSON.parse(lsscsi), 'data')) { if (!_.has(JSON.parse(lsscsi), 'data')) {
@ -67,71 +79,72 @@ var MonorailWrapper = {
} }
}) })
.then(function () { .then(function () {
return self.get_catalog_data_by_source(node.id, 'bmc') return self.get_catalog_data_by_source(node.id, 'bmc');
}) })
.then(function (bmc) { .then(function (bmc) {
if (!_.has(JSON.parse(bmc), 'data')) { if (!_.has(JSON.parse(bmc), 'data')) {
return false; return false;
} } else {
else {
return true; return true;
} }
}) })
.catch(function (err) { .catch(function () {
return false; return false;
}) });
}, },
nodeDiskSize: function nodeDiskSize(node) { nodeDiskSize: function nodeDiskSize(node) {
var local_gb = 0; 'use strict';
var localGb = 0;
var self = this; var self = this;
return self.get_catalog_data_by_source(node.id, 'lsscsi'). return self.get_catalog_data_by_source(node.id, 'lsscsi').
then(function (scsi) { then(function (scsi) {
scsi = JSON.parse(scsi); scsi = JSON.parse(scsi);
if (scsi.data) { if (scsi.data) {
for (var elem in scsi.data) { for (var elem = 0; elem < scsi.data.length; elem++) {
var item = (scsi.data[elem]); var item = scsi.data[elem];
if (item['peripheralType'] == 'disk') { if (item.peripheralType === 'disk') {
local_gb += parseFloat(item['size'].replace('GB', '').trim()); localGb += parseFloat(item.size.replace('GB', '').trim());
} }
} }
} }
return Promise.resolve(local_gb); return Promise.resolve(localGb);
}) })
.catch(function (err) { .catch(function (err) {
throw err; throw err;
}) });
}, },
get_node_memory_cpu: function get_node_memory_cpu(computeNode) { getNodeMemoryCpu: function getNodeMemoryCpu(computeNode) {
'use strict';
var self = this; var self = this;
var dmiData = { cpus: 0, memory: 0 }; var dmiData = { cpus: 0, memory: 0 };
return self.get_catalog_data_by_source(computeNode.id, 'dmi'). return self.get_catalog_data_by_source(computeNode.id, 'dmi').
then(function (dmi) { then(function (dmi) {
dmi = JSON.parse(dmi); dmi = JSON.parse(dmi);
if (dmi.data) { if (dmi.data) {
var dmi_total = 0; var dmiTotal = 0;
if (dmi.data['Memory Device']) { if (dmi.data['Memory Device']) {
var memory_device = dmi.data['Memory Device']; var memoryDevice = dmi.data['Memory Device'];
for (var elem in memory_device) { for (var elem = 0; elem < memoryDevice.length; elem++) {
var item = memory_device[elem]; var item = memoryDevice[elem];
//logger.info(item['Size']); //logger.info(item['Size']);
if (item['Size'].indexOf('GB') > -1) { if (item.Size.indexOf('GB') > -1) {
dmi_total += parseFloat(item['Size'].replace('GB', '').trim()) * 1000; dmiTotal += parseFloat(item.Size.replace('GB', '').trim()) * 1000;
} }
if (item['Size'].indexOf('MB') > -1) { if (item.Size.indexOf('MB') > -1) {
dmi_total += parseFloat(item['Size'].replace('MB', '').trim()); dmiTotal += parseFloat(item.Size.replace('MB', '').trim());
} }
} }
dmiData.memory = dmi_total; dmiData.memory = dmiTotal;
} }
if (dmi['data'].hasOwnProperty('Processor Information')) { if (dmi.data.hasOwnProperty('Processor Information')) {
dmiData.cpus = dmi['data']['Processor Information'].length; dmiData.cpus = dmi.data['Processor Information'].length;
} }
} }
return Promise.resolve(dmiData); return Promise.resolve(dmiData);
}) })
.catch(function (err) { .catch(function (err) {
throw err; throw err;
}) });
} }
}; };

View File

@ -1,3 +1,6 @@
// Copyright 2015, EMC, Inc.
/*eslint-env node*/
var config = require('./../../../config.json'); var config = require('./../../../config.json');
var client = require('./../client'); var client = require('./../client');
var Promise = require('bluebird'); var Promise = require('bluebird');
@ -17,6 +20,7 @@ var request = {
*/ */
var glanceWrapper = { var glanceWrapper = {
get_images: function (token) { get_images: function (token) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/images'; request.path = pfx + '/images';
return client.GetAsync(request); return client.GetAsync(request);

View File

@ -1,3 +1,6 @@
// Copyright 2015, EMC, Inc.
/*eslint-env node*/
var config = require('./../../../config.json'); var config = require('./../../../config.json');
var client = require('./../client'); var client = require('./../client');
var Promise = require('bluebird'); var Promise = require('bluebird');
@ -9,68 +12,82 @@ var request = {
path: pfx, path: pfx,
token: '', token: '',
data: '', data: '',
api: { 'name': 'X-OpenStack-Ironic-API-Version', 'version': '1.6' } /* TODO: set version from ironic */ api: {
'name': 'X-OpenStack-Ironic-API-Version',
'version': '1.6'
}
}; };
/* /*
* Ironic wrapper functions * Ironic wrapper functions
*/ */
var ironicWrapper = { var ironicWrapper = {
get_chassis: function (token, ret) { get_chassis: function (token) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/chassis'; request.path = pfx + '/chassis';
return client.GetAsync(request); return client.GetAsync(request);
}, },
get_chassis_by_id: function (token, identifier, ret) { get_chassis_by_id: function (token, identifier) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/chassis/' + identifier; request.path = pfx + '/chassis/' + identifier;
return client.GetAsync(request); return client.GetAsync(request);
}, },
get_node_list: function (token) { get_node_list: function (token) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/nodes/detail'; request.path = pfx + '/nodes/detail';
return client.GetAsync(request); return client.GetAsync(request);
}, },
get_node: function (token, identifier, ret) { get_node: function (token, identifier) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/nodes/' + identifier; request.path = pfx + '/nodes/' + identifier;
return client.GetAsync(request); return client.GetAsync(request);
}, },
create_node: function (token, node, ret) { create_node: function (token, node) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/nodes'; request.path = pfx + '/nodes';
request.data = node; request.data = node;
return client.PostAsync(request); return client.PostAsync(request);
}, },
patch_node: function (token, identifier, data, ret) { patch_node: function (token, identifier, data) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/nodes/' + identifier; request.path = pfx + '/nodes/' + identifier;
request.data = data; request.data = data;
return client.PatchAsync(request); return client.PatchAsync(request);
}, },
delete_node: function (token, identifier, ret) { delete_node: function (token, identifier) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/nodes/' + identifier; request.path = pfx + '/nodes/' + identifier;
return client.DeleteAsync(request); return client.DeleteAsync(request);
}, },
get_port_list: function (token, ret) { get_port_list: function (token) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/ports'; request.path = pfx + '/ports';
return client.GetAsync(request); return client.GetAsync(request);
}, },
get_port: function (token, identifier, ret) { get_port: function (token, identifier) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/ports/' + identifier; request.path = pfx + '/ports/' + identifier;
return client.GetAsync(request);; return client.GetAsync(request);
}, },
create_port: function (token, port, ret) { create_port: function (token, port) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/ports'; request.path = pfx + '/ports';
request.data = port; request.data = port;
return client.PostAsync(request); return client.PostAsync(request);
}, },
set_power_state: function (token, identifier, state, ret) { set_power_state: function (token, identifier, state) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/nodes/' + identifier + '/states/power'; request.path = pfx + '/nodes/' + identifier + '/states/power';
if (state === 'off' || state === 'on') { if (state === 'off' || state === 'on') {
@ -81,7 +98,8 @@ var ironicWrapper = {
} }
return client.PutAsync(request); return client.PutAsync(request);
}, },
get_driver_list: function (token, ret) { get_driver_list: function (token) {
'use strict';
request.token = token; request.token = token;
request.path = pfx + '/drivers'; request.path = pfx + '/drivers';
return client.GetAsync(request); return client.GetAsync(request);
@ -89,6 +107,3 @@ var ironicWrapper = {
}; };
module.exports = Object.create(ironicWrapper); module.exports = Object.create(ironicWrapper);

View File

@ -1,3 +1,6 @@
// Copyright 2015, EMC, Inc.
/*eslint-env node*/
/* keystone authentication */ /* keystone authentication */
var config = require('./../../../config.json'); var config = require('./../../../config.json');
var client = require('./../client'); var client = require('./../client');
@ -19,15 +22,15 @@ var request = {
var KeystoneAuthentication = { var KeystoneAuthentication = {
authenticatePassword: function (tenantName, username, password) { authenticatePassword: function (tenantName, username, password) {
'use strict';
var decrypted; var decrypted;
try { try {
decrypted = encryption.decrypt(password); decrypted = encryption.decrypt(password);
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
//return empty promise //return empty promise
return (Promise.resolve()); return Promise.resolve();
}; }
request.data = JSON.stringify( request.data = JSON.stringify(
{ {
'auth': { 'auth': {
@ -39,20 +42,21 @@ var KeystoneAuthentication = {
} }
} }
}); });
return (client.PostAsync(request)); return client.PostAsync(request);
}, },
authenticateToken: function (tenantName, username, token) { authenticateToken: function (tenantName, username, token) {
'use strict';
request.data = JSON.stringify( request.data = JSON.stringify(
{ {
"auth": { 'auth': {
"tenantName": tenantName, 'tenantName': tenantName,
"token": { 'token': {
"id": token 'id': token
} }
} }
}); });
return (client.PostAsync(request)); return client.PostAsync(request);
} }
} };
module.exports = Object.create(KeystoneAuthentication); module.exports = Object.create(KeystoneAuthentication);

View File

@ -1,15 +1,16 @@
var crypto = require('crypto') /*eslint-env node*/
var crypto = require('crypto');
var config = require('./../../config.json'); var config = require('./../../config.json');
var CryptoFuncs = { var CryptoFuncs = {
encrypt: function (text) {
encrypt: function (text){ 'use strict';
var cipher = crypto.createCipher('aes-256-cbc', config.key);
var cipher = crypto.createCipher('aes-256-cbc', config.key); var cipher = crypto.createCipher('aes-256-cbc', config.key);
cipher.update(text, 'utf8', 'base64'); cipher.update(text, 'utf8', 'base64');
return cipher.final('base64'); return cipher.final('base64');
}, },
decrypt: function (text){ decrypt: function (text) {
'use strict';
var decipher = crypto.createDecipher('aes-256-cbc', config.key); var decipher = crypto.createDecipher('aes-256-cbc', config.key);
decipher.update(text, 'base64', 'utf8'); decipher.update(text, 'base64', 'utf8');
return decipher.final('utf8'); return decipher.final('utf8');

View File

@ -1,9 +1,11 @@
// Copyright 2015, EMC, Inc. // Copyright 2015, EMC, Inc.
/*eslint-env node*/
var winston = require('winston'); var winston = require('winston');
module.exports.Logger = function Logger(level) { module.exports.Logger = function Logger(level) {
var logger = new (winston.Logger)({ 'use strict';
var logger = new winston.Logger({
levels: { levels: {
verbose: 5, verbose: 5,
debug: 4, debug: 4,
@ -27,4 +29,4 @@ module.exports.Logger = function Logger(level) {
timestamp: true timestamp: true
}); });
return logger; return logger;
} };

View File

@ -1,15 +1,18 @@
// Copyright 2015, EMC, Inc. // Copyright 2015, EMC, Inc.
/*eslint-env node*/
var monorail = require('./../api/monorail/monorail'); var monorail = require('./../api/monorail/monorail');
var ironic = require('./../api/openstack/ironic'); var ironic = require('./../api/openstack/ironic');
var keystone = require('./../api/openstack/keystone'); var keystone = require('./../api/openstack/keystone');
var _ = require('underscore'); var _ = require('underscore');
var config = require('./../../config.json'); var config = require('./../../config.json');
var logger = require('./logger').Logger('info'); var logger = require('./logger').Logger('info');
var Promise = require('bluebird');
module.exports = Poller; module.exports = Poller;
var ironicConfig = config.ironic; var ironicConfig = config.ironic;
function Poller(timeInterval) { function Poller(timeInterval) {
'use strict';
this._timeInterval = timeInterval; this._timeInterval = timeInterval;
this._ironicToken = null; this._ironicToken = null;
this._timeObj = null; this._timeObj = null;
@ -24,7 +27,7 @@ function Poller(timeInterval) {
}) })
.catch(function (err) { .catch(function (err) {
logger.error(err); logger.error(err);
return (null); return null;
}); });
}; };
@ -32,63 +35,63 @@ function Poller(timeInterval) {
try { try {
clearInterval(this.timeObj); clearInterval(this.timeObj);
this.timeObj = 0; this.timeObj = 0;
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
} }
}; };
Poller.prototype.runPoller = function (ironic_nodes) { Poller.prototype.runPoller = function (ironicNodes) {
var self = this; var self = this;
for (var i in ironic_nodes) { if (ironicNodes !== null) {
logger.info('Running poller on :' + ironic_nodes[i].uuid + ':'); for (var i = 0; i < ironicNodes.length; i++) {
self.searchIronic(ironic_nodes[i]); logger.info('Running poller on :' + ironicNodes[i].uuid + ':');
self.searchIronic(ironicNodes[i]);
}
} }
}; };
Poller.prototype.searchIronic = function (ironic_node) { Poller.prototype.searchIronic = function (ironicNode) {
var self = this; var self = this;
var node_data = ironic_node; var nodeData = ironicNode;
try { try {
if (node_data != undefined && if (nodeData !== null &&
node_data.extra && node_data.extra.timer) { nodeData.extra && nodeData.extra.timer) {
if (!node_data.extra.timer.stop) { if (!nodeData.extra.timer.stop) {
var timeNow = new Date(); var timeNow = new Date();
var timeFinished = node_data.extra.timer.finish; var timeFinished = nodeData.extra.timer.finish;
var _timeInterval = node_data.extra.timer.timeInterval; var _timeInterval = nodeData.extra.timer.timeInterval;
var parsedDate = new Date(Date.parse(timeFinished)); var parsedDate = new Date(Date.parse(timeFinished));
var newDate = new Date(parsedDate.getTime() + _timeInterval); var newDate = new Date(parsedDate.getTime() + _timeInterval);
if (newDate < timeNow) { if (newDate < timeNow) {
node_data.extra.timer.start = new Date().toJSON(); nodeData.extra.timer.start = new Date().toJSON();
if (node_data.extra.timer.isDone) { if (nodeData.extra.timer.isDone) {
node_data.extra.timer.isDone = false; nodeData.extra.timer.isDone = false;
return self.updateInfo(self._ironicToken, node_data). return self.updateInfo(self._ironicToken, nodeData).
then(function (data) { then(function (data) {
return self.patchData(node_data.uuid, JSON.stringify(data)); return self.patchData(nodeData.uuid, JSON.stringify(data));
}). }).
then(function (result) { then(function (result) {
return (result); return result;
}); });
} }
} }
} }
} }
return Promise.resolve(null); return Promise.resolve(null);
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
return Promise.resolve(null); return Promise.resolve(null);
}; }
}; };
Poller.prototype.getNodes = function (token) { Poller.prototype.getNodes = function (token) {
return ironic.get_node_list(token). return ironic.get_node_list(token).
then(function (result) { then(function (result) {
return (JSON.parse(result).nodes); return JSON.parse(result).nodes;
}) })
.catch(function (err) { .catch(function (err) {
logger.error(err); logger.error(err);
return (null); return null;
}); });
}; };
@ -97,75 +100,80 @@ function Poller(timeInterval) {
return ironic.patch_node(self._ironicToken, uuid, data). return ironic.patch_node(self._ironicToken, uuid, data).
then(function (result) { then(function (result) {
result = JSON.parse(result); result = JSON.parse(result);
if (result != undefined) { if (typeof result !== 'undefined') {
return (result.extra); return result.extra;
} }
return (null); return null;
}) })
.catch(function (err) { .catch(function (err) {
logger.error(err); logger.error(err);
return (null); return null;
}); });
}; };
Poller.prototype.updateInfo = function (token, node_data) { Poller.prototype.updateInfo = function (token, nodeData) {
return this.getSeldata(node_data.extra.nodeid). return this.getSeldata(nodeData.extra.nodeid).
then(function (result) { then(function (result) {
if (result != null) {
var lastEvent = {}; var lastEvent = {};
if (result !== null) {
result = JSON.parse(result); result = JSON.parse(result);
if (result[0] && result[0].hasOwnProperty('sel')) { if (result[0] && result[0].hasOwnProperty('sel')) {
if (node_data.extra.eventre != undefined) { if (nodeData.extra.eventre !== null) {
var arr = result[0].sel; var arr = result[0].sel;
var events = _.where(arr, { event: node_data.extra.eventre }); var events = _.where(arr, { event: nodeData.extra.eventre });
if (events != undefined) { if (events !== null) {
logger.info(events); logger.info(events);
node_data.extra.eventcnt = events.length; nodeData.extra.eventcnt = events.length;
lastEvent = events[events.length - 1]; lastEvent = events[events.length - 1];
} }
} }
} }
} }
//update finish time //update finish time
node_data.extra.timer.finish = new Date().toJSON(); nodeData.extra.timer.finish = new Date().toJSON();
node_data.extra.timer.isDone = true; nodeData.extra.timer.isDone = true;
node_data.extra.events = lastEvent; nodeData.extra.events = lastEvent;
var data = [{ 'path': '/extra', 'value': node_data.extra, 'op': 'replace' }]; var data = [
return (data); {
'path': '/extra',
'value': nodeData.extra,
'op': 'replace'
}];
return data;
}) })
.catch(function (err) { .catch(function (err) {
logger.error(err); logger.error(err);
return (null); return null;
}); });
} };
Poller.prototype.getSeldata = function (identifier) { Poller.prototype.getSeldata = function (identifier) {
return monorail.request_poller_get(identifier). return monorail.request_poller_get(identifier).
then(function (pollers) { then(function (pollers) {
if (typeof pollers !== 'undefined') { if (typeof pollers !== 'undefined') {
pollers = JSON.parse(pollers); pollers = JSON.parse(pollers);
for (var i in pollers) { return Promise.filter(pollers, function (poller) {
if (pollers[i]['config']['command'] === 'sel') { return poller.config.command === 'sel';
return monorail.request_poller_data_get(pollers[i]['id']). })
then(function (data) { .then(function (sel) {
return (data); if (sel.length > 0) {
return monorail.request_poller_data_get(sel[0].id)
.then(function (data) {
return data;
}) })
.catch(function (e) { .catch(function (e) {
logger.error(e); logger.error(e);
}); });
} }
});
} }
return (null); return null;
}
else {
return (null);
}
}) })
.catch(function (err) { .catch(function (err) {
logger.error(err); logger.error(err);
return (null); return null;
}); });
} };
Poller.prototype.startServer = function () { Poller.prototype.startServer = function () {
var self = this; var self = this;
@ -175,13 +183,12 @@ function Poller(timeInterval) {
.then(function (token) { .then(function (token) {
return self.getNodes(token); return self.getNodes(token);
}) })
.then(function (ironic_nodes) { .then(function (ironicNodes) {
return self.runPoller(ironic_nodes); return self.runPoller(ironicNodes);
}); });
}, self._timeInterval); }, self._timeInterval);
} } catch (err) {
catch (err) {
logger.error(err); logger.error(err);
}; }
}; };
} }

View File

@ -1,17 +1,16 @@
{ {
"name": "", "name": "Shovel",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "RackHD-OpenStack Coordinator",
"main": "index.js", "main": "main.js",
"keywords": [ "keywords": [
"swagger" "swagger"
], ],
"license": "MIT", "license": "Apache-2.0",
"private": true,
"dependencies": { "dependencies": {
"bluebird": "3.1.1",
"connect": "^3.2.0", "connect": "^3.2.0",
"swagger-tools": "0.8.*", "swagger-tools": "0.8.*"
"bluebird": ""
}, },
"devDependencies": { "devDependencies": {
"should": "~7.0.1", "should": "~7.0.1",
@ -24,7 +23,9 @@
"jsonfile": "2.2.3", "jsonfile": "2.2.3",
"crypto": "0.0.3", "crypto": "0.0.3",
"istanbul": "0.4.1", "istanbul": "0.4.1",
"nock": "3.6.0" "nock": "3.6.0",
"eslint-config-openstack": "~1.2.3",
"eslint": "~1.10.3"
}, },
"scripts": { "scripts": {
"postinstall": "scripts/post-install.sh", "postinstall": "scripts/post-install.sh",

View File

@ -16,8 +16,8 @@ describe('****Monorail Lib****',function(){
var rackhdNode =[ { workflows: [], autoDiscover: false, identifiers: ["2c:60:0c:83:f5:d1"], name: "2c:60:0c:83:f5:d1", sku: null, type: "compute", id: "5668b6ad8bee16a10989e4e5" }]; var rackhdNode =[ { workflows: [], autoDiscover: false, identifiers: ["2c:60:0c:83:f5:d1"], name: "2c:60:0c:83:f5:d1", sku: null, type: "compute", id: "5668b6ad8bee16a10989e4e5" }];
var identifier = '123456789'; var identifier = '123456789';
describe('monorail nodeDiskSize get_node_memory_cpu', function () { describe('monorail nodeDiskSize getNodeMemoryCpu', function () {
describe('monorail get_node_memory_cpu', function () { describe('monorail getNodeMemoryCpu', function () {
afterEach('teardown mocks', function () { afterEach('teardown mocks', function () {
//monorail //monorail
monorail['get_catalog_data_by_source'].restore(); monorail['get_catalog_data_by_source'].restore();
@ -25,7 +25,7 @@ describe('****Monorail Lib****',function(){
it('response should returns an integer with value equal to memory size 2000MB and cpus=2', function (done) { it('response should returns an integer with value equal to memory size 2000MB and cpus=2', function (done) {
sinon.stub(monorail, 'get_catalog_data_by_source').returns(Promise.resolve(JSON.stringify(catalogSource[0]))); sinon.stub(monorail, 'get_catalog_data_by_source').returns(Promise.resolve(JSON.stringify(catalogSource[0])));
return monorail.get_node_memory_cpu(rackhdNode). return monorail.getNodeMemoryCpu(rackhdNode).
then(function (result) { then(function (result) {
result.cpus.should.be.exactly(2); result.cpus.should.be.exactly(2);
result.memory.should.be.exactly(2000); result.memory.should.be.exactly(2000);

View File

@ -52,7 +52,7 @@ describe('****SHOVEL API Interface****', function () {
sinon.stub(monorail, 'request_poller_data_get').returns(Promise.resolve(JSON.stringify(_sel))); sinon.stub(monorail, 'request_poller_data_get').returns(Promise.resolve(JSON.stringify(_sel)));
sinon.stub(monorail, 'request_whitelist_del').returns(Promise.resolve('')); sinon.stub(monorail, 'request_whitelist_del').returns(Promise.resolve(''));
sinon.stub(monorail, 'nodeDiskSize').returns(Promise.resolve(0)); sinon.stub(monorail, 'nodeDiskSize').returns(Promise.resolve(0));
sinon.stub(monorail, 'get_node_memory_cpu').returns(Promise.resolve(dmiData)); sinon.stub(monorail, 'getNodeMemoryCpu').returns(Promise.resolve(dmiData));
sinon.stub(monorail, 'get_catalog_data_by_source').returns(Promise.resolve(JSON.stringify(catalogSource[0]))); sinon.stub(monorail, 'get_catalog_data_by_source').returns(Promise.resolve(JSON.stringify(catalogSource[0])));
//glance //glance
sinon.stub(glance, 'get_images').returns(Promise.resolve(JSON.stringify(glanceImages))); sinon.stub(glance, 'get_images').returns(Promise.resolve(JSON.stringify(glanceImages)));
@ -77,7 +77,7 @@ describe('****SHOVEL API Interface****', function () {
monorail['lookupCatalog'].restore(); monorail['lookupCatalog'].restore();
monorail['request_whitelist_del'].restore(); monorail['request_whitelist_del'].restore();
monorail['nodeDiskSize'].restore(); monorail['nodeDiskSize'].restore();
monorail['get_node_memory_cpu'].restore(); monorail['getNodeMemoryCpu'].restore();
monorail['get_catalog_data_by_source'].restore(); monorail['get_catalog_data_by_source'].restore();
//ironic //ironic
ironic['patch_node'].restore(); ironic['patch_node'].restore();
@ -506,7 +506,7 @@ describe('****SHOVEL API Interface****', function () {
afterEach('teardown mocks', function () { afterEach('teardown mocks', function () {
//monorail //monorail
monorail['nodeDiskSize'].restore(); monorail['nodeDiskSize'].restore();
monorail['get_node_memory_cpu'].restore(); monorail['getNodeMemoryCpu'].restore();
monorail['request_node_get'].restore(); monorail['request_node_get'].restore();
//keystone //keystone
keystone['authenticatePassword'].restore(); keystone['authenticatePassword'].restore();
@ -517,7 +517,7 @@ describe('****SHOVEL API Interface****', function () {
it('response in register should have property error_message when any of node info equal to 0 ', function (done) { it('response in register should have property error_message when any of node info equal to 0 ', function (done) {
sinon.stub(monorail, 'request_node_get').returns(Promise.resolve(JSON.stringify(rackhdNode[0]))); sinon.stub(monorail, 'request_node_get').returns(Promise.resolve(JSON.stringify(rackhdNode[0])));
sinon.stub(monorail, 'nodeDiskSize').returns(Promise.resolve(0)); sinon.stub(monorail, 'nodeDiskSize').returns(Promise.resolve(0));
sinon.stub(monorail, 'get_node_memory_cpu').returns(Promise.resolve({ cpus: 0, memory: 0 })); sinon.stub(monorail, 'getNodeMemoryCpu').returns(Promise.resolve({ cpus: 0, memory: 0 }));
request(url) request(url)
.post('/api/1.1/register') .post('/api/1.1/register')
@ -535,7 +535,7 @@ describe('****SHOVEL API Interface****', function () {
it('response in register should have property error_message create node return error in ironic', function (done) { it('response in register should have property error_message create node return error in ironic', function (done) {
sinon.stub(monorail, 'request_node_get').returns(Promise.resolve(JSON.stringify(rackhdNode[0]))); sinon.stub(monorail, 'request_node_get').returns(Promise.resolve(JSON.stringify(rackhdNode[0])));
sinon.stub(monorail, 'nodeDiskSize').returns(Promise.resolve(1)); sinon.stub(monorail, 'nodeDiskSize').returns(Promise.resolve(1));
sinon.stub(monorail, 'get_node_memory_cpu').returns(Promise.resolve({ cpus: 1, memory: 1 })); sinon.stub(monorail, 'getNodeMemoryCpu').returns(Promise.resolve({ cpus: 1, memory: 1 }));
request(url) request(url)
.post('/api/1.1/register') .post('/api/1.1/register')

View File

@ -98,8 +98,8 @@ describe('*****Shovel poller Class****', function () {
}); });
it('pollerInstance.getSeldata() have property sensorNumber, event', function (done) { it('pollerInstance.getSeldata() have property sensorNumber, event', function (done) {
return pollerInstance.getSeldata(identifier). return pollerInstance.getSeldata(identifier)
then(function (data) { .then(function (data) {
var result = JSON.parse(data); var result = JSON.parse(data);
result.should.have.property('sel'); result.should.have.property('sel');
_.each(result[0], function (item) { _.each(result[0], function (item) {