Skip to content

Commit a057da8

Browse files
feat(console): use CLI upgrade wizard and snapshots before upgrade (#1716)
1 parent 4074d7a commit a057da8

File tree

4 files changed

+93
-25
lines changed

4 files changed

+93
-25
lines changed

scripts/console.snapshots.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
import { CONSOLE_ID } from './constants.mjs';
4+
import { listSnapshots } from './module.snapshots.mjs';
5+
6+
await listSnapshots({
7+
canisterId: CONSOLE_ID
8+
});

scripts/module.snapshots.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { encodeSnapshotId, ICManagementCanister } from '@dfinity/ic-management';
2+
import { icAgent, localAgent } from './actor.mjs';
3+
import { targetMainnet } from './utils.mjs';
4+
5+
const fnAgent = targetMainnet() ? icAgent : localAgent;
6+
const agent = await fnAgent();
7+
8+
export const listSnapshots = async ({ canisterId }) => {
9+
const { listCanisterSnapshots } = ICManagementCanister.create({
10+
agent
11+
});
12+
13+
const snapshots = await listCanisterSnapshots({ canisterId });
14+
15+
if (snapshots.length === 0) {
16+
console.log('No snapshots found.');
17+
return;
18+
}
19+
20+
console.table(
21+
snapshots.map((snapshot) => ({
22+
...snapshot,
23+
id: `0x${encodeSnapshotId(snapshot.id)}`
24+
}))
25+
);
26+
};

scripts/module.upgrade.mjs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env node
22

33
import { IDL } from '@dfinity/candid';
4-
import { ICManagementCanister } from '@dfinity/ic-management';
5-
import { fromNullable, uint8ArrayToHexString } from '@dfinity/utils';
4+
import { UpgradeCodeProgressStep, upgradeModule } from '@junobuild/admin';
65
import { fileExists } from '@junobuild/cli-tools';
76
import { createHash } from 'crypto';
87
import { readFile } from 'fs/promises';
98
import { join } from 'node:path';
109
import { icAgent, localAgent } from './actor.mjs';
10+
import { getIdentity } from './console.config.utils.mjs';
1111
import { targetMainnet } from './utils.mjs';
1212

1313
const INSTALL_MODE_UPGRADE = {
@@ -20,46 +20,72 @@ const loadGzippedWasm = async (destination) => {
2020
const buffer = await readFile(destination);
2121

2222
return {
23-
wasm: [...new Uint8Array(buffer)],
23+
wasm: buffer,
2424
hash: createHash('sha256').update(buffer).digest('hex')
2525
};
2626
};
2727

28-
const fnAgent = targetMainnet() ? icAgent : localAgent;
28+
const mainnet = targetMainnet();
29+
30+
const fnAgent = mainnet ? icAgent : localAgent;
2931
const agent = await fnAgent();
3032

33+
const identity = await getIdentity(mainnet);
34+
35+
const onProgress = ({ step, state }) => {
36+
switch (step) {
37+
case UpgradeCodeProgressStep.AssertingExistingCode:
38+
console.log(`Validating: ${state}`);
39+
break;
40+
case UpgradeCodeProgressStep.StoppingCanister:
41+
console.log(`Stopping: ${state}`);
42+
break;
43+
case UpgradeCodeProgressStep.TakingSnapshot:
44+
console.log(`Creating a snapshot: ${state}`);
45+
break;
46+
case UpgradeCodeProgressStep.UpgradingCode:
47+
console.log(`Upgrading: ${state}`);
48+
break;
49+
case UpgradeCodeProgressStep.RestartingCanister:
50+
console.log(`Restarting: ${state}`);
51+
break;
52+
}
53+
};
54+
3155
export const upgrade = async ({ sourceFilename, canisterId }) => {
3256
const source = join(target, sourceFilename);
3357

34-
console.log(`About to upgrade module ${canisterId} with source ${source}...`);
58+
console.log(`About to upgrade module ${canisterId.toText()} with source ${source}...`);
3559

3660
if (!(await fileExists(source))) {
3761
throw new Error(`${source} not found.`);
3862
}
3963

40-
const EMPTY_ARG = IDL.encode([], []);
41-
42-
const { installCode, canisterStatus } = ICManagementCanister.create({
43-
agent
44-
});
64+
console.log('');
4565

4666
const { wasm, hash } = await loadGzippedWasm(source);
4767

48-
const { module_hash } = await canisterStatus(canisterId);
49-
50-
const currentHash = fromNullable(module_hash);
68+
const EMPTY_ARG = IDL.encode([], []);
5169

52-
if (uint8ArrayToHexString(currentHash) === hash) {
53-
console.log(`Module hash ${hash} already installed.`);
54-
return;
70+
try {
71+
await upgradeModule({
72+
actor: {
73+
agent,
74+
identity
75+
},
76+
mode: INSTALL_MODE_UPGRADE,
77+
canisterId,
78+
wasmModule: wasm,
79+
arg: new Uint8Array(EMPTY_ARG),
80+
takeSnapshot: true,
81+
onProgress
82+
});
83+
84+
console.log('');
85+
console.log(`Module upgraded to hash ${hash}.`);
86+
} catch (err) {
87+
console.log('');
88+
console.error('message' in err ? err.message : err);
89+
process.exit(1);
5590
}
56-
57-
await installCode({
58-
mode: INSTALL_MODE_UPGRADE,
59-
canisterId,
60-
wasmModule: wasm,
61-
arg: new Uint8Array(EMPTY_ARG)
62-
});
63-
64-
console.log(`Module upgraded to hash ${hash}.`);
6591
};

scripts/observatory.snapshots.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
import { OBSERVATORY_ID } from './constants.mjs';
4+
import { listSnapshots } from './module.snapshots.mjs';
5+
6+
await listSnapshots({
7+
canisterId: OBSERVATORY_ID
8+
});

0 commit comments

Comments
 (0)