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

patch: Add support for downloading balenaOS from any environment #1193

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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: 16 additions & 6 deletions core/lib/components/balena/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,23 @@ module.exports = class BalenaSDK {
*
* @category helper
*/
async fetchOS(versionOrRange = 'latest', deviceType, osType = 'default') {
// normalize the version string/range, supports 'latest', 'recommended', etc
const balenaSdkProd = getSdk({
apiUrl: "https://api.balena-cloud.com",
async fetchOS(versionOrRange = 'latest', deviceType, osType = 'default', balenaDownloadApiUrl, balenaDownloadApiKey) {
const balenaDownloadSdk = getSdk({
apiUrl: balenaDownloadApiUrl ? balenaDownloadApiUrl : "https://api.balena-cloud.com",
});

if (balenaDownloadApiKey) {
await balenaDownloadSdk.auth.loginWithToken(balenaDownloadApiKey)
const username = await this.sdk.auth.whoami()
if (username) {
console.log(`Downloading with ${await this.sdk.auth.whoami()}'s account on ${this.balenaApiUrl} using balenaSDK`);
} else {
throw new Error('Failed to authenticate with balenaSDK. Check your API key or balenaCloud API URL address.');
}
}

let version = await balenaSdkProd.models.os.getMaxSatisfyingVersion(
// normalize the version string/range, supports 'latest', 'recommended', etc
let version = await balenaDownloadSdk.models.os.getMaxSatisfyingVersion(
deviceType,
versionOrRange,
osType,
Expand All @@ -369,7 +379,7 @@ module.exports = class BalenaSDK {
);

return await new Promise(async (resolve, reject) => {
await balenaSdkProd.models.os.download(
await balenaDownloadSdk.models.os.download(
deviceType,
version,
function (error, stream) {
Expand Down