Skip to content

Commit

Permalink
Merge branch 'development' of github.com:threefoldtech/tfgrid-sdk-ts …
Browse files Browse the repository at this point in the history
…into development_get_nodeid_for_rentContract
  • Loading branch information
0oM4R committed Jan 13, 2025
2 parents c76ba86 + fd63f33 commit 34d7c15
Show file tree
Hide file tree
Showing 102 changed files with 2,281 additions and 247 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"eslint-plugin-svelte3": "^4.0.0",
"eslint-plugin-vue": "^9.10.0",
"husky": "^8.0.3",
"lerna": "^6.6.1",
"lerna": "8.0.0",
"lint-staged": "^13.2.0",
"prettier": "^2.8.7",
"prettier-plugin-svelte": "^2.10.0"
Expand Down
117 changes: 117 additions & 0 deletions packages/grid_client/docs/deployment_flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Deployment Flow

This document outlines the process for determining whether to deploy on a zos3 or a zos4 node.

## Machine Model Initialization

The deployment process begins with initializing the machine model as follows:

```ts
const vms: MachinesModel = {
name: "newMY",
network: {
name: "hellotest",
ip_range: "10.249.0.0/16",
myceliumSeeds: [
{
nodeId: 168,
seed: "050d109829d8492d48bfb33b711056080571c69e46bfde6b4294c4c5bf468a76", //(HexSeed of length 32)
},
],
},
machines: [
{
name: "testvmMY",
node_id: 168,
disks: [
{
name: "wedDisk",
size: 8,
mountpoint: "/testdisk",
},
],
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: true,
myceliumSeed: "1e1404279b3d", //(HexSeed of length 6)
cpu: 1,
memory: 1024 * 2,
rootfs_size: 0,
flist: "https://hub.grid.tf/tf-official-apps/base:latest.flist",
entrypoint: "/sbin/zinit init",
env: {
SSH_KEY: config.ssh_key,
},
},
],
metadata: "",
description: "test deploying single VM with mycelium via ts grid3 client",
};
```

## Deployment Execution

- The next step is invoking the `deploy` function:
- Takes the `MachinesModel` object as a parameter
- Checks if a machine with the same name already exists and if so throws an error
- If not, then it calls the `_createDeployment` function along some othe functions
- Finally, the function returns the created contracts. If the deployment is on a zos3 node, then the wireguard configuration is returned as well.

```ts
await client.machines.deploy(vms);
```

- The `_createDeployment` function:
- Takes the `MachinesModel` object as a parameter
- Retrieves the features of the node using: `await this.rmb.request([nodeTwinId], "zos.system.node_features_get", "", 20, 3);`
- Examines the retrieved features to determine the network's primitive type (`Network` or `ZNetworkLight`) and initializes it accordingly.
- Sets the contractMetadata based on the network type.
- Invokes the `create` function

```ts
await this._createDeployment(options);
```

- The `create` function
- Validates or assigns IP addresses based on the network type.
- Determines network type (`network` or `network-light`) based on node features.
- Adds access points and updates network configurations as necessary.
- Initialize the VM primitive (`VMPrimitive` or `VMLightPrimitive`) based on the network type
- Configures the VM with networking, storage, and environment variables.
- Generates a Mycelium seed if not provided.
- Generate the deployments
- Finally, the function returns the created contracts. If the deployment is on a zos3 node, then the wireguard configuration is returned as well.

```ts
await this.vm.create(
machine.name,
machine.node_id,
machine.flist,
machine.cpu,
machine.memory,
machine.rootfs_size,
machine.disks!,
machine.public_ip,
machine.public_ip6!,
machine.planetary,
machine.mycelium,
machine.myceliumSeed!,
network,
options.network.myceliumSeeds!,
machine.entrypoint,
machine.env,
contractMetadata,
options.metadata,
options.description,
machine.qsfs_disks,
this.config.projectName,
options.network.addAccess,
options.network.accessNodeId,
machine.ip,
machine.corex,
machine.solutionProviderId!,
machine.zlogsOutput,
machine.gpus,
);
```
3 changes: 2 additions & 1 deletion packages/grid_client/scripts/applications/casperlabs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FilterOptions, GatewayNameModel, MachinesModel } from "../../src";
import { Features, FilterOptions, GatewayNameModel, MachinesModel } from "../../src";
import { config, getClient } from "../client_loader";
import { log, pingNodes } from "../utils";

Expand Down Expand Up @@ -55,6 +55,7 @@ async function main() {
sru: instanceCapacity.sru,
availableFor: grid3.twinId,
farmId: 1,
features: [Features.wireguard],
};
//GatewayNode Selection
const gatewayQueryOptions: FilterOptions = {
Expand Down
3 changes: 2 additions & 1 deletion packages/grid_client/scripts/applications/discourse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from "buffer";
import TweetNACL from "tweetnacl";

import { FilterOptions, GatewayNameModel, MachinesModel } from "../../src";
import { Features, FilterOptions, GatewayNameModel, MachinesModel } from "../../src";
import { config, getClient } from "../client_loader";
import { log, pingNodes } from "../utils";

Expand Down Expand Up @@ -63,6 +63,7 @@ async function main() {
sru: instanceCapacity.sru,
availableFor: grid3.twinId,
farmId: 1,
features: [Features.ip, Features.ipv4, Features.wireguard],
};
//GatewayNode Selection
const gatewayQueryOptions: FilterOptions = {
Expand Down
3 changes: 2 additions & 1 deletion packages/grid_client/scripts/applications/funkwhale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FilterOptions, GatewayNameModel, MachinesModel } from "../../src";
import { Features, FilterOptions, GatewayNameModel, MachinesModel } from "../../src";
import { config, getClient } from "../client_loader";
import { log, pingNodes } from "../utils";

Expand Down Expand Up @@ -55,6 +55,7 @@ async function main() {
sru: instanceCapacity.sru,
availableFor: grid3.twinId,
farmId: 1,
features: [Features.wireguard],
};
//GatewayNode Selection
const gatewayQueryOptions: FilterOptions = {
Expand Down
128 changes: 128 additions & 0 deletions packages/grid_client/scripts/applications/gitea.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { FilterOptions, GatewayNameModel, MachinesModel } from "../../src";
import { config, getClient } from "../client_loader";
import { log, pingNodes } from "../utils";

async function deploy(client, vms, subdomain, gatewayNode) {
const resultVM = await client.machines.deploy(vms);
log("================= Deploying VM =================");
log(resultVM);
log("================= Deploying VM =================");

const vmPlanetary = (await client.machines.getObj(vms.name))[0].planetary;

// Name Gateway Model
const gw: GatewayNameModel = {
name: subdomain,
node_id: gatewayNode.nodeId,
tls_passthrough: false,
backends: [`http://[${vmPlanetary}]:3000`],
};

const resultGateway = await client.gateway.deploy_name(gw);
log("================= Deploying name gateway =================");
log(resultGateway);
log("================= Deploying name gateway =================");
}

async function getDeployment(client, vms, gw) {
const resultVM = await client.machines.getObj(vms.name);
const resultGateway = await client.gateway.getObj(gw);
log("================= Getting deployment information =================");
log(resultVM);
log(resultGateway);
log("https://" + resultGateway[0].domain);
log("================= Getting deployment information =================");
}

async function cancel(client, vms, gw) {

Check warning on line 37 in packages/grid_client/scripts/applications/gitea.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

'cancel' is defined but never used
const resultVM = await client.machines.delete(vms);
const resultGateway = await client.gateway.delete_name(gw);
log("================= Canceling the deployment =================");
log(resultVM);
log(resultGateway);
log("================= Canceling the deployment =================");
}

async function main() {
const name = "giteainstance";
const networkName = "giteanetwork";
const grid3 = await getClient(`gitea/${name}`);
const subdomain = "gt" + grid3.twinId + name;
const instanceCapacity = { cru: 2, mru: 4, sru: 50 };

// VM Node Selection
const vmQueryOptions: FilterOptions = {
cru: instanceCapacity.cru,
mru: instanceCapacity.mru,
sru: instanceCapacity.sru,
availableFor: grid3.twinId,
farmId: 1,
};

// Gateway Node Selection
const gatewayQueryOptions: FilterOptions = {
gateway: true,
availableFor: grid3.twinId,
};
const gatewayNode = (await grid3.capacity.filterNodes(gatewayQueryOptions))[0];
const nodes = await grid3.capacity.filterNodes(vmQueryOptions);
const vmNode = await pingNodes(grid3, nodes);
const domain = subdomain + "." + gatewayNode.publicConfig.domain;

const vms: MachinesModel = {
name,
network: {
name: networkName,
ip_range: "10.249.0.0/16",
},
machines: [
{
name: "giteavm",
node_id: vmNode,
disks: [
{
name: "giteadisk",
size: instanceCapacity.sru,
mountpoint: "/mnt/data",
},
],
planetary: true,
public_ip: false,
public_ip6: false,
mycelium: true,
cpu: instanceCapacity.cru,
memory: 1024 * instanceCapacity.mru,
rootfs_size: 0,
flist: "https://hub.grid.tf/petep.3bot/threefolddev-gitea-latest.flist",
entrypoint: "/sbin/zinit init",
env: {
SSH_KEY: config.ssh_key,
GITEA__HOSTNAME: domain,
//Uncomment incase of using smtp server
// GITEA__mailer__PROTOCOL: "smtp",
// GITEA__mailer__ENABLED: "true",
// GITEA__mailer__HOST: "smtp.example.com",
// GITEA__mailer__FROM: "[email protected]",
// GITEA__mailer__PORT: "587",
// GITEA__mailer__USER: "admin",
// GITEA__mailer__PASSWD: "password123",
},
},
],
metadata: "",
description: "Deploying Gitea via TS Grid3 client",
};

// Deploy VMs
await deploy(grid3, vms, subdomain, gatewayNode);

// Get the deployment
await getDeployment(grid3, vms, subdomain);

// Uncomment the line below to cancel the deployment
// await cancel(grid3, { name }, { name: subdomain });

await grid3.disconnect();
}

main();
Loading

0 comments on commit 34d7c15

Please sign in to comment.