Merge pull request #1974 from balena-io/sdk-15

Update balena-sdk to 15.x
This commit is contained in:
bulldozer-balena[bot] 2020-08-11 17:21:59 +00:00 committed by GitHub
commit afded27692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 483 additions and 459 deletions

View File

@ -80,20 +80,22 @@ export default class DevicesSupportedCmd extends Command {
SDK.DeviceTypeJson.DeviceType
>> = await getBalenaSdk()
.models.config.getDeviceTypes()
.map((d) => {
if (d.aliases && d.aliases.length) {
// remove aliases that are equal to the slug
d.aliases = d.aliases.filter((alias: string) => alias !== d.slug);
if (!options.json) {
// stringify the aliases array with commas and spaces
d.aliases = [d.aliases.join(', ')];
.then((dts) =>
dts.map((d) => {
if (d.aliases && d.aliases.length) {
// remove aliases that are equal to the slug
d.aliases = d.aliases.filter((alias: string) => alias !== d.slug);
if (!options.json) {
// stringify the aliases array with commas and spaces
d.aliases = [d.aliases.join(', ')];
}
} else {
// ensure it is always an array (for the benefit of JSON output)
d.aliases = [];
}
} else {
// ensure it is always an array (for the benefit of JSON output)
d.aliases = [];
}
return d;
});
return d;
}),
);
if (!options.discontinued) {
deviceTypes = deviceTypes.filter((dt) => dt.state !== 'DISCONTINUED');
}

View File

@ -53,8 +53,9 @@ const getDeviceTypes = async function () {
return balena.models.config
.getDeviceTypes()
.then((deviceTypes) => _.sortBy(deviceTypes, 'name'))
.tap((dt) => {
.then((dt) => {
allDeviceTypes = dt;
return dt;
});
};

View File

@ -63,30 +63,27 @@ export const getDashboardLoginURL = (callbackUrl: string) => {
* if loggedIn
* console.log('Token is valid!')
*/
export const loginIfTokenValid = async (token: string) => {
export const loginIfTokenValid = async (token: string): Promise<boolean> => {
if (_.isEmpty(token?.trim())) {
return false;
}
const balena = getBalenaSdk();
return balena.auth
.getToken()
.catchReturn(undefined)
.then((currentToken) =>
balena.auth
.loginWithToken(token)
.return(token)
.then(balena.auth.isLoggedIn)
.tap((isLoggedIn) => {
if (isLoggedIn) {
return;
}
let currentToken;
try {
currentToken = await balena.auth.getToken();
} catch {
// ignore
}
if (currentToken != null) {
return balena.auth.loginWithToken(currentToken);
} else {
return balena.auth.logout();
}
}),
);
await balena.auth.loginWithToken(token);
const isLoggedIn = await balena.auth.isLoggedIn();
if (!isLoggedIn) {
if (currentToken != null) {
await balena.auth.loginWithToken(currentToken);
} else {
await balena.auth.logout();
}
}
return isLoggedIn;
};

View File

@ -695,9 +695,8 @@ export const authorizePush = function (
scope: images.map((repo) => `repository:${repo}:pull,push`),
},
})
.get('body')
.get('token')
.catchReturn('');
.then(({ body }) => body.token)
.catch(() => '');
};
/**

View File

@ -139,16 +139,18 @@ export function generateDeviceConfig(
function addApplicationKey(config: any, applicationNameOrId: string | number) {
return getBalenaSdk()
.models.application.generateApiKey(applicationNameOrId)
.tap((apiKey) => {
.then((apiKey) => {
config.apiKey = apiKey;
return apiKey;
});
}
function addProvisioningKey(config: any, applicationNameOrId: string | number) {
return getBalenaSdk()
.models.application.generateProvisioningKey(applicationNameOrId)
.tap((apiKey) => {
.then((apiKey) => {
config.apiKey = apiKey;
return apiKey;
});
}

View File

@ -183,6 +183,7 @@ export function getAppWithArch(
});
}
// TODO: Drop this. The sdk now has this baked in application.get().
function getApplication(
applicationName: string,
): Promise<ApplicationWithDeviceType> {
@ -207,10 +208,13 @@ function getApplication(
match[1],
match[0],
extraOptions,
);
) as Promise<ApplicationWithDeviceType>;
}
return balena.models.application.get(applicationName, extraOptions);
return balena.models.application.get(
applicationName,
extraOptions,
) as Promise<ApplicationWithDeviceType>;
}
export const delay = promisify(setTimeout);

View File

@ -174,14 +174,21 @@ export function selectApplication(
const balena = getBalenaSdk();
return balena.models.application
.hasAny()
.then(function (hasAnyApplications) {
.then(async (hasAnyApplications) => {
if (!hasAnyApplications) {
throw new ExpectedError("You don't have any applications");
}
return balena.models.application.getAll() as Promise<ApplicationWithDeviceType[]>;
const apps = (await balena.models.application.getAll({
$select: 'app_name',
$expand: {
is_for__device_type: {
$select: 'slug',
},
},
})) as ApplicationWithDeviceType[];
return apps.filter(filter || _.constant(true));
})
.filter(filter || _.constant(true))
.then((applications) => {
if (errorOnEmptySelection && applications.length === 0) {
throw new ExpectedError('No suitable applications found for selection');
@ -327,31 +334,29 @@ export async function awaitDeviceOsUpdate(
export function inferOrSelectDevice(preferredUuid: string) {
const balena = getBalenaSdk();
return balena.models.device
.getAll()
.filter<BalenaSdk.Device>((device) => device.is_online)
.then((onlineDevices) => {
if (_.isEmpty(onlineDevices)) {
throw new ExpectedError("You don't have any devices online");
}
return balena.models.device.getAll().then((devices) => {
const onlineDevices = devices.filter((device) => device.is_online);
if (_.isEmpty(onlineDevices)) {
throw new ExpectedError("You don't have any devices online");
}
const defaultUuid = _(onlineDevices).map('uuid').includes(preferredUuid)
? preferredUuid
: onlineDevices[0].uuid;
const defaultUuid = _(onlineDevices).map('uuid').includes(preferredUuid)
? preferredUuid
: onlineDevices[0].uuid;
return getCliForm().ask({
message: 'Select a device',
type: 'list',
default: defaultUuid,
choices: _.map(onlineDevices, (device) => ({
name: `${device.device_name || 'Untitled'} (${device.uuid.slice(
0,
7,
)})`,
value: device.uuid,
})),
});
return getCliForm().ask({
message: 'Select a device',
type: 'list',
default: defaultUuid,
choices: _.map(onlineDevices, (device) => ({
name: `${device.device_name || 'Untitled'} (${device.uuid.slice(
0,
7,
)})`,
value: device.uuid,
})),
});
});
}
export async function getOnlineTargetUuid(

717
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -133,8 +133,9 @@
"@types/intercept-stdout": "^0.1.0",
"@types/is-root": "^2.1.2",
"@types/js-yaml": "^3.12.3",
"@types/jsonwebtoken": "^8.5.0",
"@types/klaw": "^3.0.1",
"@types/lodash": "^4.14.150",
"@types/lodash": "^4.14.159",
"@types/mixpanel": "^2.14.2",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "^5.2.7",
@ -172,6 +173,7 @@
"http-proxy": "^1.18.1",
"husky": "^4.2.5",
"intercept-stdout": "^0.1.2",
"jsonwebtoken": "^8.5.1",
"mocha": "^6.2.3",
"mock-require": "^3.0.3",
"nock": "^12.0.3",
@ -194,17 +196,17 @@
"@zeit/dockerignore": "0.0.3",
"JSONStream": "^1.0.3",
"archiver": "^3.1.1",
"balena-config-json": "^4.0.0",
"balena-config-json": "^4.1.0",
"balena-device-init": "^5.0.2",
"balena-errors": "^4.3.0",
"balena-image-manager": "^7.0.1",
"balena-preload": "^10.2.0",
"balena-errors": "^4.4.1",
"balena-image-manager": "^7.0.3",
"balena-preload": "^10.2.4",
"balena-release": "^3.0.0",
"balena-sdk": "^14.8.0",
"balena-semver": "^2.2.0",
"balena-sdk": "^15.2.3",
"balena-semver": "^2.3.0",
"balena-settings-client": "^4.0.5",
"balena-settings-storage": "^6.0.0",
"balena-sync": "^11.0.0",
"balena-settings-storage": "^6.0.1",
"balena-sync": "^11.0.2",
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",
"capitano": "^1.9.2",
@ -236,7 +238,7 @@
"js-yaml": "^3.13.1",
"klaw": "^3.0.0",
"livepush": "^3.5.0",
"lodash": "^4.17.15",
"lodash": "^4.17.19",
"minimatch": "^3.0.4",
"mixpanel": "^0.10.3",
"mkdirp": "^0.5.1",
@ -269,7 +271,7 @@
"tar-utils": "^2.1.0",
"through2": "^2.0.3",
"tmp": "^0.2.1",
"typed-error": "^3.2.0",
"typed-error": "^3.2.1",
"umount": "^1.1.6",
"update-notifier": "^4.1.0",
"which": "^2.0.2",

View File

@ -4,3 +4,11 @@ publishMetadata: true
upstream:
- repo: 'balena-sdk'
url: 'https://github.com/balena-io/balena-sdk'
- repo: 'balena-config-json'
url: 'https://github.com/balena-io-modules/balena-config-json'
- repo: 'balena-image-manager'
url: 'https://github.com/balena-io-modules/balena-image-manager'
- repo: 'balena-preload'
url: 'https://github.com/balena-io-modules/balena-preload'
- repo: 'balena-sync'
url: 'https://github.com/balena-io-modules/balena-sync'

View File

@ -1,20 +1,39 @@
import * as jsonwebtoken from 'jsonwebtoken';
const johnDoe = {
username: 'johndoe1',
email: 'johndoe@johndoe.com',
gitlab_id: 1325,
social_service_account: null,
hasPasswordSet: true,
needsPasswordReset: false,
public_key: false,
features: [],
id: 1344,
intercomUserHash:
'e03778dd29e157445f272acc921170cf2810b62f502645265cc349d6deda3524',
permissions: [],
};
const janeDoe = {
id: 152,
username: 'janedoe',
email: 'janedoe@asdf.com',
social_service_account: null,
has_disabled_newsletter: true,
hasPasswordSet: true,
needsPasswordReset: false,
public_key: false,
features: [],
intercomUserHash:
'0b4f9eb44b371f0e328ef5fe03ad7eb2f5f72dd418ef23149d5287096558ce03',
permissions: [],
};
export default {
johndoe: {
token:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImpvaG5kb2UxIiwiZW1haWwiOiJqb2huZG9lQGpvaG5kb2UuY29tIiwiZ2l0bGFiX2lkIjoxMzI1LCJzb2NpYWxfc2VydmljZV9hY2NvdW50IjpudWxsLCJoYXNQYXNzd29yZFNldCI6dHJ1ZSwibmVlZHNQYXNzd29yZFJlc2V0IjpmYWxzZSwicHVibGljX2tleSI6ZmFsc2UsImZlYXR1cmVzIjpbXSwiaWQiOjEzNDQsImludGVyY29tVXNlckhhc2giOiJlMDM3NzhkZDI5ZTE1NzQ0NWYyNzJhY2M5MjExNzBjZjI4MTBiNjJmNTAyNjQ1MjY1Y2MzNDlkNmRlZGEzNTI0IiwicGVybWlzc2lvbnMiOltdLCJpYXQiOjE0MjY3ODMzMTJ9.v5bmh9HwyUZu8zhh1rA79mTL-1jzDOO8eUr_lVaBwhg',
data: {
email: 'johndoe@johndoe.com',
username: 'johndoe1',
id: 1344,
},
token: jsonwebtoken.sign(johnDoe, 'very-secret'),
},
janedoe: {
token:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTUyLCJ1c2VybmFtZSI6ImphbmVkb2UiLCJlbWFpbCI6ImphbmVkb2VAYXNkZi5jb20iLCJzb2NpYWxfc2VydmljZV9hY2NvdW50IjpudWxsLCJoYXNfZGlzYWJsZWRfbmV3c2xldHRlciI6dHJ1ZSwiaGFzUGFzc3dvcmRTZXQiOnRydWUsIm5lZWRzUGFzc3dvcmRSZXNldCI6ZmFsc2UsInB1YmxpY19rZXkiOmZhbHNlLCJmZWF0dXJlcyI6W10sImludGVyY29tVXNlckhhc2giOiIwYjRmOWViNDRiMzcxZjBlMzI4ZWY1ZmUwM2FkN2ViMmY1ZjcyZGQ0MThlZjIzMTQ5ZDUyODcwOTY1NThjZTAzIiwicGVybWlzc2lvbnMiOltdLCJpYXQiOjE0MzUzMjAyNjN9.jVzUFu58vzdJFctR8ulyjGL0Em1kjIZSbSxX2SeU03Y',
data: {
email: 'janedoe@asdf.com',
username: 'janedoe',
id: 152,
},
token: jsonwebtoken.sign(janeDoe, 'very-secret'),
},
};

View File

@ -70,7 +70,7 @@ describe('Utils:', function () {
describe('given the token does not authenticate with the server', function () {
beforeEach(function () {
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
return this.balenaAuthIsLoggedInStub.returns(Bluebird.resolve(false));
return this.balenaAuthIsLoggedInStub.resolves(false);
});
afterEach(function () {
@ -112,7 +112,7 @@ describe('Utils:', function () {
return describe('given the token does authenticate with the server', function () {
beforeEach(function () {
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
return this.balenaAuthIsLoggedInStub.returns(Bluebird.resolve(true));
return this.balenaAuthIsLoggedInStub.resolves(true);
});
afterEach(function () {