From 15fca80d2ddb16b368f0d2c34fb56b5b9bb6217f Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Thu, 26 Mar 2020 17:47:56 +0000 Subject: [PATCH] Convert src/device-api/common.coffee to javascript Change-type: patch --- src/device-api/common.coffee | 59 ----------------------- src/device-api/common.js | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 59 deletions(-) delete mode 100644 src/device-api/common.coffee create mode 100644 src/device-api/common.js diff --git a/src/device-api/common.coffee b/src/device-api/common.coffee deleted file mode 100644 index 1cdf8b3f..00000000 --- a/src/device-api/common.coffee +++ /dev/null @@ -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 } diff --git a/src/device-api/common.js b/src/device-api/common.js new file mode 100644 index 00000000..a8197033 --- /dev/null +++ b/src/device-api/common.js @@ -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 }; +}