UNPKG

balena-cli

Version:

The official balena Command Line Interface

145 lines 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeviceAPI = void 0; const helpers_1 = require("../helpers"); const ApiErrors = require("./errors"); const lazy_1 = require("../lazy"); const deviceEndpoints = { setTargetState: 'v2/local/target-state', getTargetState: 'v2/local/target-state', getDeviceInformation: 'v2/local/device-info', logs: 'v2/local/logs', ping: 'ping', version: 'v2/version', status: 'v2/state/status', containerId: 'v2/containerId', }; class DeviceAPI { constructor(logger, addr, port = 48484) { this.logger = logger; this.deviceAddress = `http://${addr}:${port}/`; } async setTargetState(state) { const url = this.getUrlForAction('setTargetState'); await DeviceAPI.sendRequest({ method: 'POST', url, json: true, body: state, }, this.logger); } async getTargetState() { const url = this.getUrlForAction('getTargetState'); return await DeviceAPI.sendRequest({ method: 'GET', url, json: true, }, this.logger).then(({ state }) => { return state; }); } async getDeviceInformation() { const url = this.getUrlForAction('getDeviceInformation'); return await DeviceAPI.sendRequest({ method: 'GET', url, json: true, }, this.logger).then(({ info }) => { return info; }); } async getContainerId(serviceName) { const url = this.getUrlForAction('containerId'); const body = await DeviceAPI.sendRequest({ method: 'GET', url, json: true, qs: { serviceName, }, }, this.logger); if (body.status !== 'success') { throw new ApiErrors.DeviceAPIError('Non-successful response from supervisor containerId endpoint'); } return body.containerId; } async ping() { const url = this.getUrlForAction('ping'); await DeviceAPI.sendRequest({ method: 'GET', url, }, this.logger); } async getVersion() { const url = this.getUrlForAction('version'); return await DeviceAPI.sendRequest({ method: 'GET', url, json: true, }).then((body) => { if (body.status !== 'success') { throw new ApiErrors.DeviceAPIError('Non-successful response from supervisor version endpoint'); } return body.version; }); } async getStatus() { const url = this.getUrlForAction('status'); return await DeviceAPI.sendRequest({ method: 'GET', url, json: true, }).then((body) => { if (body.status !== 'success') { throw new ApiErrors.DeviceAPIError('Non-successful response from supervisor status endpoint'); } delete body.status; return body; }); } async getLogStream() { const url = this.getUrlForAction('logs'); const sdk = (0, lazy_1.getBalenaSdk)(); const stream = await sdk.request.stream({ url }); stream.on('response', (res) => { if (res.statusCode !== 200) { throw new ApiErrors.DeviceAPIError('Non-200 response from log streaming endpoint'); } res.socket.setKeepAlive(true, 1000); }); return stream; } getUrlForAction(action) { return `${this.deviceAddress}${deviceEndpoints[action]}`; } static async sendRequest(opts, logger) { if (logger != null && opts.url != null) { logger.logDebug(`Sending request to ${opts.url}`); } const sdk = (0, lazy_1.getBalenaSdk)(); const doRequest = async () => { const response = await sdk.request.send(opts); const bodyError = typeof response.body === 'string' ? response.body : response.body.message; switch (response.statusCode) { case 200: return response.body; case 400: throw new ApiErrors.BadRequestDeviceAPIError(bodyError); case 503: throw new ApiErrors.ServiceUnavailableAPIError(bodyError); default: new ApiErrors.DeviceAPIError(bodyError); } }; return await (0, helpers_1.retry)({ func: doRequest, initialDelayMs: 2000, maxAttempts: 6, label: `Supervisor API (${opts.method} ${opts.url})`, }); } } exports.DeviceAPI = DeviceAPI; //# sourceMappingURL=api.js.map