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