-
Notifications
You must be signed in to change notification settings - Fork 204
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
Optimize Docker Build Script for Maintainability and Error Handling #989
Conversation
WalkthroughThe Changes
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
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this 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 ]; thenBy 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 fiThis 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?
There was a problem hiding this 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
📒 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")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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)
There was a problem hiding this 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
Server/controllers/authController.js
Outdated
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 ?? []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
Server/controllers/authController.js
Outdated
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); | ||
}) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Server/controllers/authController.js
Outdated
const userForToken = { ...newUser._doc }; | ||
delete userForToken.profileImage; | ||
delete userForToken.avatarImage; | ||
const { password, avatarImage, ...userWithoutPassword } = user._doc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
const { password, avatarImage, ...userWithoutPassword } = user._doc; | |
const { password, avatarImage, ...userWithoutPassword } = newUser._doc; |
There was a problem hiding this 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!
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.