chore: more stable db connect/bootstrap

This commit is contained in:
geek 2017-09-12 20:39:39 -05:00 committed by Sérgio Ramos
parent 87bd45b415
commit a58be6be37
10 changed files with 64 additions and 43 deletions

View File

@ -28,7 +28,7 @@ consul:
# it is included here for demo purposes and is not required # it is included here for demo purposes and is not required
############################################################################# #############################################################################
prometheus: prometheus:
image: autopilotpattern/prometheus:1.7.1-r20 image: autopilotpattern/prometheus:1.7.1-r24
restart: always restart: always
mem_limit: 1g mem_limit: 1g
ports: ports:
@ -46,7 +46,7 @@ prometheus:
# FRONTEND # FRONTEND
############################################################################# #############################################################################
frontend: frontend:
image: joyent/copilot-frontend:1.0.0 image: joyent/copilot-frontend:1.3.4
mem_limit: 512m mem_limit: 512m
links: links:
- consul:consul - consul:consul
@ -66,7 +66,7 @@ frontend:
# BACKEND # BACKEND
############################################################################# #############################################################################
api: api:
image: joyent/copilot-api:1.1.0 image: joyent/copilot-api:1.8.5
mem_limit: 512m mem_limit: 512m
links: links:
- consul:consul - consul:consul
@ -83,7 +83,7 @@ api:
# Docker-compose wrapper # Docker-compose wrapper
# Create _env file from running ./setup.sh # Create _env file from running ./setup.sh
compose-api: compose-api:
image: joyent/copilot-compose:1.0.0 image: joyent/copilot-compose:1.1.0
links: links:
- consul:consul - consul:consul
expose: expose:

View File

@ -17,8 +17,8 @@ RUN curl --retry 7 --fail -vo /tmp/consul.zip "https://releases.hashicorp.com/co
&& mkdir /config && mkdir /config
# Install Containerpilot # Install Containerpilot
ENV CONTAINERPILOT_VERSION 3.4.1 ENV CONTAINERPILOT_VERSION 3.4.2
RUN export CONTAINERPILOT_CHECKSUM=4d13cfb345de86135ab2271b77516c6b6a7bed3a \ RUN export CONTAINERPILOT_CHECKSUM=5c99ae9ede01e8fcb9b027b5b3cb0cfd8c0b8b88 \
&& export archive=containerpilot-${CONTAINERPILOT_VERSION}.tar.gz \ && export archive=containerpilot-${CONTAINERPILOT_VERSION}.tar.gz \
&& curl -Lso /tmp/${archive} \ && curl -Lso /tmp/${archive} \
"https://github.com/joyent/containerpilot/releases/download/${CONTAINERPILOT_VERSION}/${archive}" \ "https://github.com/joyent/containerpilot/releases/download/${CONTAINERPILOT_VERSION}/${archive}" \

View File

@ -13,13 +13,24 @@ const loadConfig = function () {
const docker = Piloted.service('docker-compose-api'); const docker = Piloted.service('docker-compose-api');
const rethink = Piloted.service('rethinkdb'); const rethink = Piloted.service('rethinkdb');
if (docker && rethink) { const retry = () => {
bootstrap({ docker, rethink });
} else if (!timeoutId) {
timeoutId = setTimeout(() => { timeoutId = setTimeout(() => {
timeoutId = null; timeoutId = null;
Piloted.refresh(); Piloted.refresh();
}, 1000); }, 1000);
};
if (docker && rethink) {
bootstrap({ docker, rethink }, (err) => {
if (err) {
console.error(err);
return retry();
}
process.exit(0);
});
} else if (!timeoutId) {
retry();
} }
}; };
@ -28,7 +39,7 @@ Piloted.on('refresh', () => {
}); });
const bootstrap = function ({ docker, rethink }) { const bootstrap = function ({ docker, rethink }, cb) {
const settings = { const settings = {
db: { db: {
host: rethink.address host: rethink.address
@ -57,27 +68,24 @@ const bootstrap = function ({ docker, rethink }) {
const data = new Data(settings); const data = new Data(settings);
const region = process.env.TRITON_DC || 'us-sw-1'; const region = process.env.TRITON_DC || 'us-sw-1';
data.connect(err => { data.connect((err) => {
if (err) { if (err) {
console.error(err); return cb(err);
return;
} }
data.getDatacenters((err, datacenters) => { data.getDatacenters((err, datacenters) => {
if (err) { if (err) {
console.error(err); return cb(err);
return;
} }
// Don't continue since data is already bootstrapped
if (datacenters && datacenters.length) { if (datacenters && datacenters.length) {
process.exit(0); return cb();
return;
} }
data.createDatacenter({ region, name: region }, (err, datacenter) => { data.createDatacenter({ region, name: region }, (err, datacenter) => {
if (err) { if (err) {
console.error(err); return cb(err);
return;
} }
Triton.createClient( Triton.createClient(
@ -86,22 +94,19 @@ const bootstrap = function ({ docker, rethink }) {
}, },
(err, { cloudapi }) => { (err, { cloudapi }) => {
if (err) { if (err) {
console.error(err); return cb(err);
return;
} }
cloudapi.getAccount((err, { firstName, lastName, email, login }) => { cloudapi.getAccount((err, { firstName, lastName, email, login }) => {
if (err) { if (err) {
console.error(err); return cb(err);
return;
} }
data.createUser( data.createUser(
{ firstName, lastName, email, login }, { firstName, lastName, email, login },
(err, user) => { (err, user) => {
if (err) { if (err) {
console.error(err); return cb(err);
return;
} }
data.createPortal( data.createPortal(
@ -111,12 +116,11 @@ const bootstrap = function ({ docker, rethink }) {
}, },
(err, portal) => { (err, portal) => {
if (err) { if (err) {
console.error(err); return cb(err);
return;
} }
console.log('data bootstrapped'); console.log('data bootstrapped');
process.exit(0); cb();
} }
); );
} }

View File

@ -36,8 +36,8 @@
'-log-level=err', '-log-level=err',
'-rejoin', '-rejoin',
'-retry-join', '{{ .CONSUL | default "consul" }}', '-retry-join', '{{ .CONSUL | default "consul" }}',
'-retry-max', '10', '-retry-max', '20',
'-retry-interval', '10s'], '-retry-interval', '5s'],
restarts: 'unlimited' restarts: 'unlimited'
}, },
{ {

View File

@ -16,12 +16,12 @@
"good-console": "^6.4.0", "good-console": "^6.4.0",
"good-squeeze": "^5.0.2", "good-squeeze": "^5.0.2",
"graphi": "^2.3.0", "graphi": "^2.3.0",
"hapi": "^16.1.0", "hapi": "^16.6.0",
"hoek": "^4.1.1", "hoek": "^4.1.1",
"joi": "^10.6.0", "joi": "^10.6.0",
"joyent-cp-gql-schema": "^1.7.0", "joyent-cp-gql-schema": "^1.7.0",
"piloted": "^3.1.1", "piloted": "^3.1.1",
"portal-api": "^1.8.2", "portal-api": "^1.8.5",
"toppsy": "^1.1.0", "toppsy": "^1.1.0",
"triton": "^5.2.0" "triton": "^5.2.0"
} }

View File

@ -18,8 +18,6 @@ consul:
mem_limit: 128m mem_limit: 128m
ports: ports:
- 8500:8500 - 8500:8500
dns:
- 127.0.0.1
############################################################################# #############################################################################
# PROMETHEUS # PROMETHEUS
@ -28,7 +26,7 @@ consul:
# it is included here for demo purposes and is not required # it is included here for demo purposes and is not required
############################################################################# #############################################################################
prometheus: prometheus:
image: autopilotpattern/prometheus:1.7.1-r20 image: autopilotpattern/prometheus:1.7.1-r24
restart: always restart: always
mem_limit: 1g mem_limit: 1g
ports: ports:
@ -38,8 +36,6 @@ prometheus:
environment: environment:
- CONSUL=consul - CONSUL=consul
- CONSUL_AGENT=1 - CONSUL_AGENT=1
dns:
- 127.0.0.1
############################################################################# #############################################################################
@ -58,8 +54,6 @@ frontend:
ports: ports:
- "80:80" - "80:80"
- "443:443" - "443:443"
dns:
- 127.0.0.1
############################################################################# #############################################################################

View File

@ -148,7 +148,26 @@ class Data extends EventEmitter {
} }
connect (cb) { connect (cb) {
if (!this._db._connection) {
return this._db._connect((err) => {
if (err) {
return cb(err);
}
return this.connect(cb);
});
}
this._db._exists((err, exists) => {
if (exists) {
const tables = this._db._normalizeTables(internals.tables);
this._db.table(tables);
return cb();
}
this._db.establish(internals.tables, cb); this._db.establish(internals.tables, cb);
});
} }
reconnectCompose (dockerComposeHost) { reconnectCompose (dockerComposeHost) {

View File

@ -110,7 +110,7 @@ module.exports = class ContainerPilotWatcher extends Events {
}; };
const getDeploymentGroups = (err, portal) => { const getDeploymentGroups = (err, portal) => {
if (err) { if (err || !portal) {
return cb(err); return cb(err);
} }
@ -519,6 +519,10 @@ module.exports = class ContainerPilotWatcher extends Events {
return cb(); return cb();
} }
if (!dgs || !dgs.length) {
return cb();
}
VAsync.forEachParallel({ VAsync.forEachParallel({
inputs: dgs, inputs: dgs,
func: (dg, next) => { func: (dg, next) => {

View File

@ -1,6 +1,6 @@
{ {
"name": "portal-api", "name": "portal-api",
"version": "1.8.2", "version": "1.8.5",
"description": "", "description": "",
"main": "./lib/index.js", "main": "./lib/index.js",
"scripts": { "scripts": {

View File

@ -81,7 +81,7 @@ frontend:
# BACKEND # BACKEND
############################################################################# #############################################################################
api: api:
image: joyent/copilot-api:1.8.2 image: joyent/copilot-api:1.8.5
mem_limit: 1g mem_limit: 1g
expose: expose:
- 3000 - 3000