Skip to content

Commit c5c0192

Browse files
feat:took care of merge conflicts
1 parent 6bba2e3 commit c5c0192

File tree

9 files changed

+75
-9
lines changed

9 files changed

+75
-9
lines changed

docker/named_pipe/listen.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
#!/bin/sh
2-
3-
while true; do eval "$(cat pipe)"; done
2+
while true; do
3+
raw=$(cat pipe)
4+
5+
if echo "$raw" | grep -q "^RESPOND::"; then
6+
cmd="${raw##RESPOND::}"
7+
eval "$cmd" > output_pipe 2>&1
8+
else
9+
eval "$raw" &
10+
fi
11+
done

docs/admin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ docker compose up --build -d
4747
### 4. Setup Named Pipes
4848

4949
Create a pipe in the `docker/named_pipe` directory by executing `mkfifo docker/named_pipe/pipe`.
50+
Create another one by executing `mkfifo docker/named_pipe/output_pipe`.
5051
Navigate to the `docker/named_pipe` directory and execute the `listen.sh` script to allow the application to run commands on the host.
5152
```bash
5253
cd docker/named_pipe

src/backend/main.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { addScript, deleteScript } from "./scripts.ts";
33
import { checkJWT } from "./utils/jwt.ts";
44
import { addMaps, deleteMaps, getMaps, getDeploymentsByRepo, getUserToken } from "./db.ts";
55
import { encryptEnv, decryptEnv } from "./utils/crypto.ts";
6-
6+
import { canAllocateStorage } from "./utils/container-storage.ts";
77
// ... skipping to githubWebhook
88

99

@@ -57,6 +57,19 @@ async function addSubdomain(ctx: Context) {
5757
}
5858

5959
// We keep deployment config (port, stack, etc.) in the document to store them in DB for webhook usage
60+
if(copy.volume_needed=="Yes"){const storageCheck=await canAllocateStorage(100);
61+
if(!storageCheck.can_allocate){
62+
ctx.response.status=400;
63+
ctx.response.body = {
64+
status: "failed",
65+
error: "INSUFFICIENT_STORAGE",
66+
message: storageCheck.reason || "Not enough disk space",
67+
available_mb: storageCheck.available_mb,
68+
requested_mb: storageCheck.requested_mb,
69+
};
70+
console.log(storageCheck.available_mb);
71+
return;
72+
}}
6073
const success: boolean = await addMaps(document);
6174

6275

@@ -146,7 +159,7 @@ async function addSubdomain(ctx: Context) {
146159
ctx.response.body = { "status": "failed" };
147160
}
148161
}
149-
//!add volume removal logic on deleting the subdomain
162+
150163
async function deleteSubdomain(ctx: Context) {
151164
if (!ctx.request.hasBody) {
152165
ctx.throw(415);

src/backend/scripts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function safeExec(command: string): Promise<void> {
2626
throw error;
2727
}
2828
}
29-
//! what if it is url or port
29+
3030
async function addScript(
3131
document: DfContentMap,
3232
env_content: string,
@@ -43,7 +43,7 @@ async function addScript(
4343
const memLimit = shellEscape(MEMORY_LIMIT || "512m", "MEMORY_LIMIT");
4444
volume_needed=(volume_needed=="Yes").toString();
4545
const volumeNeeded=shellEscape(volume_needed,"false");
46-
46+
console.log(`volume neeeded is ${volumeNeeded}`);
4747
if (document.resource_type === "URL") {
4848
await safeExec(
4949
`bash -c "echo 'bash ../../src/backend/shell_scripts/automate.sh -u ${resource} ${subdomain}' > /hostpipe/pipe"`,

src/backend/shell_scripts/container.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ STORAGE_ROOT="/mnt/storage"
1212
PROJECT_STORAGE="$STORAGE_ROOT/$name"
1313
PROJECT_IMG="$STORAGE_ROOT/$name.img"
1414
SIZE_MB=100
15+
sudo mkdir -p $STORAGE_ROOT
16+
sudo chmod 755 $STORAGE_ROOT
1517
for ((port=PORT_MIN; port<=PORT_MAX; port++)); do
1618
if ! ss -ln src :$port | grep -q "\<$port\>"; then
1719
available_ports+=($port)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {exec } from "../dependencies.ts";
2+
export async function canAllocateStorage(requestedMb: number) {
3+
const STORAGE_PATH = "/mnt/storage";
4+
const SAFETY_BUFFER_MB = 200; // keep buffer for system + docker
5+
6+
try {
7+
const responseProcess = new Deno.Command("sh", {
8+
args: ["-c", "cat /hostpipe/output_pipe"],
9+
}).output(); // don't await yet, just start it
10+
await exec(`bash -c "echo 'RESPOND::df ${STORAGE_PATH} --output=avail' > /hostpipe/pipe"`);
11+
const response=await responseProcess;
12+
const output = new TextDecoder().decode(response.stdout).trim().split("\n");
13+
const availableKb = parseInt(output[1].trim());
14+
if (isNaN(availableKb)) {
15+
throw new Error(`Unexpected df output: ${output}`);
16+
}
17+
const availableMb = Math.floor(availableKb / 1024);
18+
const usableMb = availableMb - SAFETY_BUFFER_MB;
19+
const canAllocate = usableMb >= requestedMb;
20+
console.log(`can allocate ${canAllocate} memory`);
21+
console.log(`Available memory is ${availableMb} requested is ${requestedMb}`);
22+
return {
23+
can_allocate: canAllocate,
24+
available_mb: usableMb,
25+
requested_mb: requestedMb,
26+
reason: canAllocate ? null : "Not enough disk space",
27+
};
28+
} catch (err) {
29+
console.log(`Error during memory check volume`);
30+
return {
31+
can_allocate: false,
32+
available_mb: 0,
33+
requested_mb: requestedMb,
34+
reason: "Failed to check disk space",
35+
};
36+
}
37+
}

src/cli/features/createDomain.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export async function createDomain(userApiKey: string, user: string, provider: s
8585
if (response.data.status === 'success') {
8686
console.log(`✅ Domain '${subdomain}.${domain}' created successfully!`);
8787
} else {
88+
if(response.error=="INSUFFICIENT_STORAGE")console.log("INSUFFICIENT_STORAGE")
8889
console.log('❌ Domain creation failed!');
8990
console.log("Either the domain exist or the domain is not created");
9091
}

src/frontend/src/components/modal.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export default {
9191
this.closeModalAndReload();
9292
} else {
9393
this.closeModal();
94-
alert('Failed to create subdomain');
94+
if(res=="insufficient_storage")alert("Insufficient storage to mount a volume");
95+
else alert('Failed to create subdomain');
9596
setTimeout(() => {
9697
window.location.reload();
9798
}, 1000);

src/frontend/src/utils/create.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ export async function create(
108108
body: JSON.stringify(body),
109109
});
110110
const data = await resp.json();
111-
if (data.status === "failed") {
112-
return "Failed";
111+
if(data.error=="INSUFFICIENT_STORAGE"){
112+
return "insufficient_storage";
113+
}
114+
else if (data.status === "failed") {
115+
return "failed";
113116
}
114117
return "Submitted";
115118
}

0 commit comments

Comments
 (0)