@@ -35,6 +35,11 @@ log() {
35
35
echo " [$( date) ]" " $@ " >&2
36
36
}
37
37
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
+
38
43
netmask_to_prefix () {
39
44
local pfx=0 cmp msk
40
45
for cmp in ${1// ./ } 0; do
@@ -76,8 +81,8 @@ update_shadow_if_changed() {
76
81
process_interface () {
77
82
local url=$1
78
83
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)
81
86
local interface=
82
87
local cand path
83
88
for cand in $( ls /sys/class/net) ; do
@@ -97,26 +102,26 @@ process_interface() {
97
102
[Network]
98
103
EOF
99
104
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) )
102
107
echo " Address=${address} /${prefix} "
103
108
if [ " ${type} " != " private" ]; then
104
- echo " Gateway=$( curl -Lsf ${url} ipv4/gateway) "
109
+ echo " Gateway=$( http_get ${url} ipv4/gateway) "
105
110
fi
106
111
log " Added IPv4 address ${address} /${prefix} on ${interface} ."
107
112
fi
108
113
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) )
111
116
echo " Address=${address} /${prefix} "
112
117
log " Added Anchor IPv4 address ${address} /${prefix} on ${interface} ."
113
118
fi
114
119
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)
117
122
echo " Address=${address} /${prefix} "
118
123
if [ " ${type} " != " private" ]; then
119
- echo " Gateway=$( curl -Lsf ${url} ipv6/gateway) "
124
+ echo " Gateway=$( http_get ${url} ipv6/gateway) "
120
125
fi
121
126
log " Added IPv6 address ${address} /${prefix} on ${interface} ."
122
127
fi
@@ -130,7 +135,7 @@ process_interface() {
130
135
131
136
traverse_interfaces () {
132
137
local url=$1
133
- set -- $( curl -LSsf ${url} )
138
+ set -- $( http_get -S ${url} )
134
139
if [[ " $* " =~ " mac " ]]; then
135
140
process_interface ${url} " $* "
136
141
else
@@ -145,7 +150,7 @@ traverse_interfaces() {
145
150
146
151
setup_from_metadata_service () {
147
152
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
149
154
[ -d /root/.ssh ] || mkdir -m 0700 /root/.ssh
150
155
[ -e /root/.ssh/authorized_keys ] || touch /root/.ssh/authorized_keys
151
156
if ! grep -q " ${sshkeys} " /root/.ssh/authorized_keys; then
@@ -154,7 +159,7 @@ setup_from_metadata_service() {
154
159
fi
155
160
fi
156
161
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
158
163
echo " ${hostname} " > /etc/hostname
159
164
hostnamectl set-hostname " ${hostname} "
160
165
log " Hostname set to ${hostname} from metadata service."
@@ -176,7 +181,7 @@ digitalocean_synchronize() {
176
181
local retry
177
182
for retry in {1..20}; do
178
183
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
180
185
setup_from_metadata_service
181
186
break
182
187
else
0 commit comments