From 3b72e4963586aa9687aad5f6339910f587201fc2 Mon Sep 17 00:00:00 2001 From: rcooke-warwick Date: Thu, 29 Jun 2023 11:10:25 +0100 Subject: [PATCH 1/2] core: allow configurable ssh target Change-type: patch Signed-off-by: Ryan Cooke --- core/lib/common/worker.js | 14 +++++++------- core/lib/components/balena/sdk.js | 29 +++++++++++++++++++---------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/core/lib/common/worker.js b/core/lib/common/worker.js index 83ccd53d1..41b6cdd99 100644 --- a/core/lib/common/worker.js +++ b/core/lib/common/worker.js @@ -79,6 +79,7 @@ module.exports = class Worker { url, username, sshKey, + sshConfig = {} ) { this.deviceType = deviceType; this.url = url; @@ -96,16 +97,15 @@ module.exports = class Worker { this.url.includes(`worker`) || this.url.includes('unix:') ); - if (this.url.includes(`balena-devices.com`)) { - // worker is a testbot connected to balena cloud - we ssh into it via the vpn + if(!this.directConnect){ this.uuid = this.url.match( - /(?<=https:\/\/)(.*)(?=.balena-devices.com)/, + /https:\/\/([^\.]+)\./, )[1]; - this.workerHost = `ssh.balena-devices.com`; - this.workerUser = this.username; - this.workerPort = '22'; this.sshPrefix = `host ${this.uuid} `; - } + this.workerUser = this.username; + this.workerPort = sshConfig.port || 22; + this.workerHost = sshConfig.host || 'ssh.balena-devices.com' + } } /** diff --git a/core/lib/components/balena/sdk.js b/core/lib/components/balena/sdk.js index 5f8332146..923b1a6c0 100644 --- a/core/lib/components/balena/sdk.js +++ b/core/lib/components/balena/sdk.js @@ -65,12 +65,18 @@ module.exports = class BalenaSDK { constructor( apiUrl, logger = { log: console.log, status: console.log, info: console.log }, + sshConfig = {} ) { this.balena = getSdk({ apiUrl: `https://api.${apiUrl}`, }); + this.apiUrl = apiUrl this.pine = this.balena.pine; this.logger = logger; + this.sshConfig = { + host: sshConfig.host || 'ssh.balena-devices.com', + port: sshConfig.port || 22 + } } /** @@ -91,7 +97,6 @@ module.exports = class BalenaSDK { tries: 600, }, ) { - const sshPort = 22; return retry( async () => { @@ -102,9 +107,9 @@ module.exports = class BalenaSDK { const result = await utils.executeCommandOverSSH( `host -s ${device} source /etc/profile ; ${command}`, { - host: `ssh.${await this.balena.settings.get('proxyUrl')}`, + host: this.sshConfig.host, username: await this.balena.auth.whoami(), - port: sshPort, + port: this.sshConfig.sshPort, }, ); @@ -126,7 +131,7 @@ module.exports = class BalenaSDK { /** * Creates application for a devicetype with the required config - * + * * @param {string} name Name of the application that needs to be created * @param {string} deviceType The device type for which application needs to be created * @param {object} config specify configuration needed for the application @@ -157,9 +162,9 @@ module.exports = class BalenaSDK { /** * Removes SSH key from balenaCloud account - * - * @param {string} label SSH key label - * + * + * @param {string} label SSH key label + * * @category helper */ async removeSSHKey(label) { @@ -336,7 +341,11 @@ module.exports = class BalenaSDK { */ async fetchOS(versionOrRange = 'latest', deviceType, osType = 'default') { // normalize the version string/range, supports 'latest', 'recommended', etc - let version = await this.balena.models.os.getMaxSatisfyingVersion( + const balenaSdkProd = getSdk({ + apiUrl: "https://api.balena-cloud.com", + }); + + let version = await balenaSdkProd.models.os.getMaxSatisfyingVersion( deviceType, versionOrRange, osType, @@ -352,15 +361,15 @@ module.exports = class BalenaSDK { ); // Caching implementation if needed - Check https://github.com/balena-os/leviathan/issues/441 - let attempt = 0; const downloadLatestOS = async () => { attempt++; this.logger.log( `Fetching balenaOS version ${version}, attempt ${attempt}...`, ); + return await new Promise(async (resolve, reject) => { - await this.balena.models.os.download( + await balenaSdkProd.models.os.download( deviceType, version, function (error, stream) { From 1f44580c2010f0e88eb395da951cb539b6afbcdb Mon Sep 17 00:00:00 2001 From: rcooke-warwick Date: Mon, 3 Jul 2023 13:44:16 +0100 Subject: [PATCH 2/2] update e2e tests to use sshconfig Change-type: patch Signed-off-by: Ryan Cooke --- core/lib/common/worker.js | 12 ++++++------ core/lib/components/balena/sdk.js | 2 +- suites/e2e/suite.js | 3 ++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/lib/common/worker.js b/core/lib/common/worker.js index 41b6cdd99..8576f1961 100644 --- a/core/lib/common/worker.js +++ b/core/lib/common/worker.js @@ -88,8 +88,8 @@ module.exports = class Worker { this.sshKey = sshKey; this.dutSshKey = `/tmp/id`; this.logger = logger; - this.workerHost = new URL(this.url).hostname; - this.workerPort = '22222'; + this.workerHost = sshConfig.host || 'ssh.balena-devices.com' + this.workerPort = sshConfig.port || 22; this.workerUser = 'root'; this.sshPrefix = ''; this.uuid = ''; @@ -103,8 +103,6 @@ module.exports = class Worker { )[1]; this.sshPrefix = `host ${this.uuid} `; this.workerUser = this.username; - this.workerPort = sshConfig.port || 22; - this.workerHost = sshConfig.host || 'ssh.balena-devices.com' } } @@ -331,10 +329,12 @@ module.exports = class Worker { }; } else { config = { - host: 'ssh.balena-devices.com', - port: '22', + host: this.workerHost, + port: this.workerPort, username: this.username, }; + console.log('local ssh attempt') + console.log(config) command = `host ${target} ${command}`; } diff --git a/core/lib/components/balena/sdk.js b/core/lib/components/balena/sdk.js index 923b1a6c0..274538843 100644 --- a/core/lib/components/balena/sdk.js +++ b/core/lib/components/balena/sdk.js @@ -109,7 +109,7 @@ module.exports = class BalenaSDK { { host: this.sshConfig.host, username: await this.balena.auth.whoami(), - port: this.sshConfig.sshPort, + port: this.sshConfig.port, }, ); diff --git a/suites/e2e/suite.js b/suites/e2e/suite.js index 5dcd46647..8f60cafe1 100644 --- a/suites/e2e/suite.js +++ b/suites/e2e/suite.js @@ -75,7 +75,7 @@ module.exports = { utils: this.require('common/utils'), sshKeyPath: join(homedir(), 'id'), sshKeyLabel: this.suite.options.id, - sdk: new Balena(this.suite.options.balena.apiUrl, this.getLogger()), + sdk: new Balena(this.suite.options.balena.apiUrl, this.getLogger(), this.suite.options.config.sshConfig), link: `${this.suite.options.balenaOS.config.uuid.slice(0, 7)}.local`, worker: new Worker( this.suite.deviceType.slug, @@ -83,6 +83,7 @@ module.exports = { this.suite.options.workerUrl, this.suite.options.balena.organization, join(homedir(), 'id'), + this.suite.options.config.sshConfig ), });