-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinstall.sh
executable file
·330 lines (293 loc) · 10.7 KB
/
install.sh
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/sh
BRANCH=${SCAF_SCRIPT_BRANCH:-main}
SCAF_SCRIPT_URL="https://raw.githubusercontent.com/sixfeetup/scaf/${BRANCH}/scaf"
TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmp')/$(echo -n "$(date +%s%N)" | md5sum | awk '{print $1}')
mkdir -p "$TEMP_DIR"
TEMP_DOWNLOAD="$TEMP_DIR/scaf"
PREFERRED_BIN_FOLDER=${PREFERRED_BIN_FOLDER:-"${HOME}/.local/bin"}
DESTINATION="${PREFERRED_BIN_FOLDER}/scaf"
# These are dependencies that we depend on the user to have installed:
dependencies="bash>=4.0 curl>=7.8.0 make>=3.8 python3>=3.6 docker>=19.0 git>=2.0 rsync>=3.2.0"
# ^^ Must be in the format of "name>=version" or just "name" if no version is required.
ensure_bin_folder() {
if [ ! -d "$PREFERRED_BIN_FOLDER" ]; then
echo "Creating $PREFERRED_BIN_FOLDER..."
mkdir -p $PREFERRED_BIN_FOLDER
fi
if [ ! -w "$PREFERRED_BIN_FOLDER" ]; then
echo "You don't have write permission to $PREFERRED_BIN_FOLDER."
echo "Please run the script as a user who has write permission to $PREFERRED_BIN_FOLDER"
echo "Or set PREFERRED_BIN_FOLDER to a writable directory."
exit 1
fi
if [ -z "$PATH" ] || [ -z "$HOME" ] || ! echo $PATH | grep -qE ":?$PREFERRED_BIN_FOLDER:?"; then
echo "Your PATH is missing $PREFERRED_BIN_FOLDER."
echo "Please add the following line to your shell profile file (e.g. ~/.bashrc, ~/.zshrc, etc.)"
echo "export PATH=\$PATH:$PREFERRED_BIN_FOLDER"
echo "Or you can set PREFERRED_BIN_FOLDER to a writable directory that is already in your PATH."
exit 1
fi
}
command_exists() {
command -v "$1" > /dev/null 2>&1
}
version_compare() {
installed="$1"
required="$2"
IFS='.'
set -- $installed
installed_parts="$@"
set -- $required
required_parts="$@"
unset IFS
i=1
for req in $required_parts; do
inst=$(echo "$installed_parts" | cut -d' ' -f$i)
if [ "${inst:-0}" -lt "$req" ]; then
return 1
elif [ "${inst:-0}" -gt "$req" ]; then
return 0
fi
i=$((i + 1))
done
return 0
}
version_satisfies() {
name="$1"
required="$2"
installed_version=$("$name" --version | grep -oE '[0-9]+(\.[0-9]+)+' | head -n 1)
if [ -z "$installed_version" ]; then
return 1
fi
if ! version_compare "$installed_version" "$required"; then
return 1
fi
return 0
}
check_top_level_dependencies() {
missing=""
for dep in $dependencies; do
name=$(echo "$dep" | cut -d'>' -f1)
version=$(echo "$dep" | grep -o '[0-9.]\+$')
if ! command_exists "$name"; then
missing="$missing $name"
elif [ -n "$version" ]; then
if ! version_satisfies "$name" "$version"; then
detected_version=$("$name" --version | grep -oE '[0-9]+(\.[0-9]+)+' | head -n 1)
missing="$missing\n$name>=$version but found $detected_version"
fi
fi
done
# Check if python commands exist
if command -v python &>/dev/null || command -v python3 &>/dev/null; then
:
else
missing="$missing python3"
fi
if [ -z "$missing" ]; then
echo "All top-level dependencies are installed and meet the required versions."
return 0
fi
echo "The following dependencies are missing or do not meet the required versions:"
echo $missing
os=$(uname -s 2>/dev/null || echo "Unknown")
if [ "$os" = "Linux" ] && [ -f /proc/version ] && grep -qi microsoft /proc/version; then
os="WSL"
elif [ "$os" = "Unknown" ] && [ "${OSTYPE#msys}" != "$OSTYPE" ]; then
os="Windows"
fi
# TODO: I wonder if we should look for nix, brew, apt-get, yum, etc. and provide instructions
# for those rather then strictly by OS.
# Process $missing line by line instead of word by word.
echo "$missing" | while IFS= read -r dep; do
[ -z "$dep" ] && continue # Skip empty lines.
case $dep in
bash*)
echo "Please install bash for consistent shell execution."
;;
curl*)
echo "Please install curl:"
echo " - Ubuntu/Debian: sudo apt-get update && sudo apt-get install --only-upgrade curl"
echo " - CentOS/Fedora: sudo yum install curl || sudo yum upgrade curl"
echo " - macOS: brew install curl || brew upgrade curl"
echo " - Windows: Download from https://curl.se/windows/"
;;
make*)
echo "Please install make:"
echo " - Ubuntu/Debian: sudo apt-get update && sudo apt-get install --only-upgrade make"
echo " - CentOS/Fedora: sudo yum install make || sudo yum upgrade make"
echo " - macOS: xcode-select --install"
echo " or: brew install make"
echo " - Windows: Install MinGW or use WSL"
;;
python3*)
echo "Please install python3:"
echo " - Ubuntu/Debian: sudo apt-get update && sudo apt-get install --only-upgrade python3"
echo " - CentOS/Fedora: sudo yum install python3 || sudo yum upgrade python3"
echo " - macOS: brew install python || brew upgrade python"
echo " - Windows: Download from https://www.python.org/downloads/windows/"
;;
rsync*)
echo "Please install rsync:"
echo " - Ubuntu/Debian: sudo apt-get update && sudo apt-get install --only-upgrade rsync"
echo " - CentOS/Fedora: sudo yum install rsync || sudo yum upgrade rsync"
echo " - macOS: brew install rsync || brew upgrade rsync"
echo " - Windows: Can be obtained from https://www.itefix.net/cwrsync"
echo " or: Install WSL and usesudo apt-get install rsync"
;;
docker*)
case $os in
"Darwin")
echo "Please install Docker Desktop for macOS:"
echo "Download from https://www.docker.com/products/docker-desktop"
;;
"Linux")
echo "Please install Docker:"
echo "Ubuntu: sudo apt-get update && sudo apt-get install docker.io"
echo "CentOS: sudo yum install -y docker && sudo systemctl start docker"
;;
"Windows"|"WSL")
echo "Please install Docker Desktop for Windows:"
echo "Download from https://docs.docker.com/desktop/install/windows-install/"
;;
*)
echo "Please install Docker. Visit https://docs.docker.com/engine/install/ for instructions."
;;
esac
;;
*)
echo "Please install $dep."
;;
esac
echo
done
exit 1
}
check_git_config() {
git config --get user.name > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Git user.name is not set. Please run:"
echo " git config --global user.name \"Your Name\""
exit 1
fi
git config --get user.email > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Git user.email is not set. Please run:"
echo " git config --global user.email \"[email protected]\""
exit 1
fi
}
detect_os_and_arch() {
os=$(uname -s)
arch=$(uname -m)
case "$os" in
Linux*)
case "$arch" in
x86_64) os_arch="linux-amd64" ;;
aarch64) os_arch="linux-arm64" ;;
*) os_arch="linux-unknown" ;;
esac
;;
Darwin*)
case "$arch" in
x86_64) os_arch="darwin-amd64" ;;
arm64) os_arch="darwin-arm64" ;;
*) os_arch="darwin-unknown" ;;
esac
;;
*)
os_arch="unknown"
;;
esac
echo $os_arch
}
move_to_bin_folder() {
DEST=${2:-$PREFERRED_BIN_FOLDER}
echo "Moving $1 to ${DEST}..."
# if the DEST folder is not writable, use sudo to move the file
if [ -w $DEST ]; then
mv $1 $DEST
else
sudo mv $1 $DEST
fi
}
install_kubectl() {
echo "Installing kubectl..."
os_arch=$(detect_os_and_arch)
case $os_arch in
"linux-amd64"|"darwin-amd64"|"darwin-arm64"|"linux-arm64")
os=${os_arch%-*}
arch=${os_arch#*-}
url="https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/${os}/${arch}/kubectl"
curl -LO $url
chmod +x ./kubectl
move_to_bin_folder ./kubectl
;;
*)
echo "Unsupported OS or architecture: $os_arch"
exit 1
;;
esac
kubectl version
}
install_kind() {
echo "Installing kind..."
os_arch=$(detect_os_and_arch)
case $os_arch in
"linux-amd64"|"darwin-amd64"|"darwin-arm64"|"linux-arm64"|"windows-amd64")
curl -Lo ./kind "https://kind.sigs.k8s.io/dl/v0.23.0/kind-$os_arch"
chmod +x ./kind
move_to_bin_folder ./kind
;;
*)
echo "Unsupported OS or architecture: $os_arch"
exit 1
;;
esac
kind version
}
install_tilt() {
echo "Installing tilt..."
curl -fsSL https://raw.githubusercontent.com/tilt-dev/tilt/master/scripts/install.sh | bash
tilt version
}
install_scaf() {
# Install/re-install scaf
if [ -f "$TEMP_DOWNLOAD" ]; then
rm $TEMP_DOWNLOAD
fi
# Download and install scaf
echo "Downloading scaf from $SCAF_SCRIPT_URL..."
curl -L $SCAF_SCRIPT_URL -o $TEMP_DOWNLOAD
if [ -f "$TEMP_DOWNLOAD" ]; then
chmod +x $TEMP_DOWNLOAD
echo "Moving scaf to $DESTINATION..."
move_to_bin_folder $TEMP_DOWNLOAD $(dirname $DESTINATION)
if [ -f "$DESTINATION" ]; then
echo "🎉 scaf installed successfully at $DESTINATION 🎉"
else
echo "🧨 Failed to move scaf to the destination. 🧨"
exit 1
fi
else
echo "🧨 Failed to download scaf. 🧨"
exit 1
fi
}
# Start the installation process
echo "Installing scaf from $SCAF_SCRIPT_URL for the $BRANCH branch..."
[ -n "$DEBUG" ] && echo "Ensuring bin folder exists"
ensure_bin_folder
[ -n "$DEBUG" ] && echo "Checking top level dependencies"
check_top_level_dependencies
[ -n "$DEBUG" ] && echo "Checking Git Config"
check_git_config
for tool in kubectl kind tilt; do
if ! command_exists $tool; then
echo "$tool is not installed."
install_$tool
else
[ -n "$DEBUG" ] && echo "$tool is already installed."
fi
done
install_scaf