Skip to content

fix(day03): move cd outside if-block, add docker-compose down before deploy#17

Open
Farhan-Mendhro wants to merge 1 commit intoLondheShubham153:mainfrom
Farhan-Mendhro:fix/deploy-django-app
Open

fix(day03): move cd outside if-block, add docker-compose down before deploy#17
Farhan-Mendhro wants to merge 1 commit intoLondheShubham153:mainfrom
Farhan-Mendhro:fix/deploy-django-app

Conversation

@Farhan-Mendhro
Copy link
Copy Markdown

@Farhan-Mendhro Farhan-Mendhro commented Mar 27, 2026

What was broken

  1. The cd command only ran if clone failed, so the script stayed in the wrong folder on first run.
  2. Old Docker networks from previous runs caused the new deployment to crash.

What I fixed

  1. Moved cd outside the if/else so it always enters the app folder.
  2. Added docker-compose down at the start of deploy() to clean up old containers and networks.

Tested

Ran locally in WSL — both a fresh clone and a re-run worked correctly.

Summary by CodeRabbit

Release Notes

  • Refactor
    • Streamlined deployment script logic and simplified setup processes for improved clarity and maintainability.
    • Deployment now cleans up existing containers before rebuilding and restarting services, ensuring a fresh state with each deployment.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 27, 2026

📝 Walkthrough

Walkthrough

A bash script for deploying a Django application was refactored: repository cloning became unconditional, helper functions were renamed and simplified with reduced error handling, and the deployment process now includes a docker-compose down step before rebuilding containers.

Changes

Cohort / File(s) Summary
Django Deployment Script Refactoring
day03/deploy_django_app.sh
Refactored control flow by removing conditional logic around git cloning, renaming helper functions (install_requirements()install_req(), required_restarts()req_restarts()), simplifying them with reduced error handling, and updating the deployment step to include docker-compose down before rebuilding containers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A script evolved with care and grace,
Removed the guards we don't need in place,
Clone with conviction, restart with glee,
Docker takes a pause, then runs free!
Simpler paths lead to victory. 🚀

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly addresses the two main changes: moving cd outside the if-block and adding docker-compose down before deploy, matching the core fixes described in objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
day03/deploy_django_app.sh (1)

29-29: Quote the $USER variable for safety.

Unquoted variables can cause issues if they contain spaces or special characters (unlikely for $USER, but good practice).

Proposed fix
-        sudo chown $USER /var/run/docker.sock
+        sudo chown "$USER" /var/run/docker.sock
🤖 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 29, The chown invocation using sudo chown
$USER /var/run/docker.sock should quote the variable to avoid word-splitting or
globbing; update the command referenced (sudo chown $USER /var/run/docker.sock)
to use a quoted variable (e.g., "$USER") so it becomes sudo chown "$USER"
/var/run/docker.sock.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@day03/deploy_django_app.sh`:
- Around line 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.
- 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).
- Around line 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.
- Around line 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.

---

Nitpick comments:
In `@day03/deploy_django_app.sh`:
- Line 29: The chown invocation using sudo chown $USER /var/run/docker.sock
should quote the variable to avoid word-splitting or globbing; update the
command referenced (sudo chown $USER /var/run/docker.sock) to use a quoted
variable (e.g., "$USER") so it becomes sudo chown "$USER" /var/run/docker.sock.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7afcc1f3-db2d-4075-a1c1-db1c0d8ce43b

📥 Commits

Reviewing files that changed from the base of the PR and between 6671dd0 and d5242ab.

📒 Files selected for processing (1)
  • day03/deploy_django_app.sh

Comment on lines +4 to +7
<< task
deploy django app
and handle errors
task
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.

Comment on lines +11 to 15
code_clone(){

echo "Cloning the Django app..."
git clone https://github.com/LondheShubham153/django-notes-app.git
}
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.

Comment on lines +18 to 22
install_req() {

echo " Installing dependencies..."
sudo apt-get install docker.io nginx -y docker-compose
}
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.


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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant