Skip to content

Commit

Permalink
feat: display upgrade progress steps (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Nov 26, 2024
1 parent ab17eae commit 1fe3fc6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
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;
}

0 comments on commit 1fe3fc6

Please sign in to comment.