Skip to content

Commit

Permalink
Merged in JRD-331-mc-acionar-ipe (pull request #1)
Browse files Browse the repository at this point in the history
[JRD-331] mc acionar ipe
  • Loading branch information
alinekaori committed Sep 17, 2015
2 parents 1e5a02a + 49a02ab commit ba14b22
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 16 deletions.
94 changes: 94 additions & 0 deletions app/controllers/infra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';
var CJM = require('carbono-json-messages');
var uuid = require('node-uuid');
var bo = require('../lib/mission-control-bo');
var pjson = require('../../package.json');

module.exports = function (app, etcdManager) {

this.createContainer = function (req, res) {

var projectId = req.body.projectId;
var machineAlias = req.body.machineAlias;
var cjm = null;
var error = null;

// Check data consistency
// @todo Create an definitive helper for handling errors.
// There are too much repeated code in the repositories.
if (!projectId || !machineAlias) {
cjm = new CJM({apiVersion: pjson.version});
res.status(400);
error = {
code: 400,
message: 'Missing mandatory parameters',
errors:
'projectId and machineAlias should have an valid value',
};
cjm.setError(error);
res.json(cjm);
res.end();
return;
}

// Get the IPE module URL
var ipeURL = etcdManager.getIpeUrl();
if (!ipeURL) {
cjm = new CJM({apiVersion: pjson.version});
error = {
code: 500,
message: 'Internal data is missing',
errors:
'ipeURL is not set in etcdManager, I can\'t send the request!',
};
res.status(500);
cjm.setError(error);
res.json(cjm);
res.end();
return;
}

// Now we can make the call to the business object.
bo.createUserContainer(ipeURL, projectId, machineAlias,
/**
* Callback function
*/
function (err, ret) {
var cjm = new CJM({apiVersion: pjson.version});
try {
if (err) {
res.status(400);
var error = {
code: 400,
message: 'Could not create container',
errors: err,
};
cjm.setError(error);
} else {
// Probably an empty answer, but let's send it anyway
cjm.setData(
{
id: uuid.v4(),
items: [ret],
}
);
}
res.json(cjm);
res.end();
} catch (e) {
// Unknown error...
var error = {
code: 500,
message: 'Unknown internal error',
errors: e,
};
cjm.setError(error);
res.json(cjm);
res.status(500).end();
}
}
);
};

return this;
};
60 changes: 49 additions & 11 deletions app/lib/etcd-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ require('colors');

var MC_SERVICE_KEY = 'mc';
var DCM_SERVICE_KEY = 'dcm';
var IPE_SERVICE_KEY = 'ipe';

var dcmURL = null;
var ipeURL = null;
var serviceManager = null;
var registered = false;

Expand Down Expand Up @@ -38,6 +40,7 @@ EtcdManager.prototype.init = function () {
serviceManager = new ServiceManager(process.env.ETCD_SERVER);

this.findDCM();
this.findIPE();
this.register();
}
};
Expand Down Expand Up @@ -68,26 +71,52 @@ EtcdManager.prototype.register = function () {
};

/**
* Try to find Development Container Manager ('dcm') service. It saves the
* URL at this.dcmURL.
* (Internal) Helper function to find specifics services in Etcd Manager
*
* @function
* @param serviceKey The Key to be found in the Etdc Manager
* @param serviceName Friendly name to the service, only useful for logging
* @param _cb Callback function which will receive the service URL
*/
EtcdManager.prototype.findDCM = function () {
function serviceFinder(serviceKey, serviceName, _cb) {
serviceName = serviceName || serviceKey;
if (serviceManager) {
var promise = serviceManager.findService(DCM_SERVICE_KEY);
var promise = serviceManager.findService(serviceKey);

promise.then(
function (url) {
console.log('Development Container Manager found at etcd'
.green);
dcmURL = url;
var msg = serviceName + 'found at etcd';
console.log(msg.green);
_cb(url);
}, function (err) {
console.log('[ERROR] Finding DCM with etcd: '.red +
JSON.stringify(err));
dcmURL = null;
var msg = '[ERROR] Finding ' + serviceName + ' with etcd: ';
console.log(msg.red + JSON.stringify(err));
_cb(null);
});
} else {
_cb(null);
}
}

/**
* Try to find Development Container Manager ('dcm') service. It saves the
* URL at this.dcmURL.
*
* @function
*/
EtcdManager.prototype.findDCM = function () {
serviceFinder(DCM_SERVICE_KEY, 'Development Container Manager',
function (url) {
dcmURL = url;
});
};

/**
* Try to find the Mogno-IPE host ('ipe'). It saves the URL at this.ipeURL.
*/
EtcdManager.prototype.findIPE = function () {
serviceFinder(IPE_SERVICE_KEY, 'Mogno IPE', function (url) {
ipeURL = url;
});
};

/**
Expand All @@ -100,4 +129,13 @@ EtcdManager.prototype.getDcmUrl = function () {
return dcmURL;
};

/**
* Try to get IPE url retrieved by etcd.
*
* @return {string} ipeURL A url to access the mogno-IPE address
*/
EtcdManager.prototype.getIpeUrl = function () {
return ipeURL;
};

module.exports = EtcdManager;
18 changes: 18 additions & 0 deletions app/lib/mission-control-bo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var CM = require('./code-machine-cli');
var dcm = require('./ide-development-container-manager-cli');
var ipe = require('./mogno-ipe-client');

/**
* Pedir uma maquina pro dcm e nesse momento criar uma
Expand Down Expand Up @@ -41,3 +42,20 @@ exports.createDevContainer = function (dcmURL, project, cb) {
cb(err, res, cm);
});
};

/**
* Sends an message to the IPE module requesting an specific machine to be
* instantiated
*/
exports.createUserContainer = function (ipeURL, project, machineAlias, cb) {

// NOTE: The machineAlias is not necessarily the docker image name.
// This is the place where we sholud map machineAlias to image name
// (Assuming the b.o. is the one who knows everything)
var dockerImage = machineAlias;

ipe.create(ipeURL, project, dockerImage, function (err, res) {
// Nothing else to do?
cb(err, res);
});
};
77 changes: 77 additions & 0 deletions app/lib/mogno-ipe-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
var request = require('request');
var CJM = require('carbono-json-messages');
var pjson = require('../../package.json');

/**
* Creates and/or retrieves an docker instance and
* returns a callback function.
*
* @todo Parse errors and throw them.
* @todo Use carbono-json-messages
*
* @param {string} ipeURL - URL retrieved from etcd.
* @param {string} proj - Id of the project
* @param {onbject} cb - Callback function that receives errors and/or
* other information
*
* @return function (err, res)
*/
exports.create = function (ipeURL, proj, alias, cb) {
if (ipeURL) {
var url = ipeURL + '/container';
var headers = {
'Content-Type': 'application/json',
};
var aux = {
apiVersion: pjson.version,
id: 'xyzzy-wyzzy',
data: {
id: '1337',
items:
[
{
projectId: proj,
machineAlias: alias,
},
],
},
};
var load = {
url: url,
headers: headers,
json: aux,
};

/**
* Callback function that tries to parse (JSON) the body object
*
* @param {Object} err - Error object
* @param {Object} httpResponse - Response object
* @param {string} body - String containing a JSON structure
*/
var _cb = function (err, httpResponse, body) {
try {
body = JSON.parse(body);
} catch (err) {
this.err = err;
}
cb(err, body);
};
request.post(load, _cb);

} else {
// If there is no IPE reference, nothing can be done. Sends an Error.
var cjm = new CJM({apiVersion: pjson.version});
try {
var err = {
code: 400,
message: 'ipeURL is required for this operation',
};
cjm.setError(err);
cb(cjm, null);
} catch (e) {
cb(e, null);
}
}
};
8 changes: 8 additions & 0 deletions app/routes/infra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
module.exports = function (app) {
var infra = app.controllers.infra;

app.post('/createContainer', infra.createContainer);

return this;
};
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
'use strict';
var consign = require('consign');
var express = require('express');
var config = require('config');
var ws = require('socket.io');
var app = express();
var consign = require('consign');
var express = require('express');
var config = require('config');
var ws = require('socket.io');
var bodyParser = require('body-parser');
var app = express();

var htPort = config.get('htPort');
var wsPort = config.get('wsPort');

var EtcdManager = require('./app/lib/etcd-manager.js');
var etcdManager = new EtcdManager();

// Parse JSON data in post requests
app.use(bodyParser.json());

app.ws = ws(wsPort);

consign({cwd: 'app'})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"license": "MIT",
"dependencies": {
"body-parser": "^1.13.3",
"carbono-json-messages": "0.0.1",
"carbono-service-manager": "^0.0.3",
"colors": "^1.1.2",
Expand Down
27 changes: 27 additions & 0 deletions test/test-1-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,33 @@ describe('Routing tests --> ', function () {
});
}
);

});

describe('IPE calls ar working well when:', function() {
it('I can request an specific container',
function(done){
var load = {
projectId: 'myProject001',
machineAlias: 'simonfan/code-machine'
};
server
.post('/createContainer', load)
.expect(200);
done();
}
);

it('Previous request should fail when I forget important data',
function(done){
var load = {
projectId: 'myProject001',
}
server
.post('/createContainer', load)
.expect(400)
done();
}
);
})
});

0 comments on commit ba14b22

Please sign in to comment.