Skip to content

Commit f0dc92b

Browse files
feat: added initial support for volumes
1 parent 437c1d8 commit f0dc92b

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

src/backend/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async function addSubdomain(ctx: Context) {
3939
delete document.stack;
4040
delete document.env_content;
4141
delete document.static_content;
42+
delete document.volume_needed;
4243

4344
if (document.author != await checkJWT(provider, token)) {
4445
ctx.throw(401);
@@ -52,6 +53,7 @@ async function addSubdomain(ctx: Context) {
5253
copy.env_content,
5354
copy.static_content,
5455
copy.dockerfile_present,
56+
copy.volume_needed,
5557
copy.stack,
5658
copy.port,
5759
copy.build_cmds,
@@ -65,7 +67,7 @@ async function addSubdomain(ctx: Context) {
6567
ctx.response.body = { "status": "failed" };
6668
}
6769
}
68-
70+
//!add volume removal logic on deleting the subdomain
6971
async function deleteSubdomain(ctx: Context) {
7072
if (!ctx.request.hasBody) {
7173
ctx.throw(415);

src/backend/scripts.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ async function safeExec(command: string): Promise<void> {
2626
throw error;
2727
}
2828
}
29-
29+
//! what if it is url or port
3030
async function addScript(
3131
document: DfContentMap,
3232
env_content: string,
3333
static_content: string,
3434
dockerfile_present: string,
35+
volume_needed:string,
3536
stack: string,
3637
port: string,
3738
build_cmds: string,
@@ -40,7 +41,9 @@ async function addScript(
4041
const resource = shellEscape(document.resource, "resource");
4142
const safePort = shellEscape(port, "port");
4243
const memLimit = shellEscape(MEMORY_LIMIT || "512m", "MEMORY_LIMIT");
43-
44+
volume_needed=(volume_needed=="Yes").toString();
45+
const volumeNeeded=shellEscape(volume_needed,"false");
46+
4447
if (document.resource_type === "URL") {
4548
await safeExec(
4649
`bash -c "echo 'bash ../../src/backend/shell_scripts/automate.sh -u ${resource} ${subdomain}' > /hostpipe/pipe"`,
@@ -52,19 +55,19 @@ async function addScript(
5255
} else if (document.resource_type === "GITHUB" && static_content == "Yes") {
5356
await Deno.writeTextFile(`/hostpipe/.env`, env_content);
5457
await safeExec(
55-
`bash -c "echo 'bash ../../src/backend/shell_scripts/container.sh -s ${subdomain} ${resource} 80 ${memLimit}' > /hostpipe/pipe"`,
58+
`bash -c "echo 'bash ../../src/backend/shell_scripts/container.sh -s ${subdomain} ${resource} 80 ${memLimit} ${volumeNeeded}' > /hostpipe/pipe"`,
5659
);
5760
} else if (document.resource_type === "GITHUB" && static_content == "No") {
5861
if (dockerfile_present === 'No') {
5962
await Deno.writeTextFile(`/hostpipe/Dockerfile`, dockerize(stack, safePort, build_cmds));
6063
await Deno.writeTextFile(`/hostpipe/.dockerignore`, dockerignore(stack));
6164
await Deno.writeTextFile(`/hostpipe/.env`, env_content);
6265
await safeExec(
63-
`bash -c "echo 'bash ../../src/backend/shell_scripts/container.sh -g ${subdomain} ${resource} ${safePort} ${memLimit}' > /hostpipe/pipe"`,
66+
`bash -c "echo 'bash ../../src/backend/shell_scripts/container.sh -g ${subdomain} ${resource} ${safePort} ${memLimit} ${volumeNeeded}' > /hostpipe/pipe"`,
6467
);
6568
} else if (dockerfile_present === 'Yes') {
6669
await safeExec(
67-
`bash -c "echo 'bash ../../src/backend/shell_scripts/container.sh -d ${subdomain} ${resource} ${safePort} ${memLimit}' > /hostpipe/pipe"`,
70+
`bash -c "echo 'bash ../../src/backend/shell_scripts/container.sh -d ${subdomain} ${resource} ${safePort} ${memLimit} ${volumeNeeded}' > /hostpipe/pipe"`,
6871
);
6972
}
7073
}

src/backend/shell_scripts/container.sh

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ name=$2
55
resource=$3
66
exp_port=$4
77
max_mem=$5
8+
enable_voume=$6
89

910
available_ports=()
10-
11+
STORAGE_ROOT="/mnt/storage"
12+
PROJECT_STORAGE="$STORAGE_ROOT/$name"
1113
for ((port=PORT_MIN; port<=PORT_MAX; port++)); do
1214
if ! ss -ln src :$port | grep -q "\<$port\>"; then
1315
available_ports+=($port)
@@ -30,9 +32,29 @@ elif [ $flag = "-s" ]; then
3032
COPY . /usr/share/nginx/html
3133
" > Dockerfile
3234
fi
33-
35+
if [ "$enable_volume" = "true" ]; then
36+
echo "Creating persistent storage at $PROJECT_STORAGE"
37+
sudo mkdir -p $PROJECT_STORAGE
38+
sudo chmod 777 $PROJECT_STORAGE # tighten later
39+
fi
3440
sudo docker build -t $name .
35-
sudo docker run --memory=$max_mem --name=$name -d -p ${available_ports[$AVAILABLE]}:$exp_port $name
41+
if [ "$enable_volume" = "true" ]; then
42+
sudo docker run \
43+
--memory=$max_mem \
44+
--name=$name \
45+
-d \
46+
-p ${available_ports[$AVAILABLE]}:$exp_port \
47+
-v $PROJECT_STORAGE:/app/data \
48+
-e DATA_DIR=/app/data \
49+
$name
50+
else
51+
sudo docker run \
52+
--memory=$max_mem \
53+
--name=$name \
54+
-d \
55+
-p ${available_ports[$AVAILABLE]}:$exp_port \
56+
$name
57+
fi
3658
cd ..
3759
sudo rm -rf $name
3860
sudo rm Dockerfile

src/frontend/src/components/modal.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ const domain = import.meta.env.VITE_APP_DOMAIN
3535
<option v-for="option in stacks" :key="option">{{ option }}</option>
3636
</select>
3737
</div>
38+
<div class="volume-needed">
39+
<label for="volume">Do you need persistent storage (Volume)?</label><br>
40+
<input name="radio" type="radio" value="Yes" v-model="volume_needed"> Yes
41+
<input name="radio" type="radio" value="No" v-model="volume_needed"> No
42+
</div>
3843
<p>Port:<br><input class="input-field" v-model="port" /></p>
3944
<div v-if="dockerfile_present === 'No'" class="dockerfile-section">
4045
<p>Build Commands:<br><textarea class="textarea-field" cols="50" rows="10" v-model="build_cmds"></textarea></p>
@@ -61,6 +66,7 @@ export default {
6166
env_content: 'key1 = value1', // Default prompt text
6267
static_content: 'No',
6368
dockerfile_present :'No',
69+
volume_needed: 'No',
6470
port: '',
6571
stack: '',
6672
build_cmds: '',
@@ -71,7 +77,7 @@ export default {
7177
methods: {
7278
submitForm() {
7379
console.log(this.subdomain, this.resource_type, this.resource);
74-
create(this.subdomain, this.resource_type, this.resource, this.env_content, this.static_content,this.dockerfile_present,this.port, this.stack, this.build_cmds)
80+
create(this.subdomain, this.resource_type, this.resource, this.env_content, this.static_content,this.dockerfile_present, this.volume_needed, this.port, this.stack, this.build_cmds)
7581
.then((res) => {
7682
console.log(res);
7783
if (res === 'Submitted') {

src/frontend/src/utils/create.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export async function create(
6161
env_content: string,
6262
static_content: string,
6363
dockerfile_present:string,
64+
volume_needed:string,
6465
port: string,
6566
stack: string,
6667
build_cmds: string,
@@ -88,6 +89,7 @@ export async function create(
8889
"env_content": env_content,
8990
"static_content": static_content,
9091
"dockerfile_present":dockerfile_present,
92+
"volume_needed":volume_needed,
9193
"port": port,
9294
"build_cmds": build_cmds,
9395
"stack": stack,

0 commit comments

Comments
 (0)