Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Docker Build Script for Maintainability and Error Handling #989

Merged
merged 6 commits into from
Oct 21, 2024

Conversation

abhayjoshi201
Copy link
Contributor

This PR refactors the Docker build script to:

Reduce redundancy using an array for Dockerfiles.
Add error handling to stop on failed builds.
Improve script readability and maintainability.

Copy link

coderabbitai bot commented Oct 19, 2024

Walkthrough

The build_images.sh script has been restructured to enhance the Docker image building process for various services. The previous method of defining individual variables for each Dockerfile has been replaced with an associative array that maps service names to their Dockerfile paths. A loop now iterates through this array to execute the docker build commands. Additionally, error handling has been introduced to manage build failures effectively, ensuring that any issues are reported immediately.

Changes

File Change Summary
Docker/dev/build_images.sh Restructured to use an associative array for service names and Dockerfile paths; added error handling for build commands; modified final output message for successful completion.
Server/controllers/authController.js Updated user registration, login, editing, and deletion functionalities with destructuring; enhanced password handling; improved email retrieval from JWT; modernized code with async/await patterns.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Script
    participant Docker

    User->>Script: Start build process
    Script->>Docker: Build image for uptime_client
    Docker-->>Script: Build success
    Script->>Docker: Build image for uptime_database_mongo
    Docker-->>Script: Build success
    Script->>Docker: Build image for uptime_database_postgres
    Docker-->>Script: Build success
    Script-->>User: All images built successfully
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (3)
Docker/dev/build_images.sh (3)

6-12: Yo, this array's lit! But let's make it pop even more, ya feel me?

Aight, so this associative array is dope. It's like, you know when you're at a party and you got all your homies in one place? That's what this array is doing for our services and Dockerfiles. It's bringing the whole squad together, making it easier to manage and less repetitive than your mom's spaghetti.

But check it, we could make this even more fire by adding some comments to explain what each service is about. It'd be like giving each homie a name tag at the party, you know what I'm saying?

Here's how we could spice it up:

 declare -A services=(
-  ["uptime_client"]="./Docker/dev/client.Dockerfile"
-  ["uptime_database_mongo"]="./Docker/dev/mongoDB.Dockerfile"
-  ["uptime_redis"]="./Docker/dev/redis.Dockerfile"
-  ["uptime_server"]="./Docker/dev/server.Dockerfile"
+  ["uptime_client"]="./Docker/dev/client.Dockerfile"       # Frontend service
+  ["uptime_database_mongo"]="./Docker/dev/mongoDB.Dockerfile"  # MongoDB database
+  ["uptime_redis"]="./Docker/dev/redis.Dockerfile"         # Redis cache
+  ["uptime_server"]="./Docker/dev/server.Dockerfile"       # Backend service
 )

This way, even if someone's new to the party, they'll know who's who, you feel me?


14-23: This loop's got me in my feelings, but in a good way, you know?

Yo, this loop is straight fire! It's like when you're at the club and the DJ's spinning all your favorite tracks one after another. Each service gets its moment to shine, and we're building these Docker images like we're building a sick beat.

But listen, I got a small nitpick that could make this even more lit. You know how in rap battles, consistency is key? Let's apply that here too.

Check out this small tweak:

 for service in "${!services[@]}"; do
-  docker build -f "${services[$service]}" -t "$service" .
+  docker build -t "$service" -f "${services[$service]}" .
   
   ## Check if the build succeeded
   if [ $? -ne 0 ]; then

By putting the -t flag before the -f flag, we're keeping the order consistent with the Docker CLI docs. It's like keeping your rhymes in the right order, you feel me? Small change, but it makes the flow even smoother.


18-22: This error handling is tighter than my rhymes on a good day!

Yo, this error handling is like having a bouncer at your party. If something goes wrong, it's gonna let you know and shut it down before things get out of hand. That's some next-level thinking right there!

But you know what? We could make this even more explicit, like when you're spitting bars and you want everyone to catch every word.

Check out this small addition:

   ## Check if the build succeeded
   if [ $? -ne 0 ]; then
-    echo "Error building $service image. Exiting..."
+    echo "Error: Failed to build $service image. Build process halted."
     exit 1
   fi

This change makes it crystal clear what went down and what's happening next. It's like when you're on stage and you want to make sure even the people in the back can hear you, you know what I'm saying?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2871171 and d28d049.

📒 Files selected for processing (1)
  • Docker/dev/build_images.sh (1 hunks)
🧰 Additional context used

Copy link

@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: 1

🧹 Outside diff range and nitpick comments (2)
Docker/dev/build_images.sh (2)

19-23: Error handlin' like a boss, but let's make it even tighter!

Yo, I'm diggin' this error check, it's like you're the bouncer at Club Docker, kickin' out them bad builds! But let's make it even more lit, you know what I'm sayin'?

How 'bout we drop the service name in the error message? Like this:

-    echo "Error building $service image. Exiting..."
+    echo "Yo, we hit a snag buildin' the $service image. This party's over, fam!"

Now we're talkin' specifics, and keepin' it real with our homies!


26-26: Let's make this success message pop like champagne, yo!

Aight, so we're celebratin' at the end, but let's make it rain with some more deets! How 'bout we flex on 'em with the number of images we just dropped?

Try this on for size:

-echo "All images built successfully"
+echo "Boom! All ${#services[@]} images are fresh out the oven. We eatin' good tonight!"

Now that's a party announcement, you feel me?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d28d049 and c8b8128.

📒 Files selected for processing (1)
  • Docker/dev/build_images.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck
Docker/dev/build_images.sh

[warning] 4-4: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

(SC2164)

🔇 Additional comments (2)
Docker/dev/build_images.sh (2)

7-13: Yo, this array's droppin' beats like it's hot!

Mad props for this sick associative array, homie! It's like you took all them Dockerfile variables and mixed 'em into one fresh track. This joint's gonna be easier to remix later, you feel me?


15-17: This loop's spinnin' like a fresh vinyl, yo!

You're killin' it with this loop, G! It's like you're the DJ, scratchin' through them services and buildin' images like it ain't no thang. Smooth operator, for real!

#!/bin/bash

# Change directory to root Server directory for correct Docker Context
cd "$(dirname "$0")"
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Yo, dawg! Let's beef up that directory change, ya feel me?

Aight, so check it. We're switchin' directories like it's all good, but what if it ain't? We gotta handle that potential failure, you know what I'm sayin'?

Let's drop this hot fix:

-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || { echo "Couldn't slide into the script's crib. I'm out!"; exit 1; }

This way, if the CD player skips, we bounce outta here with style, you dig?

📝 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
cd "$(dirname "$0")"
cd "$(dirname "$0")" || { echo "Couldn't slide into the script's crib. I'm out!"; exit 1; }
🧰 Tools
🪛 Shellcheck

[warning] 4-4: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

(SC2164)

Copy link

@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: 3

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c8b8128 and dc1cc07.

📒 Files selected for processing (1)
  • Server/controllers/authController.js (2 hunks)
🧰 Additional context used

Comment on lines 209 to 217
const { email } = jwt.verify(token, (await req.settingsService.getSettings()).jwtSecret);
if (req.body.password && req.body.newPassword) {
// Get token from headers
const token = getTokenFromHeaders(req.headers);
// Get email from token
const { jwtSecret } = req.settingsService.getSettings();
const { email } = jwt.verify(token, jwtSecret);
// Add user email to body for DB operation
const monitors = teamMonitors?.monitors ?? [];
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Double Take: Redundant Token and Email Extraction

Starting at line 209, you've already verified the token and extracted the email:

const { email } = jwt.verify(token, (await req.settingsService.getSettings()).jwtSecret);

But then inside the if block, you repeat these steps:

const token = getTokenFromHeaders(req.headers);
const { jwtSecret } = req.settingsService.getSettings();
const { email } = jwt.verify(token, jwtSecret);

This duplication might cause unnecessary confusion and could make the code a bit harder to maintain. Additionally, remember to await asynchronous functions like req.settingsService.getSettings() to prevent any unexpected behaviour.

Let's streamline this:

-    const { email } = jwt.verify(token, (await req.settingsService.getSettings()).jwtSecret);
     if (req.body.password && req.body.newPassword) {
-        const token = getTokenFromHeaders(req.headers);
-        const { jwtSecret } = req.settingsService.getSettings();
-        const { email } = jwt.verify(token, jwtSecret);
         // Add user email to body for DB operation
+        const token = getTokenFromHeaders(req.headers);
+        const appSettings = await req.settingsService.getSettings();
+        const { email } = jwt.verify(token, appSettings.jwtSecret);
         req.body.email = email;

Comment on lines 217 to 225
const monitors = teamMonitors?.monitors ?? [];
await Promise.all(
monitors.map(async (monitor) => {
await req.jobQueue.deleteJob(monitor);
await req.db.deleteChecks(monitor._id);
await req.db.deletePageSpeedChecksByMonitorId(monitor._id);
await req.db.deleteNotificationsByMonitorId(monitor._id);
})
);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Hold Up! Unintended Deletion of Monitors

In lines 217-225, you're deleting monitors and related data when a user edits their profile:

const monitors = teamMonitors?.monitors ?? [];
await Promise.all(
    monitors.map(async (monitor) => {
        await req.jobQueue.deleteJob(monitor);
        await req.db.deleteChecks(monitor._id);
        await req.db.deletePageSpeedChecksByMonitorId(monitor._id);
        await req.db.deleteNotificationsByMonitorId(monitor._id);
    })
);

This seems a bit drastic for a profile edit action. Users might be surprised (and unhappy) if their monitors disappear after updating their password or other details. Let's avoid any unintended consequences!

Consider revising this logic to ensure monitors are only deleted when absolutely necessary.

const userForToken = { ...newUser._doc };
delete userForToken.profileImage;
delete userForToken.avatarImage;
const { password, avatarImage, ...userWithoutPassword } = user._doc;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Oops! Undefined Variable user Detected

At line 88, you're attempting to destructure user._doc, but it seems user isn't defined in this scope. Did you mean to use newUser instead? This little oversight could lead to a runtime error.

Here's a quick fix:

-    const { password, avatarImage, ...userWithoutPassword } = user._doc;
+    const { password, avatarImage, ...userWithoutPassword } = newUser._doc;
📝 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
const { password, avatarImage, ...userWithoutPassword } = user._doc;
const { password, avatarImage, ...userWithoutPassword } = newUser._doc;

Copy link
Collaborator

@ajhollid ajhollid left a comment

Choose a reason for hiding this comment

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

I'm OK with updating the build script, but please remove the empty git file ad remove the authController from this PR.

PRs should be granular and targeted to one specific feature only. Thank you!

@ajhollid ajhollid merged commit 02dc186 into bluewave-labs:develop Oct 21, 2024
1 check passed
@ajhollid ajhollid mentioned this pull request Oct 21, 2024
2 tasks
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.

2 participants