1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-15 07:40:07 +02:00
copilot/packages/portal-api/lib/index.js

120 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-04-28 00:59:03 +03:00
'use strict';
2017-05-27 19:35:38 +03:00
const Schema = require('joyent-cp-gql-schema');
2017-05-23 03:13:15 +03:00
const Graphi = require('graphi');
const Hoek = require('hoek');
const Piloted = require('piloted');
const Data = require('./data');
2017-04-28 00:59:03 +03:00
const Pack = require('../package.json');
2017-05-27 19:35:38 +03:00
const Resolvers = require('./resolvers');
const ContainerPilotWatcher = require('./watch/container-pilot');
const MachinesWatcher = require('./watch/machines');
2017-04-28 00:59:03 +03:00
const internals = {
namespace: process.env.NAMESPACE ? `/${process.env.NAMESPACE}` : '',
defaults: {
data: {
db: {
host: 'localhost'
},
dockerComposeHost: 'tcp://0.0.0.0:4242'
},
watch: {
url: process.env.SDC_URL,
account: process.env.SDC_ACCOUNT,
keyId: process.env.SDC_KEY_ID
}
}
};
2017-04-28 00:59:03 +03:00
module.exports = function (server, options, next) {
const settings = Hoek.applyToDefaults(internals.defaults, options || {});
try {
const docker = Piloted.service('docker-compose-api');
if (docker) {
settings.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`;
}
const rethinkdb = Piloted.service('rethinkdb');
if (rethinkdb) {
settings.data.db.host = rethinkdb.address;
}
} catch (ex) {
server.log(['error'], ex);
}
settings.watch.server = server;
settings.data.server = server;
const data = new Data(settings.data);
2017-09-15 03:08:35 +03:00
const watcherOptions = settings.watch;
watcherOptions.data = data;
const cpWatcher = new ContainerPilotWatcher(watcherOptions);
const machinesWatcher = new MachinesWatcher(watcherOptions);
// watcher <-> watcher
// portal depends on watcher and vice-versa
// I'm sure there is a better way to organize this domains
// but this works for now
data.setMachinesWatcher(machinesWatcher);
cpWatcher.on('error', (err) => {
server.log(['error'], err);
});
data.on('error', (err) => {
server.log(['error'], err);
});
2017-05-02 01:28:03 +03:00
data.connect((err) => {
if (err) {
return next(err);
}
2017-04-28 00:59:03 +03:00
server.bind(data);
2017-05-02 01:28:03 +03:00
Piloted.on('refresh', internals.refresh(data));
machinesWatcher.poll();
cpWatcher.poll();
2017-05-02 01:28:03 +03:00
server.register([
{
2017-05-23 03:13:15 +03:00
register: Graphi,
2017-05-27 19:35:38 +03:00
options: {
graphqlPath: `${internals.namespace}/graphql`,
graphiqlPath: `${internals.namespace}/graphiql`,
2017-05-27 19:35:38 +03:00
schema: Schema,
resolvers: Resolvers(data)
}
2017-04-28 00:59:03 +03:00
}
2017-05-02 01:28:03 +03:00
]);
next();
});
2017-04-28 00:59:03 +03:00
};
module.exports.attributes = {
name: Pack.name,
version: Pack.version,
once: true,
multiple: false
};
internals.refresh = function (data) {
return () => {
const docker = Piloted.service('docker-compose-api');
if (docker) {
data.reconnectCompose(`tcp://${docker.address}:${docker.port}`);
}
const rethinkdb = Piloted.service('rethinkdb');
if (rethinkdb) {
data.reconnectDb({ host: rethinkdb.address });
}
};
2017-06-21 22:12:57 +03:00
};