Skip to content

Commit e81aacf

Browse files
committed
Update ecs-anywhere install script to support IPv6-only environments (#4747)
1 parent 5d9f695 commit e81aacf

File tree

1 file changed

+94
-2
lines changed

1 file changed

+94
-2
lines changed

scripts/ecs-anywhere-install.sh

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ check-option-value() {
1515
}
1616

1717
usage() {
18-
echo "$(basename "$0") [--help] --region REGION --activation-code CODE --activation-id ID [--cluster CLUSTER] [--enable-gpu] [--docker-install-source all|docker|distro|none] [--ecs-version VERSION] [--ecs-endpoint ENDPOINT] [--skip-registration] [--no-start]
18+
echo "$(basename "$0") [--help] --region REGION --activation-code CODE --activation-id ID [--cluster CLUSTER] [--enable-gpu] [--docker-install-source all|docker|distro|none] [--ecs-version VERSION] [--ecs-endpoint ENDPOINT] [--skip-registration] [--no-start] [--ip-compatibility ipv4|ipv6]
1919
2020
--help
2121
(optional) display this help message.
@@ -37,6 +37,8 @@ usage() {
3737
(optional) if this is enabled, SSM agent install and instance registration with SSM is skipped.
3838
--certs-file
3939
(optional) TLS certs for execute command feature. Defaults to searching for certs in known possible locations.
40+
--ip-compatibility string
41+
(optional) IP compatibility mode. Possible values are 'ipv4' or 'ipv6'. If not specified, auto-detects based on routing table.
4042
--no-start
4143
(optional) if this flag is provided, SSM agent, docker and ECS agent will not be started by the script despite being installed."
4244
}
@@ -59,6 +61,8 @@ CERTS_FILE=""
5961
# without having to sign it).
6062
CHECK_SIG=true
6163
NO_START=false
64+
IP_COMPATIBILITY=""
65+
6266
while :; do
6367
case "$1" in
6468
--help)
@@ -131,6 +135,15 @@ while :; do
131135
CHECK_SIG=false
132136
shift 1
133137
;;
138+
--ip-compatibility)
139+
check-option-value "$1" "$2"
140+
if [ "$2" != "ipv4" ] && [ "$2" != "ipv6" ]; then
141+
echo "Invalid value for --ip-compatibility: $2. Must be 'ipv4' or 'ipv6'."
142+
fail
143+
fi
144+
IP_COMPATIBILITY="$2"
145+
shift 2
146+
;;
134147
*)
135148
[ -z "$1" ] && break
136149
echo "invalid option: [$1]"
@@ -200,14 +213,56 @@ else
200213
fail
201214
fi
202215

216+
# Determines if IPv6-only configuration should be used.
217+
# Uses --ip-compatibility flag if set, otherwise auto-detects from routing table.
218+
# Returns 0 for IPv6-only, 1 for IPv4
219+
is-ipv6() {
220+
# If explicitly set to ipv6, return true
221+
if [ "$IP_COMPATIBILITY" = "ipv6" ]; then
222+
return 0
223+
fi
224+
225+
# If explicitly set to ipv4, return false
226+
if [ "$IP_COMPATIBILITY" = "ipv4" ]; then
227+
return 1
228+
fi
229+
230+
# Auto-detect when not explicitly set
231+
# Check if ip command is available
232+
if ! command -v ip >/dev/null 2>&1; then
233+
echo "WARNING: ip command not found while detecting IP compatibility, assuming IPv4"
234+
return 1
235+
fi
236+
237+
# Auto-detect IPv6-only environment by checking routes
238+
# Check if there's a default IPv4 route
239+
if ip route show default | grep -q "default"; then
240+
# IPv4 default route exists, not IPv6-only
241+
return 1
242+
fi
243+
244+
# Check if there's a default IPv6 route
245+
if ip -6 route show default | grep -q "default"; then
246+
# IPv6 default route exists but no IPv4, this is IPv6-only
247+
return 0
248+
fi
249+
250+
# No default routes found, assume IPv4
251+
return 1
252+
}
253+
203254
S3_BUCKET="amazon-ecs-agent-$REGION"
204255
RPM_PKG_NAME="amazon-ecs-init-$ECS_VERSION.$ARCH.rpm"
205256
DEB_PKG_NAME="amazon-ecs-init-$ECS_VERSION.$ARCH_ALT.deb"
206257
S3_URL_SUFFIX=""
207258
if grep -q "^cn-" <<< "$REGION"; then
208259
S3_URL_SUFFIX=".cn"
209260
fi
210-
S3_URL="https://s3.${REGION}.amazonaws.com${S3_URL_SUFFIX}"
261+
S3_URL_DUALSTACK=""
262+
if is-ipv6; then
263+
S3_URL_DUALSTACK="dualstack."
264+
fi
265+
S3_URL="https://s3.${S3_URL_DUALSTACK}${REGION}.amazonaws.com${S3_URL_SUFFIX}"
211266
SSM_S3_BUCKET="amazon-ssm-$REGION"
212267

213268
if [ -z "$RPM_URL" ]; then
@@ -316,7 +371,44 @@ register-ssm-agent() {
316371
ok
317372
}
318373

374+
configure-ssm-agent-ipv6() {
375+
try "configure SSM agent for IPv6-only environment"
376+
local ssm_config_dir="/etc/amazon/ssm"
377+
local ssm_config_file="$ssm_config_dir/amazon-ssm-agent.json"
378+
379+
if [ -f "$ssm_config_file" ]; then
380+
echo "SSM agent configuration file already exists at $ssm_config_file, skipping creation."
381+
else
382+
echo "Creating SSM agent configuration for IPv6-only environment"
383+
mkdir -p "$ssm_config_dir"
384+
385+
local endpoint_suffix="api.aws"
386+
if grep -q "^cn-" <<< "$REGION"; then
387+
endpoint_suffix="api.amazonwebservices.com.cn"
388+
fi
389+
390+
cat > "$ssm_config_file" << EOF
391+
{
392+
"Ssm": {
393+
"Endpoint": "https://ssm.$REGION.$endpoint_suffix"
394+
},
395+
"Mgs": {
396+
"Endpoint": "https://ssmmessages.$REGION.$endpoint_suffix"
397+
},
398+
"Mds": {
399+
"Endpoint": "https://ec2messages.$REGION.$endpoint_suffix"
400+
}
401+
}
402+
EOF
403+
echo "Created SSM agent configuration file at $ssm_config_file"
404+
fi
405+
ok
406+
}
407+
319408
install-ssm-agent() {
409+
if is-ipv6; then
410+
configure-ssm-agent-ipv6
411+
fi
320412
try "install ssm agent"
321413
if systemctl is-enabled $SSM_SERVICE_NAME &>/dev/null; then
322414
echo "SSM agent is already installed."

0 commit comments

Comments
 (0)