mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-21 10:01:55 +00:00
Merge pull request #1228 from balena-io/device-api-common-js
Convert src/device-api/common.coffee to javascript
This commit is contained in:
commit
74dffcfad7
@ -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
90
src/device-api/common.js
Normal 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 };
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user