2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2019-01-14 11:58:10 +00:00
|
|
|
import * as _ from 'lodash';
|
2020-02-27 12:38:50 +00:00
|
|
|
import type { NodeJSSocketWithFileDescriptor } from 'net-keepalive';
|
2019-05-13 10:51:41 +00:00
|
|
|
import * as os from 'os';
|
2018-10-16 10:25:37 +00:00
|
|
|
import * as request from 'request';
|
2020-02-27 12:38:50 +00:00
|
|
|
import type * as Stream from 'stream';
|
2018-10-16 10:25:37 +00:00
|
|
|
|
|
|
|
import Logger = require('../logger');
|
|
|
|
import * as ApiErrors from './errors';
|
|
|
|
|
|
|
|
export interface DeviceResponse {
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
|
|
status: 'success' | 'failed';
|
|
|
|
message?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DeviceInfo {
|
|
|
|
deviceType: string;
|
|
|
|
arch: string;
|
|
|
|
}
|
|
|
|
|
2019-01-14 11:58:10 +00:00
|
|
|
export interface Status {
|
|
|
|
appState: 'applied' | 'applying';
|
|
|
|
overallDownloadProgress: null | number;
|
|
|
|
containers: Array<{
|
|
|
|
status: string;
|
|
|
|
serviceName: string;
|
|
|
|
appId: number;
|
|
|
|
imageId: number;
|
|
|
|
serviceId: number;
|
|
|
|
containerId: string;
|
|
|
|
createdAt: string;
|
|
|
|
}>;
|
|
|
|
images: Array<{
|
|
|
|
name: string;
|
|
|
|
appId: number;
|
|
|
|
serviceName: string;
|
|
|
|
imageId: number;
|
|
|
|
dockerImageId: string;
|
|
|
|
status: string;
|
|
|
|
downloadProgress: null | number;
|
|
|
|
}>;
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:25:37 +00:00
|
|
|
const deviceEndpoints = {
|
|
|
|
setTargetState: 'v2/local/target-state',
|
|
|
|
getTargetState: 'v2/local/target-state',
|
|
|
|
getDeviceInformation: 'v2/local/device-info',
|
|
|
|
logs: 'v2/local/logs',
|
|
|
|
ping: 'ping',
|
2018-10-16 15:41:38 +00:00
|
|
|
version: 'v2/version',
|
2019-01-14 11:58:10 +00:00
|
|
|
status: 'v2/state/status',
|
2019-05-27 12:18:19 +00:00
|
|
|
containerId: 'v2/containerId',
|
2018-10-16 10:25:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export class DeviceAPI {
|
|
|
|
private deviceAddress: string;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
private logger: Logger,
|
|
|
|
addr: string,
|
|
|
|
port: number = 48484,
|
|
|
|
) {
|
|
|
|
this.deviceAddress = `http://${addr}:${port}/`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either return nothing, or throw an error with the info
|
|
|
|
public async setTargetState(state: any): Promise<void> {
|
|
|
|
const url = this.getUrlForAction('setTargetState');
|
|
|
|
return DeviceAPI.promisifiedRequest(
|
|
|
|
{
|
2020-08-04 15:24:02 +00:00
|
|
|
method: 'POST',
|
2018-10-16 10:25:37 +00:00
|
|
|
url,
|
|
|
|
json: true,
|
|
|
|
body: state,
|
|
|
|
},
|
|
|
|
this.logger,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getTargetState(): Promise<any> {
|
|
|
|
const url = this.getUrlForAction('getTargetState');
|
|
|
|
|
|
|
|
return DeviceAPI.promisifiedRequest(
|
|
|
|
{
|
2020-08-04 15:24:02 +00:00
|
|
|
method: 'GET',
|
2018-10-16 10:25:37 +00:00
|
|
|
url,
|
|
|
|
json: true,
|
|
|
|
},
|
|
|
|
this.logger,
|
2020-06-15 22:53:07 +00:00
|
|
|
).then((body) => {
|
2018-10-16 10:25:37 +00:00
|
|
|
return body.state;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getDeviceInformation(): Promise<DeviceInfo> {
|
|
|
|
const url = this.getUrlForAction('getDeviceInformation');
|
|
|
|
|
|
|
|
return DeviceAPI.promisifiedRequest(
|
|
|
|
{
|
2020-08-04 15:24:02 +00:00
|
|
|
method: 'GET',
|
2018-10-16 10:25:37 +00:00
|
|
|
url,
|
|
|
|
json: true,
|
|
|
|
},
|
|
|
|
this.logger,
|
2020-06-15 22:53:07 +00:00
|
|
|
).then((body) => {
|
2018-10-16 10:25:37 +00:00
|
|
|
return body.info;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-27 12:18:19 +00:00
|
|
|
public async getContainerId(serviceName: string): Promise<string> {
|
|
|
|
const url = this.getUrlForAction('containerId');
|
|
|
|
|
|
|
|
const body = await DeviceAPI.promisifiedRequest(
|
|
|
|
{
|
2020-08-04 15:24:02 +00:00
|
|
|
method: 'GET',
|
2019-05-27 12:18:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:25:37 +00:00
|
|
|
public async ping(): Promise<void> {
|
|
|
|
const url = this.getUrlForAction('ping');
|
|
|
|
|
|
|
|
return DeviceAPI.promisifiedRequest(
|
|
|
|
{
|
2020-08-04 15:24:02 +00:00
|
|
|
method: 'GET',
|
2018-10-16 10:25:37 +00:00
|
|
|
url,
|
|
|
|
},
|
|
|
|
this.logger,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:41:38 +00:00
|
|
|
public getVersion(): Promise<string> {
|
|
|
|
const url = this.getUrlForAction('version');
|
|
|
|
|
2020-08-04 15:24:02 +00:00
|
|
|
return DeviceAPI.promisifiedRequest({
|
|
|
|
method: 'GET',
|
2018-10-16 15:41:38 +00:00
|
|
|
url,
|
|
|
|
json: true,
|
2020-06-15 22:53:07 +00:00
|
|
|
}).then((body) => {
|
2018-10-16 15:41:38 +00:00
|
|
|
if (body.status !== 'success') {
|
|
|
|
throw new ApiErrors.DeviceAPIError(
|
|
|
|
'Non-successful response from supervisor version endpoint',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body.version;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-14 11:58:10 +00:00
|
|
|
public getStatus(): Promise<Status> {
|
|
|
|
const url = this.getUrlForAction('status');
|
|
|
|
|
2020-08-04 15:24:02 +00:00
|
|
|
return DeviceAPI.promisifiedRequest({
|
|
|
|
method: 'GET',
|
2019-01-14 11:58:10 +00:00
|
|
|
url,
|
|
|
|
json: true,
|
2020-06-15 22:53:07 +00:00
|
|
|
}).then((body) => {
|
2019-01-14 11:58:10 +00:00
|
|
|
if (body.status !== 'success') {
|
|
|
|
throw new ApiErrors.DeviceAPIError(
|
|
|
|
'Non-successful response from supervisor status endpoint',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.omit(body, 'status') as Status;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:24:35 +00:00
|
|
|
public getLogStream(): Promise<Stream.Readable> {
|
2018-10-16 10:25:37 +00:00
|
|
|
const url = this.getUrlForAction('logs');
|
|
|
|
|
|
|
|
// Don't use the promisified version here as we want to stream the output
|
2020-06-30 20:24:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-10-16 10:25:37 +00:00
|
|
|
const req = request.get(url);
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
req.on('error', reject).on('response', async (res) => {
|
2018-10-16 10:25:37 +00:00
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
reject(
|
|
|
|
new ApiErrors.DeviceAPIError(
|
|
|
|
'Non-200 response from log streaming endpoint',
|
|
|
|
),
|
|
|
|
);
|
2020-06-15 22:53:04 +00:00
|
|
|
return;
|
2018-10-16 10:25:37 +00:00
|
|
|
}
|
2019-05-13 10:51:41 +00:00
|
|
|
res.socket.setKeepAlive(true, 1000);
|
|
|
|
if (os.platform() !== 'win32') {
|
|
|
|
const NetKeepalive = await import('net-keepalive');
|
|
|
|
// Certain versions of typescript won't convert
|
|
|
|
// this automatically
|
2021-07-20 13:57:00 +00:00
|
|
|
const sock = res.socket as any as NodeJSSocketWithFileDescriptor;
|
2019-05-13 10:51:41 +00:00
|
|
|
// We send a tcp keepalive probe once every 5 seconds
|
|
|
|
NetKeepalive.setKeepAliveInterval(sock, 5000);
|
|
|
|
// After 5 failed probes, the connection is marked as
|
|
|
|
// closed
|
|
|
|
NetKeepalive.setKeepAliveProbes(sock, 5);
|
|
|
|
}
|
2018-10-16 10:25:37 +00:00
|
|
|
resolve(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private getUrlForAction(action: keyof typeof deviceEndpoints): string {
|
|
|
|
return `${this.deviceAddress}${deviceEndpoints[action]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper method for promisifying general (non-streaming) requests. Streaming
|
|
|
|
// requests should use a seperate setup
|
2020-08-04 15:24:02 +00:00
|
|
|
private static async promisifiedRequest<
|
2021-07-20 13:57:00 +00:00
|
|
|
T extends Parameters<typeof request>[0],
|
2020-08-04 15:24:02 +00:00
|
|
|
>(opts: T, logger?: Logger): Promise<any> {
|
2019-03-12 22:07:57 +00:00
|
|
|
interface ObjectWithUrl {
|
|
|
|
url?: string;
|
|
|
|
}
|
2018-10-16 10:25:37 +00:00
|
|
|
|
|
|
|
if (logger != null) {
|
|
|
|
let url: string | null = null;
|
|
|
|
if (_.isObject(opts) && (opts as ObjectWithUrl).url != null) {
|
|
|
|
// the `as string` shouldn't be necessary, but the type system
|
|
|
|
// is getting a little confused
|
|
|
|
url = (opts as ObjectWithUrl).url as string;
|
2020-03-13 16:13:21 +00:00
|
|
|
} else if (typeof opts === 'string') {
|
2018-10-16 10:25:37 +00:00
|
|
|
url = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url != null) {
|
|
|
|
logger.logDebug(`Sending request to ${url}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 15:24:02 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return request(opts, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
switch (response.statusCode) {
|
|
|
|
case 200:
|
|
|
|
return resolve(body);
|
|
|
|
case 400:
|
|
|
|
return reject(new ApiErrors.BadRequestDeviceAPIError(body.message));
|
|
|
|
case 503:
|
|
|
|
return reject(
|
|
|
|
new ApiErrors.ServiceUnavailableAPIError(body.message),
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return reject(new ApiErrors.DeviceAPIError(body.message));
|
|
|
|
}
|
|
|
|
});
|
2018-10-16 10:25:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DeviceAPI;
|