From b9e946ddf86a4a07605992b2d1fdd894174bbabf Mon Sep 17 00:00:00 2001 From: Wyatt Preul Date: Fri, 15 Sep 2017 16:36:31 -0500 Subject: [PATCH] fix: health checks work for private ips --- docker-compose.yml | 6 +-- docker/api/bootstrap-data.js | 2 +- docker/api/etc/containerpilot.json5 | 4 +- docker/api/package.json | 4 +- docker/api/server.js | 2 +- docker/compose-api/Dockerfile | 4 +- docker/compose-api/etc/containerpilot.json | 2 +- docker/frontend/Dockerfile | 4 +- docker/frontend/etc/containerpilot.json5 | 2 +- local-compose.yml | 10 ++-- packages/portal-api/lib/index.js | 4 +- .../portal-api/lib/watch/container-pilot.js | 54 +++++++++++-------- packages/portal-api/package.json | 2 +- packages/portal-api/test/containerpilot.json5 | 2 +- 14 files changed, 55 insertions(+), 47 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index f1d52ea3..4442ae6b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -63,7 +63,7 @@ rethinkdb: # FRONTEND ############################################################################# frontend: - image: joyent/copilot-frontend:1.3.4 + image: joyent/copilot-frontend:1.3.5 mem_limit: 512m labels: - triton.cns.services=copilot @@ -81,7 +81,7 @@ frontend: # BACKEND ############################################################################# api: - image: joyent/copilot-api:1.8.10 + image: joyent/copilot-api:1.8.11 mem_limit: 1g expose: - 3000 @@ -94,7 +94,7 @@ api: # Docker-compose wrapper # Create _env file from running ./setup.sh compose-api: - image: joyent/copilot-compose:1.1.0 + image: joyent/copilot-compose:1.1.1 expose: - 4242 env_file: diff --git a/docker/api/bootstrap-data.js b/docker/api/bootstrap-data.js index aa49a3a9..6103b49e 100644 --- a/docker/api/bootstrap-data.js +++ b/docker/api/bootstrap-data.js @@ -10,7 +10,7 @@ const Url = require('url'); let timeoutId; const loadConfig = function () { - const docker = Piloted.service('docker-compose-api'); + const docker = Piloted.service('compose-api'); const rethink = Piloted.service('rethinkdb'); const retry = () => { diff --git a/docker/api/etc/containerpilot.json5 b/docker/api/etc/containerpilot.json5 index 1408053c..f2c857a0 100644 --- a/docker/api/etc/containerpilot.json5 +++ b/docker/api/etc/containerpilot.json5 @@ -89,7 +89,7 @@ name: 'onchange-compose-api', exec: 'pkill -SIGHUP node', when: { - source: 'watch.docker-compose-api', + source: 'watch.compose-api', each: 'changed' } }, @@ -104,7 +104,7 @@ ], watches: [ { - name: 'docker-compose-api', + name: 'compose-api', interval: 3 }, { diff --git a/docker/api/package.json b/docker/api/package.json index 54a09b44..25176c4c 100644 --- a/docker/api/package.json +++ b/docker/api/package.json @@ -1,6 +1,6 @@ { "name": "api", - "version": "1.8.10", + "version": "1.8.11", "description": "", "main": "./server.js", "scripts": { @@ -21,7 +21,7 @@ "joi": "^10.6.0", "joyent-cp-gql-schema": "^1.7.0", "piloted": "^3.1.1", - "portal-api": "^1.8.10", + "portal-api": "^1.8.11", "toppsy": "^1.1.0", "triton": "^5.2.0" } diff --git a/docker/api/server.js b/docker/api/server.js index 4ceb7cbc..11dc6d9b 100644 --- a/docker/api/server.js +++ b/docker/api/server.js @@ -14,7 +14,7 @@ const Url = require('url'); let started = false; let timeoutId; const loadConfig = function () { - const docker = Piloted.service('docker-compose-api'); + const docker = Piloted.service('compose-api'); const rethink = Piloted.service('rethinkdb'); if (docker && rethink && !started) { diff --git a/docker/compose-api/Dockerfile b/docker/compose-api/Dockerfile index 563eaf27..9fdead2a 100644 --- a/docker/compose-api/Dockerfile +++ b/docker/compose-api/Dockerfile @@ -11,8 +11,8 @@ RUN export CONSUL_VERSION=0.7.0 \ && mkdir /config # Install Containerpilot -ENV CONTAINERPILOT_VERSION 3.4.1 -RUN export CONTAINERPILOT_CHECKSUM=4d13cfb345de86135ab2271b77516c6b6a7bed3a \ +ENV CONTAINERPILOT_VERSION 3.4.2 +RUN export CONTAINERPILOT_CHECKSUM=5c99ae9ede01e8fcb9b027b5b3cb0cfd8c0b8b88 \ && export archive=containerpilot-${CONTAINERPILOT_VERSION}.tar.gz \ && curl -Lso /tmp/${archive} \ "https://github.com/joyent/containerpilot/releases/download/${CONTAINERPILOT_VERSION}/${archive}" \ diff --git a/docker/compose-api/etc/containerpilot.json b/docker/compose-api/etc/containerpilot.json index ed5494f6..4041a87e 100644 --- a/docker/compose-api/etc/containerpilot.json +++ b/docker/compose-api/etc/containerpilot.json @@ -6,7 +6,7 @@ "exec": "/bin/prestart.sh" }, { - "name": "docker-compose-api", + "name": "compose-api", "port": 4242, "exec": [ "python", diff --git a/docker/frontend/Dockerfile b/docker/frontend/Dockerfile index e47dced9..298194e4 100644 --- a/docker/frontend/Dockerfile +++ b/docker/frontend/Dockerfile @@ -27,8 +27,8 @@ RUN curl --retry 7 --fail -vo /tmp/consul.zip "https://releases.hashicorp.com/co && mkdir /config # Install Containerpilot -ENV CONTAINERPILOT_VERSION 3.4.1 -RUN export CONTAINERPILOT_CHECKSUM=4d13cfb345de86135ab2271b77516c6b6a7bed3a \ +ENV CONTAINERPILOT_VERSION 3.4.2 +RUN export CONTAINERPILOT_CHECKSUM=5c99ae9ede01e8fcb9b027b5b3cb0cfd8c0b8b88 \ && export archive=containerpilot-${CONTAINERPILOT_VERSION}.tar.gz \ && curl -Lso /tmp/${archive} \ "https://github.com/joyent/containerpilot/releases/download/${CONTAINERPILOT_VERSION}/${archive}" \ diff --git a/docker/frontend/etc/containerpilot.json5 b/docker/frontend/etc/containerpilot.json5 index 46c532b8..6bcb99cb 100644 --- a/docker/frontend/etc/containerpilot.json5 +++ b/docker/frontend/etc/containerpilot.json5 @@ -27,7 +27,7 @@ }, }, { - name: 'cp-frontend', + name: 'frontend', port: {{.PORT}}, exec: 'nginx', interfaces: ["eth0", "eth1"], diff --git a/local-compose.yml b/local-compose.yml index f809db94..7e862c0d 100644 --- a/local-compose.yml +++ b/local-compose.yml @@ -28,7 +28,7 @@ consul: # it is included here for demo purposes and is not required ############################################################################# prometheus: - image: autopilotpattern/prometheus:1.7.1-r20 + image: autopilotpattern/prometheus:1.7.1-r24 restart: always mem_limit: 1g ports: @@ -46,7 +46,7 @@ prometheus: # FRONTEND ############################################################################# frontend: - image: joyent/copilot-frontend:1.3.4 + image: joyent/copilot-frontend:1.3.5 mem_limit: 512m links: - consul:consul @@ -66,7 +66,7 @@ frontend: # BACKEND ############################################################################# api: - image: joyent/copilot-api:1.8.10 + image: joyent/copilot-api:1.8.11 mem_limit: 512m links: - consul:consul @@ -83,7 +83,7 @@ api: # Docker-compose wrapper # Create _env file from running ./setup.sh compose-api: - image: joyent/copilot-compose:1.0.0 + image: joyent/copilot-compose:1.1.1 links: - consul:consul expose: @@ -95,7 +95,7 @@ compose-api: restart: always rethinkdb: - image: autopilotpattern/rethinkdb:2.3.5r1 + image: autopilotpattern/rethinkdb:2.3.5r2 restart: always mem_limit: 1g links: diff --git a/packages/portal-api/lib/index.js b/packages/portal-api/lib/index.js index ea7db301..1c2ea9c6 100644 --- a/packages/portal-api/lib/index.js +++ b/packages/portal-api/lib/index.js @@ -32,7 +32,7 @@ module.exports = function (server, options, next) { const settings = Hoek.applyToDefaults(internals.defaults, options || {}); try { - const docker = Piloted.service('docker-compose-api'); + const docker = Piloted.service('compose-api'); if (docker) { settings.data.dockerComposeHost = `tcp://${docker.address}:${docker.port}`; } @@ -106,7 +106,7 @@ module.exports.attributes = { internals.refresh = function (data) { return () => { - const docker = Piloted.service('docker-compose-api'); + const docker = Piloted.service('compose-api'); if (docker) { data.reconnectCompose(`tcp://${docker.address}:${docker.port}`); } diff --git a/packages/portal-api/lib/watch/container-pilot.js b/packages/portal-api/lib/watch/container-pilot.js index 0415c872..4202baf7 100644 --- a/packages/portal-api/lib/watch/container-pilot.js +++ b/packages/portal-api/lib/watch/container-pilot.js @@ -132,24 +132,26 @@ module.exports = class ContainerPilotWatcher extends Events { return Name !== 'containerpilot'; }); - cb(null, Object.assign({}, instance, { - cp: Object.assign({}, status, { - Services: services - }) - })); + instance.cp = { + status, + Services: services + }; + + cb(null, instance); }; const fetchStatus = (ip) => { - Wreck.get(`http://${ip}:9090/status`, { - timeout: 2000, // 2 seconds - json: 'force' - }, (err, res, status) => { - if (err) { - this.emit('error', err); - return cb(); - } + return new Promise((resolve, reject) => { + Wreck.get(`http://${ip}:9090/status`, { + timeout: 2000, // 2 seconds + json: 'force' + }, (err, res, status) => { + if (err) { + return reject(err); + } - handleStatus(status); + resolve(status); + }); }); }; @@ -159,11 +161,21 @@ module.exports = class ContainerPilotWatcher extends Events { return cb(); } - if (!machine) { - return cb(); + if (!machine || !Array.isArray(machine.ips)) { + return cb(null, instance); } - fetchStatus(machine.primaryIp); + machine.ips.find(async (ip) => { + try { + const status = await fetchStatus(ip); + return handleStatus(status); + } catch (ex) { + this.emit('error', err); + return; + } + }); + + return cb(null, instance); }); } @@ -288,11 +300,7 @@ module.exports = class ContainerPilotWatcher extends Events { } static _resolveInstanceHealth ({ name }, instance) { - if (!instance) { - return 'UNAVAILABLE'; - } - - if (!instance.cp) { + if (!instance || !instance.cp) { return 'UNAVAILABLE'; } @@ -306,7 +314,7 @@ module.exports = class ContainerPilotWatcher extends Events { return serviceJobs.shift().Status.toUpperCase(); } - const almostJobNameRegexp = new RegExp(`${name}-.*`); + const almostJobNameRegexp = new RegExp(`${name}`, 'ig'); const almostServiceJobs = jobNames.filter(({ Name }) => { return almostJobNameRegexp.test(Name); }); diff --git a/packages/portal-api/package.json b/packages/portal-api/package.json index 06abeb2d..9e31f206 100644 --- a/packages/portal-api/package.json +++ b/packages/portal-api/package.json @@ -1,6 +1,6 @@ { "name": "portal-api", - "version": "1.8.10", + "version": "1.8.11", "description": "", "main": "./lib/index.js", "scripts": { diff --git a/packages/portal-api/test/containerpilot.json5 b/packages/portal-api/test/containerpilot.json5 index 3edb4695..d84f438c 100644 --- a/packages/portal-api/test/containerpilot.json5 +++ b/packages/portal-api/test/containerpilot.json5 @@ -24,7 +24,7 @@ interval: 5 }, { - name: 'docker-compose-api', + name: 'compose-api', interval: 5 } ]