Skip to content

Commit 6425f4d

Browse files
committed
cherry pick PR #86
1 parent 3a42115 commit 6425f4d

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

aur/digitalocean-synchronize.sh

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ log() {
3535
echo "[$(date)]" "$@" >&2
3636
}
3737

38+
http_get() {
39+
# Sometimes the API request fails with 'connection reset by peer'
40+
curl --location --silent --fail --retry 3 --retry-all-errors "$@"
41+
}
42+
3843
netmask_to_prefix() {
3944
local pfx=0 cmp msk
4045
for cmp in ${1//./ } 0; do
@@ -76,8 +81,8 @@ update_shadow_if_changed() {
7681
process_interface() {
7782
local url=$1
7883
local attrs=$2
79-
local mac=$(curl -LSsf ${url}mac)
80-
local type=$(curl -LSsf ${url}type)
84+
local mac=$(http_get -S ${url}mac)
85+
local type=$(http_get -S ${url}type)
8186
local interface=
8287
local cand path
8388
for cand in $(ls /sys/class/net); do
@@ -97,26 +102,26 @@ process_interface() {
97102
[Network]
98103
EOF
99104
if [[ " ${attrs} " =~ " ipv4/ " ]]; then
100-
local address=$(curl -Lsf ${url}ipv4/address)
101-
local prefix=$(netmask_to_prefix $(curl -Lsf ${url}ipv4/netmask))
105+
local address=$(http_get ${url}ipv4/address)
106+
local prefix=$(netmask_to_prefix $(http_get ${url}ipv4/netmask))
102107
echo "Address=${address}/${prefix}"
103108
if [ "${type}" != "private" ]; then
104-
echo "Gateway=$(curl -Lsf ${url}ipv4/gateway)"
109+
echo "Gateway=$(http_get ${url}ipv4/gateway)"
105110
fi
106111
log "Added IPv4 address ${address}/${prefix} on ${interface}."
107112
fi
108113
if [[ " ${attrs} " =~ " anchor_ipv4/ " ]]; then
109-
local address=$(curl -Lsf ${url}anchor_ipv4/address)
110-
local prefix=$(netmask_to_prefix $(curl -Lsf ${url}anchor_ipv4/netmask))
114+
local address=$(http_get ${url}anchor_ipv4/address)
115+
local prefix=$(netmask_to_prefix $(http_get ${url}anchor_ipv4/netmask))
111116
echo "Address=${address}/${prefix}"
112117
log "Added Anchor IPv4 address ${address}/${prefix} on ${interface}."
113118
fi
114119
if [[ " ${attrs} " =~ " ipv6/ " ]]; then
115-
local address=$(curl -Lsf ${url}ipv6/address)
116-
local prefix=$(curl -Lsf ${url}ipv6/cidr)
120+
local address=$(http_get ${url}ipv6/address)
121+
local prefix=$(http_get ${url}ipv6/cidr)
117122
echo "Address=${address}/${prefix}"
118123
if [ "${type}" != "private" ]; then
119-
echo "Gateway=$(curl -Lsf ${url}ipv6/gateway)"
124+
echo "Gateway=$(http_get ${url}ipv6/gateway)"
120125
fi
121126
log "Added IPv6 address ${address}/${prefix} on ${interface}."
122127
fi
@@ -130,7 +135,7 @@ process_interface() {
130135

131136
traverse_interfaces() {
132137
local url=$1
133-
set -- $(curl -LSsf ${url})
138+
set -- $(http_get -S ${url})
134139
if [[ " $* " =~ " mac " ]]; then
135140
process_interface ${url} "$*"
136141
else
@@ -145,7 +150,7 @@ traverse_interfaces() {
145150

146151
setup_from_metadata_service() {
147152
local sshkeys
148-
if sshkeys=$(curl -LSsf ${meta_base}public-keys) && test -n "${sshkeys}"; then
153+
if sshkeys=$(http_get -S ${meta_base}public-keys) && test -n "${sshkeys}"; then
149154
[ -d /root/.ssh ] || mkdir -m 0700 /root/.ssh
150155
[ -e /root/.ssh/authorized_keys ] || touch /root/.ssh/authorized_keys
151156
if ! grep -q "${sshkeys}" /root/.ssh/authorized_keys; then
@@ -154,7 +159,7 @@ setup_from_metadata_service() {
154159
fi
155160
fi
156161
local hostname
157-
if ! test -e /etc/hostname && hostname=$(curl -LSsf ${meta_base}hostname); then
162+
if ! test -e /etc/hostname && hostname=$(http_get -S ${meta_base}hostname); then
158163
echo "${hostname}" > /etc/hostname
159164
hostnamectl set-hostname "${hostname}"
160165
log "Hostname set to ${hostname} from metadata service."
@@ -176,7 +181,7 @@ digitalocean_synchronize() {
176181
local retry
177182
for retry in {1..20}; do
178183
log "Attempting to connect to metadata service ..."
179-
if curl -LSsf -m 1 ${meta_base} >/dev/null; then
184+
if http_get -S -m 1 ${meta_base} >/dev/null; then
180185
setup_from_metadata_service
181186
break
182187
else

0 commit comments

Comments
 (0)