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

fix: Can't list deployments #2701

Merged
merged 9 commits into from
May 21, 2024
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class BaseModule {
if (fetch || !this.contracts) {
let contracts = await this.tfClient.contracts.listMyNodeContracts({
graphqlURL: this.config.graphqlURL,
type: modulesNames[this.moduleName],
type: modulesNames[this.moduleName] ?? this.moduleName,
projectName: this.projectName,
});
const alreadyFetchedContracts: GqlNodeContract[] = [];
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from "./farmerbot";
export * from "./farms";
export * from "./networks";
export * from "./bridge";
export * from "./base";
54 changes: 51 additions & 3 deletions packages/playground/src/utils/migration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
import type { BaseModule } from "@threefold/grid_client/dist/es6/modules/base";
import type { Contract } from "@threefold/tfchain_client";
import { BaseModule } from "@threefold/grid_client";
import type { Contract, ExtrinsicResult } from "@threefold/tfchain_client";

/**
* `_migrateOldFullVMs` is a function is implemented to list all old `FullVM` that listed in the old `Playground` with type `Fullvm`
* @param `module` takes the `module` which is an instance of the `BaseModule`.
* @returns Applying the extrinsic in the chain with the batch request.
*/
async function _migrateOldFullVMs(module: BaseModule) {
const moduleName = module.moduleName;
module.moduleName = "Fullvm"; // Load old deployments that deployed with `Fullvm` type.
const deploymentNames = await module._list();
const contracts = await Promise.all(
deploymentNames.map(deploymentName => module.getDeploymentContracts(deploymentName)),
);

const loadUpdateExtrinsics = () => {
return Promise.all(
contracts.flat().map(contract => {
const oldData = JSON.parse(contract.deploymentData || "{}") as unknown as {
type: string;
name: string;
projectName: string;
};

if (oldData.type === "vm") {
return;
}

oldData.type = "vm";
BaseModule.newContracts.push(contract);
return module.tfClient.contracts.updateNode({
id: +contract.contractID,
data: JSON.stringify(oldData),
hash: contract.deploymentHash,
});
}),
);
};

module.moduleName = moduleName;
if (deploymentNames.length) {
const extrinsics = await loadUpdateExtrinsics().then(exts => exts.filter(Boolean) as ExtrinsicResult<Contract>[]);
return module.tfClient.applyAllExtrinsics<Contract>(extrinsics);
}
}

export async function migrateModule(module: BaseModule) {
await module._list(); // force grid_client migration
// To list only `Full` and `Micro` VMs.
if (module.moduleName === "machines") {
await _migrateOldFullVMs(module);
}

await module._list(); // force grid_client migration
const path = module.getNewDeploymentPath();
const keys = await module.backendStorage.list(path);

Expand Down
Loading