Skip to content

Commit

Permalink
Merge branch 'development' into development_manage_domain_k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Emad committed Aug 28, 2024
2 parents f954774 + b00d6f5 commit f452629
Show file tree
Hide file tree
Showing 31 changed files with 2,384 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/grid_client_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ jobs:
lerna run build --no-private
- name: Run tests
run: yarn workspace @threefold/grid_client test --runInBand --ci --coverage --colors --forceExit
env:
NODE_TLS_REJECT_UNAUTHORIZED: '0' # Disable TLS/SSL certificate validation
- name: Cleanup
continue-on-error: true
run: yarn run ts-node --project tsconfig-node.json packages/grid_client/tests/global_teardown.ts
Expand Down
121 changes: 121 additions & 0 deletions packages/grid_client/scripts/applications/static_website.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { FilterOptions, GatewayNameModel, GridClient, MachinesModel, NodeInfo } from "../../src";
import { config, getClient } from "../client_loader";
import { log, pingNodes } from "../utils";

async function deploy(client: GridClient, vms: MachinesModel, subdomain: string, gatewayNode: NodeInfo) {
// Deploying VM
const resultVM = await client.machines.deploy(vms);
log("================= Deploying VM =================");
log(resultVM);
log("================= Deploying VM =================");
//Name Gateway Model
const vmPlanetary = (await client.machines.getObj(vms.name))[0].planetary;
const gw: GatewayNameModel = {
name: subdomain,
node_id: gatewayNode.nodeId,
tls_passthrough: false,
backends: ["http://[" + vmPlanetary + "]:9000"],
};
// Deploying Gateway
const resultGateway = await client.gateway.deploy_name(gw);
log("================= Deploying name gateway =================");
log(resultGateway);
log("================= Deploying name gateway =================");
}

async function getDeployment(client: GridClient, vms: MachinesModel, gw: string) {
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: GridClient, vms: string, gw: string) {
const resultVM = await client.machines.delete({ name: vms });
const resultGateway = await client.gateway.delete_name({ name: gw });
log("================= Canceling the deployment =================");
log(resultVM);
log(resultGateway);
log("================= Canceling the deployment =================");
}

async function main() {
const name = "newstaticwebsite";
const grid3 = await getClient(`staticwebsite/${name}`);
const subdomain = "sw" + grid3.twinId + name;
const instanceCapacity = { cru: 1, mru: 2, sru: 50 }; // Update the instance capacity values according to your requirements.

//VMNode Selection
const vmQueryOptions: FilterOptions = {
cru: instanceCapacity.cru,
mru: instanceCapacity.mru,
sru: instanceCapacity.sru,
availableFor: grid3.twinId,
farmId: 1,
};
//GatewayNode 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: "wedtest",
ip_range: "10.249.0.0/16",
},
machines: [
{
name: "staticwebsite",
node_id: vmNode,
disks: [
{
name: "wedDisk",
size: instanceCapacity.sru,
mountpoint: "/var/lib/docker",
},
],
planetary: true,
public_ip: false,
public_ip6: false,
mycelium: false,
cpu: instanceCapacity.cru,
memory: 1024 * instanceCapacity.mru,
rootfs_size: 0,
flist: "https://hub.grid.tf/tf-official-apps/staticwebsite-latest.flist",
entrypoint: "/sbin/zinit init",
env: {
SSH_KEY: config.ssh_key,
STATICWEBSITE_DOMAIN: domain,
// Enter the HTTPS URL for the Git repository that needs to be cloned.
GITHUB_URL: "https://github.com/cloudacademy/static-website-example",
GITHUB_BRANCH: "", // Optional, Enter the Git Branch if available
HTML_DIR: "website", // Optional, Enter the html directory that needs to be served if available
},
},
],
metadata: "",
description: "test deploying Static Website 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, subdomain);

await grid3.disconnect();
}

main();
62 changes: 28 additions & 34 deletions packages/grid_client/scripts/orchestrators/kubernetes_with_qsfs.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { FilterOptions, K8SModel, QSFSZDBSModel } from "../../src";
import { FilterOptions, GridClient, K8SModel, QSFSZDBSModel } from "../../src";
import { config, getClient } from "../client_loader";
import { log } from "../utils";
import { log, pingNodes } from "../utils";

async function deployQsfs(client, qsfs) {
async function deployQsfs(client: GridClient, qsfs: QSFSZDBSModel) {
const res = await client.qsfs_zdbs.deploy(qsfs);
log("================= Deploying QSFS =================");
log(res);
log("================= Deploying QSFS =================");
}

async function deploy(client, k8s) {
async function deploy(client: GridClient, k8s: K8SModel) {
const res = await client.k8s.deploy(k8s);
log("================= Deploying K8s =================");
log(res);
log("================= Deploying K8s =================");
}

async function getDeployment(client, k8s) {
async function getDeployment(client: GridClient, k8s: string) {
const res = await client.k8s.getObj(k8s);
log("================= Getting deployment information =================");
log(res);
log("================= Getting deployment information =================");
}

async function cancel(client, k8s) {
const res = await client.k8s.delete(k8s);
async function cancel(client: GridClient, k8s: string) {
const res = await client.k8s.delete({ name: k8s });
log("================= Canceling the deployment =================");
log(res);
log("================= Canceling the deployment =================");
}

async function deleteQsfs(client, qsfs) {
const res = await client.qsfs_zdbs.delete(qsfs);
async function deleteQsfs(client: GridClient, qsfs: string) {
const res = await client.qsfs_zdbs.delete({ name: qsfs });
log("================= Deleting QSFS =================");
log(res);
log("================= Deleting QSFS =================");
Expand All @@ -42,45 +42,39 @@ async function main() {
const grid3 = await getClient(`kubernetes/${name}`);

const qsfs_name = "testQsfsK8sq1";

const masterQueryOptions: FilterOptions = {
const disk_size = 1;
const count = 8;
const options: FilterOptions = {
cru: 2,
mru: 2, // GB
sru: 6,
availableFor: grid3.twinId,
farmId: 1,
};

const workerQueryOptions: FilterOptions = {
cru: 1,
mru: 1, // GB
sru: 3,
availableFor: grid3.twinId,
farmId: 1,
};

const qsfsQueryOptions: FilterOptions = {
hru: 15,
hru: count * disk_size,
availableFor: grid3.twinId,
farmId: 1,
};

const qsfsNodes: number[] = [];

const allNodes = await grid3.capacity.filterNodes(qsfsQueryOptions);
if (allNodes.length >= 2) {
qsfsNodes.push(+allNodes[0].nodeId, +allNodes[1].nodeId);
} else {
throw Error("Couldn't find nodes for qsfs");
async function getNodeId(client: GridClient, options: FilterOptions) {
const nodes = await client.capacity.filterNodes(options);
const nodeId = await pingNodes(client, nodes);
return nodeId;
}

const qsfsNode = await getNodeId(grid3, qsfsQueryOptions);
const masterNode = await getNodeId(grid3, options);
const workerNode = await getNodeId(grid3, { ...options, nodeExclude: [masterNode] });

//create qsfs object
const qsfs: QSFSZDBSModel = {
name: qsfs_name,
count: 8,
node_ids: qsfsNodes,
count,
node_ids: [qsfsNode],
password: "mypassword1",
disk_size: 1,
disk_size,
description: "my qsfs test",
metadata: "",
};
Expand All @@ -95,7 +89,7 @@ async function main() {
masters: [
{
name: "master",
node_id: +(await grid3.capacity.filterNodes(masterQueryOptions))[0].nodeId,
node_id: masterNode,
cpu: 1,
memory: 1024,
rootfs_size: 0,
Expand All @@ -121,7 +115,7 @@ async function main() {
workers: [
{
name: "worker",
node_id: +(await grid3.capacity.filterNodes(workerQueryOptions))[0].nodeId,
node_id: workerNode,
cpu: 1,
memory: 1024,
rootfs_size: 0,
Expand All @@ -147,8 +141,8 @@ async function main() {
await getDeployment(grid3, name);

// //Uncomment the line below to cancel the deployment
// await cancel(grid3, { name: k.name });
// await deleteQsfs(grid3, { name: qsfs_name });
// await cancel(grid3, k.name);
// await deleteQsfs(grid3, qsfs_name);

await grid3.disconnect();
}
Expand Down
44 changes: 31 additions & 13 deletions packages/grid_client/scripts/single_vm_mycelium.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { MachinesModel } from "../src";
import { generateRandomHexSeed, GridClient, MachinesDeleteModel, MachinesModel } from "../src";
import { config, getClient } from "./client_loader";
import { log } from "./utils";

async function deploy(client, vms) {
async function deploy(client: GridClient, vms: MachinesModel) {
const res = await client.machines.deploy(vms);
log("================= Deploying VM =================");
log(res);
log("================= Deploying VM =================");
}

async function getDeployment(client, vms) {
const res = await client.machines.getObj(vms);
async function getDeployment(client: GridClient, name: string) {
const res = await client.machines.getObj(name);
log("================= Getting deployment information =================");
log(res);
log("================= Getting deployment information =================");
}

async function cancel(client, vms) {
const res = await client.machines.delete(vms);
async function cancel(client: GridClient, options: MachinesDeleteModel) {
const res = await client.machines.delete(options);
log("================= Canceling the deployment =================");
log(res);
log("================= Canceling the deployment =================");
Expand All @@ -32,12 +32,18 @@ async function main() {
network: {
name: "hellotest",
ip_range: "10.249.0.0/16",
// myceliumSeeds: [
// {
// nodeId: 168,
// seed: "050d109829d8492d48bfb33b711056080571c69e46bfde6b4294c4c5bf468a76", //(HexSeed of length 32)
// },
// ],
myceliumSeeds: [
{
nodeId: 168,
/**
* ### Mycelium Network Seed:
* - The `seed` is an optional field used to provide a specific seed for the Mycelium network.
* - If not provided, the `GridClient` will generate a seed automatically when the `mycelium` flag is enabled.
* - **Use Case:** If you need the new machine to have the same IP address as a previously deleted machine, set the `seed` field to the old seed value.
*/
seed: generateRandomHexSeed(32),
},
],
},
machines: [
{
Expand All @@ -53,8 +59,20 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
/**
* ### Mycelium Flag Behavior:
* - When the `mycelium` flag is enabled, there’s no need to manually provide the `myceliumSeed` flag.
* - The `GridClient` will automatically generate the necessary seed for you.
* - **However**, if you have **an existing seed** from a previously deleted machine and wish to deploy a new machine that retains the same IP address,
* - **you can simply pass in the old seed during deployment instead of calling the `generateRandomHexSeed()` function**.
*/
mycelium: true,
// myceliumSeed: "1e1404279b3d", //(HexSeed of length 6)
/**
* ### Mycelium Seed:
* - The `myceliumSeed` is an optional field used to provide a specific seed for the Mycelium network.
* - If not provided, the `GridClient` will generate a seed automatically when the `mycelium` flag is enabled.
* - **Use Case:** If you need the new machine to have the same IP address as a previously deleted machine, set the `seed` field to the old seed value. */
myceliumSeed: generateRandomHexSeed(6), // (HexSeed of length 6)
cpu: 1,
memory: 1024 * 2,
rootfs_size: 0,
Expand Down
19 changes: 19 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class NetworkModel {
}

class MyceliumNetworkModel {
/**
* ### Mycelium Network Seed:
* - The `seed` is an optional field used to provide a specific seed for the Mycelium network.
* - If not provided, the `GridClient` will generate a seed automatically when the `mycelium` flag is enabled.
* - **Use Case:** If you need the new machine to have the same IP address as a previously deleted machine, set the `seed` field to the old seed value.
*/
@Expose() @IsString() @Length(32) seed?: string;
@Expose() @IsInt() @Min(1) nodeId: number;
}
Expand All @@ -110,7 +116,20 @@ class MachineModel {
@Expose() @IsBoolean() public_ip: boolean;
@Expose() @IsOptional() @IsBoolean() public_ip6?: boolean;
@Expose() @IsBoolean() planetary: boolean;
/**
* ### Mycelium Flag Behavior:
* - When the `mycelium` flag is enabled, there’s no need to manually provide the `myceliumSeed` flag.
* - The `GridClient` will automatically generate the necessary seed for you.
* - **However**, if you have **an existing seed** from a previously deleted machine and wish to deploy a new machine that retains the same IP address,
* - **you can simply pass in the old seed during deployment instead of calling the `generateRandomHexSeed()` function**.
*/
@Expose() @IsBoolean() mycelium: boolean;
/**
* ### Mycelium Seed:
* - The `myceliumSeed` is an optional field used to provide a specific seed for the Mycelium network.
* - If not provided, the `GridClient` will generate a seed automatically when the `mycelium` flag is enabled.
* - **Use Case:** If you need the new machine to have the same IP address as a previously deleted machine, set the `seed` field to the old seed value.
*/
@Expose() @IsOptional() @IsString() @Length(6) myceliumSeed?: string;
@Expose() @IsInt() @Min(1) cpu: number;
@Expose() @Min(256) memory: number; // in MB
Expand Down
Loading

0 comments on commit f452629

Please sign in to comment.