From 2bdb34d78d2f8e76b1d3df1d6e1a7b334e7c6d72 Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Fri, 2 Nov 2018 14:49:52 +0000 Subject: [PATCH 1/3] fix: Correctly type bluebird promises in application-manager typings Signed-off-by: Cameron Diver --- src/application-manager.d.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/application-manager.d.ts b/src/application-manager.d.ts index 99b771e3..a4575943 100644 --- a/src/application-manager.d.ts +++ b/src/application-manager.d.ts @@ -1,3 +1,4 @@ +import * as Bluebird from 'bluebird'; import { EventEmitter } from 'events'; import { ServiceAction } from './device-api/common'; @@ -40,7 +41,7 @@ export class ApplicationManager extends EventEmitter { public db: DB; public images: Images; - public getCurrentApp(appId: number): Promise; + public getCurrentApp(appId: number): Bluebird; // TODO: This actually returns an object, but we don't need the values just yet public setTargetVolatileForService(serviceId: number, opts: Options): void; @@ -48,11 +49,11 @@ export class ApplicationManager extends EventEmitter { public executeStepAction( serviceAction: ServiceAction, opts: Options, - ): Promise; + ): Bluebird; public getStatus(): Promise; - public serviceNameFromId(serviceId: number): Promise; + public serviceNameFromId(serviceId: number): Bluebird; } export default ApplicationManager; From 9963d24675a9155213ceb61238c776d651bcdb9f Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Fri, 2 Nov 2018 14:50:22 +0000 Subject: [PATCH 2/3] fix: Fix typo in image cleanup code Signed-off-by: Cameron Diver --- src/compose/images.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compose/images.coffee b/src/compose/images.coffee index e3458e06..ec4ed2ff 100644 --- a/src/compose/images.coffee +++ b/src/compose/images.coffee @@ -249,7 +249,7 @@ module.exports = class Images extends EventEmitter supervisorRepos = [ supervisorImageInfo.imageName ] if _.startsWith(supervisorImageInfo.imageName, 'balena/') # We're on a new balena/ARCH-supervisor image supervisorRepos.push(supervisorImageInfo.imageName.replace(/^balena/, 'resin/')) - return _.some(supervisorRepos, (repo) -> imageName == img) and tagName != supervisorImageInfo.tagName + return _.some(supervisorRepos, (repo) -> imageName == repo) and tagName != supervisorImageInfo.tagName isDangling = (image) -> # Looks like dangling images show up with these weird RepoTags and RepoDigests sometimes (_.isEmpty(image.RepoTags) or _.isEqual(image.RepoTags, [ ':' ])) and From dfdc371a9c13aa5dcdf79dbfadf234d09327f7ab Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Fri, 2 Nov 2018 14:50:43 +0000 Subject: [PATCH 3/3] device-api: Also support serviceName in v2 service endpoints Now you can either pass a serviceName or imageId to restart a specific service, which is much easier from a device container. Change-type: minor Signed-off-by: Cameron Diver --- src/device-api/v2.ts | 26 +++++++++++++++++++++++--- src/lib/messages.ts | 3 +++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/device-api/v2.ts b/src/device-api/v2.ts index a5a5b6dd..09f37dad 100644 --- a/src/device-api/v2.ts +++ b/src/device-api/v2.ts @@ -5,7 +5,11 @@ import { fs } from 'mz'; import { ApplicationManager } from '../application-manager'; import { Service } from '../compose/service'; -import { appNotFoundMessage, serviceNotFoundMessage } from '../lib/messages'; +import { + appNotFoundMessage, + serviceNotFoundMessage, + v2ServiceEndpointInputErrorMessage, +} from '../lib/messages'; import { checkTruthy } from '../lib/validation'; import { doPurge, doRestart, serviceAction } from './common'; @@ -31,7 +35,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) { res: Response, action: any, ): Bluebird => { - const { imageId, force } = req.body; + const { imageId, serviceName, force } = req.body; const { appId } = req.params; return _lockingIfNecessary(appId, { force }, () => { @@ -42,7 +46,23 @@ export function createV2Api(router: Router, applications: ApplicationManager) { res.status(404).send(appNotFoundMessage); return; } - const service = _.find(app.services, { imageId }) as Service | null; + + // Work if we have a service name or an image id + if (imageId == null) { + if (serviceName == null) { + throw new Error(v2ServiceEndpointInputErrorMessage); + } + } + + let service: Service | undefined; + if (imageId != null) { + service = _.find(app.services, svc => svc.imageId === imageId); + } else { + service = _.find( + app.services, + svc => svc.serviceName === serviceName, + ); + } if (service == null) { res.status(404).send(serviceNotFoundMessage); return; diff --git a/src/lib/messages.ts b/src/lib/messages.ts index a1eae4fd..95606c8e 100644 --- a/src/lib/messages.ts +++ b/src/lib/messages.ts @@ -4,3 +4,6 @@ export const appNotFoundMessage = `App not found: an app needs to be installed f export const serviceNotFoundMessage = 'Service not found, a container must exist for this endpoint to work'; + +export const v2ServiceEndpointInputErrorMessage = + 'This endpoint requires one of imageId or serviceName';