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

fix: avoid RPM lock issue #44

Merged
merged 16 commits into from
Nov 25, 2024
Merged

fix: avoid RPM lock issue #44

merged 16 commits into from
Nov 25, 2024

Conversation

gberenice
Copy link
Member

@gberenice gberenice commented Nov 21, 2024

what

This:

  • Removes unnecessary sudo commands since the user-data script runs as the root user.
  • Adds waiting for the RPM lock to be released.
    • We've seen the following logs during an instance start-up:
      user-data: RPM: error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Resource temporarily unavailable)
      user-data: The downloaded packages were saved in cache until the next successful transaction.
      user-data: You can remove cached packages by executing 'yum clean packages'.
      user-data: Error: Could not run transaction.
    • RPM database lock is being held by another process when script attempts to run yum/dnf commands. Amazon Linux 2023 (AL2023) may perform automatic updates or other package management tasks during boot, causing the RPM database to be locked temporarily.
  • Uses dnf instead of yum: Amazon Linux 2023 uses dnf as the default package manager. dnf is the successor to yum.

why

  • Prevents RPM lock issue and follows AWS recommended practices.

references

  • N/A

Summary by CodeRabbit

  • New Features

    • Updated script for Tailscale installation and configuration, improving efficiency with a retry mechanism for command execution.
  • Bug Fixes

    • Removed unnecessary sudo calls for a cleaner execution process.
  • Documentation

    • Enhanced readability of the ssm_state_enabled variable description in the configuration file.

@gberenice gberenice requested a review from a team as a code owner November 21, 2024 17:00
Copy link

coderabbitai bot commented Nov 21, 2024

Walkthrough

The pull request introduces modifications to the userdata.sh.tmpl script and variables.tf file. Key updates in userdata.sh.tmpl include the removal of sudo from multiple commands, a switch from yum to dnf for package management, and the addition of a retry mechanism for command execution. The variables.tf file sees a reformatting of the ssm_state_enabled variable's description for improved clarity, without altering its functionality.

Changes

File Change Summary
userdata.sh.tmpl - Removed sudo from multiple commands.
- Replaced yum with dnf for package management.
- Introduced retry_command function for command execution.
- Updated sed command for exporting flags without sudo.
- Clarified comments.
variables.tf - Reformatted description of ssm_state_enabled variable for better readability.

Possibly related PRs

  • feat: support AWS SSM tailscaled state #41: The main PR modifies the userdata.sh.tmpl script to enhance the Tailscale installation process, while PR feat: support AWS SSM tailscaled state #41 introduces support for storing the Tailscale state in AWS SSM, which is directly related to the configuration and management of Tailscale. Both PRs involve changes that affect how Tailscale is set up and managed in the infrastructure.

Suggested reviewers

  • Gowiem

🌟 In the land of code, where scripts do play,
Tailscale's setup shines brighter each day.
With dnf in hand and sudo set free,
Our script dances lightly, as smooth as can be.
Variables reformed, clarity's the key,
Here’s to the changes, as grand as a spree! 🎉


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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: 2

🧹 Outside diff range and nitpick comments (3)
userdata.sh.tmpl (3)

7-9: Consider using /etc/sysctl.d/ for modular configuration

While the current approach works, it's considered a best practice to place custom sysctl configurations in /etc/sysctl.d/ for better organization and maintainability.

-echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
-echo 'net.ipv6.conf.all.forwarding = 1' >> /etc/sysctl.conf
-sysctl -p /etc/sysctl.conf
+echo 'net.ipv4.ip_forward = 1' > /etc/sysctl.d/99-tailscale.conf
+echo 'net.ipv6.conf.all.forwarding = 1' >> /etc/sysctl.d/99-tailscale.conf
+sysctl -p /etc/sysctl.d/99-tailscale.conf

17-19: Add error handling for package installation

The transition to dnf is appropriate for Amazon Linux 2023. Consider adding error handling for package installation failures.

+set +e  # Temporarily disable exit on error
 dnf install -y dnf-utils
+if [ $? -ne 0 ]; then
+    echo "Failed to install dnf-utils"
+    exit 1
+fi
 dnf config-manager --add-repo https://pkgs.tailscale.com/stable/amazon-linux/2/tailscale.repo
+if [ $? -ne 0 ]; then
+    echo "Failed to add Tailscale repository"
+    exit 1
+fi
 dnf install -y tailscale
+if [ $? -ne 0 ]; then
+    echo "Failed to install Tailscale"
+    exit 1
+fi
+set -e  # Re-enable exit on error

Line range hint 36-44: Validate Tailscale configuration before applying

The Tailscale configuration looks correct, but it would be beneficial to validate the inputs before applying them.

+# Validate required inputs
+if [ -z "${routes}" ]; then
+    echo "Error: routes must be specified"
+    exit 1
+fi
+if [ -z "${tags}" ]; then
+    echo "Error: tags must be specified"
+    exit 1
+fi
+if [ -z "${hostname}" ]; then
+    echo "Error: hostname must be specified"
+    exit 1
+fi
+if [ -z "${authkey}" ]; then
+    echo "Error: authkey must be specified"
+    exit 1
+fi
+
 tailscale up \
   %{ if ssh_enabled == true }--ssh%{ endif } \
   %{ if exit_node_enabled == true }--advertise-exit-node%{ endif } \
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 585e814 and bdafa2b.

📒 Files selected for processing (2)
  • userdata.sh.tmpl (1 hunks)
  • variables.tf (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • variables.tf

userdata.sh.tmpl Outdated
Comment on lines 11 to 14
echo "Waiting for the RPM lock to be released..."
while fuser /var/lib/rpm/.rpm.lock >/dev/null 2>&1 ; do
sleep 1
done
Copy link

@coderabbitai coderabbitai bot Nov 21, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add timeout and error handling to RPM lock check

While the lock check is a good addition, it could benefit from a timeout to prevent infinite waiting and proper error handling.

 echo "Waiting for the RPM lock to be released..."
+TIMEOUT=300  # 5 minutes timeout
+start_time=$(date +%s)
+if ! command -v fuser >/dev/null 2>&1; then
+    echo "fuser command not found, installing psmisc..."
+    dnf install -y psmisc
+fi
 while fuser /var/lib/rpm/.rpm.lock >/dev/null 2>&1 ; do
+    current_time=$(date +%s)
+    if (( current_time - start_time > TIMEOUT )); then
+        echo "Timeout waiting for RPM lock to be released"
+        exit 1
+    fi
     sleep 1
 done
📝 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 "Waiting for the RPM lock to be released..."
while fuser /var/lib/rpm/.rpm.lock >/dev/null 2>&1 ; do
sleep 1
done
echo "Waiting for the RPM lock to be released..."
TIMEOUT=300 # 5 minutes timeout
start_time=$(date +%s)
if ! command -v fuser >/dev/null 2>&1; then
echo "fuser command not found, installing psmisc..."
dnf install -y psmisc
fi
while fuser /var/lib/rpm/.rpm.lock >/dev/null 2>&1 ; do
current_time=$(date +%s)
if (( current_time - start_time > TIMEOUT )); then
echo "Timeout waiting for RPM lock to be released"
exit 1
fi
sleep 1
done

Copy link
Member Author

Choose a reason for hiding this comment

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

This might be a good one.

Copy link

Choose a reason for hiding this comment

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

@​gberenice, glad you think this could be useful! Incorporating a timeout and error handling will ensure your script doesn't hang indefinitely if the RPM lock isn't released, improving its robustness. If you need any assistance implementing it, feel free to ask.

Copy link
Member

Choose a reason for hiding this comment

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

@gberenice yeah, I think this is good. Make the timeout 30 seconds -- the lock should never be that long and we want to have this user-data script be speedy if we can.

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Member Author

Choose a reason for hiding this comment

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

@Gowiem I've researched more about this issue, and it's recommended the following:

when using user data via cloud-init it is possible that other processes will be running simultaneously. If those processes are out of your control, it is best to just implement the required retry mechanisms in your own scripts.

I'll add a retry tomorrow and ping you for another round of review.

Copy link
Member Author

Choose a reason for hiding this comment

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

@Gowiem PR updated with retry logic.

userdata.sh.tmpl Show resolved Hide resolved
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)
userdata.sh.tmpl (2)

40-42: Optimize package installation checks

The installation commands are properly sequenced and use the correct package manager (dnf) for AL2023. Consider adding existence checks to avoid unnecessary reinstallation attempts.

+if ! rpm -q dnf-utils >/dev/null 2>&1; then
   retry_command "dnf install -y dnf-utils" 5
+fi
+if ! rpm -q tailscale >/dev/null 2>&1; then
   retry_command "dnf config-manager --add-repo https://pkgs.tailscale.com/stable/amazon-linux/2/tailscale.repo" 5
   retry_command "dnf install -y tailscale" 5
+fi

46-46: Add error handling for FLAGS configuration

The sed command could fail silently if the file or pattern doesn't exist.

-sed -i "s|^FLAGS=.*|FLAGS=\"${tailscaled_extra_flags}\"|" /etc/default/tailscaled
+if [ -f /etc/default/tailscaled ]; then
+  sed -i "s|^FLAGS=.*|FLAGS=\"${tailscaled_extra_flags}\"|" /etc/default/tailscaled || {
+    echo "Failed to update tailscaled FLAGS"
+    exit 1
+  }
+else
+  echo "ERROR: /etc/default/tailscaled not found"
+  exit 1
+fi
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5a5b8d2 and fb8e943.

📒 Files selected for processing (1)
  • userdata.sh.tmpl (1 hunks)
🔇 Additional comments (1)
userdata.sh.tmpl (1)

7-9: LGTM: IP forwarding configuration is correct

The IP forwarding setup is properly configured for both IPv4 and IPv6, and the removal of sudo is appropriate since the script runs with root privileges.

Comment on lines +11 to +37
# Function to retry a command up to a maximum number of attempts
retry_command() {
local cmd="$1"
local max_attempts="$2"
local attempt=1
local exit_code=0

while [ $attempt -le $max_attempts ]; do
echo "Attempt $attempt of $max_attempts: $cmd"
eval "$cmd"
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Command succeeded: $cmd"
return 0
else
echo "Command failed with exit code $exit_code: $cmd"
attempt=$((attempt + 1))
if [ $attempt -le $max_attempts ]; then
echo "Retrying in 2 seconds..."
sleep 2
fi
fi
done

echo "Command failed after $max_attempts attempts: $cmd"
return $exit_code
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add timeout and cleanup handling to retry mechanism

While the retry logic is well-implemented, consider these enhancements:

  1. Add the agreed-upon 30-second timeout
  2. Include cleanup handling for interrupted execution
 retry_command() {
   local cmd="$1"
   local max_attempts="$2"
   local attempt=1
   local exit_code=0
+  local start_time=$(date +%s)
+  local timeout=30  # 30-second timeout
+
+  # Setup cleanup trap
+  trap 'echo "Command interrupted: $cmd"; return 1' INT TERM
 
   while [ $attempt -le $max_attempts ]; do
+    current_time=$(date +%s)
+    if (( current_time - start_time > timeout )); then
+      echo "Timeout after ${timeout} seconds: $cmd"
+      return 124  # Standard timeout exit code
+    fi
     echo "Attempt $attempt of $max_attempts: $cmd"
📝 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
# Function to retry a command up to a maximum number of attempts
retry_command() {
local cmd="$1"
local max_attempts="$2"
local attempt=1
local exit_code=0
while [ $attempt -le $max_attempts ]; do
echo "Attempt $attempt of $max_attempts: $cmd"
eval "$cmd"
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Command succeeded: $cmd"
return 0
else
echo "Command failed with exit code $exit_code: $cmd"
attempt=$((attempt + 1))
if [ $attempt -le $max_attempts ]; then
echo "Retrying in 2 seconds..."
sleep 2
fi
fi
done
echo "Command failed after $max_attempts attempts: $cmd"
return $exit_code
}
# Function to retry a command up to a maximum number of attempts
retry_command() {
local cmd="$1"
local max_attempts="$2"
local attempt=1
local exit_code=0
local start_time=$(date +%s)
local timeout=30 # 30-second timeout
# Setup cleanup trap
trap 'echo "Command interrupted: $cmd"; return 1' INT TERM
while [ $attempt -le $max_attempts ]; do
current_time=$(date +%s)
if (( current_time - start_time > timeout )); then
echo "Timeout after ${timeout} seconds: $cmd"
return 124 # Standard timeout exit code
fi
echo "Attempt $attempt of $max_attempts: $cmd"
eval "$cmd"
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Command succeeded: $cmd"
return 0
else
echo "Command failed with exit code $exit_code: $cmd"
attempt=$((attempt + 1))
if [ $attempt -le $max_attempts ]; then
echo "Retrying in 2 seconds..."
sleep 2
fi
fi
done
echo "Command failed after $max_attempts attempts: $cmd"
return $exit_code
}

Copy link
Member

@Gowiem Gowiem left a comment

Choose a reason for hiding this comment

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

@gberenice gberenice merged commit 30b0aca into main Nov 25, 2024
3 checks passed
@gberenice gberenice deleted the fix/avoid-rpm-lock-issue branch November 25, 2024 11:51
gberenice pushed a commit that referenced this pull request Nov 25, 2024
🤖 I have created a release *beep* *boop*
---


##
[1.5.1](v1.5.0...v1.5.1)
(2024-11-25)


### Bug Fixes

* avoid RPM lock issue
([#44](#44))
([30b0aca](30b0aca))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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