Add several local mode enabling endpoints

Change-type: minor
Signed-off-by: Cameron Diver <cameron@resin.io>
This commit is contained in:
Cameron Diver 2018-10-02 14:53:02 +01:00
parent 524d185c4c
commit e0237e826e
No known key found for this signature in database
GPG Key ID: 69264F9C923F55C1

View File

@ -1,6 +1,7 @@
import * as Bluebird from 'bluebird';
import { Request, Response, Router } from 'express';
import * as _ from 'lodash';
import { fs } from 'mz';
import { ApplicationManager } from '../application-manager';
import { Service } from '../compose/service';
@ -199,4 +200,95 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
res.status(200).json(apps);
});
});
router.get('/v2/local/target-state', async (_req, res) => {
try {
const localMode = checkTruthy(await deviceState.config.get('localMode'));
if (!localMode) {
return res.status(400).json({
status: 'failed',
message: 'Target state can only be retrieved when in local mode',
});
}
res.status(200).json({
status: 'success',
state: await deviceState.getTarget(),
});
} catch (err) {
res.status(503).send({
status: 'failed',
message: messageFromError(err),
});
}
});
router.post('/v2/local/target-state', async (req, res) => {
// let's first ensure that we're in local mode, otherwise
// this function should not do anything
// TODO: We really should refactor the config module to provide bools
// as bools etc
try {
const localMode = checkTruthy(await deviceState.config.get('localMode'));
if (!localMode) {
return res.status(400).json({
status: 'failed',
message: 'Target state can only set when device is in local mode',
});
}
// Now attempt to set the state
const force = req.body.force;
const targetState = req.body;
try {
await deviceState.setTarget(targetState);
await deviceState.config.set({ localModeTargetSet: true });
await deviceState.triggerApplyTarget({ force });
res.status(200).json({
status: 'success',
message: 'OK',
});
} catch (e) {
res.status(400).json({
status: 'failed',
message: e.message,
});
}
} catch (e) {
const message = 'Could not apply target state: ';
res.status(503).json({
status: 'failed',
message: message + messageFromError(e),
});
}
});
router.get('/v2/local/device-info', async (_req, res) => {
// Return the device type and slug so that local mode builds can use this to
// resolve builds
try {
// FIXME: We should be mounting the following file into the supervisor from the
// start-resin-supervisor script, changed in meta-resin - but until then, hardcode it
const data = await fs.readFile('/mnt/root/resin-boot/device-type.json', 'utf8');
const deviceInfo = JSON.parse(data);
return res.status(200).json({
status: 'sucess',
info: {
arch: deviceInfo.arch,
deviceType: deviceInfo.slug,
},
});
} catch (e) {
const message = 'Could not fetch device information: ';
res.status(503).json({
status: 'failed',
message: message + messageFromError(e),
});
}
});
}