File tree Expand file tree Collapse file tree 4 files changed +35
-21
lines changed
Expand file tree Collapse file tree 4 files changed +35
-21
lines changed Original file line number Diff line number Diff line change 2727 "type" : " shell" ,
2828 "inline" : [" cloud-init status --long --wait" ]
2929 },
30- {
31- "type" : " shell" ,
32- "script" : " provisioners/wait-for-dpkg-lock.sh"
33- },
3430 {
3531 "type" : " shell" ,
3632 "scripts" : [
Original file line number Diff line number Diff line change 2626 "type" : " shell" ,
2727 "inline" : [" cloud-init status --long --wait" ]
2828 },
29- {
30- "type" : " shell" ,
31- "script" : " provisioners/wait-for-dpkg-lock.sh"
32- },
3329 {
3430 "type" : " shell" ,
3531 "scripts" : [
Original file line number Diff line number Diff line change 55
66set -e
77
8- sudo apt-get update
8+ #
9+ # Run apt-get update, but retry in case the lock is held by another process.
10+ #
11+ # A better way of handling apt races is the `-o DPkg::Lock::Timeout=X` option,
12+ # but it does not work with `apt-get update`.
13+ #
14+ # This function was added specifically for the `oci` backend, where our build
15+ # process conflicts with OCI's instance agent.
16+ #
17+ apt_update_with_retry () {
18+ local MAX_RETRIES=10
19+ local RETRY_DELAY=3
20+ local COUNT=0
21+ local LOGFILE=$( mktemp)
22+
23+ while [ $COUNT -lt $MAX_RETRIES ]; do
24+ set +e
25+ sudo apt-get update 2>&1 | tee " $LOGFILE "
26+ local EXIT_CODE=${PIPESTATUS[0]}
27+ set -e
28+
29+ if grep -q " Could not get lock" " $LOGFILE " ; then
30+ echo " apt lock file is held by another process. Retrying in $RETRY_DELAY seconds..."
31+ COUNT=$(( COUNT + 1 ))
32+ sleep $RETRY_DELAY
33+ else
34+ return $EXIT_CODE
35+ fi
36+ done
37+
38+ echo " apt-get update failed due to lock being held after $MAX_RETRIES attempts."
39+ return 1
40+ }
41+
42+ apt_update_with_retry
943sudo DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=60 dist-upgrade -y -q
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments