1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-28 06:00:06 +02:00

fix: get instance status works correctly

This commit is contained in:
geek 2017-09-17 11:12:25 -05:00 committed by Sérgio Ramos
parent 85b7d0ef57
commit d449bb2250
6 changed files with 73 additions and 78 deletions

View File

@ -20,6 +20,8 @@ consul:
- triton.cns.services=copilot-consul - triton.cns.services=copilot-consul
ports: ports:
- 8500:8500 - 8500:8500
expose:
- 9090
############################################################################# #############################################################################
# PROMETHEUS # PROMETHEUS
@ -46,19 +48,6 @@ prometheus:
- CONSUL_AGENT=1 - CONSUL_AGENT=1
rethinkdb:
image: autopilotpattern/rethinkdb:2.3.5r1
restart: always
mem_limit: 1g
env_file:
- _env
environment:
- CONSUL_AGENT=1
expose:
- 28015
- 29015
############################################################################# #############################################################################
# FRONTEND # FRONTEND
############################################################################# #############################################################################
@ -74,6 +63,8 @@ frontend:
ports: ports:
- "80:80" - "80:80"
- "443:443" - "443:443"
expose:
- 9090
restart: always restart: always
@ -81,14 +72,17 @@ frontend:
# BACKEND # BACKEND
############################################################################# #############################################################################
api: api:
image: joyent/copilot-api:1.8.12 image: joyent/copilot-api:1.8.15
mem_limit: 1g mem_limit: 1g
expose: expose:
- 3000 - 3000
- 9090
env_file: env_file:
- _env - _env
environment: environment:
- PORT=3000 - PORT=3000
ports:
- "8080:8080"
restart: always restart: always
# Docker-compose wrapper # Docker-compose wrapper
@ -97,6 +91,7 @@ compose-api:
image: joyent/copilot-compose:1.1.1 image: joyent/copilot-compose:1.1.1
expose: expose:
- 4242 - 4242
- 9090
env_file: env_file:
- _env - _env
restart: always restart: always
@ -112,3 +107,4 @@ rethinkdb:
expose: expose:
- 28015 - 28015
- 29015 - 29015
- 9090

View File

@ -1,6 +1,6 @@
{ {
"name": "api", "name": "api",
"version": "1.8.12", "version": "1.8.15",
"description": "", "description": "",
"main": "./server.js", "main": "./server.js",
"scripts": { "scripts": {
@ -21,7 +21,7 @@
"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.12", "portal-api": "^1.8.15",
"toppsy": "^1.1.0", "toppsy": "^1.1.0",
"triton": "^5.2.0" "triton": "^5.2.0"
} }

View File

@ -66,7 +66,7 @@ frontend:
# BACKEND # BACKEND
############################################################################# #############################################################################
api: api:
image: joyent/copilot-api:1.8.12 image: joyent/copilot-api:1.8.15
mem_limit: 512m mem_limit: 512m
links: links:
- consul:consul - consul:consul

View File

@ -125,7 +125,7 @@ module.exports = class ContainerPilotWatcher extends Events {
const handleStatus = (status) => { const handleStatus = (status) => {
if (!status) { if (!status) {
return cb(null, instance); return;
} }
const services = ForceArray(status.Services).filter(({ Name }) => { const services = ForceArray(status.Services).filter(({ Name }) => {
@ -134,24 +134,23 @@ module.exports = class ContainerPilotWatcher extends Events {
instance.cp = { instance.cp = {
status, status,
Services: services Services: services,
Watches: status.Watches
}; };
cb(null, instance); return instance;
}; };
const fetchStatus = (ip) => { const fetchStatus = (ip, next) => {
return new Promise((resolve, reject) => { Wreck.get(`http://${ip}:9090/status`, {
Wreck.get(`http://${ip}:9090/status`, { timeout: 2000, // 2 seconds
timeout: 2000, // 2 seconds json: 'force'
json: 'force' }, (err, res, status) => {
}, (err, res, status) => { if (err) {
if (err) { return next(err);
return reject(err); }
}
resolve(status); next(null, status);
});
}); });
}; };
@ -162,20 +161,20 @@ module.exports = class ContainerPilotWatcher extends Events {
} }
if (!machine || !Array.isArray(machine.ips)) { if (!machine || !Array.isArray(machine.ips)) {
return cb(null, instance); return cb();
} }
machine.ips.find(async (ip) => { VAsync.forEachParallel({
try { func: fetchStatus,
const status = await fetchStatus(ip); inputs: machine.ips
return handleStatus(status); }, (err, results) => {
} catch (ex) { if (err || !results.successes || !results.successes.length) {
this.emit('error', err); return cb(err);
return;
} }
});
return cb(null, instance); const instance = handleStatus(results.successes[0]);
return cb(null, instance);
});
}); });
} }

View File

@ -64,7 +64,7 @@ module.exports = class MachineWatcher {
// todo assert options // todo assert options
this._data = options.data; this._data = options.data;
this._server = options.server; this._server = options.server;
this._frequency = 200; this._frequency = 500;
this._tritonWatch = new TritonWatch({ this._tritonWatch = new TritonWatch({
frequency: this._frequency, frequency: this._frequency,
@ -166,7 +166,7 @@ module.exports = class MachineWatcher {
} }
createInstance ({ deploymentGroup, machine, instances, service }, cb) { createInstance ({ deploymentGroup, machine, instances, service }, cb) {
this._server.log(['debug', 'error'], `-> detected that machine ${machine.name} was created`); this._server.log(['debug'], `-> detected that machine ${machine.name} was created`);
const status = (machine.state || '').toUpperCase(); const status = (machine.state || '').toUpperCase();
@ -199,7 +199,7 @@ module.exports = class MachineWatcher {
} }
updateInstance ({ machine, instance, instances, service }, cb) { updateInstance ({ machine, instance, instances, service }, cb) {
console.error(`-> detected that machine ${machine.name} was updated`); this._server.log(['debug'], `-> detected that machine ${machine.name} was updated`);
const updatedInstance = { const updatedInstance = {
id: instance.id, id: instance.id,
@ -231,7 +231,7 @@ module.exports = class MachineWatcher {
} }
resolveChange ({ deploymentGroup, version, service, instances, machine }, cb) { resolveChange ({ deploymentGroup, version, service, instances, machine }, cb) {
console.error(`-> resolving change for machine ${machine.name}`); this._server.log(['debug'], `-> resolving change for machine ${machine.name}`);
const SERVICE_STATUS = Get(service, 'status', 'UNKNOWN').toUpperCase(); const SERVICE_STATUS = Get(service, 'status', 'UNKNOWN').toUpperCase();
const MACHINE_STATUS = Get(machine, 'state', 'UNKNOWN').toUpperCase(); const MACHINE_STATUS = Get(machine, 'state', 'UNKNOWN').toUpperCase();
@ -239,7 +239,7 @@ module.exports = class MachineWatcher {
const hasPlan = Boolean(Get(version, 'plan.hasPlan', true)); const hasPlan = Boolean(Get(version, 'plan.hasPlan', true));
const serviceName = service.name; const serviceName = service.name;
console.error(`-> detected meta for machine ${machine.name} ${Util.inspect({ this._server.log(['debug'],`-> detected meta for machine ${machine.name} ${Util.inspect({
SERVICE_STATUS, SERVICE_STATUS,
MACHINE_STATUS, MACHINE_STATUS,
hasPlan, hasPlan,
@ -248,13 +248,13 @@ module.exports = class MachineWatcher {
const ActionResolvers = { const ActionResolvers = {
'_CREATE_OR_REMOVE': (action, cb) => { '_CREATE_OR_REMOVE': (action, cb) => {
console.error(`-> got _CREATE_OR_REMOVE action for "${machine.name}"`); this._server.log(['debug'], `-> got _CREATE_OR_REMOVE action for "${machine.name}"`);
let processed = ForceArray(action.processed); let processed = ForceArray(action.processed);
const completed = processed.length === action.toProcess; const completed = processed.length === action.toProcess;
if (completed) { if (completed) {
console.error('-> action was already completed'); this._server.log(['debug'], '-> action was already completed');
return cb(null, { return cb(null, {
action, action,
completed: true completed: true
@ -262,7 +262,7 @@ module.exports = class MachineWatcher {
} }
if (processed.indexOf(machine.id) >= 0) { if (processed.indexOf(machine.id) >= 0) {
console.error('-> machine was already processed'); this._server.log(['debug'], '-> machine was already processed');
return cb(null, { return cb(null, {
action, action,
completed completed
@ -279,7 +279,7 @@ module.exports = class MachineWatcher {
}); });
}, },
'NOOP': (action, cb) => { 'NOOP': (action, cb) => {
console.error(`-> got NOOP action for "${machine.name}"`); this._server.log(['debug'], `-> got NOOP action for "${machine.name}"`);
cb(null, { cb(null, {
action, action,
@ -289,10 +289,10 @@ module.exports = class MachineWatcher {
// scenarios: scale down or removed service // scenarios: scale down or removed service
// so far, the logic is the same for CREATE and REMOVE // so far, the logic is the same for CREATE and REMOVE
'REMOVE': (action, cb) => { 'REMOVE': (action, cb) => {
console.error(`-> got REMOVE action for "${machine.name}"`); this._server.log(['debug'], `-> got REMOVE action for "${machine.name}"`);
if (ACTION_REMOVE_STATUSES.indexOf(MACHINE_STATUS) < 0) { if (ACTION_REMOVE_STATUSES.indexOf(MACHINE_STATUS) < 0) {
console.error(`-> since "${machine.name}" is "${MACHINE_STATUS}", nothing to do here`); this._server.log(['debug'], `-> since "${machine.name}" is "${MACHINE_STATUS}", nothing to do here`);
return cb(null, { return cb(null, {
action, action,
@ -301,7 +301,7 @@ module.exports = class MachineWatcher {
} }
if (action.machines.indexOf(machine.id) < 0) { if (action.machines.indexOf(machine.id) < 0) {
console.error(`-> since "${machine.name}" didn't exist, no need to process its removal`); this._server.log(['debug'], `-> since "${machine.name}" didn't exist, no need to process its removal`);
return cb(null, { return cb(null, {
action, action,
completed: false completed: false
@ -313,10 +313,10 @@ module.exports = class MachineWatcher {
// scenarios: scale up, recreate, create // scenarios: scale up, recreate, create
// so far, the logic is the same for CREATE and REMOVE // so far, the logic is the same for CREATE and REMOVE
'CREATE': (action, cb) => { 'CREATE': (action, cb) => {
console.error(`-> got CREATE action for "${machine.name}"`); this._server.log(['debug'], `-> got CREATE action for "${machine.name}"`);
if (ACTION_CREATE_STATUSES.indexOf(MACHINE_STATUS) < 0) { if (ACTION_CREATE_STATUSES.indexOf(MACHINE_STATUS) < 0) {
console.error(`-> since "${machine.name}" is "${MACHINE_STATUS}", nothing to do here`); this._server.log(['debug'], `-> since "${machine.name}" is "${MACHINE_STATUS}", nothing to do here`);
return cb(null, { return cb(null, {
action, action,
@ -325,7 +325,7 @@ module.exports = class MachineWatcher {
} }
if (action.machines.indexOf(machine.id) >= 0) { if (action.machines.indexOf(machine.id) >= 0) {
console.error(`-> since "${machine.name}" already existed, no need to process its creation`); this._server.log(['debug'], `-> since "${machine.name}" already existed, no need to process its creation`);
return cb(null, { return cb(null, {
action, action,
completed: false completed: false
@ -335,7 +335,7 @@ module.exports = class MachineWatcher {
ActionResolvers._CREATE_OR_REMOVE(action, cb); ActionResolvers._CREATE_OR_REMOVE(action, cb);
}, },
'START': (action, cb) => { 'START': (action, cb) => {
console.error(`-> got START action for "${machine.name}". redirecting`); this._server.log(['debug'], `-> got START action for "${machine.name}". redirecting`);
return ActionResolvers.NOOP(action, cb); return ActionResolvers.NOOP(action, cb);
} }
}; };
@ -364,7 +364,7 @@ module.exports = class MachineWatcher {
const newActions = ForceArray(result.successes); const newActions = ForceArray(result.successes);
console.error(`-> got new actions for "${service.name}" ${Util.inspect(newActions)}`); this._server.log(['debug'], `-> got new actions for "${service.name}" ${Util.inspect(newActions)}`);
const newServiceActions = newActions.filter(({ action }) => { const newServiceActions = newActions.filter(({ action }) => {
return action.service === serviceName; return action.service === serviceName;
@ -374,7 +374,7 @@ module.exports = class MachineWatcher {
return completed; return completed;
}); });
console.error(`-> are all actions for "${service.name}" completed? ${isCompleted}`); this._server.log(['debug'], `-> are all actions for "${service.name}" completed? ${isCompleted}`);
const newPlan = newActions.map(({ action }) => { const newPlan = newActions.map(({ action }) => {
return action; return action;
@ -383,7 +383,7 @@ module.exports = class MachineWatcher {
VAsync.parallel({ VAsync.parallel({
funcs: [ funcs: [
(cb) => { (cb) => {
console.error(`-> updating Version ${version.id} with new plan ${Util.inspect(newPlan)}`); this._server.log(['debug'], `-> updating Version ${version.id} with new plan ${Util.inspect(newPlan)}`);
this._data.updateVersion({ this._data.updateVersion({
id: version.id, id: version.id,
@ -395,7 +395,7 @@ module.exports = class MachineWatcher {
return cb(); return cb();
} }
console.error(`-> updating Service ${service.name} with new status: ACTIVE`); this._server.log(['debug'], `-> updating Service ${service.name} with new status: ACTIVE`);
return this._data.updateService({ return this._data.updateService({
id: service.id, id: service.id,
@ -409,22 +409,22 @@ module.exports = class MachineWatcher {
const ServiceResolvers = { const ServiceResolvers = {
'ACTIVE': (cb) => { 'ACTIVE': (cb) => {
console.error(`-> got ACTIVE service "${service.name}". nothing to do`); this._server.log(['debug'], `-> got ACTIVE service "${service.name}". nothing to do`);
cb(); cb();
}, },
'PROVISIONING': (cb) => { 'PROVISIONING': (cb) => {
console.error(`-> got PROVISIONING service "${service.name}"`); this._server.log(['debug'], `-> got PROVISIONING service "${service.name}"`);
toBeActiveServiceResolver(cb); toBeActiveServiceResolver(cb);
}, },
'SCALING': (cb) => { 'SCALING': (cb) => {
console.error(`-> got SCALING service "${service.name}"`); this._server.log(['debug'], `-> got SCALING service "${service.name}"`);
toBeActiveServiceResolver(cb); toBeActiveServiceResolver(cb);
}, },
'STOPPING': (cb) => { 'STOPPING': (cb) => {
console.error(`-> got STOPPING service "${service.name}"`); this._server.log(['debug'], `-> got STOPPING service "${service.name}"`);
if (SERVICE_STOPPING_STATUSES.indexOf(MACHINE_STATUS) < 0) { if (SERVICE_STOPPING_STATUSES.indexOf(MACHINE_STATUS) < 0) {
return cb(); return cb();
@ -451,7 +451,7 @@ module.exports = class MachineWatcher {
return ServiceResolvers.ACTIVE(cb); return ServiceResolvers.ACTIVE(cb);
}, },
'DELETING': (cb) => { 'DELETING': (cb) => {
console.error(`-> got DELETING service "${service.name}"`); this._server.log(['debug'], `-> got DELETING service "${service.name}"`);
if (SERVICE_DELETING_STATUSES.indexOf(MACHINE_STATUS) < 0) { if (SERVICE_DELETING_STATUSES.indexOf(MACHINE_STATUS) < 0) {
return cb(); return cb();
@ -472,7 +472,7 @@ module.exports = class MachineWatcher {
VAsync.parallel({ VAsync.parallel({
funcs: [ funcs: [
(cb) => { (cb) => {
console.error(`-> updating Service ${service.name} to set it DELETED`); this._server.log(['debug'], `-> updating Service ${service.name} to set it DELETED`);
this._data.updateService({ this._data.updateService({
id: service.id, id: service.id,
@ -480,7 +480,7 @@ module.exports = class MachineWatcher {
}, cb); }, cb);
}, },
(cb) => { (cb) => {
console.error(`-> updating DeploymentGroup ${deploymentGroup.id} to remove Service ${service.name}`); this._server.log(['debug'], `-> updating DeploymentGroup ${deploymentGroup.id} to remove Service ${service.name}`);
deploymentGroup.services({}, (err, services) => { deploymentGroup.services({}, (err, services) => {
if (err) { if (err) {
@ -521,10 +521,10 @@ module.exports = class MachineWatcher {
return cb(err); return cb(err);
} }
console.error(`-> created/updated machine ${machine.name}`); this._server.log(['debug'], `-> created/updated machine ${machine.name}`);
if (!hasPlan) { if (!hasPlan) {
console.error(`-> plan for ${service.name} is still not available. queuing`); this._server.log(['debug'], `-> plan for ${service.name} is still not available. queuing`);
this._waitingForPlan.push(machine); this._waitingForPlan.push(machine);
return cb(); return cb();
} }
@ -551,7 +551,7 @@ module.exports = class MachineWatcher {
onChange (machine) { onChange (machine) {
if (!machine) { if (!machine) {
console.error('-> `change` event received without machine data'); this._server.log(['debug'], '-> `change` event received without machine data');
return; return;
} }
@ -561,7 +561,7 @@ module.exports = class MachineWatcher {
// assert id existence // assert id existence
if (!id) { if (!id) {
console.error('-> `change` event received for a machine without `id`'); this._server.log(['debug'], '-> `change` event received for a machine without `id`');
return; return;
} }
@ -573,7 +573,7 @@ module.exports = class MachineWatcher {
); );
if (!isCompose) { if (!isCompose) {
console.error(`-> Changed machine ${id} was not provisioned by docker-compose`); this._server.log(['debug'], `-> Changed machine ${id} was not provisioned by docker-compose`);
return; return;
} }
@ -612,7 +612,7 @@ module.exports = class MachineWatcher {
deploymentGroupId: deploymentGroup.id deploymentGroupId: deploymentGroup.id
}, (err, service) => { }, (err, service) => {
if (isNotFound(err) || !service) { if (isNotFound(err) || !service) {
console.error(`Service "${serviceName}" form DeploymentGroup "${deploymentGroupName}" for machine ${id} not found`); this._server.log(['debug'], `Service "${serviceName}" form DeploymentGroup "${deploymentGroupName}" for machine ${id} not found`);
return cb(); return cb();
} }
@ -631,12 +631,12 @@ module.exports = class MachineWatcher {
// also, lock into `deploymentGroupId` queue // also, lock into `deploymentGroupId` queue
this.getDeploymentGroup(deploymentGroupName, (err, deploymentGroup) => { this.getDeploymentGroup(deploymentGroupName, (err, deploymentGroup) => {
if (isNotFound(err) || !deploymentGroup) { if (isNotFound(err) || !deploymentGroup) {
console.error(`DeploymentGroup "${deploymentGroupName}" for machine ${id} not found`); this._server.log(['debug', 'error'], `DeploymentGroup "${deploymentGroupName}" for machine ${id} not found`);
return; return;
} }
if (err) { if (err) {
console.error(err); this._server.log(['error'], err);
return; return;
} }
@ -644,7 +644,7 @@ module.exports = class MachineWatcher {
return new Promise((resolve) => { return new Promise((resolve) => {
assertService(deploymentGroup, (err) => { assertService(deploymentGroup, (err) => {
if (err) { if (err) {
console.error(err); this._server.log(['error'], err);
} }
resolve(); resolve();

View File

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