Skip to content

Commit 30b0aca

Browse files
authored
fix: avoid RPM lock issue (#44)
## what This: * Removes unnecessary `sudo` commands since the [user-data script runs as the root user](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts). * Adds waiting for the RPM lock to be released. * We've seen the following logs during an instance start-up: ```sh 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](https://docs.aws.amazon.com/linux/al2023/ug/package-management.html). `dnf` is the successor to `yum`. ## why * Prevents RPM lock issue and follows AWS recommended practices. ## references * N/A <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## 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. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 585e814 commit 30b0aca

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

Diff for: userdata.sh.tmpl

+38-10
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,59 @@ exec > >(tee /var/log/user-data.log | logger -t user-data -s 2>/dev/console) 2>&
44
echo "Starting user-data script..."
55

66
echo "Enabling IP forwarding..."
7-
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
8-
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.conf
9-
sudo sysctl -p /etc/sysctl.conf
7+
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
8+
echo 'net.ipv6.conf.all.forwarding = 1' >> /etc/sysctl.conf
9+
sysctl -p /etc/sysctl.conf
10+
11+
# Function to retry a command up to a maximum number of attempts
12+
retry_command() {
13+
local cmd="$1"
14+
local max_attempts="$2"
15+
local attempt=1
16+
local exit_code=0
17+
18+
while [ $attempt -le $max_attempts ]; do
19+
echo "Attempt $attempt of $max_attempts: $cmd"
20+
eval "$cmd"
21+
exit_code=$?
22+
if [ $exit_code -eq 0 ]; then
23+
echo "Command succeeded: $cmd"
24+
return 0
25+
else
26+
echo "Command failed with exit code $exit_code: $cmd"
27+
attempt=$((attempt + 1))
28+
if [ $attempt -le $max_attempts ]; then
29+
echo "Retrying in 2 seconds..."
30+
sleep 2
31+
fi
32+
fi
33+
done
34+
35+
echo "Command failed after $max_attempts attempts: $cmd"
36+
return $exit_code
37+
}
1038

1139
echo "Installing Tailscale..."
12-
sudo yum install -y yum-utils
13-
sudo yum-config-manager --add-repo https://pkgs.tailscale.com/stable/amazon-linux/2/tailscale.repo
14-
sudo yum install -y tailscale
40+
retry_command "dnf install -y dnf-utils" 5
41+
retry_command "dnf config-manager --add-repo https://pkgs.tailscale.com/stable/amazon-linux/2/tailscale.repo" 5
42+
retry_command "dnf install -y tailscale" 5
1543

1644
%{ if tailscaled_extra_flags_enabled == true }
1745
echo "Exporting FLAGS to /etc/default/tailscaled..."
18-
sudo sed -i "s|^FLAGS=.*|FLAGS=\"${tailscaled_extra_flags}\"|" /etc/default/tailscaled
46+
sed -i "s|^FLAGS=.*|FLAGS=\"${tailscaled_extra_flags}\"|" /etc/default/tailscaled
1947
%{ endif }
2048

21-
# Setup tailscale
49+
# Setup Tailscale
2250
echo "Enabling and starting tailscaled service..."
23-
sudo systemctl enable --now tailscaled
51+
systemctl enable --now tailscaled
2452

2553
echo "Waiting for tailscaled to initialize..."
2654
sleep 5
2755

2856
# Start tailscale
2957
# We pass --advertise-tags below even though the authkey being created with those tags should result
3058
# in the same effect. This is to be more explicit because tailscale tags are a complicated topic.
31-
sudo tailscale up \
59+
tailscale up \
3260
%{ if ssh_enabled == true }--ssh%{ endif } \
3361
%{ if exit_node_enabled == true }--advertise-exit-node%{ endif } \
3462
%{ if tailscale_up_extra_flags_enabled == true }${tailscale_up_extra_flags}%{ endif } \

Diff for: variables.tf

+3-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ variable "ssm_state_enabled" {
202202
default = false
203203
type = bool
204204
description = <<-EOT
205-
Control if tailscaled state is stored in AWS SSM (including preferences and keys). This tells the Tailscale daemon to write + read state from SSM, which unlocks important features like retaining the existing tailscale machine name.
205+
Control if tailscaled state is stored in AWS SSM (including preferences and keys).
206+
This tells the Tailscale daemon to write + read state from SSM,
207+
which unlocks important features like retaining the existing tailscale machine name.
206208
See more in the [docs](https://tailscale.com/kb/1278/tailscaled#flags-to-tailscaled).
207209
EOT
208210
}

0 commit comments

Comments
 (0)