chore: update docker files

This commit is contained in:
Sérgio Ramos 2017-07-17 15:45:14 +01:00
parent ee5a071bd9
commit 3ddc156c18
2 changed files with 110 additions and 75 deletions

View File

@ -4,8 +4,8 @@ const Triton = require('triton');
const Url = require('url');
const Path = require('path');
const Fs = require('fs');
const Data = require('portal-api/lib/data');
const Data = require('./lib/data');
const {
DOCKER_HOST,
@ -15,9 +15,7 @@ const {
SDC_KEY_ID
} = process.env;
const DOCKER_HOST_URL = DOCKER_HOST ?
Url.parse(DOCKER_HOST) :
{};
const DOCKER_HOST_URL = DOCKER_HOST ? Url.parse(DOCKER_HOST) : {};
const settings = {
db: {
@ -27,15 +25,15 @@ const settings = {
protocol: 'https',
host: DOCKER_HOST_URL.hostname,
port: DOCKER_HOST_URL.port,
ca: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'ca.pem')) :
undefined,
cert: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'cert.pem')) :
undefined,
key: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'key.pem')) :
undefined
ca: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'ca.pem'))
: undefined,
cert: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'cert.pem'))
: undefined,
key: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'key.pem'))
: undefined
},
triton: {
url: SDC_URL,
@ -44,45 +42,54 @@ const settings = {
}
};
const ifError = function (err) {
const ifError = function(err) {
if (err) {
console.error(err);
process.exit(1);
}
};
const bootstrap = function () {
const bootstrap = function() {
const data = new Data(settings);
const region = process.env.TRITON_DC || 'us-sw-1';
data.connect((err) => {
data.connect(err => {
ifError(err);
data.createDatacenter({ region, name: region }, (err, datacenter) => {
ifError(err);
Triton.createClient({
profile: settings.triton
}, (err, { cloudapi }) => {
ifError(err);
cloudapi.getAccount((err, { firstName, lastName, email, login }) => {
Triton.createClient(
{
profile: settings.triton
},
(err, { cloudapi }) => {
ifError(err);
data.createUser({ firstName, lastName, email, login }, (err, user) => {
cloudapi.getAccount((err, { firstName, lastName, email, login }) => {
ifError(err);
data.createPortal({
user,
datacenter
}, (err, portal) => {
ifError(err);
console.log('data bootstrapped');
process.exit(0);
});
data.createUser(
{ firstName, lastName, email, login },
(err, user) => {
ifError(err);
data.createPortal(
{
user,
datacenter
},
(err, portal) => {
ifError(err);
console.log('data bootstrapped');
process.exit(0);
}
);
}
);
});
});
});
}
);
});
});
};

View File

@ -1,44 +1,61 @@
'use strict';
const Fs = require('fs');
const Path = require('path');
const Brule = require('brule');
const Good = require('good');
const Hapi = require('hapi');
const HapiSwagger = require('hapi-swagger');
const Inert = require('inert');
const Toppsy = require('toppsy');
const Vision = require('vision');
const Pack = require('./package');
const Portal = require('portal-api');
const Portal = require('./lib');
const Path = require('path');
const Fs = require('fs');
const Url = require('url');
const server = new Hapi.Server();
server.connection({ port: process.env.PORT || 3000 });
server.connection({ port: 3000 });
const swaggerOptions = {
info: {
title: 'Portal API Documentation',
version: Pack.version
}
};
const {
DOCKER_HOST,
DOCKER_CERT_PATH,
DOCKER_CLIENT_TIMEOUT,
SDC_URL,
SDC_ACCOUNT,
SDC_KEY_ID
} = process.env;
const DOCKER_HOST_URL = DOCKER_HOST ? Url.parse(DOCKER_HOST) : {};
const portalOptions = {
data: {
db: {
host: process.env.RETHINK_HOST || 'localhost'
},
docker: {
host: DOCKER_HOST,
ca: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'ca.pem')) :
undefined,
cert: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'cert.pem')) :
undefined,
key: DOCKER_CERT_PATH ?
Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'key.pem')) :
undefined,
timeout: DOCKER_CLIENT_TIMEOUT
protocol: 'https',
host: DOCKER_HOST_URL.hostname,
port: DOCKER_HOST_URL.port,
ca: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'ca.pem'))
: undefined,
cert: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'cert.pem'))
: undefined,
key: DOCKER_CERT_PATH
? Fs.readFileSync(Path.join(DOCKER_CERT_PATH, 'key.pem'))
: undefined
},
triton: {
url: SDC_URL,
account: SDC_ACCOUNT,
keyId: SDC_KEY_ID
}
},
watch: {
@ -53,41 +70,52 @@ const goodOptions = {
interval: 1000
},
reporters: {
consoleReporter: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ log: '*', response: '*', error: '*' }]
}, {
module: 'good-console'
}, 'stdout']
consoleReporter: [
{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ log: '*', response: '*', error: '*' }]
},
{
module: 'good-console'
},
'stdout'
]
}
};
server.register([
Brule,
{
register: Good,
options: goodOptions
},
{
register: Portal,
options: portalOptions
},
{
register: Toppsy,
options: { namespace: 'portal', subsystem: 'api' }
}
],
(err) => {
server.register(
[
Brule,
Inert,
Vision,
{
register: Good,
options: goodOptions
},
{
register: Portal,
options: portalOptions
},
{
register: HapiSwagger,
options: swaggerOptions
},
{
register: Toppsy,
options: { namespace: 'portal', subsystem: 'api' }
}
],
err => {
handlerError(err);
server.start((err) => {
server.start(err => {
handlerError(err);
console.log(`server started at http://localhost:${server.info.port}`);
});
}
);
function handlerError (error) {
function handlerError(error) {
if (error) {
console.error(error);
process.exit(1);