-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbalena.sh
executable file
·69 lines (59 loc) · 1.89 KB
/
balena.sh
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
set -ex
devices="$(blkid | grep resin-boot | awk -F':' '{print $1}' | tr '\n' ' ')"
for device in $devices; do
if [[ "${device}" == *mapper* ]]; then
continue
else
echo "${device}"
fi
done
# 1. AWS
# 2. DigitalOcean
# 3. Azure
# 4. Equinix
metadata_urls=(
'http://169.254.169.254/latest/user-data'
'http://169.254.169.254/metadata/v1/user-data'
'http://169.254.169.254/metadata/instance/compute/userData?api-version=2023-07-01&format=text;-H Metadata:true'
'https://metadata.platformequinix.com/userdata'
)
function cleanup() {
(sync && umount "${1}") || true
}
trap 'cleanup ${tmpmnt}' EXIT
function mount_boot() {
local tmpmnt
tmpmnt="$(mktemp -d)"
mount "${1}" "${tmpmnt}"
echo "${tmpmnt}"
}
function curl_with_opts() {
curl --fail --silent --connect-timeout 3 "$@"
}
function config_from_metadata() {
#shellcheck disable=SC2034,SC2039 # /bin/sh is a symbolic link to bash on balenaOS
for metadata_url in "${metadata_urls[@]}"; do
url="$(echo "${metadata_url}" | awk -F';' '{print $1}')"
headers="$(echo "${metadata_url}" | awk -F';' '{print $2}')"
response="$(curl_with_opts ${headers} "${url}")"
user_data="$(echo "${response}" | base64 -d)"
if [ -z "${user_data}" ]; then
user_data="${response}"
fi
if [ -n "${user_data}" ] && echo "${user_data}" | jq -e . > /dev/null; then
echo "${user_data}" | jq -r '.cloudConfig="done"'
break
fi
done
}
tmpmnt="$(mount_boot "${device}")"
tmpconf="$(mktemp)"
config_from_metadata > "${tmpconf}"
if [[ -f "${tmpmnt}/config.json" ]] && [[ -f "${tmpconf}" ]]; then
cloud_config="$(cat < "${tmpmnt}/config.json" | jq -r '.cloudConfig')"
if ! [[ "${cloud_config}" =~ ^done$ ]]; then
cat < "${tmpconf}" > "${tmpmnt}/config.json"
fi
cleanup "${tmpmnt}" && balena-idle
fi