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

feat: display upgrade progress steps #158

Merged
merged 2 commits into from
Nov 26, 2024
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
6 changes: 3 additions & 3 deletions src/services/upgrade/upgrade-assert.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {satelliteBuildType, type BuildType, type SatelliteParameters} from '@jun
import {gunzipFile, isGzip} from '@junobuild/cli-tools';
import {isNullish, nonNullish} from '@junobuild/utils';
import {cyan, yellow} from 'kleur';
import {type UpgradeWasm, type UpgradeWasmModule} from '../../types/upgrade';
import type {AssertWasmModule, UpgradeWasm} from '../../types/upgrade';
import {NEW_CMD_LINE, confirmAndExit} from '../../utils/prompt.utils';

const wasmBuildType = async ({wasmModule}: UpgradeWasmModule): Promise<BuildType | undefined> => {
const wasmBuildType = async ({wasmModule}: AssertWasmModule): Promise<BuildType | undefined> => {
const buffer = Buffer.from(wasmModule);

const wasm = isGzip(buffer)
Expand All @@ -29,7 +29,7 @@ const wasmBuildType = async ({wasmModule}: UpgradeWasmModule): Promise<BuildType
export const assertSatelliteBuildType = async ({
satellite,
wasmModule
}: {satellite: SatelliteParameters} & UpgradeWasmModule) => {
}: {satellite: SatelliteParameters} & AssertWasmModule) => {
// TODO: Workaround for agent-js. Disable console.warn.
// See https://github.com/dfinity/agent-js/issues/843
// eslint-disable-next-line @typescript-eslint/unbound-method
Expand Down
7 changes: 6 additions & 1 deletion src/services/upgrade/upgrade.mission-control.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,10 @@ const upgradeMissionControlCustom = async ({
});
};

return await upgradeWasmLocal({src, nocheck, upgrade: upgradeMissionControlWasm});
return await upgradeWasmLocal({
src,
nocheck,
assetKey: 'mission_control',
upgrade: upgradeMissionControlWasm
});
};
8 changes: 7 additions & 1 deletion src/services/upgrade/upgrade.orbiter.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ const upgradeOrbiterCustom = async ({
});
};

return await upgradeWasmLocal({src, nocheck, upgrade: upgradeOrbiterWasm, reset});
return await upgradeWasmLocal({
src,
nocheck,
assetKey: 'orbiter',
upgrade: upgradeOrbiterWasm,
reset
});
};

const updateOrbiterRelease = async ({
Expand Down
6 changes: 3 additions & 3 deletions src/services/upgrade/upgrade.satellite.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {cyan, red} from 'kleur';
import {compare} from 'semver';
import {junoConfigExist, readJunoConfig} from '../../configs/juno.config';
import {SATELLITE_WASM_NAME} from '../../constants/constants';
import type {UpgradeWasm, UpgradeWasmModule} from '../../types/upgrade';
import type {AssertWasmModule, UpgradeWasm, UpgradeWasmModule} from '../../types/upgrade';
import {configEnv} from '../../utils/config.utils';
import {consoleNoConfigFound} from '../../utils/msg.utils';
import {NEW_CMD_LINE} from '../../utils/prompt.utils';
Expand Down Expand Up @@ -82,7 +82,7 @@ const upgradeSatelliteCustom = async ({
const upgrade = async (
params: Pick<UpgradeWasm, 'upgrade' | 'reset' | 'assert'>
): Promise<{success: boolean; err?: unknown}> => {
return await upgradeWasmLocal({src, nocheck, ...params});
return await upgradeWasmLocal({src, assetKey: 'satellite', nocheck, ...params});
};

return await executeUpgradeSatellite({
Expand Down Expand Up @@ -162,7 +162,7 @@ const executeUpgradeSatellite = async ({
});
};

const assert = async (params: UpgradeWasmModule) => {
const assert = async (params: AssertWasmModule) => {
await assertSatelliteBuildType({satellite, ...params});
};

Expand Down
36 changes: 29 additions & 7 deletions src/services/upgrade/upgrade.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
checkUpgradeVersion,
setCustomDomains,
UpgradeCodeProgress,
UpgradeCodeUnchangedError,
type CustomDomain,
type SatelliteParameters
Expand All @@ -26,18 +27,37 @@ const executeUpgradeWasm = async ({
hash,
assert,
reset = false,
nocheck
}: UpgradeWasm) => {
nocheck,
assetKey
}: {assetKey: AssetKey} & UpgradeWasm) => {
if (!nocheck) {
await assert?.({wasmModule: wasm});
await assertUpgradeHash({hash, reset});
}

const spinner = ora(`Upgrading Wasm${reset ? ' and resetting state' : ''}...`).start();
const spinner = ora().start();

const onProgress = (process: UpgradeCodeProgress) => {
switch (process) {
case UpgradeCodeProgress.AssertingExistingCode:
spinner.text = 'Validating if an update is needed...';
break;
case UpgradeCodeProgress.StoppingCanister:
spinner.text = `Stopping ${assetKey} before update...`;
break;
case UpgradeCodeProgress.UpgradingCode:
spinner.text = `Upgrading${reset ? ' and resetting state' : ''}...`;
break;
case UpgradeCodeProgress.RestartingCanister:
spinner.text = `Restarting ${assetKey}...`;
break;
}
};

try {
await upgrade({
wasmModule: wasm
wasmModule: wasm,
onProgress
});
} finally {
spinner.stop();
Expand All @@ -49,9 +69,11 @@ export const upgradeWasmLocal = async ({
upgrade,
reset,
assert,
nocheck
nocheck,
assetKey
}: {
src: string;
assetKey: AssetKey;
} & Pick<UpgradeWasm, 'reset' | 'upgrade' | 'assert' | 'nocheck'>): Promise<{
success: boolean;
err?: unknown;
Expand All @@ -72,7 +94,7 @@ export const upgradeWasmLocal = async ({

spinner.stop();

await executeUpgradeWasm({upgrade, wasm, hash, reset, assert, nocheck});
await executeUpgradeWasm({upgrade, wasm, hash, reset, assert, nocheck, assetKey});

return {success: true};
} catch (err: unknown) {
Expand Down Expand Up @@ -120,7 +142,7 @@ export const upgradeWasmCdn = async ({

spinner.stop();

await executeUpgradeWasm({upgrade, wasm, hash, reset, assert, nocheck});
await executeUpgradeWasm({upgrade, wasm, hash, reset, assert, nocheck, assetKey});

return {success: true};
} catch (err: unknown) {
Expand Down
9 changes: 6 additions & 3 deletions src/types/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type {InstallCodeParams} from '@dfinity/ic-management';
import type {UpgradeCodeParams} from '@junobuild/admin';

export type UpgradeWasmModule = Pick<InstallCodeParams, 'wasmModule'>;
export type UpgradeWasmModule = Pick<UpgradeCodeParams, 'wasmModule'> &
Required<Pick<UpgradeCodeParams, 'onProgress'>>;

export type AssertWasmModule = Pick<UpgradeCodeParams, 'wasmModule'>;

export interface UpgradeWasm {
wasm: Buffer;
hash: string;
upgrade: (params: UpgradeWasmModule) => Promise<void>;
assert?: (params: UpgradeWasmModule) => Promise<void>;
assert?: (params: AssertWasmModule) => Promise<void>;
reset?: boolean;
nocheck: boolean;
}