Skip to content

Commit 6e8e7b6

Browse files
committed
feat: Support Debian-based distros
1 parent 6a822e2 commit 6e8e7b6

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

userdata.sh.tmpl

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ exec > >(tee /var/log/user-data.log | logger -t user-data -s 2>/dev/console) 2>&
33

44
echo "Starting user-data script..."
55

6+
echo "Determining package manager..."
7+
8+
# Work with both dnf and apt-get.
9+
if command -v apt-get >/dev/null 2>&1; then
10+
PKG_MANAGER=apt-get
11+
INSTALL_CMD="apt-get install -y"
12+
else
13+
PKG_MANAGER=dnf
14+
INSTALL_CMD="dnf install -y"
15+
fi
16+
17+
echo "Detected the following package manager: $PKG_MANAGER."
18+
619
echo "Enabling IP forwarding..."
720
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
821
echo 'net.ipv6.conf.all.forwarding = 1' >> /etc/sysctl.conf
@@ -54,26 +67,61 @@ retry_command() {
5467
return $exit_code
5568
}
5669

57-
# Install CloudWatch Agent
58-
echo "Installing CloudWatch Agent..."
59-
retry_command "dnf install -y amazon-cloudwatch-agent" 5
70+
# Function to install necessary packages per distro.
71+
install_packages() {
72+
case "$PKG_MANAGER" in
73+
apt-get)
74+
# Update package cache.
75+
echo "Updating package cache..."
76+
retry_command "$PKG_MANAGER update" 5
77+
78+
# Install utilities.
79+
echo "Installing utilities..."
80+
retry_command "$INSTALL_CMD curl wget" 5
81+
82+
# Install CloudWatch Agent.
83+
echo "Installing CloudWatch Agent..."
84+
distro=$(grep '^ID=' /etc/os-release | cut -d'=' -f2)
85+
arch=$(uname -m)
86+
case "$arch" in
87+
x86_64)
88+
arch=amd64
89+
;;
90+
*)
91+
arch=arm64
92+
;;
93+
esac
94+
retry_command "wget https://amazoncloudwatch-agent.s3.amazonaws.com/$distro/$arch/latest/amazon-cloudwatch-agent.deb" 5
95+
retry_command "dpkg -i -E ./amazon-cloudwatch-agent.deb" 5
96+
;;
97+
*)
98+
# Install utilities.
99+
echo "Installing utilities..."
100+
retry_command "$INSTALL_CMD dnf-utils" 5
101+
102+
# Install CloudWatch Agent.
103+
echo "Installing CloudWatch Agent..."
104+
retry_command "$INSTALL_CMD amazon-cloudwatch-agent" 5
105+
;;
106+
esac
107+
}
108+
109+
# Install necessary packages.
110+
echo "Installing necessary packages..."
111+
install_packages
112+
113+
# Start the CloudWatch Agent.
60114
amazon-cloudwatch-agent-ctl -a start -m ec2
61115

62116
# Install Tailscale
63117
echo "Installing Tailscale..."
64-
retry_command "dnf install -y dnf-utils" 5
65-
retry_command "dnf config-manager --add-repo https://pkgs.tailscale.com/stable/amazon-linux/2/tailscale.repo" 5
66-
retry_command "dnf install -y tailscale" 5
118+
retry_command "curl -fsSL https://tailscale.com/install.sh | sh" 5
67119

68120
%{ if tailscaled_extra_flags_enabled == true }
69121
echo "Exporting FLAGS to /etc/default/tailscaled..."
70122
sed -i "s|^FLAGS=.*|FLAGS=\"${tailscaled_extra_flags}\"|" /etc/default/tailscaled
71123
%{ endif }
72124

73-
# Setup Tailscale
74-
echo "Enabling and starting tailscaled service..."
75-
systemctl enable --now tailscaled
76-
77125
echo "Waiting for tailscaled to initialize..."
78126
sleep 5
79127

0 commit comments

Comments
 (0)