When we have many canisters, it is troublesome to upgrade them one by one.
Or if our canister is created by the root canister and we are not the controller.
And when we need to disperse permissions and do not want to deploy directly by someone’s local identity.
You can consider using this method for version management through the motoko language
We need the latest wasm file
of the module corresponding to the target canister, and when creating canister, you need to specify the module that calls the upgrade api as its controller.
First, you need to copy the ic-manage declaration to the local
https://github.com/johnxiaohe/ICP-Spark/blob/dev/src/spark_backend/management.mo
Then declare the IC management module client and upgrade method in the module responsible for the upgrade
actor{
type Management = management.Management;
let mng : Management = actor("aaaaa-aa");
public shared func updatecodeWithoutArgs(wasm: [Nat8], cids:[Text]): async(Bool){
for(cid in Iter.fromArray(cids)){
await mng.install_code({
arg = to_candid();
wasm_module = wasm;
mode = #upgrade;
canister_id = Principal.fromText(cid);
});
};
return true;
};
public shared func updatecodeWithArgs(wasm: [Nat8], cids:[Text]): async(Bool){
for(cid in Iter.fromArray(cids)){
await mng.install_code({
arg = to_candid('arg1','arg2','arg3','arg4');
wasm_module = wasm;
mode = #upgrade;
canister_id = Principal.fromText(cid);
});
};
return true;
};
}
wasm file stream can obtain wasm file generation stream through js client, call motoko canister to upgrade our main canister, child canister
When our child Canister has many dynamic init parameters. When updating the code, the same init parameters are needed as arg. You can use the following method to pass the parameters in order to generate blob.
to_candid('arg1','arg2','arg3','arg4')
Because the init parameters of child are dynamic and unpredictable. It is recommended that the child canister provide an argsblob interface, and obtain the init args blob information of the canister through the interface during the upgrade
Thanks to these authors for their ideas