Skip to content

Commit

Permalink
Update deployment scripts for Gitea, Jenkins, and Nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledyoussef24 committed Dec 12, 2024
1 parent 4229c06 commit e8f0006
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 221 deletions.
153 changes: 39 additions & 114 deletions packages/grid_client/tests/modules/applications/gittea.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { setTimeout } from "timers/promises";

import { FilterOptions, GatewayNameModel, generateString, GridClient, MachinesModel, randomChoice } from "../../../src";
import { config, getClient } from "../../client_loader";
import { GBToBytes, generateInt, getOnlineNode, log, RemoteRun, splitIP } from "../../utils"; // Corrected path
import { GBToBytes, generateInt, getOnlineNode, log, RemoteRun, splitIP } from "../../utils";

jest.setTimeout(1250000);

let gridClient: GridClient;
let deploymentName: string;
let result: any;

beforeAll(async () => {
gridClient = await getClient();
Expand All @@ -22,53 +22,31 @@ beforeAll(async () => {
const ipRegex = /(^127\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/;

test("TCXXXX - Applications: Deploy Gitea", async () => {
/**********************************************
Test Suite: Grid3_Client_TS (Automated)
Test Cases: TC2689 - Applications: Deploy Mattermost
Scenario:
- Generate Test Data/mattermost Config/Gateway Config.
- Select a Node To Deploy the mattermost on.
- Select a Gateway Node To Deploy the gateway on.
- Deploy the mattermost solution.
- Assert that the generated data matches
the deployment details.
- Pass the IP of the Created mattermost to the Gateway
Config.
- Deploy the Gateway.
- Assert that the generated data matches
the deployment details.
- Assert that the Gateway points at the IP
of the created mattermost.
- Assert that the returned domain is working
and returns correct data.
**********************************************/

//Test Data
const name = "gw" + generateString(10).toLowerCase();
const tlsPassthrough = false;
const cpu = 2;
const memory = 4;
const cpu = 1;
const memory = 2;
const rootfsSize = 2;
const diskSize = 50;
const diskSize = 15;
const networkName = generateString(15);
const vmName = generateString(15);
const diskName = generateString(15);
const mountPoint = "/mnt/data";
const mountPoint = "/var/lib/docker";
const publicIp = false;
const ipRangeClassA = "10." + generateInt(1, 255) + ".0.0/16";
const ipRangeClassB = "172." + generateInt(16, 31) + ".0.0/16";
const ipRangeClassC = "192.168.0.0/16";
const ipRange = randomChoice([ipRangeClassA, ipRangeClassB, ipRangeClassC]);
const metadata = "{'deploymentType': 'gitea'}";
const description = "Test deploying Gitea via ts grid3 client";
const description = "test deploying Gitea via ts grid3 client";

// GatewayNode Selection
// Gateway Node Selection
const gatewayNodes = await gridClient.capacity.filterNodes({
gateway: true,
farmId: 1,
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
if (gatewayNodes.length == 0) throw new Error("No nodes available to complete this test");
if (gatewayNodes.length === 0) throw new Error("No nodes available to complete this test");
const GatewayNode = gatewayNodes[generateInt(0, gatewayNodes.length - 1)];

// Node Selection
Expand All @@ -80,9 +58,10 @@ test("TCXXXX - Applications: Deploy Gitea", async () => {
availableFor: await gridClient.twins.get_my_twin_id(),
} as FilterOptions);
const nodeId = await getOnlineNode(nodes);
if (nodeId == -1) throw new Error("No nodes available to complete this test");
if (nodeId === -1) throw new Error("No nodes available to complete this test");
const domain = name + "." + GatewayNode.publicConfig.domain;

const fs = require("fs");
// VM Model
const vms: MachinesModel = {
name: deploymentName,
Expand Down Expand Up @@ -110,14 +89,17 @@ test("TCXXXX - Applications: Deploy Gitea", async () => {
planetary: true,
mycelium: true,
env: {
SSH_KEY: config.ssh_key,
SSH_KEY: fs.readFileSync("/Users/khaledyoussef/.ssh/id_ed25519.pub", "utf8"), // Add the public key here
GITEA__HOSTNAME: domain,
GITEA__ROOT_EMAIL: "[email protected]",
GITEA__ROOT_PASSWORD: "adminpassword",
},
},
],
metadata: metadata,
description: description,
};

const res = await gridClient.machines.deploy(vms);
log(res);

Expand All @@ -126,54 +108,14 @@ test("TCXXXX - Applications: Deploy Gitea", async () => {
expect(res.contracts.updated).toHaveLength(0);
expect(res.contracts.deleted).toHaveLength(0);

const vmsList = await gridClient.machines.list();
log(vmsList);

// VM List Assertions
expect(vmsList.length).toBeGreaterThanOrEqual(1);
expect(vmsList).toContain(vms.name);

const result = await gridClient.machines.getObj(vms.name);
log(result);

// VM Assertions
expect(result[0].nodeId).toBe(nodeId);
expect(result[0].status).toBe("ok");
expect(result[0].flist).toBe(vms.machines[0].flist);
expect(result[0].entrypoint).toBe(vms.machines[0].entrypoint);
expect(result[0].mounts).toHaveLength(1);
expect(result[0].interfaces[0]["network"]).toBe(vms.network.name);
expect(result[0].interfaces[0]["ip"]).toContain(splitIP(vms.network.ip_range));
expect(result[0].interfaces[0]["ip"]).toMatch(ipRegex);
expect(result[0].capacity["cpu"]).toBe(cpu);
expect(result[0].capacity["memory"]).toBe(memory * 1024);
expect(result[0].planetary).toBeDefined();
expect(result[0].myceliumIP).toBeDefined();
expect(result[0].publicIP).toBeNull();
expect(result[0].description).toBe(description);
expect(result[0].mounts[0]["name"]).toBe(diskName);
expect(result[0].mounts[0]["size"]).toBe(GBToBytes(diskSize));
expect(result[0].mounts[0]["mountPoint"]).toBe(mountPoint);
expect(result[0].mounts[0]["state"]).toBe("ok");

const host = result[0].planetary;
const user = "root";

//SSH to the Created VM
const ssh = await RemoteRun(host, user);

try {
//Verify that the added env var was successfully passed to the VM.
await ssh.execCommand("ufw allow 3000/tcp").then(async function (result) {});
} finally {
//Disconnect from the machine
await ssh.dispose();
}

const backends = ["http://[" + result[0].myceliumIP + "]:3000"];
// Gateway Backend Configuration
const backends = ["http://[" + result[0].planetary + "]:3000"];
log(backends);

// Name Gateway Model
// Gateway Model
const gw: GatewayNameModel = {
name: name,
node_id: GatewayNode.nodeId,
Expand All @@ -184,49 +126,52 @@ test("TCXXXX - Applications: Deploy Gitea", async () => {
const gatewayRes = await gridClient.gateway.deploy_name(gw);
log(gatewayRes);

// Contracts Assertions
// Gateway Assertions
expect(gatewayRes.contracts.created).toHaveLength(1);
expect(gatewayRes.contracts.updated).toHaveLength(0);
expect(gatewayRes.contracts.deleted).toHaveLength(0);
expect(gatewayRes.contracts.created[0].contractType.nodeContract.nodeId).toBe(GatewayNode.nodeId);

const gatewayResult = await gridClient.gateway.getObj(gw.name);
log(gatewayResult);

// Gateway Assertions
expect(gatewayResult[0].name).toBe(name);
expect(gatewayResult[0].status).toBe("ok");
expect(gatewayResult[0].type).toContain("name");
expect(gatewayResult[0].domain).toContain(name);
expect(gatewayResult[0].tls_passthrough).toBe(tlsPassthrough);
expect(gatewayResult[0].backends).toStrictEqual(backends);

const site = "http://" + gatewayResult[0].domain;
const site = "https://" + gatewayResult[0].domain;
let reachable = false;

const host = result[0].planetary;
const user = "root";

//SSH to the Created VM
const ssh = await RemoteRun(host, user);

try {
//Verify that the added env var was successfully passed to the VM.
await ssh.execCommand("ufw allow 3000/tcp").then(async function (result) {
log(result.stdout);
});
} finally {
//Disconnect from the machine
await ssh.dispose();
}
for (let i = 0; i <= 250; i++) {
const wait = await setTimeout(5000, "Waiting for gateway to be ready");
log(wait);

await axios
.get(site)
.then(res => {
log("gateway is reachable");
log("Gateway is reachable");
log(res.status);
log(res.statusText);
log(res.data);
expect(res.status).toBe(200);
expect(res.statusText).toBe("OK");
reachable = true;
})
.catch(() => {
log("gateway is not reachable");
log("Gateway is not reachable");
});
if (reachable) {
break;
} else if (i === 250) {
throw new Error("Gateway is unreachable after multiple retries");
}
if (reachable) break;
if (i === 250) throw new Error("Gateway is unreachable after retries");
}
});

Expand All @@ -235,32 +180,12 @@ afterAll(async () => {
for (const name of vmNames) {
const res = await gridClient.machines.delete({ name });
log(res);
expect(res.created).toHaveLength(0);
expect(res.updated).toHaveLength(0);
expect(res.deleted).toBeDefined();
}

// const host = result[0].myceliumIP;
// const user = "root";

// //SSH to the Created VM
// const ssh = await RemoteRun(host, user);

// try {
// //Verify that the added env var was successfully passed to the VM.
// await ssh.execCommand("ufw allow 3000/tcp").then(async function (result) {});
// } finally {
// //Disconnect from the machine
// await ssh.dispose();
// }

const gwNames = await gridClient.gateway.list();
for (const name of gwNames) {
const res = await gridClient.gateway.delete_name({ name });
log(res);
expect(res.created).toHaveLength(0);
expect(res.updated).toHaveLength(0);
expect(res.deleted).toBeDefined();
}

return await gridClient.disconnect();
Expand Down
Loading

0 comments on commit e8f0006

Please sign in to comment.