-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·399 lines (313 loc) · 12.4 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#! /bin/bash
# Make sure script ends as soon as an error arises
set -e
#################
### VARIABLES ###
#################
date_YYYYMMDDHHMMSS=$(date +%Y%m%d%H%M%S) # Get the current date in YYYYMMDD format
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
#####################
### ERROR HANDLER ###
#####################
# Define error handler function
trap 'catch $? $BASH_COMMAND' EXIT
catch() {
if [ "$1" != "0" ]; then
# error handling goes here
echo "Error: $2: exit code $1"
echo
echo "Rolling back installation..."
if pipx list >/dev/null 2>&1 | grep -q 'froster'; then
pipx uninstall froster >/dev/null 2>&1
fi
# Restore (if any) backed up froster config files
if [[ -d ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak ]]; then
mv -f ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak ${XDG_CONFIG_HOME}/froster >/dev/null 2>&1
fi
# Restore (if any) backed up froster data files
if [[ -d ${XDG_DATA_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak ]]; then
mv -f ${XDG_DATA_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak ${XDG_DATA_HOME}/froster >/dev/null 2>&1
fi
rm -rf ${pwalk_path} >/dev/null 2>&1
rm -rf rclone-current-linux-*.zip rclone-v*/ >/dev/null 2>&1
echo "...done"
echo
echo "Installation failed!"
fi
}
spinner() {
pid=$1
spin='-\|/'
i=0
while kill -0 $pid 2>/dev/null; do
i=$(((i + 1) % 4))
# If we are in a github actions workflow, we don't want to print the spinner
if [ "$GITHUB_ACTIONS" != "true" ]; then
printf "\r${spin:$i:1}"
fi
sleep .1
done
# If we are in a github actions workflow, we don't want to print this return line
if [ "$GITHUB_ACTIONS" != "true" ]; then
printf "\r"
fi
}
#################
### FUNCTIONS ###
#################
# Check all needed apt dependencies to install froster
check_apt_dependencies() {
# Check if curl is installed
if [[ -z $(command -v curl) ]]; then
echo "Error: curl is not installed."
echo
echo "In most linux distros you can install the latest version of curl by running the following commands:"
echo " sudo apt update"
echo " sudo apt install -y curl"
echo
exit 1
fi
# Check if pipx is installed
if [[ -z $(command -v pipx) ]]; then
echo "Error: pipx is not installed."
echo
echo "Please install pipx"
echo "In most linux distros you can install the latest version of pipx by running the following commands:"
echo " sudo apt update"
echo " sudo apt install -y pipx"
echo " pipx ensurepath"
echo
exit 1
fi
# Check if gcc is installed
if [[ -z $(command -v gcc) ]]; then
echo "Error: gcc is not installed."
echo
echo "Please install gcc"
echo "In most linux distros you can install the latest version of gcc by running the following commands:"
echo " sudo apt update"
echo " sudo apt install -y gcc"
echo
exit 1
fi
# Check if lib32gcc-s1 is installed (pwalk compilation requirement)
if [[ $(dpkg -l lib32gcc-s1 >/dev/null 2>&1) ]]; then
echo "Error: lib32gcc-s1 is not installed."
echo
echo "Please install lib32gcc-s1"
echo "In most linux distros you can install the latest version of lib32gcc-s1 by running the following commands:"
echo " sudo apt update"
echo " sudo apt install -y lib32gcc-s1"
echo
exit 1
fi
# Check if git is installed (pipx installation requirement)
# TODO: Get rid of this requirement once froster is in PyPi repository
if [[ -z $(command -v git) ]]; then
echo "Error: git is not installed."
echo
echo "Please install git"
echo "In most linux distros you can install the latest version of git by running the following commands:"
echo " sudo apt update"
echo " sudo apt install -y git"
echo
exit 1
fi
# Check if unzip is installed (rclone requirement)
if [[ -z $(command -v unzip) ]]; then
echo "Error: unzip is not installed."
echo
echo "Please install unzip"
echo "In most linux distros you can install the latest version of unzip by running the following commands:"
echo " sudo apt update"
echo " sudo apt install -y unzip"
echo
exit 1
fi
# Check if fuse3 is installed
if [[ -z $(command -v fusermount3) ]]; then
echo "Error: fusermount3 is not installed."
echo
echo "Please install fuse3"
echo "In most linux distros you can install the latest version of fuse3 by running the following commands:"
echo " sudo apt update"
echo " sudo apt install -y fuse3"
echo
exit 1
fi
}
# Backup older installations (if any) but keep the froster-archive.json and config.ini files
backup_old_installation() {
echo
echo "Backing up older froster installation (if any)..."
# Back up (if any) older froster data files
if [[ -d ${XDG_DATA_HOME}/froster ]]; then
# Copy the froster directory to froster_YYYYMMDD.bak
cp -rf ${XDG_DATA_HOME}/froster ${XDG_DATA_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak
echo " Data back up at ${XDG_DATA_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak"
fi
# Back up (if any) older froster configurations
if [[ -d ${XDG_CONFIG_HOME}/froster ]]; then
# Move the froster config directory to froster.bak
cp -rf ${XDG_CONFIG_HOME}/froster ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak
echo " Config back up at ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak"
fi
echo "...older froster installation backed up"
# Check if froster is already installed, if so uninstall it
if which froster >/dev/null 2>&1; then
echo
echo "Uninstalling existing froster installation..."
if pip list | grep -q froster >/dev/null 2>&1; then
pip uninstall froster >/dev/null 2>&1 &
spinner $!
fi
if pipx list | grep -q froster >/dev/null 2>&1; then
pipx uninstall froster >/dev/null 2>&1 &
spinner $!
fi
echo "...froster uninstalled"
fi
# Keep the froster-archives.json file (if any)
if [[ -f ${XDG_DATA_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak/froster-archives.json ]]; then
# Create the froster directory if it does not exist
mkdir -p ${XDG_DATA_HOME}/froster
# Copy the froster-archives.json file to the data directory
cp -f ${XDG_DATA_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak/froster-archives.json ${XDG_DATA_HOME}/froster/froster-archives.json
fi
# Remove old files
rm -rf ${XDG_DATA_HOME}/froster
rm -rf ${XDG_CONFIG_HOME}/froster
rm -f ${HOME}/.local/bin/froster
rm -f ${HOME}/.local/bin/froster.py
rm -f ${HOME}/.local/bin/s3-restore.py
}
install_froster() {
# Ensure ~/.local/bin is in the PATH
pipx ensurepath >/dev/null 2>&1
# TODO: Update path once froster is in PyPi repository
# Get the repository and branch from the github environment variables (if any)
# or set the default repository and branch
if [[ -v REPO ]]; then
REPO="https://github.com/$REPO"
else
REPO="https://github.com/dirkpetersen/froster.git"
fi
BRANCH=${BRANCH:-"main"}
echo
echo "Installing latest version of froster from \"$REPO@$BRANCH\"..."
pipx install git+$REPO@$BRANCH >/dev/null 2>&1 &
spinner $!
# Keep the config.ini file (if any)
if [[ -f ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak/config.ini ]]; then
# Create the froster directory if it does not exist
mkdir -p ${XDG_CONFIG_HOME}/froster
# Copy the config file to the data directory
cp -f ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak/config.ini ${XDG_CONFIG_HOME}/froster/config.ini
fi
# Keep the froster-archives.json file (if any)
if [[ -f ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak/froster-archives.json ]]; then
# Create the froster directory if it does not exist
mkdir -p ${XDG_DATA_HOME}/froster
# Copy the froster-archives.json file to the data directory
cp -f ${XDG_CONFIG_HOME}/froster_${date_YYYYMMDDHHMMSS}.bak/froster-archives.json ${XDG_DATA_HOME}/froster/froster-archives.json
fi
echo "...froster installed"
}
install_pwalk() {
echo
echo "Installing third-party dependency: pwalk... "
# Variables of pwalk third-party tool froster is using
pwalk_commit=1df438e9345487b9c51d1eea3c93611e9198f173 # update this commit when new pwalk version released
pwalk_repository=https://github.com/fizwit/filesystem-reporting-tools/archive/${pwalk_commit}.tar.gz
pwalk_path=filesystem-reporting-tools-${pwalk_commit}
# Delete previous downloaded pwalk files (if any)
rm -rf ${pwalk_path} >/dev/null 2>&1
# Gather pwalk repository files
echo " Downloading pwalk files"
curl -s -L ${pwalk_repository} | tar xzf - >/dev/null 2>&1 &
spinner $!
# Compile pwalk tool and put exec file in froster's binaries folder
echo " Compiling pwalk"
gcc -pthread ${pwalk_path}/pwalk.c ${pwalk_path}/exclude.c ${pwalk_path}/fileProcess.c -o ${pwalk_path}/pwalk >/dev/null 2>&1 &
spinner $!
# Move pwalk to froster's binaries folder
echo " Moving pwalk to froster's binaries folder"
if [ -d "${XDG_DATA_HOME}/pipx" ]; then
mv ${pwalk_path}/pwalk ${XDG_DATA_HOME}/pipx/venvs/froster/bin/pwalk >/dev/null 2>&1
elif [ -d "${HOME}/.local/pipx" ]; then
mv ${pwalk_path}/pwalk ${HOME}/.local/pipx/venvs/froster/bin/pwalk >/dev/null 2>&1
elif [ -v PIPX_HOME ]; then
mv ${pwalk_path}/pwalk ${PIPX_HOME}/venvs/froster/bin/pwalk >/dev/null 2>&1
else
echo "Error: pipx installation path not found."
exit 1
fi
# Delete downloaded pwalk files
echo " Cleaning up pwalk installation files"
rm -rf ${pwalk_path} >/dev/null 2>&1
echo "...pwalk installed"
}
# Install rclone
install_rclone() {
echo
echo "Installing third-party dependency: rclone... "
# Check the architecture of the system
arch=$(uname -m)
# Get the rclone download URL based on the architecture
if [[ "$arch" == "x86_64" ]] || [[ "$arch" == "amd64" ]]; then
rclone_url='https://downloads.rclone.org/rclone-current-linux-amd64.zip'
elif [[ "$arch" == "arm" ]] || [[ "$arch" == "arm64" ]] || [[ "$arch" == "aarch64" ]]; then
rclone_url='https://downloads.rclone.org/rclone-current-linux-arm64.zip'
else
echo "Unsupported architecture: ${arch}"
exit 1
fi
# Remove previous downloaded zip file (if any)
rm -rf rclone-current-linux-*.zip rclone-v*/ >/dev/null 2>&1
# Download the rclone zip file
echo " Downloading rclone files"
curl -LO $rclone_url >/dev/null 2>&1 &
spinner $!
# Extract the zip file
echo " Extracting rclone files"
unzip rclone-current-linux-*.zip >/dev/null 2>&1 &
spinner $!
# Move rclone to froster's binaries folder
echo " Moving rclone to froster's binaries folder"
if [ -d "${XDG_DATA_HOME}/pipx" ]; then
mv rclone-v*/rclone ${XDG_DATA_HOME}/pipx/venvs/froster/bin/rclone >/dev/null 2>&1
elif [ -d "${HOME}/.local/pipx" ]; then
mv rclone-v*/rclone ${HOME}/.local/pipx/venvs/froster/bin/rclone >/dev/null 2>&1
elif [ -v PIPX_HOME ]; then
mv rclone-v*/rclone ${PIPX_HOME}/venvs/froster/bin/rclone >/dev/null 2>&1
else
echo "Error: pipx installation path not found."
exit 1
fi
# Remove the downloaded zip file
echo " Cleaning up rclone installation files"
rm -rf rclone-current-linux-*.zip rclone-v*/ >/dev/null 2>&1
echo "...rclone installed"
}
############
### CODE ###
############
# Check linux package dependencies
check_apt_dependencies
# Set rw permissions on anyone in file's group
umask 0002
# Backup old installation (if any)
backup_old_installation
# Install froster
install_froster
# Install pwalk
install_pwalk
# Install rclone
install_rclone
echo
echo "Installation complete!"
echo
echo "You will need to open a new terminal or refresh your current terminal session by running command:"
echo " source ~/.bashrc"
echo