|
| 1 | +#!/bin/sh |
| 2 | +# vim:ts=4:sts=4:sw=4:et |
| 3 | +# |
| 4 | +# Author: Hari Sekhon |
| 5 | +# Date: 2020-06-02 17:43:35 +0100 (Tue, 02 Jun 2020) |
| 6 | +# |
| 7 | +# https://github.com/harisekhon/devops-python-tools |
| 8 | +# |
| 9 | +# License: see accompanying Hari Sekhon LICENSE file |
| 10 | +# |
| 11 | +# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish |
| 12 | +# |
| 13 | +# https://www.linkedin.com/in/harisekhon |
| 14 | +# |
| 15 | + |
| 16 | +# Designed to bootstrap all CI systems with retries to make sure the networking, package lists and package repos works before proceeding |
| 17 | +# |
| 18 | +# Minimizes CI build failures due to temporary networking blips, which happens more often than you would think when you have a large number of CI builds across a lot of disparate systems |
| 19 | + |
| 20 | +set -eu |
| 21 | +[ -n "${DEBUG:-}" ] && set -x |
| 22 | + |
| 23 | +max_tries=10 |
| 24 | +interval=60 # secs |
| 25 | + |
| 26 | +sudo="" |
| 27 | +# EUID undefined in posix sh |
| 28 | +#[ $EUID = 0 ] || sudo=sudo |
| 29 | +[ "$(whoami)" = root ] || sudo=sudo |
| 30 | + |
| 31 | +retry(){ |
| 32 | + # no local in posix sh |
| 33 | + count=0 |
| 34 | + while true; do |
| 35 | + # no let or bare (()) in posix sh, must discard output rather than execute it |
| 36 | + _=$((count+=1)) |
| 37 | + printf "%s try %d: " "$(date '+%F %T')" "$count" |
| 38 | + echo "$*" |
| 39 | + "$@" && |
| 40 | + break; |
| 41 | + echo |
| 42 | + if [ $count -ge $max_tries ]; then |
| 43 | + echo "$count tries failed, aborting..." |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + echo "sleeping for $interval secs before retrying" |
| 47 | + sleep "$interval" |
| 48 | + echo |
| 49 | + done |
| 50 | +} |
| 51 | + |
| 52 | +if [ "$(uname -s)" = Darwin ]; then |
| 53 | + echo "Bootstrapping Mac" |
| 54 | + # removing adjacent dependency to be able to curl from github to avoid submodule circular dependency (git / submodule / install git & make) |
| 55 | + #retry "$srcdir/install_homebrew.sh" |
| 56 | + if command -v brew 2>&1; then |
| 57 | + retry $sudo brew update |
| 58 | + fi |
| 59 | +elif [ "$(uname -s)" = Linux ]; then |
| 60 | + echo "Bootstrapping Linux" |
| 61 | + if type -P apk >/dev/null 2>&1; then |
| 62 | + retry $sudo apk update |
| 63 | + retry $sudo apk add --no-progress bash git make |
| 64 | + elif type apt-get >/dev/null 2>&1; then |
| 65 | + retry $sudo apt-get update -q |
| 66 | + retry $sudo apt-get install -qy git make |
| 67 | + elif type yum >/dev/null 2>&1; then |
| 68 | + #retry $sudo yum makecache |
| 69 | + retry $sudo yum install -qy git make |
| 70 | + else |
| 71 | + echo "Package Manager not found on Linux, cannot bootstrap" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | +else |
| 75 | + echo "Only Mac & Linux are supported for conveniently bootstrapping all install scripts at this time" |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +#retry make init |
| 80 | + |
| 81 | +# not calling make because in some CI systems we call 'make ci' which includes retries but in others with more restrictive build minutes we only run 'make' for a single shot build |
| 82 | +# |
| 83 | +#make |
0 commit comments