Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 56 additions & 56 deletions day03/deploy_django_app.sh
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
#!/bin/bash

# Deploy a Django app and handle errors

# Function to clone the Django app code
code_clone() {
echo "Cloning the Django app..."
if [ -d "django-notes-app" ]; then
echo "The code directory already exists. Skipping clone."
else
git clone https://github.com/LondheShubham153/django-notes-app.git || {
echo "Failed to clone the code."
return 1
}
fi

<< task
deploy django app
and handle errors
task
Comment on lines +4 to +7
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Heredoc used without a command triggers Shellcheck warning SC2188.

The << task heredoc is used as a comment but has no command to consume it. Either prefix with : << (no-op) or use standard # comments.

Proposed fix
-<< task
-deploy django app
-and handle errors
-task
+# Task:
+# Deploy django app and handle errors

Or use a no-op heredoc:

-<< task
+: << 'task'
 deploy django app
 and handle errors
 task
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<< task
deploy django app
and handle errors
task
# Task:
# Deploy django app and handle errors
🧰 Tools
🪛 Shellcheck (0.11.0)

[warning] 4-4: This redirection doesn't have a command. Move to its command (or use 'true' as no-op).

(SC2188)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@day03/deploy_django_app.sh` around lines 4 - 7, The heredoc opener "<<" task
is being used as a comment and triggers Shellcheck SC2188; replace it with a
no-op heredoc by prefixing with a colon (change "<< task" to ": << task") or
simply convert the block to normal line comments (prepend "#" to each line) so
the heredoc has a consuming command and the Shellcheck warning is resolved.



#Function to clone the django app
code_clone(){

echo "Cloning the Django app..."
git clone https://github.com/LondheShubham153/django-notes-app.git
}
Comment on lines +11 to 15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Fragile error handling: clone failure assumed to mean directory exists.

git clone can fail for multiple reasons (network error, authentication, disk space), not just because the directory exists. The current logic silently continues on any failure, which could leave the script in an undefined state.

Consider checking explicitly:

Proposed fix
 code_clone(){
-
         echo "Cloning the Django app..."
-        git clone https://github.com/LondheShubham153/django-notes-app.git
+        if [ -d "django-notes-app" ]; then
+                echo "Directory already exists, skipping clone..."
+        else
+                git clone https://github.com/LondheShubham153/django-notes-app.git || { echo "Git clone failed"; return 1; }
+        fi
 }

Then simplify the call site:

-if ! code_clone;
-then
-        echo "directory already exists, skipping cloning...."
-fi
+if ! code_clone; then
+        echo "Failed to clone repository"
+        exit 1
+fi

Also applies to: 46-49

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@day03/deploy_django_app.sh` around lines 11 - 15, The code_clone function
assumes any git clone failure means the repo already exists and continues
silently; change it to first check for the target directory (e.g., test -d
django-notes-app) and skip cloning if present, otherwise run git clone and
immediately check its exit status ($?) and on failure print a descriptive error
with the exit code and exit the script non‑zero; apply the same pattern to the
other clone usage noted around the second clone call (lines 46-49) so both
locations explicitly validate directory existence first and handle git clone
failures instead of continuing silently.


# Function to install required dependencies
install_requirements() {
echo "Installing dependencies..."
sudo apt-get update && sudo apt-get install -y docker.io nginx docker-compose || {
echo "Failed to install dependencies."
return 1
}
#Function to install required dependencies
install_req() {

echo " Installing dependencies..."
sudo apt-get install docker.io nginx -y docker-compose
}
Comment on lines +18 to 22
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Missing apt-get update before package installation.

The function omits apt-get update, which could cause installation failures or install stale package versions on systems without a fresh apt cache. The codebase pattern in batch-10-scripts/install_packages.sh pairs update with install.

Proposed fix
 install_req() {
-
         echo " Installing dependencies..."
-        sudo apt-get install docker.io nginx -y docker-compose
+        sudo apt-get update && sudo apt-get install -y docker.io nginx docker-compose
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
install_req() {
echo " Installing dependencies..."
sudo apt-get install docker.io nginx -y docker-compose
}
install_req() {
echo " Installing dependencies..."
sudo apt-get update && sudo apt-get install -y docker.io nginx docker-compose
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@day03/deploy_django_app.sh` around lines 18 - 22, The install_req function is
missing an apt-get update before installing packages; update the apt cache first
and then install packages with proper flags and ordering: inside install_req
call sudo apt-get update && sudo apt-get install -y docker.io nginx
docker-compose (or split update and install into two commands), keeping the
function name install_req and ensuring the -y flag and package list are passed
to apt-get install rather than appended after the command.


# Function to perform required restarts
required_restarts() {
echo "Performing required restarts..."
sudo chown "$USER" /var/run/docker.sock || {
echo "Failed to change ownership of docker.sock."
return 1
}

# Uncomment the following lines if needed:
# sudo systemctl enable docker
# sudo systemctl enable nginx
# sudo systemctl restart docker
#Function to perform required restarts
req_restarts() {

echo "Performing required restarts"

sudo chown $USER /var/run/docker.sock
#sudo systemctl enable docker
#sudo systemctl enable nginx
#sudo systemctl restart docker
}

# Function to deploy the Django app
deploy() {
echo "Building and deploying the Django app..."
docker build -t notes-app . && docker-compose up -d || {
echo "Failed to build and deploy the app."
return 1
}
#Fucntion to deploy Django app
deploy(){
echo "Building and Deploying Django app..." #Fixed deployment : Old networks from previous runs had different settings, causing the new deployment to crash.
#Added docker-compose down to the start of the deploy function to wipe the slate clean.
docker-compose down
docker build -t notes-app .
docker-compose up -d
}

# Main deployment script
echo "********** DEPLOYMENT STARTED *********"
echo "***************** DEPLOYMENT STARTED ******************"

# Clone the code
if ! code_clone; then
cd django-notes-app || exit 1
if ! code_clone;
then
echo "directory already exists, skipping cloning...."
fi

# Install dependencies
if ! install_requirements; then
exit 1
#Moved cd outside the logic so it always enters the app folder before building.
cd django-notes-app || { echo "Failed to enter directory" ; exit 1; }


if ! install_req;
then
echo "Failed to install requirements"
exit 1
fi

# Perform required restarts
if ! required_restarts; then
exit 1

if ! req_restarts;
then
echo "System fault identitfied"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Typo: "identitfied" should be "identified".

Proposed fix
-        echo "System fault identitfied"
+        echo "System fault identified"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
echo "System fault identitfied"
echo "System fault identified"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@day03/deploy_django_app.sh` at line 64, Fix the typo in the echo string on
the failing line: change the message passed to echo in deploy_django_app.sh from
"System fault identitfied" to "System fault identified" (update the echo
statement to use the corrected spelling).

exit 1
fi

# Deploy the app
if ! deploy; then
echo "Deployment failed. Mailing the admin..."
# Add your sendmail or notification logic here
exit 1
if ! deploy;
then
echo "Deployment Failed"
# sendmail
exit 1
fi

echo "********** DEPLOYMENT DONE *********"
echo "***************** DEPLOYMENT DONE ******************"