Merge pull request #1180 from balena-io/tunnel-short-uuid

tunnel: allow using partial device uuids
This commit is contained in:
Will Boyce 2019-04-24 11:15:05 +01:00 committed by GitHub
commit 12615cd0dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ import * as _ from 'lodash';
import { createServer, Server, Socket } from 'net'; import { createServer, Server, Socket } from 'net';
import { isArray } from 'util'; import { isArray } from 'util';
import { inferOrSelectDevice } from '../utils/patterns';
import { tunnelConnectionToDevice } from '../utils/tunnel'; import { tunnelConnectionToDevice } from '../utils/tunnel';
interface Args { interface Args {
@ -128,12 +129,11 @@ export const tunnel: CommandDefinition<Args, Options> = {
? (options.port as string[]) ? (options.port as string[])
: [options.port as string]; : [options.port as string];
return Bluebird.try(() => return inferOrSelectDevice(params.uuid)
sdk.models.device .then(sdk.models.device.get)
.get(params.uuid)
.then(device => { .then(device => {
if (!device.is_online) { if (!device.is_online) {
throw new DeviceIsOfflineError(params.uuid); throw new DeviceIsOfflineError(device.uuid);
} }
const localListeners = _.chain(ports) const localListeners = _.chain(ports)
@ -174,7 +174,7 @@ export const tunnel: CommandDefinition<Args, Options> = {
}; };
}) })
.map(({ localPort, localAddress, remotePort }) => { .map(({ localPort, localAddress, remotePort }) => {
return tunnelConnectionToDevice(params.uuid, remotePort, sdk) return tunnelConnectionToDevice(device.uuid, remotePort, sdk)
.then(handler => .then(handler =>
createServer((client: Socket) => { createServer((client: Socket) => {
return handler(client) return handler(client)
@ -212,16 +212,18 @@ export const tunnel: CommandDefinition<Args, Options> = {
) )
.then(() => { .then(() => {
logger.logInfo( logger.logInfo(
` - tunnelling ${localAddress}:${localPort} to device:${remotePort}`, ` - tunnelling ${localAddress}:${localPort} to ${
device.uuid
}:${remotePort}`,
); );
return true; return true;
}) })
.catch((err: Error) => { .catch((err: Error) => {
logger.logWarn( logger.logWarn(
` - not tunnelling ${localAddress}:${localPort} to device:${remotePort}, failed ${JSON.stringify( ` - not tunnelling ${localAddress}:${localPort} to ${
err.message, device.uuid
)}`, }:${remotePort}, failed ${JSON.stringify(err.message)}`,
); );
return false; return false;
@ -237,7 +239,7 @@ export const tunnel: CommandDefinition<Args, Options> = {
} }
logger.logInfo('Waiting for connections...'); logger.logInfo('Waiting for connections...');
}), })
).nodeify(done); .nodeify(done);
}, },
}; };