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'], $select: ['commit'],
}); });
} else { } else {
const [{ id: userId }, auth, apiEndpoint] = await Promise.all([
sdk.auth.getUserInfo(),
sdk.auth.getToken(),
sdk.settings.get('apiUrl'),
]);
release = await $deployProject( release = await $deployProject(
docker, docker,
sdk,
logger, logger,
project.composition, project.composition,
images, images,
opts.app.id, opts.app.id,
userId,
`Bearer ${auth}`,
apiEndpoint,
!opts.shouldUploadLogs, !opts.shouldUploadLogs,
composeOpts.projectPath, composeOpts.projectPath,
opts.createAsDraft, opts.createAsDraft,

View File

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

View File

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

View File

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

View File

@ -67,6 +67,7 @@
"test:standalone": "npm run build:standalone && npm run test:standalone:fast", "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: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": "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: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\"", "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", "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 expectedResponseLines = ['[Error] Deploy failed'];
const errMsg = 'Patch Image Error'; const errMsg = 'Patch Image Error';
const expectedErrorLines = [errMsg]; const expectedErrorLines = [`Request error: ${errMsg}`];
// The SDK should produce an "unexpected" BalenaRequestError, which // The SDK should produce an "unexpected" BalenaRequestError, which
// causes the CLI to call process.exit() with process.exitCode = 1 // causes the CLI to call process.exit() with process.exitCode = 1
const expectedExitCode = 1; const expectedExitCode = 1;

View File

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