deploy: Use the sdk's pine instance with balena-compose

Change-type: patch
This commit is contained in:
Thodoris Greasidis 2024-07-15 13:29:20 +03:00
parent 232b9678bc
commit efa0d67f0a
7 changed files with 34 additions and 40 deletions

View File

@ -364,20 +364,13 @@ ${dockerignoreHelp}
$select: ['commit'],
});
} else {
const [{ id: userId }, auth, apiEndpoint] = await Promise.all([
sdk.auth.getUserInfo(),
sdk.auth.getToken(),
sdk.settings.get('apiUrl'),
]);
release = await $deployProject(
docker,
sdk,
logger,
project.composition,
images,
opts.app.id,
userId,
`Bearer ${auth}`,
apiEndpoint,
!opts.shouldUploadLogs,
composeOpts.projectPath,
opts.createAsDraft,

View File

@ -80,9 +80,7 @@ export interface ComposeProject {
}
export interface Release {
client: ReturnType<
typeof import('@balena/compose/dist/release').createClient
>;
client: import('@balena/compose').release.Request['client'];
release: Pick<
ReleaseModel,
| 'id'

View File

@ -121,10 +121,8 @@ const getRequestRetryParameters = (): RetryParametersObj => {
};
export const createRelease = async function (
sdk: SDK.BalenaSDK,
logger: Logger,
apiEndpoint: string,
auth: string,
userId: number,
appId: number,
composition: Composition,
draft: boolean,
@ -136,24 +134,32 @@ export const createRelease = async function (
const releaseMod =
require('@balena/compose/dist/release') as typeof import('@balena/compose/dist/release');
const client = releaseMod.createClient({
apiEndpoint,
auth,
retry: {
...getRequestRetryParameters(),
onRetry: (err, delayMs, attempt, maxAttempts) => {
const code = err?.statusCode ?? 0;
logger.logDebug(
`API call failed with code ${code}. Attempting retry ${attempt} of ${maxAttempts} in ${
delayMs / 1000
} seconds`,
);
// @ts-expect-error - Once we start using the pinejs-client-core@^6.15.0 types in the SDK's
// pine instance, this ts-expect-error should no longer be needed.
const pinejsClient: import('@balena/compose').release.Request['client'] =
sdk.pine.clone(
{
retry: {
...getRequestRetryParameters(),
onRetry: (err, delayMs, attempt, maxAttempts) => {
const code = err?.statusCode ?? 0;
logger.logDebug(
`API call failed with code ${code}. Attempting retry ${attempt} of ${maxAttempts} in ${
delayMs / 1000
} seconds`,
);
},
},
},
},
});
{
// @balena/compose atm works with v6, bump it once @balena/compose moves to v7.
apiVersion: 'v6',
},
);
const { id: userId } = await sdk.auth.getUserInfo();
const { release, serviceImages } = await releaseMod.create({
client,
client: pinejsClient,
user: userId,
application: appId,
composition,
@ -165,7 +171,7 @@ export const createRelease = async function (
});
return {
client,
client: pinejsClient,
release: _.pick(release, [
'id',
'status',

View File

@ -1326,9 +1326,7 @@ async function pushAndUpdateServiceImages(
async function pushServiceImages(
docker: Dockerode,
logger: Logger,
pineClient: ReturnType<
typeof import('@balena/compose/dist/release').createClient
>,
pineClient: import('@balena/compose').release.Request['client'],
taggedImages: TaggedImage[],
token: string,
skipLogUpload: boolean,
@ -1353,13 +1351,11 @@ async function pushServiceImages(
export async function deployProject(
docker: Dockerode,
sdk: BalenaSDK,
logger: Logger,
composition: Composition,
images: BuiltImage[],
appId: number,
userId: number,
auth: string,
apiEndpoint: string,
skipLogUpload: boolean,
projectPath: string,
isDraft: boolean,
@ -1378,6 +1374,7 @@ export async function deployProject(
Error: the version field in "${contractPath}"
is not a valid semver`);
}
const apiEndpoint = await sdk.settings.get('apiUrl');
const $release = await runSpinner(
tty,
@ -1385,10 +1382,8 @@ export async function deployProject(
`${prefix}Creating release...`,
() =>
createRelease(
sdk,
logger,
apiEndpoint,
auth,
userId,
appId,
composition,
isDraft,

View File

@ -67,6 +67,7 @@
"test:standalone": "npm run build:standalone && npm run test:standalone:fast",
"test:standalone:fast": "cross-env BALENA_CLI_TEST_TYPE=standalone mocha --config .mocharc-standalone.js",
"test:fast": "npm run build:fast && npm run test:source",
"test:fast-profile": "npm run test:fast -- -- --inspect-brk=0.0.0.0",
"test:debug": "cross-env BALENA_CLI_TEST_TYPE=source mocha --inspect-brk=0.0.0.0",
"test:only": "npm run build:fast && cross-env BALENA_CLI_TEST_TYPE=source mocha \"tests/**/${npm_config_test}.spec.ts\"",
"catch-uncommitted": "ts-node --transpile-only automation/run.ts catch-uncommitted",

View File

@ -276,7 +276,7 @@ describe('balena deploy', function () {
);
const expectedResponseLines = ['[Error] Deploy failed'];
const errMsg = 'Patch Image Error';
const expectedErrorLines = [errMsg];
const expectedErrorLines = [`Request error: ${errMsg}`];
// The SDK should produce an "unexpected" BalenaRequestError, which
// causes the CLI to call process.exit() with process.exitCode = 1
const expectedExitCode = 1;

View File

@ -162,6 +162,7 @@ export class BalenaAPIMock extends NockMock {
inspectRequest,
path.join(apiResponsePath, 'release-POST-v6.json'),
),
jHeader,
);
}