-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfleet.sh
More file actions
executable file
·149 lines (137 loc) · 4.76 KB
/
Copy pathfleet.sh
File metadata and controls
executable file
·149 lines (137 loc) · 4.76 KB
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
#!/bin/sh
set -eu
REPOSITORY="${FLEET_REPOSITORY:-extoci/fleet}"
VERSION="${FLEET_VERSION:-latest}"
INSTALL_DIR="${FLEET_INSTALL_DIR:-}"
fail() {
printf 'fleet installer: %s\n' "$*" >&2
exit 1
}
command -v uname >/dev/null 2>&1 || fail "uname is required"
command -v install >/dev/null 2>&1 || fail "install is required"
command -v tar >/dev/null 2>&1 || fail "tar is required"
if [ -z "$INSTALL_DIR" ]; then
preferred="$HOME/.local/bin"
case ":$PATH:" in
*":$preferred:"*) INSTALL_DIR="$preferred" ;;
*)
old_ifs=$IFS
IFS=:
for directory in $PATH; do
case "$directory" in
"$HOME"/*|/usr/local/bin|/opt/homebrew/bin)
if [ -d "$directory" ] && [ -w "$directory" ]; then
INSTALL_DIR="$directory"
break
fi
;;
esac
done
IFS=$old_ifs
INSTALL_DIR="${INSTALL_DIR:-$preferred}"
;;
esac
fi
previous_version=""
if [ -x "$INSTALL_DIR/fleet" ]; then
previous_version="$("$INSTALL_DIR/fleet" --version 2>/dev/null | awk 'NR == 1 { print $2 }' || true)"
fi
case "$(uname -s):$(uname -m)" in
Darwin:arm64) target="aarch64-apple-darwin" ;;
Darwin:x86_64) target="x86_64-apple-darwin" ;;
Linux:aarch64|Linux:arm64) target="aarch64-unknown-linux-gnu" ;;
Linux:x86_64|Linux:amd64) target="x86_64-unknown-linux-gnu" ;;
*) fail "unsupported platform: $(uname -s) $(uname -m)" ;;
esac
temporary="$(mktemp -d 2>/dev/null || mktemp -d -t fleet)"
installing=""
cleanup() {
rm -rf "$temporary"
if [ -n "$installing" ]; then
rm -f "$installing"
fi
}
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
if [ -n "${FLEET_BINARY:-}" ]; then
[ -x "$FLEET_BINARY" ] || fail "FLEET_BINARY is not executable: $FLEET_BINARY"
source_binary="$FLEET_BINARY"
else
command -v curl >/dev/null 2>&1 || fail "curl is required"
archive="fleet-$target.tar.gz"
if [ "$VERSION" = latest ]; then
base="https://github.com/$REPOSITORY/releases/latest/download"
else
base="https://github.com/$REPOSITORY/releases/download/$VERSION"
fi
printf 'Downloading Fleet for %s...\n' "$target"
curl -fL --proto '=https' --tlsv1.2 "$base/$archive" -o "$temporary/$archive"
curl -fL --proto '=https' --tlsv1.2 "$base/$archive.sha256" -o "$temporary/$archive.sha256"
expected="$(awk '{print $1}' "$temporary/$archive.sha256")"
case "$expected" in
*[!0-9a-fA-F]*|'') fail "release checksum is invalid" ;;
esac
if command -v sha256sum >/dev/null 2>&1; then
actual="$(sha256sum "$temporary/$archive" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
actual="$(shasum -a 256 "$temporary/$archive" | awk '{print $1}')"
elif command -v openssl >/dev/null 2>&1; then
actual="$(openssl dgst -sha256 "$temporary/$archive" | awk '{print $NF}')"
else
fail "sha256sum, shasum, or openssl is required to verify Fleet"
fi
[ "$expected" = "$actual" ] || fail "release checksum did not match"
tar -xzf "$temporary/$archive" -C "$temporary"
source_binary="$temporary/fleet"
[ -x "$source_binary" ] || fail "release archive did not contain an executable named fleet"
fi
mkdir -p "$INSTALL_DIR"
installing="$INSTALL_DIR/.fleet.install.$$"
install -m 755 "$source_binary" "$installing"
mv "$installing" "$INSTALL_DIR/fleet"
installing=""
handoff_output=""
handoff_status=0
handoff_output="$("$INSTALL_DIR/fleet" __complete-update --from "${previous_version:-0.0.0}" 2>&1)" || handoff_status=$?
if [ "$handoff_status" -ne 0 ]; then
case "$handoff_output" in
*"unrecognized subcommand"*|*"unrecognized subcommand '__complete-update'"*)
printf 'Installed Fleet without managed-setup handoff (this release predates it).\n' >&2
;;
*)
printf '%s\n' "$handoff_output" >&2
fail "Fleet was installed, but managed setup could not be refreshed"
;;
esac
fi
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*)
case "${SHELL:-}" in
*/zsh) rc="$HOME/.zshrc" ;;
*/bash) rc="$HOME/.bashrc" ;;
*) rc="" ;;
esac
if [ -n "$rc" ]; then
if [ "$INSTALL_DIR" = "$HOME/.local/bin" ]; then
line='export PATH="$HOME/.local/bin:$PATH"'
else
line="export PATH=\"$INSTALL_DIR:\$PATH\""
fi
if ! grep -F "$line" "$rc" >/dev/null 2>&1; then
{
printf '\n# Fleet installer\n'
printf '%s\n' "$line"
} >>"$rc"
fi
printf 'Added %s to PATH in %s for future terminals.\n' "$INSTALL_DIR" "$rc"
printf 'This shell has no writable user directory on PATH; to use Fleet here, run:\n'
printf ' export PATH="%s:$PATH"\n' "$INSTALL_DIR"
else
printf 'Add %s to PATH before running Fleet.\n' "$INSTALL_DIR"
fi
;;
esac
printf 'Fleet installed at %s/fleet\n' "$INSTALL_DIR"
printf 'Run: fleet init # on the captain\n'
printf ' or: fleet join # on a member\n'