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');
|
2017-08-10 22:56:29 +03:00
|
|
|
const Hoek = require('hoek');
|
2017-06-21 21:51:28 +03:00
|
|
|
const Piloted = require('piloted');
|
2017-06-28 20:33:34 +03:00
|
|
|
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');
|
2017-07-11 19:59:25 +03:00
|
|
|
const ContainerPilotWatcher = require('./watch/container-pilot');
|
|
|
|
const MachinesWatcher = require('./watch/machines');
|
2017-04-28 00:59:03 +03:00
|
|
|
|
2017-06-21 21:51:28 +03:00
|
|
|
|
2017-08-10 22:56:29 +03:00
|
|
|
const internals = {
|
|
|
|
namespace: process.env.NAMESPACE ? `/${process.env.NAMESPACE}` : '',
|
|
|
|
defaults: {
|
|
|
|
data: {
|
2017-08-21 18:52:57 +03:00
|
|
|
db: {
|
|
|
|
host: 'localhost'
|
2017-08-22 16:38:01 +03:00
|
|
|
},
|
|
|
|
dockerComposeHost: 'tcp://0.0.0.0:4242'
|
2017-08-10 22:56:29 +03:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
url: process.env.SDC_URL,
|
|
|
|
account: process.env.SDC_ACCOUNT,
|
|
|
|
keyId: process.env.SDC_KEY_ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-06-21 21:51:28 +03:00
|
|
|
|
2017-04-28 00:59:03 +03:00
|
|
|
module.exports = function (server, options, next) {
|
2017-08-10 22:56:29 +03:00
|
|
|
const settings = Hoek.applyToDefaults(internals.defaults, options || {});
|
|
|
|
|
2017-06-29 01:44:14 +03:00
|
|
|
try {
|
2017-09-16 00:36:31 +03:00
|
|
|
const docker = Piloted.service('compose-api');
|
2017-06-29 01:44:14 +03:00
|
|
|
if (docker) {
|
2017-08-10 22:56:29 +03:00
|
|
|
settings.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rethinkdb = Piloted.service('rethinkdb');
|
|
|
|
if (rethinkdb) {
|
|
|
|
settings.data.db.host = rethinkdb.address;
|
2017-06-29 01:44:14 +03:00
|
|
|
}
|
2017-06-29 21:19:01 +03:00
|
|
|
} catch (ex) {
|
2017-08-10 22:56:29 +03:00
|
|
|
server.log(['error'], ex);
|
2017-06-21 21:51:28 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 22:56:29 +03:00
|
|
|
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);
|
2017-06-22 20:09:13 +03:00
|
|
|
|
2017-06-26 17:29:12 +03:00
|
|
|
// 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
|
2017-07-11 19:59:25 +03:00
|
|
|
data.setMachinesWatcher(machinesWatcher);
|
2017-06-26 17:29:12 +03:00
|
|
|
|
2017-07-11 19:59:25 +03:00
|
|
|
cpWatcher.on('error', (err) => {
|
2017-06-29 21:19:01 +03:00
|
|
|
server.log(['error'], err);
|
|
|
|
});
|
|
|
|
|
2017-06-22 20:08:13 +03:00
|
|
|
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
|
|
|
|
2017-06-26 17:29:12 +03:00
|
|
|
server.bind(data);
|
2017-05-02 01:28:03 +03:00
|
|
|
|
2017-06-21 21:51:28 +03:00
|
|
|
Piloted.on('refresh', internals.refresh(data));
|
2017-07-11 19:59:25 +03:00
|
|
|
|
|
|
|
machinesWatcher.poll();
|
|
|
|
cpWatcher.poll();
|
2017-06-21 21:51:28 +03:00
|
|
|
|
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: {
|
2017-08-10 22:56:29 +03:00
|
|
|
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
|
|
|
|
};
|
2017-06-21 21:51:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
internals.refresh = function (data) {
|
|
|
|
return () => {
|
2017-09-16 00:36:31 +03:00
|
|
|
const docker = Piloted.service('compose-api');
|
2017-08-10 22:56:29 +03:00
|
|
|
if (docker) {
|
|
|
|
data.reconnectCompose(`tcp://${docker.address}:${docker.port}`);
|
2017-06-21 21:51:28 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 22:56:29 +03:00
|
|
|
const rethinkdb = Piloted.service('rethinkdb');
|
|
|
|
if (rethinkdb) {
|
|
|
|
data.reconnectDb({ host: rethinkdb.address });
|
|
|
|
}
|
2017-06-21 21:51:28 +03:00
|
|
|
};
|
2017-06-21 22:12:57 +03:00
|
|
|
};
|