Merge pull request #1228 from balena-io/device-api-common-js

Convert src/device-api/common.coffee to javascript
This commit is contained in:
Page- 2020-03-29 02:08:17 +01:00 committed by GitHub
commit 74dffcfad7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 59 deletions

View File

@ -1,59 +0,0 @@
Bluebird = require('bluebird')
_ = require('lodash')
{ appNotFoundMessage } = require('../lib/messages')
exports.doRestart = (applications, appId, force) ->
{ _lockingIfNecessary, deviceState } = applications
_lockingIfNecessary appId, { force }, ->
deviceState.getCurrentForComparison()
.then (currentState) ->
app = currentState.local.apps[appId]
imageIds = _.map(app.services, 'imageId')
applications.clearTargetVolatileForServices(imageIds)
stoppedApp = _.cloneDeep(app)
stoppedApp.services = []
currentState.local.apps[appId] = stoppedApp
deviceState.pausingApply ->
deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.then ->
currentState.local.apps[appId] = app
deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.finally ->
deviceState.triggerApplyTarget()
exports.doPurge = (applications, appId, force) ->
{ logger, _lockingIfNecessary, deviceState, volumes } = applications
logger.logSystemMessage("Purging data for app #{appId}", { appId }, 'Purge data')
_lockingIfNecessary appId, { force }, ->
deviceState.getCurrentForComparison()
.then (currentState) ->
app = currentState.local.apps[appId]
if !app?
throw new Error(appNotFoundMessage)
purgedApp = _.cloneDeep(app)
purgedApp.services = []
purgedApp.volumes = {}
currentState.local.apps[appId] = purgedApp
deviceState.pausingApply ->
deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.then ->
# Now that we're not running anything, explicitly
# remove the volumes, we must do this here, as the
# application-manager will not remove any volumes
# which are part of an active application
Bluebird.each(volumes.getAllByAppId(appId), (vol) -> vol.remove())
.then ->
currentState.local.apps[appId] = app
deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.finally ->
deviceState.triggerApplyTarget()
.tap ->
logger.logSystemMessage('Purged data', { appId }, 'Purge data success')
.tapCatch (err) ->
logger.logSystemMessage("Error purging data: #{err}", { appId, error: err }, 'Purge data error')
exports.serviceAction = (action, serviceId, current, target, options = {}) ->
return { action, serviceId, current, target, options }

90
src/device-api/common.js Normal file
View File

@ -0,0 +1,90 @@
import * as Bluebird from 'bluebird';
import * as _ from 'lodash';
import { appNotFoundMessage } from '../lib/messages';
export function doRestart(applications, appId, force) {
const { _lockingIfNecessary, deviceState } = applications;
return _lockingIfNecessary(appId, { force }, () =>
deviceState.getCurrentForComparison().then(function(currentState) {
const app = currentState.local.apps[appId];
const imageIds = _.map(app.services, 'imageId');
applications.clearTargetVolatileForServices(imageIds);
const stoppedApp = _.cloneDeep(app);
stoppedApp.services = [];
currentState.local.apps[appId] = stoppedApp;
return deviceState
.pausingApply(() =>
deviceState
.applyIntermediateTarget(currentState, { skipLock: true })
.then(function() {
currentState.local.apps[appId] = app;
return deviceState.applyIntermediateTarget(currentState, {
skipLock: true,
});
}),
)
.finally(() => deviceState.triggerApplyTarget());
}),
);
}
export function doPurge(applications, appId, force) {
const { logger, _lockingIfNecessary, deviceState, volumes } = applications;
logger.logSystemMessage(
`Purging data for app ${appId}`,
{ appId },
'Purge data',
);
return _lockingIfNecessary(appId, { force }, () =>
deviceState.getCurrentForComparison().then(function(currentState) {
const app = currentState.local.apps[appId];
if (app == null) {
throw new Error(appNotFoundMessage);
}
const purgedApp = _.cloneDeep(app);
purgedApp.services = [];
purgedApp.volumes = {};
currentState.local.apps[appId] = purgedApp;
return deviceState
.pausingApply(() =>
deviceState
.applyIntermediateTarget(currentState, { skipLock: true })
.then(() => {
// Now that we're not running anything, explicitly
// remove the volumes, we must do this here, as the
// application-manager will not remove any volumes
// which are part of an active application
return Bluebird.each(volumes.getAllByAppId(appId), vol =>
vol.remove(),
);
})
.then(function() {
currentState.local.apps[appId] = app;
return deviceState.applyIntermediateTarget(currentState, {
skipLock: true,
});
}),
)
.finally(() => deviceState.triggerApplyTarget());
}),
)
.tap(() =>
logger.logSystemMessage('Purged data', { appId }, 'Purge data success'),
)
.tapCatch(err =>
logger.logSystemMessage(
`Error purging data: ${err}`,
{ appId, error: err },
'Purge data error',
),
);
}
export function serviceAction(action, serviceId, current, target, options) {
if (options == null) {
options = {};
}
return { action, serviceId, current, target, options };
}