mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-19 11:16:34 +00:00
Rename aplication manager getStatus as getLegacyState
With the move to v3 target state and the move forward to remove database ids from the supervisor, we want to ensure the ids are only used for legacy support (such as within the API). This change renames the method and sets it as deprecated
This commit is contained in:
parent
474fc4e890
commit
ccae1f7cb8
@ -788,12 +788,17 @@ function reportOptionalContainers(serviceNames: string[]) {
|
||||
);
|
||||
}
|
||||
|
||||
// FIXME: This would be better to implement using the App class, and have each one
|
||||
// generate its status. For now we use the original from application-manager.coffee.
|
||||
export async function getStatus() {
|
||||
/**
|
||||
* This will be replaced by ApplicationManager.getState, at which
|
||||
* point the only place this will be used will be in the API endpoints
|
||||
* once, the API moves to v3 or we update the endpoints to return uuids, we will
|
||||
* be able to get rid of this
|
||||
* @deprecated
|
||||
*/
|
||||
export async function getLegacyState() {
|
||||
const [services, images] = await Promise.all([
|
||||
serviceManager.getStatus(),
|
||||
imageManager.getStatus(),
|
||||
serviceManager.getState(),
|
||||
imageManager.getState(),
|
||||
]);
|
||||
|
||||
const apps: Dictionary<any> = {};
|
||||
@ -822,7 +827,7 @@ export async function getStatus() {
|
||||
}
|
||||
if (imageId == null) {
|
||||
throw new InternalInconsistencyError(
|
||||
`imageId not defined in ApplicationManager.getStatus: ${service}`,
|
||||
`imageId not defined in ApplicationManager.getLegacyApplicationsState: ${service}`,
|
||||
);
|
||||
}
|
||||
if (apps[appId].services[imageId] == null) {
|
||||
|
@ -404,7 +404,10 @@ export async function cleanImageData(): Promise<void> {
|
||||
await db.models('image').del().whereIn('id', ids);
|
||||
}
|
||||
|
||||
export const getStatus = async () => {
|
||||
/**
|
||||
* Get the current state of all downloaded and downloading images on the device
|
||||
*/
|
||||
export const getState = async () => {
|
||||
const images = (await getAvailable()).map((img) => ({
|
||||
...img,
|
||||
status: 'Downloaded' as Image['status'],
|
||||
|
@ -103,14 +103,17 @@ export async function get(service: Service) {
|
||||
return services[0];
|
||||
}
|
||||
|
||||
export async function getStatus() {
|
||||
/**
|
||||
* Get the current state of all supervised services
|
||||
*/
|
||||
export async function getState() {
|
||||
const services = await getAll();
|
||||
const status = _.clone(volatileState);
|
||||
|
||||
for (const service of services) {
|
||||
if (service.containerId == null) {
|
||||
throw new InternalInconsistencyError(
|
||||
`containerId not defined in ServiceManager.getStatus: ${service}`,
|
||||
`containerId not defined in ServiceManager.getLegacyServicesState: ${service}`,
|
||||
);
|
||||
}
|
||||
if (status[service.containerId] == null) {
|
||||
|
@ -191,8 +191,8 @@ export function createV2Api(router: Router) {
|
||||
// It's kinda hacky to access the services and db via the application manager
|
||||
// maybe refactor this code
|
||||
Bluebird.join(
|
||||
serviceManager.getStatus(),
|
||||
images.getStatus(),
|
||||
serviceManager.getState(),
|
||||
images.getState(),
|
||||
db.models('app').select(['appId', 'commit', 'name']),
|
||||
(
|
||||
services,
|
||||
@ -284,7 +284,7 @@ export function createV2Api(router: Router) {
|
||||
// Query device for all applications
|
||||
let apps: any;
|
||||
try {
|
||||
apps = await applicationManager.getStatus();
|
||||
apps = await applicationManager.getLegacyState();
|
||||
} catch (e) {
|
||||
log.error(e.message);
|
||||
return res.status(500).json({
|
||||
@ -472,7 +472,7 @@ export function createV2Api(router: Router) {
|
||||
|
||||
let downloadProgressTotal = 0;
|
||||
let downloads = 0;
|
||||
const imagesStates = (await images.getStatus())
|
||||
const imagesStates = (await images.getState())
|
||||
.filter((img) => req.auth.isScoped({ apps: [img.appId] }))
|
||||
.map((img) => {
|
||||
appIds.push(img.appId);
|
||||
|
@ -166,7 +166,7 @@ function createDeviceStateRouter() {
|
||||
|
||||
router.get('/v1/device', async (_req, res) => {
|
||||
try {
|
||||
const state = await getStatus();
|
||||
const state = await getCurrentForReport();
|
||||
const stateToSend = _.pick(state.local, [
|
||||
'api_port',
|
||||
'ip_address',
|
||||
@ -528,8 +528,12 @@ export function getTarget({
|
||||
});
|
||||
}
|
||||
|
||||
export async function getStatus(): Promise<DeviceStatus> {
|
||||
const appsStatus = await applicationManager.getStatus();
|
||||
// This returns the current state of the device in (more or less)
|
||||
// the same format as the target state. This method,
|
||||
// getCurrent and getCurrentForComparison should probably get
|
||||
// merged into a single method
|
||||
export async function getCurrentForReport(): Promise<DeviceStatus> {
|
||||
const appsStatus = await applicationManager.getLegacyState();
|
||||
const theState: DeepPartial<DeviceStatus> = {
|
||||
local: {},
|
||||
dependent: {},
|
||||
|
@ -197,7 +197,7 @@ function handleRetry(retryInfo: OnFailureInfo) {
|
||||
async function generateStateForReport() {
|
||||
const { hardwareMetrics } = await config.getMany(['hardwareMetrics']);
|
||||
|
||||
const currentDeviceState = await deviceState.getStatus();
|
||||
const currentDeviceState = await deviceState.getCurrentForReport();
|
||||
|
||||
// If hardwareMetrics is false, send null patch for system metrics to cloud API
|
||||
const info = {
|
||||
|
@ -49,7 +49,7 @@ describe('SupervisorAPI [V2 Endpoints]', () => {
|
||||
await apiKeys.initialized;
|
||||
await apiKeys.generateCloudKey();
|
||||
serviceManagerMock = stub(serviceManager, 'getAll').resolves([]);
|
||||
imagesMock = stub(images, 'getStatus').resolves([]);
|
||||
imagesMock = stub(images, 'getState').resolves([]);
|
||||
|
||||
// We want to check the actual step that was triggered
|
||||
applicationManagerSpy = spy(applicationManager, 'executeStep');
|
||||
|
@ -188,7 +188,7 @@ function buildRoutes(): Router {
|
||||
const originalNetGetAll = networkManager.getAllByAppId;
|
||||
const originalVolGetAll = volumeManager.getAllByAppId;
|
||||
const originalSvcGetAppId = serviceManager.getAllByAppId;
|
||||
const originalSvcGetStatus = serviceManager.getStatus;
|
||||
const originalSvcGetStatus = serviceManager.getState;
|
||||
const originalReadyForUpdates = apiBinder.__get__('readyForUpdates');
|
||||
|
||||
function setupStubs() {
|
||||
@ -198,7 +198,7 @@ function setupStubs() {
|
||||
// @ts-expect-error Assigning to a RO property
|
||||
volumeManager.getAllByAppId = async () => STUBBED_VALUES.volumes;
|
||||
// @ts-expect-error Assigning to a RO property
|
||||
serviceManager.getStatus = async () => STUBBED_VALUES.services;
|
||||
serviceManager.getState = async () => STUBBED_VALUES.services;
|
||||
// @ts-expect-error Assigning to a RO property
|
||||
serviceManager.getAllByAppId = async (appId) =>
|
||||
_.filter(STUBBED_VALUES.services, (service) => service.appId === appId);
|
||||
@ -211,7 +211,7 @@ function restoreStubs() {
|
||||
// @ts-expect-error Assigning to a RO property
|
||||
volumeManager.getAllByAppId = originalVolGetAll;
|
||||
// @ts-expect-error Assigning to a RO property
|
||||
serviceManager.getStatus = originalSvcGetStatus;
|
||||
serviceManager.getState = originalSvcGetStatus;
|
||||
// @ts-expect-error Assigning to a RO property
|
||||
serviceManager.getAllByAppId = originalSvcGetAppId;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user