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
42 changes: 17 additions & 25 deletions day03/deploy_django_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,22 @@
# Deploy a Django app and handle errors

# Function to clone the Django app code
code_clone() {
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
git clone https://github.com/LondheShubham153/django-notes-app.git

}

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

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

# Uncomment the following lines if needed:
Expand All @@ -41,27 +30,30 @@ required_restarts() {
# 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
}
docker build -t notes-app .
docker run notes-app
#docker compose up

}

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

# Clone the code
if ! code_clone; then
if ! clone; then
echo "cloning failed"
cd django-notes-app || exit 1
fi
Comment on lines +43 to 46
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Logic error: error handling after clone is contradictory.

The script checks if clone fails (line 43), then prints "cloning failed" (line 44), but immediately attempts to cd into the cloned directory (line 45) regardless. This logic is backwards—if cloning failed, the directory won't exist and cd will fail. Either the cd should be outside the if block or the error handling should actually exit after the failure message.

Apply this diff to fix the logic:

-if ! clone; then
-echo "cloning failed"
-    cd django-notes-app || exit 1
+if clone; then
+    cd django-notes-app || exit 1
+else
+    echo "cloning failed"
+    exit 1
 fi
🤖 Prompt for AI Agents
In day03/deploy_django_app.sh around lines 43 to 46, the error handling after
the clone check is inverted: the script prints "cloning failed" on clone failure
but then immediately tries to cd into the repo. Update the logic so that if the
clone command fails you print the error and exit non‑zero (do not attempt to
cd), and if clone succeeds then cd into django-notes-app; i.e., move the cd into
the success path or add an exit after the failure message so the script never
attempts to change into a non‑existent directory.


# Install dependencies
if ! install_requirements; then
if ! install; then
echo "install failed"
exit 1
fi

# Perform required restarts
if ! required_restarts; then
if ! restarts; then
echo"restart failed"
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Syntax error: missing space between echo and string on line 56.

echo"restart failed" is missing a space after the echo command, which is a syntax error in bash.

Apply this diff to fix the syntax:

-echo"restart failed"
+echo "restart failed"
📝 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"restart failed"
echo "restart failed"
🤖 Prompt for AI Agents
In day03/deploy_django_app.sh around line 56, the echo invocation lacks a space
("echo\"restart failed\"") causing a bash syntax error; fix it by inserting a
space between the command and the string (e.g., change to echo "restart failed"
or echo 'restart failed') so the shell correctly recognizes the echo command and
arguments.

exit 1
fi
Comment on lines +55 to 58
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Function name mismatch: call to restarts but function is named restart.

Line 55 calls restarts() but the function defined on line 19 is named restart(). This will cause the script to fail with "command not found" error.

Apply this diff to fix the function name:

-if ! restarts; then
+if ! restart; then
📝 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
if ! restarts; then
echo"restart failed"
exit 1
fi
if ! restart; then
echo"restart failed"
exit 1
fi
🤖 Prompt for AI Agents
In day03/deploy_django_app.sh around lines 55-58 the script calls restarts()
which does not exist (the function defined at line 19 is restart()); change the
call to use restart() instead of restarts(), and while editing also fix the echo
invocation to include a space after echo (echo "restart failed") so the message
prints correctly; after the change run a quick shellcheck/syntax check to
confirm no other typos.


Expand Down