Skip to content

Commit

Permalink
feat: unbuffer sideload + validate (#72)
Browse files Browse the repository at this point in the history
* add: side-load expect durin runtime

* feat: declare not_ready on missing dependencies
  • Loading branch information
patrykcieszkowski authored Jul 5, 2022
1 parent 4614952 commit dfc1ae7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
22 changes: 0 additions & 22 deletions bin/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,6 @@

update-ca-certificates

# temp code that installs unbuffer without the need to pull the container again
# remove this code in about 3-4 months when all probes should have the container pulled
ARCHLOCAL=$(dpkg --print-architecture)

if [[ ! -f "/usr/bin/unbuffer" ]]; then

curl -O "http://ftp.nl.debian.org/debian/pool/main/e/expect/tcl-expect_5.45.4-2+b1_${ARCHLOCAL}.deb"
dpkg --extract "tcl-expect_5.45.4-2+b1_${ARCHLOCAL}.deb" /

curl -O "http://ftp.nl.debian.org/debian/pool/main/t/tcl8.6/libtcl8.6_8.6.11+dfsg-1_${ARCHLOCAL}.deb"
dpkg --extract "libtcl8.6_8.6.11+dfsg-1_${ARCHLOCAL}.deb" /

curl -O "http://ftp.nl.debian.org/debian/pool/main/t/tcl8.6/tcl8.6_8.6.11+dfsg-1_${ARCHLOCAL}.deb"
dpkg --extract "tcl8.6_8.6.11+dfsg-1_${ARCHLOCAL}.deb" /

curl -O "http://ftp.nl.debian.org/debian/pool/main/e/expect/expect_5.45.4-2+b1_${ARCHLOCAL}.deb"
dpkg --extract "expect_5.45.4-2+b1_${ARCHLOCAL}.deb" /

fi
# end temp code


function run_probe() {
node /app/dist/index.js
return
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"xo": "^0.50.0"
},
"scripts": {
"build": "tsc",
"build": "tsc && npm run copy:sh",
"copy:sh": "cp -r ./src/sh ./dist/sh",
"init:hooks": "husky install",
"lint": "xo",
"lint:fix": "xo --fix",
Expand Down
11 changes: 10 additions & 1 deletion src/helper/api-connect-handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import type {Socket} from 'socket.io-client';
import {scopedLogger} from '../lib/logger.js';
import type {ProbeLocation} from '../types.js';
import {hasRequired as hasRequiredDeps} from '../lib/dependencies.js';

const logger = scopedLogger('api:connect');

export const apiConnectLocationHandler = (data: ProbeLocation): void => {
export const apiConnectLocationHandler = (socket: Socket) => async (data: ProbeLocation): Promise<void> => {
logger.info(`connected from (${data.city}, ${data.country}, ${data.continent}) (lat: ${data.latitude} long: ${data.longitude})`);

if (await hasRequiredDeps()) {
console.log('yup its all here');
socket.emit('probe:status:ready', {});
} else {
socket.emit('probe:status:not_ready', {});
}
};
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {io} from 'socket.io-client';
import cryptoRandomString from 'crypto-random-string';
import physicalCpuCount from 'physical-cpu-count';
import type {CommandInterface, MeasurementRequest} from './types.js';
import {loadAll as loadAllDeps} from './lib/dependencies.js';
import {scopedLogger} from './lib/logger.js';
import {getConfValue} from './lib/config.js';
import {apiErrorHandler} from './helper/api-error-handler.js';
Expand All @@ -22,6 +23,8 @@ import './lib/updater.js';
// Run scheduled restart
import './lib/restart.js';

await loadAllDeps();

const logger = scopedLogger('general');
const handlersMap = new Map<string, CommandInterface<any>>();

Expand Down Expand Up @@ -69,7 +72,6 @@ function connect() {
})
.on('connect', () => {
worker.active = true;
socket.emit('probe:status:ready', {});
logger.debug('connection to API established');
})
.on('disconnect', (reason: string): void => {
Expand All @@ -90,7 +92,7 @@ function connect() {
}
})
.on('api:error', apiErrorHandler)
.on('api:connect:location', apiConnectLocationHandler)
.on('api:connect:location', apiConnectLocationHandler(socket))
.on('probe:measurement:request', (data: MeasurementRequest) => {
if (!worker.active) {
return;
Expand Down
24 changes: 24 additions & 0 deletions src/lib/dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import path, {dirname} from 'node:path';
import {fileURLToPath} from 'node:url';
import {execa} from 'execa';

const appDir = path.join(dirname(fileURLToPath(import.meta.url)), '..');

export const loadAll = async () => {
await loadUnbuffer();
};

export const loadUnbuffer = async () => {
await execa(path.join(appDir, 'sh', 'unbuffer.sh'));
};

export const hasRequired = async () => isUnbufferAvailable();

export const isUnbufferAvailable = async () => {
try {
await execa('which', ['unbuffer']);
return true;
} catch {
return false;
}
};
22 changes: 22 additions & 0 deletions src/sh/unbuffer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# temp code that installs unbuffer without the need to pull the container again
# remove this code in about 3-4 months when all probes should have the container pulled
ARCHLOCAL=$(dpkg --print-architecture)

if [[ ! -f "/usr/bin/unbuffer" ]]; then

curl "http://ftp.nl.debian.org/debian/pool/main/e/expect/tcl-expect_5.45.4-2+b1_${ARCHLOCAL}.deb" -o "/tmp/tcl-expect.deb"
dpkg --extract "/tmp/tcl-expect.deb" /

curl "http://ftp.nl.debian.org/debian/pool/main/t/tcl8.6/libtcl8.6_8.6.11+dfsg-1_${ARCHLOCAL}.deb" -o "/tmp/libtcl.deb"
dpkg --extract "/tmp/libtcl.deb" /

curl "http://ftp.nl.debian.org/debian/pool/main/t/tcl8.6/tcl8.6_8.6.11+dfsg-1_${ARCHLOCAL}.deb" -o "/tmp/tcl.deb"
dpkg --extract "/tmp/tcl.deb" /

curl "http://ftp.nl.debian.org/debian/pool/main/e/expect/expect_5.45.4-2+b1_${ARCHLOCAL}.deb" -o "/tmp/expect.deb"
dpkg --extract "/tmp/expect.deb" /

fi
# end temp code

0 comments on commit dfc1ae7

Please sign in to comment.