Merge pull request #787 from balena-io/v2-restart-improvement

V2 service device api improvments
This commit is contained in:
CameronDiver 2018-11-05 13:18:47 +01:00 committed by GitHub
commit 00efce1b0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 7 deletions

View File

@ -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<Application | null>;
public getCurrentApp(appId: number): Bluebird<Application | null>;
// 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<void>;
): Bluebird<void>;
public getStatus(): Promise<DeviceApplicationState>;
public serviceNameFromId(serviceId: number): Promise<string>;
public serviceNameFromId(serviceId: number): Bluebird<string>;
}
export default ApplicationManager;

View File

@ -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, [ '<none>:<none>' ])) and

View File

@ -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<void> => {
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;

View File

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