forked from Neutrollized/dynmotd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_cloud_detect.sh
More file actions
44 lines (39 loc) · 1.96 KB
/
Copy path00_cloud_detect.sh
File metadata and controls
44 lines (39 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# shellcheck shell=bash
# Shared cloud-provider detection. Runs once before any 01_*.sh module and
# exports CLOUD_PROVIDER (aws|gcp|azure|openstack|none). Each 01_*.sh checks
# this and returns early if it doesn't match.
#
# On non-cloud hosts this costs one connection attempt with a 1s timeout.
# On cloud hosts, the winning probe completes in well under a second.
CLOUD_PROVIDER='none'
# Quick reachability probe — most non-cloud hosts have no route to 169.254.169.254
# and fail immediately with "000".
_reach_status=$(curl -s --max-time 1 -o /dev/null -w '%{http_code}' \
http://169.254.169.254/ 2>/dev/null)
if [[ "${_reach_status}" != "000" ]]; then
# AWS IMDSv2 — token endpoint returns a non-empty body on EC2.
# Cache the token so 01_aws.sh doesn't need to re-fetch it.
AWS_METADATA_TOKEN=$(curl -s --max-time 2 -X PUT \
"http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 60" 2>/dev/null)
if [[ -n "${AWS_METADATA_TOKEN}" ]]; then
CLOUD_PROVIDER='aws'
export AWS_METADATA_TOKEN
elif [[ $(curl -s --max-time 2 -o /dev/null -w '%{http_code}' \
-H "Metadata-Flavor: Google" \
http://169.254.169.254/computeMetadata/v1/ 2>/dev/null) == '200' ]]; then
# GCP — requires Metadata-Flavor header; 200 with it, 403 without.
CLOUD_PROVIDER='gcp'
elif [[ $(curl -s --max-time 2 -o /dev/null -w '%{http_code}' \
-H "Metadata:true" --noproxy '*' \
"http://169.254.169.254/metadata/instance?api-version=2020-09-01" 2>/dev/null) == '200' ]]; then
# Azure IMDS — requires Metadata:true header.
CLOUD_PROVIDER='azure'
elif [[ $(curl -s --max-time 2 -o /dev/null -w '%{http_code}' \
http://169.254.169.254/openstack/latest/meta_data.json 2>/dev/null) == '200' ]]; then
# OpenStack — distinctive /openstack/ path.
CLOUD_PROVIDER='openstack'
fi
fi
unset _reach_status
export CLOUD_PROVIDER