From be842aba3185afcf8e018e75726276b64c413e43 Mon Sep 17 00:00:00 2001 From: geek Date: Thu, 22 Jun 2017 12:08:13 -0500 Subject: [PATCH] bug(portal-data): support compose reconnects --- local-compose.yml | 2 -- packages/portal-api/Dockerfile | 2 ++ packages/portal-api/bin/prestart.sh | 13 +++++++++++++ packages/portal-api/etc/containerpilot.json5 | 13 +++++++++++-- packages/portal-api/lib/index.js | 6 +++++- packages/portal-data/lib/index.js | 9 +++++++++ setup.sh | 4 ++++ 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100755 packages/portal-api/bin/prestart.sh diff --git a/local-compose.yml b/local-compose.yml index f5d1b793..16263032 100644 --- a/local-compose.yml +++ b/local-compose.yml @@ -54,8 +54,6 @@ docker-compose-api: environment: - CONSUL=consul restart: always - dns: - - 127.0.0.1 traefik: image: d0cker/traefik diff --git a/packages/portal-api/Dockerfile b/packages/portal-api/Dockerfile index 98d992c5..62f0f2b0 100644 --- a/packages/portal-api/Dockerfile +++ b/packages/portal-api/Dockerfile @@ -14,6 +14,8 @@ RUN npm install -g npm@^4 \ && ./node_modules/.bin/lerna bootstrap --scope portal-api --include-filtered-dependencies COPY packages/portal-api/etc/containerpilot.json5 ${CONTAINERPILOT} +COPY packages/portal-api/bin /bin + WORKDIR /opt/app/packages/portal-api CMD ["/bin/containerpilot"] diff --git a/packages/portal-api/bin/prestart.sh b/packages/portal-api/bin/prestart.sh new file mode 100755 index 00000000..795c45b4 --- /dev/null +++ b/packages/portal-api/bin/prestart.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Copy creds from env vars to files on disk +if [ -n ${!TRITON_CREDS_PATH} ] \ + && [ -n ${!TRITON_CA} ] \ + && [ -n ${!TRITON_CERT} ] \ + && [ -n ${!TRITON_KEY} ] +then + mkdir -p ${TRITON_CREDS_PATH} + echo -e "${TRITON_CA}" | tr '#' '\n' > ${TRITON_CREDS_PATH}/ca.pem + echo -e "${TRITON_CERT}" | tr '#' '\n' > ${TRITON_CREDS_PATH}/cert.pem + echo -e "${TRITON_KEY}" | tr '#' '\n' > ${TRITON_CREDS_PATH}/key.pem +fi diff --git a/packages/portal-api/etc/containerpilot.json5 b/packages/portal-api/etc/containerpilot.json5 index b989de6e..5cb14dcd 100644 --- a/packages/portal-api/etc/containerpilot.json5 +++ b/packages/portal-api/etc/containerpilot.json5 @@ -1,9 +1,17 @@ { consul: 'localhost:8500', jobs: [ + { + "name": "setup-config", + "exec": "/bin/prestart.sh" + }, { name: 'bootstrap', - exec: 'node bootstrap-data.js' + exec: 'node bootstrap-data.js', + when: { + source: 'setup-config', + once: 'exitSuccess' + } }, { name: 'api', @@ -22,7 +30,8 @@ when: { source: 'bootstrap', once: 'exitSuccess' - } + }, + restarts: 'unlimited' }, { name: 'consul-agent', diff --git a/packages/portal-api/lib/index.js b/packages/portal-api/lib/index.js index 07521aaf..d0e0c13c 100644 --- a/packages/portal-api/lib/index.js +++ b/packages/portal-api/lib/index.js @@ -24,6 +24,10 @@ module.exports = function (server, options, next) { data })); + data.on('error', (err) => { + server.log(['error'], err); + }); + data.connect((err) => { if (err) { return next(err); @@ -65,6 +69,6 @@ internals.refresh = function (data) { return; } - data._dockerCompose.client.connect(`tcp://${docker.address}:${docker.port}`); + data.reconnectCompose(`tcp://${docker.address}:${docker.port}`); }; }; diff --git a/packages/portal-data/lib/index.js b/packages/portal-data/lib/index.js index 59edb5e8..73a66bb0 100644 --- a/packages/portal-data/lib/index.js +++ b/packages/portal-data/lib/index.js @@ -61,6 +61,15 @@ module.exports = class Data extends EventEmitter { this._db.establish(internals.tables, cb); } + reconnectCompose (dockerComposeHost) { + this._dockerCompose.close(); + this._dockerCompose = new DockerClient(dockerComposeHost); + + this._dockerCompose.on('error', (err) => { + this.emit('error', err); + }); + } + // portals diff --git a/setup.sh b/setup.sh index ff96ea4d..3b8ccf42 100755 --- a/setup.sh +++ b/setup.sh @@ -31,6 +31,10 @@ check() { TRITON_CREDS_PATH=/root/.triton echo TRITON_CREDS_PATH=${TRITON_CREDS_PATH} >> _env echo DOCKER_CERT_PATH=${TRITON_CREDS_PATH} >> _env + echo DOCKER_TLS_VERIFY=1 >> _env + echo COMPOSE_HTTP_TIMEOUT=300 >> _env + echo DOCKER_CLIENT_TIMEOUT=300 >> _env + echo DOCKER_HOST=${DOCKER_HOST} >> _env echo TRITON_USER=${TRITON_USER} >> _env echo TRITON_DC=${TRITON_DC} >> _env echo TRITON_CA=$(cat "${DOCKER_CERT_PATH}"/ca.pem | tr '\n' '#') >> _env