Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: allow for balena machine ssh target #1011

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions core/lib/common/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = class Worker {
url,
username,
sshKey,
sshConfig = {}
) {
this.deviceType = deviceType;
this.url = url;
Expand All @@ -87,25 +88,22 @@ 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 = '';
this.directConnect = (
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;
}
}

/**
Expand Down Expand Up @@ -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}`;
}

Expand Down
29 changes: 19 additions & 10 deletions core/lib/components/balena/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ module.exports = class BalenaSDK {
constructor(
apiUrl,
vipulgupta2048 marked this conversation as resolved.
Show resolved Hide resolved
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
}
}

/**
Expand All @@ -91,7 +97,6 @@ module.exports = class BalenaSDK {
tries: 600,
},
) {
const sshPort = 22;

return retry(
async () => {
Expand All @@ -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.port,
},
);

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion suites/e2e/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ 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,
this.getLogger(),
this.suite.options.workerUrl,
this.suite.options.balena.organization,
join(homedir(), 'id'),
this.suite.options.config.sshConfig
),
});

Expand Down