Skip to content

Commit

Permalink
fix: resolve bugs with dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhijna-Raghavendra authored Feb 16, 2024
2 parents 2dd2226 + e429625 commit 1515890
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 85 deletions.
2 changes: 1 addition & 1 deletion docker/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ COPY . ./

RUN deno cache src/backend/dependencies.ts

CMD ["run", "--allow-net", "--allow-env", "--allow-read","--allow-run","--allow-sys","src/backend/server.ts"]
CMD ["run", "--allow-net", "--allow-env", "--allow-read","--allow-run","--allow-sys","--allow-write","src/backend/server.ts"]
38 changes: 20 additions & 18 deletions src/backend/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ async function addScript(
port: string,
build_cmds: string,
) {
if (document.resource_type === "URL") {
await exec(
`bash -c "echo 'bash ../../src/backend/utils/automate.sh -u ${document.resource} ${document.subdomain}' > /hostpipe/pipe"`,
);
} else if (document.resource_type === "PORT") {
await exec(
`bash -c "echo 'bash ../../src/backend/utils/automate.sh -p ${document.resource} ${document.subdomain}' > /hostpipe/pipe"`,
);
} else if (document.resource_type === "GITHUB" && static_content == "Yes") {
await exec(
`bash -c "echo 'bash ../../src/backend/utils/container.sh -s ${document.subdomain} ${document.resource} ${env_content}' > /hostpipe/pipe"`,
);
} else if (document.resource_type === "GITHUB" && static_content == "No") {
const dockerfile = dockerize(stack, port, build_cmds);
await exec(
`bash -c "echo 'bash ../../src/backend/utils/container.sh -g ${document.subdomain} ${document.resource} ${env_content} ${dockerfile} ${port}' > /hostpipe/pipe"`,
);
}
if (document.resource_type === "URL") {
await exec(
`bash -c "echo 'bash ../../src/backend/utils/automate.sh -u ${document.resource} ${document.subdomain}' > /hostpipe/pipe"`,
);
} else if (document.resource_type === "PORT") {
await exec(
`bash -c "echo 'bash ../../src/backend/utils/automate.sh -p ${document.resource} ${document.subdomain}' > /hostpipe/pipe"`,
);
} else if (document.resource_type === "GITHUB" && static_content == "Yes") {
Deno.writeTextFile(`/hostpipe/.env`,env_content)
await exec(
`bash -c "echo 'bash ../../src/backend/utils/container.sh -s ${document.subdomain} ${document.resource}' > /hostpipe/pipe"`,
);
} else if (document.resource_type === "GITHUB" && static_content == "No") {
let dockerfile = dockerize(stack, port, build_cmds);
Deno.writeTextFile(`/hostpipe/Dockerfile`,dockerfile)
Deno.writeTextFile(`/hostpipe/.env`,env_content)
await exec(
`bash -c "echo 'bash ../../src/backend/utils/container.sh -g ${document.subdomain} ${document.resource} ${port}' > /hostpipe/pipe"`);
}
}

async function deleteScript(document: DfContentMap) {
Expand Down
8 changes: 4 additions & 4 deletions src/backend/utils/automate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ elif [ "$arg1" = "-p" ]; then
location / {
proxy_pass http://localhost:$arg2;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
}
charset utf-8;
client_max_body_size 20M;
Expand All @@ -71,7 +71,7 @@ elif [ "$arg1" = "-p" ]; then
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}" > /etc/nginx/sites-available/$arg3.conf;
ln -s /etc/nginx/sites-available/$arg3.conf /etc/nginx/sites-enabled/$arg3.conf;
sudo ln -s /etc/nginx/sites-available/$arg3.conf /etc/nginx/sites-enabled/$arg3.conf;
sudo systemctl reload nginx;

else
Expand Down
107 changes: 49 additions & 58 deletions src/backend/utils/container.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
#!/bin/bash
if ! command -v git >/dev/null 2>&1; then
echo "Git is not installed. Exiting."
exit 1
fi
if ! command -v pack >/dev/null 2>&1; then
echo "Buildpack cli not installed. Exiting."
exit 1
fi
if ! command -v docker >/dev/null 2>&1; then
echo "docker not installed. Exiting"
exit 1
fi
PORT_MIN=8010
PORT_MAX=8099
suffix=$(echo $RANDOM | md5sum | head -c 20)
host="mdgiitr"
http_upgrade="mdgiitr"
flag=$1
name=$2-$suffix
env_content=$3
resource=$4
dockerfile=$5
exp_port=$6
echo $flag
echo $name
echo $resource
echo $dockerfile
echo $exp_port
name=$2
resource=$3
exp_port=$4

available_ports=()

Expand All @@ -39,69 +16,83 @@ done
echo "Available ports: ${available_ports[56]}"
AVAILABLE=0
if [ $flag = "-g" ]; then
echo "hello"
echo "idhar";
echo 'hello'
git clone $resource $name
sudo cp Dockerfile $name/
sudo cp .env $name/
cd $name
touch .env
echo "$env_content" > .env
touch Dockerfile
echo "
$dockerfile
" > Dockerfile
sudo docker build -t $name .
echo $port
sudo docker run -d -p ${available_ports[$AVAILABLE]}:$exp_port $name
echo ${available_ports[$AVAILABLE]};
sudo docker run -d -p ${available_ports[$AVAILABLE]}:$exp_port $2
cd ..
rm -rf $name
sudo rm -rf $name
sudo rm Dockerfile
sudo rm .env

sudo touch /etc/nginx/sites-available/$2.conf
sudo chmod 666 /etc/nginx/sites-available/$2.conf
sudo echo "# Virtual Host configuration for example.com
server {
listen 80;
listen [::]:80;
server_name $2;
location / {
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name $2;
location / {
proxy_pass http://localhost:${available_ports[$AVAILABLE]};
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}" > /etc/nginx/sites-available/$2.conf;
sudo ln -s /etc/nginx/sites-available/$2.conf /etc/nginx/sites-enabled/$2.conf;
sudo systemctl reload nginx;

proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
}
charset utf-8;
client_max_body_size 20M;
ssl_certificate /etc/letsencrypt/live/df.mdgspace.org-0001/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/df.mdgspace.org-0001/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}" > /etc/nginx/sites-available/$2.conf
sudo ln -s /etc/nginx/sites-available/$2.conf /etc/nginx/sites-enabled/$2.conf
sudo systemctl reload nginx

else
git clone $resource $name
sudo cp .env $name/
cd $name
touch .env
echo "$env_content" > .env
touch Dockerfile
echo "
sudo echo "
FROM nginx:alpine
COPY . /usr/share/nginx/html
" > Dockerfile
sudo docker build -t $name .
sudo docker run -d -p ${available_ports[$AVAILABLE]}:80 $name
sudo rm Dockerfile
cd ..
rm -rf $name
sudo rm .env
sudo rm -rf $name
sudo touch /etc/nginx/sites-available/$2.conf
sudo chmod 666 /etc/nginx/sites-available/$2.conf
sudo echo "# Virtual Host configuration for example.com
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name $2;
location / {
proxy_pass http://localhost:${available_ports[$AVAILABLE]};
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
}
charset utf-8;
client_max_body_size 20M;
ssl_certificate /etc/letsencrypt/live/df.mdgspace.org-0001/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/df.mdgspace.org-0001/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}" > /etc/nginx/sites-available/$2.conf;
sudo ln -s /etc/nginx/sites-available/$2.conf /etc/nginx/sites-enabled/$2.conf;
sudo systemctl reload nginx;
Expand Down
10 changes: 6 additions & 4 deletions src/backend/utils/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ export default function dockerize(
}).join("\n");
if (stack == "Python") {
dockerfile =
"FROM python:3.11 \n WORKDIR /app \n COPY requirements.txt . \n RUN pip install --no-cache-dir -r requirements.txt \n COPY . ." +
build_cmds_mapped + `\n EXPOSE ${port}\n` + execute_cmd;
"FROM python:3.11 \nWORKDIR /app \nCOPY requirements.txt . \nRUN pip install --no-cache-dir -r requirements.txt \nCOPY . ." +
build_cmds_mapped + `\nEXPOSE ${port}\n` + execute_cmd;
} else if (stack == "NodeJS") {
dockerfile =
"FROM node:latest \n WORKDIR /app \n COPY ./package*.json . \n RUN npm install \n" +
"FROM node:latest \n WORKDIR /app \n COPY ./package*.json . \n RUN npm install \n COPY . ." +
build_cmds_mapped + `\n EXPOSE ${port} \n` + execute_cmd;
console.log(port)
}
return dockerfile;
// dockerfile = btoa(dockerfile);
return dockerfile.toString();
}

// bash ./container.sh -g hack "https://github.com/angelmittal03/mdg-text.git" 'FROM node:latest
Expand Down

0 comments on commit 1515890

Please sign in to comment.