Skip to content

Commit 145027f

Browse files
bfix:minor fixes
1 parent b68050f commit 145027f

File tree

9 files changed

+75
-8
lines changed

9 files changed

+75
-8
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Context, Sentry } from "./dependencies.ts";
22
import { addScript, deleteScript } from "./scripts.ts";
33
import { checkJWT } from "./utils/jwt.ts";
4+
import { canAllocateStorage } from "./utils/container-storage.ts";
45
import { addMaps, deleteMaps, getMaps } from "./db.ts";
56

67
const ADMIN_LIST = Deno.env.get("ADMIN_LIST")?.split("|");
@@ -44,6 +45,19 @@ async function addSubdomain(ctx: Context) {
4445
if (document.author != await checkJWT(provider, token)) {
4546
ctx.throw(401);
4647
}
48+
if(copy.volume_needed=="Yes"){const storageCheck=await canAllocateStorage(100);
49+
if(!storageCheck.can_allocate){
50+
ctx.response.status=400;
51+
ctx.response.body = {
52+
status: "failed",
53+
error: "INSUFFICIENT_STORAGE",
54+
message: storageCheck.reason || "Not enough disk space",
55+
available_mb: storageCheck.available_mb,
56+
requested_mb: storageCheck.requested_mb,
57+
};
58+
console.log(storageCheck.available_mb);
59+
return;
60+
}}
4761
const success: boolean = await addMaps(document);
4862
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
4963

@@ -67,7 +81,7 @@ async function addSubdomain(ctx: Context) {
6781
ctx.response.body = { "status": "failed" };
6882
}
6983
}
70-
//!add volume removal logic on deleting the subdomain
84+
7185
async function deleteSubdomain(ctx: Context) {
7286
if (!ctx.request.hasBody) {
7387
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
@@ -84,7 +84,8 @@ export default {
8484
this.closeModalAndReload();
8585
} else {
8686
this.closeModal();
87-
alert('Failed to create subdomain');
87+
if(res=="insufficient_storage")alert("Insufficient storage to mount a volume");
88+
else alert('Failed to create subdomain');
8889
setTimeout(() => {
8990
window.location.reload();
9091
}, 1000);

src/frontend/src/utils/create.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ export async function create(
106106
body: JSON.stringify(body),
107107
});
108108
const data = await resp.json();
109-
if (data.status === "failed") {
110-
return "Failed";
109+
if(data.error=="INSUFFICIENT_STORAGE"){
110+
return "insufficient_storage";
111+
}
112+
else if (data.status === "failed") {
113+
return "failed";
111114
}
112115
return "Submitted";
113116
}

0 commit comments

Comments
 (0)