forked from earthly/earthly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
earthly
executable file
·161 lines (137 loc) · 5.34 KB
/
earthly
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
set -e
bindir="$HOME/.earthly"
if [ ! -d "$bindir" ]; then
mkdir -p "$bindir"
fi
# get_realpath implements "readlink -f" for both Linux and MacOX (MacOS does not support readlink -f)
function get_realpath() {
TARGET_FILE="$1"
cd "$(dirname "$TARGET_FILE")"
TARGET_FILE=$(basename "$TARGET_FILE")
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=$(readlink "$TARGET_FILE")
cd "$(dirname "$TARGET_FILE")"
TARGET_FILE=$(basename "$TARGET_FILE")
done
PHYS_DIR="$(pwd -P)"
RESULT="$PHYS_DIR/$TARGET_FILE"
echo "$RESULT"
}
scriptname=$(basename "$0")
scriptpath=$(get_realpath "$0")
script_dir="$( cd "$( dirname "$scriptpath" )" &> /dev/null && pwd )"
last_check_state_path=/tmp/last-earthly-prerelease-check
last_flag_hash_path=/tmp/last-earthly-flag-hash
detect_frontend() {
if which docker 2>/dev/null >&2 && docker info 2>/dev/null >&2; then
echo "docker"
return
elif which podman 2>/dev/null >&2 && podman info 2>/dev/null >&2; then
echo "podman"
return
fi
echo >&2 "failed to detect docker/podman frontend"
exit 1
}
# some tests run ./earthly when no front-end is present; when EARTHLY_DISABLE_FRONTEND_DETECTION=true, skip detection.
if [ "$EARTHLY_DISABLE_FRONTEND_DETECTION" != "true" ]; then
frontend_bin=$(detect_frontend)
fi
get_latest_binary() {
"$frontend_bin" rm --force earthly_binary 2>/dev/null >&2 || true
"$frontend_bin" pull docker.io/earthly/earthlybinaries:prerelease >&2
"$frontend_bin" create --name earthly_binary docker.io/earthly/earthlybinaries:prerelease >&2
earth_bin_path=/earthly-linux-amd64
bk_platform=linux/amd64
if [ "$(uname)" == "Darwin" ]; then
if [ "$(uname -m)" == "arm64" ]; then
earth_bin_path=/earthly-darwin-arm64
bk_platform=linux/arm64
else
earth_bin_path=/earthly-darwin-amd64
fi
fi
"$frontend_bin" pull --platform="$bk_platform" docker.io/earthly/buildkitd:prerelease >&2
"$frontend_bin" cp earthly_binary:"$earth_bin_path" "$bindir/earthly-prerelease" >&2
"$frontend_bin" rm earthly_binary >&2
}
do_reset() {
rm -f "$last_check_state_path"
"$frontend_bin" stop earthly-buildkitd || true
"$frontend_bin" rm -f earthly-buildkitd || true
rm -f "$bindir/earthly-prerelease"
"$frontend_bin" rm --force earthly_binary 2>/dev/null || true
"$frontend_bin" rmi -f docker.io/earthly/buildkitd:prerelease || true
"$frontend_bin" rmi -f docker.io/earthly/earthlybinaries:prerelease || true
}
do_upgrade() {
do_reset
get_latest_binary
}
do_help() {
if ! command -v "$bindir/earthly-prerelease"; then
get_latest_binary
fi
echo "------------------- earthly prerelease script help -------------------"
echo "NAME:"
echo " $scriptname - A wrapper around the earthly binary that checks for updates once an hour"
echo ""
echo "COMMANDS:"
echo " reset Removes prerelease binary and associated docker containers"
echo " upgrade Forces a new check for the latest version"
echo ""
echo ""
echo "---------------------------- earthly help -----------------------------"
exec -a "$scriptname" "$bindir/earthly-prerelease" --help
}
case "$1" in
reset)
do_reset
;;
upgrade)
do_upgrade
;;
-h)
do_help
;; # using a ;& fallthrough won't work on macOS
--help)
do_help
;;
*)
last=$(cat "$last_check_state_path" 2>/dev/null || echo 0)
now=$(date +%s)
since=$(( now - last ))
earthly_version_flag_overrides_path="$script_dir/.earthly_version_flag_overrides"
last_flag_hash=$(cat "$last_flag_hash_path" 2>/dev/null || echo null)
flagoverride_hash=$(md5sum "$earthly_version_flag_overrides_path" | awk '{print $1}')
if [ -z "$COMP_LINE" ]; then
update="false"
if [ ! -x "$bindir/earthly-prerelease" ]; then
update="true"
elif [ "$last_flag_hash" != "$flagoverride_hash" ]; then
echo ".earthly_version_flag_overrides has changed since last run, checking for prerelease binaries. " \
"If you see an \"unable to set <flag-name>: invalid flag\" error, you may have to wait for the" \
"prerelease binary to be built by GHA on the main branch, before re-attempting a ./earthly upgrade" | fold >&2
update="true"
elif [ "$since" -ge 3600 ]; then
echo "checking for latest earthly prerelease binaries" >&2
update="true"
fi
if [ "$update" = "true" ] && [ "$EARTHLY_DISABLE_AUTO_UPDATE" != "true" ]; then
get_latest_binary
echo "Updated prerelease binary. Version:" >&2
"$bindir/earthly-prerelease" --version >&2
echo "$now" >"$last_check_state_path"
echo "$flagoverride_hash" >"$last_flag_hash_path"
fi
elif [ ! -x "$bindir/earthly-prerelease" ]; then
# prerelease doesn't exist, exit silently to avoid displaying errors during tab-completion
exit 0
fi
EARTHLY_VERSION_FLAG_OVERRIDES="$(tr -d '\n' < "$earthly_version_flag_overrides_path")"
export EARTHLY_VERSION_FLAG_OVERRIDES
exec -a "$scriptname" "$bindir/earthly-prerelease" "$@"
;;
esac