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; 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 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';