-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.sh
executable file
·664 lines (573 loc) · 20.2 KB
/
setup.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#!/usr/bin/env bash
#
# venvutil_setup.sh - Setup and configure venvutil.
#
# This script installs and configures the venvutil tools and utilities.
# It supports installation and removal operations, although the removal and rollback functionality
# is not yet implemented.
#
# Usage:
# venvutil_setup.sh [-d directory] [-r] [-v] {install|remove}
#
# Options:
# -d directory Specify the directory path where venvutil will be installed.
# The default is $HOME/local
# -r Remove venvutil tools and utilities from the system.
# The location will be taken from the config.sh file in your $HOME/.venvutil
# directory.
# -v Enable verbose logging.
# -h Show this help message
#
# Description:
# Install venvutil tools and utilities from the cloned repository.
# Configure venvutil for use.
#
# Output:
# Initializes the environment, sets up venvutil, and configures it for use.
#
# Exceptions:
# Functions may return specific error codes based on their internal logic.
# If an error occurs during execution, the script will remove itself from the system.
#
# Initialization:
# Ensure the script is executed in a bash shell.
# Check if the script is already running and exit if it is.
#
# Configuration:
# Load configuration variables from the config.sh file in the .venvutil directory.
# Use the default install location $HOME/local/venvutil if the configuration file is not found.
#
# Environment Variables:
# DEBUG_SETUP: If set to "ON", enables debug mode for the script.
#
# NOTE: Yeah I do know this looks a bit like a Python script... just changing styles a bit
# to try a different way of structuring things.
#
# Dependencies:
# - bash 4.0 or higher
# - Python 3.10 or higher
#
# Author:
# Michael Sullivan <[email protected]>
# https://unixwzrd.ai/
# https://github.com/unixwzrd
#
# License:
# Apache License, Version 2.0
#
[ "${DEBUG_SETUP:-""}" = "ON" ] && set -x
set -euo pipefail
# Initialize script variables
THIS_SCRIPT=$(readlink -f "${BASH_SOURCE[$((${#BASH_SOURCE[@]} -1))]}")
MY_NAME="$(basename "${THIS_SCRIPT}")"
MY_PATH="$(dirname "${THIS_SCRIPT}")"
[[ "${BASH_VERSINFO[0]}" -lt 4 ]] \
&& echo "($MY_NAME) ERROR: This script requires Bash version 4 or higher." >&2 \
&& exit 75
# Default values
PKG_NAME="DEFAULT"
PKG_VERSION=""
INSTALL_BASE=""
INSTALL_CONFIG="$HOME/.${PKG_NAME}"
INSTALL_MANIFEST="manifest.lst"
ACTION=""
VERBOSE=false
declare -g -A pkg_config_values=()
declare -g -a pkg_config_set_vars=()
declare -g -a pkg_config_desc_vars=()
# Logging function
log_message() {
local message_level="$1"; shift
local message_out="$*"
# declare -A message_class=(
# ["INFO"]=30
# ["WARNING"]=40
# ["ERROR"]=50
# ["CRITICAL"]=60
# )
# Print message to STDERR and log file
if [ "$VERBOSE" = true ]; then
echo "($MY_NAME) [$message_level] $message_out" 2>&1 | tee -a "$INSTALL_CONFIG/install.log" >&2
return 0
fi
# Write message to log file
echo "($MY_NAME) [$message_level] $message_out" >> "$INSTALL_CONFIG/install.log" 2>&1
}
# Function to display help extracted from the script
display_help_and_exit() {
# Initialize a variable to hold the help text
local message=$1
local help_text=""
# Read the script file line by line
help_text+="$message"$'\n\n'
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip the shebang line
if [[ "$line" =~ ^#!/ ]]; then
continue
fi
# Stop at the first non-comment line (end of help section)
if [[ ! "$line" =~ ^# ]]; then
break
fi
# Remove leading '#' and spaces
help_text+="${line#\# }"$'\n'
done < "$0"
printf "%s" "$help_text" | ${PAGER:-cat}
exit 0
}
# Parse command-line arguments
parse_arguments() {
while getopts ":d:vh" opt; do
case $opt in
d) INSTALL_BASE="$OPTARG" ;;
v) VERBOSE=true ;;
h) display_help_and_exit "Usage: $MY_NAME [options] {install|remove|rollback}" ;;
\?) echo "Invalid option -$OPTARG" >&2; exit 1 ;;
:) echo "Option -$OPTARG requires an argument." >&2; exit 1 ;;
esac
done
shift $((OPTIND -1))
# Ensure at least one action is specified
ACTION="${1:-}"
if [ -z "$ACTION" ]; then
display_help_and_exit "Usage: $MY_NAME [options] {install|remove|rollback}"
fi
return 0
}
# Initialization
initialization() {
load_pkg_config
# Set PKG_NAME early to load config
PKG_NAME=${Name:-${PKG_NAME:-"DEFAULT"}}
# Set default values if not already set
PKG_VERSION=${PKG_VERSION:-$Version}
INSTALL_BASE=${INSTALL_BASE:-$prefix}
INSTALL_CONFIG="$HOME/.${PKG_NAME}"
create_pkg_config_dir
# Set default manifest path
INSTALL_MANIFEST="$MY_PATH/manifest.lst"
log_message "INFO" "Configuring $PKG_NAME for initialization..."
# Parse manifest metadata
parse_manifest_metadata
return 0
}
expand_variables() {
local input="$1"
# Validate the input: Allow variable references and valid values
if [[ ! "$input" =~ ^[A-Za-z0-9_\$\{\}]+([[:space:]]*[-+*/]?[[:space:]]*[A-Za-z0-9_\$\{\}]+)*$ ]]; then
return 1
fi
# Sanitize the input by escaping special characters if necessary
# For example, you might want to escape quotes or backslashes
sanitized_input=$(echo "$input" | sed 's/[&;|<>]/\\&/g')
# Use eval to expand variables safely, handling both ${var} and $var notation
eval "echo \"$sanitized_input\""
}
# Load package configuration from .cf file
load_pkg_config() {
local config_file="$MY_PATH/setup.cf"
if [ ! -f "$config_file" ]; then
echo "ERROR: Configuration file $config_file not found." >&2
exit 2
fi
local key=""
local value=""
while IFS= read -r line || [[ -n "$line" ]]; do
# Trim leading and trailing whitespace
line="$(sed 's/^[[:space:]]*//; s/[[:space:]]*$//' <<< "$line")"
# Skip comments and blank lines
[[ -z "$line" ]] && continue
[[ "$line" =~ ^# ]] && continue
# Check for Key=Value pattern
if [[ "$line" =~ ^([A-Za-z_]+)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
if ! value=$(expand_variables "${BASH_REMATCH[2]}"); then
echo "($MY_NAME) WARNING: Invalid variable assignment: '$line' - skipping." >&2
continue
fi
# Set shell variable
declare -g "$key"="$value"
# Append or initialize array entry
if [[ -z "${pkg_config_values[$key]:-}" ]]; then
pkg_config_values[$key]="$value"
pkg_config_set_vars+=("$key")
else
pkg_config_values[$key]+=$'\n'"$value"
fi
continue
fi
# Check for Key: Value pattern
if [[ "$line" =~ ^([A-Za-z_]+):[[:space:]]*(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
# Set shell variable
declare -g "$key"="$value"
# Append or initialize array entry
if [[ -z "${pkg_config_values[$key]:-}" ]]; then
pkg_config_values[$key]="$value"
pkg_config_desc_vars+=("$key")
else
pkg_config_values[$key]+=$'\n'"$value"
fi
continue
fi
# If a line doesn't match either pattern, assume it’s an additional value for the last key
# If no previous key is known, just ignore.
if [[ -n "$key" ]]; then
# Treat as another line for the current key
declare -g "$key"="${!key:-}\n${line}"
pkg_config_values[$key]+=$'\n'"$line"
fi
done < "$config_file"
}
# Create package configuration directory
create_pkg_config_dir() {
if [ ! -d "${INSTALL_CONFIG}" ]; then
mkdir -p "${INSTALL_CONFIG}"
mkdir -p "$INSTALL_CONFIG/log" "$INSTALL_CONFIG/freeze"
log_message "INFO" "Created ${INSTALL_CONFIG} directories..."
fi
# Create application configuration directory
return 0
}
# Parse manifest metadata
parse_manifest_metadata() {
if [ ! -f "$INSTALL_MANIFEST" ]; then
echo "ERROR: Manifest file $INSTALL_MANIFEST not found." >&2
exit 2
fi
echo "Parsing manifest metadata..." >&2
local manifest_file="$INSTALL_MANIFEST"
while IFS='| ' read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^#.*$ ]] && continue # Skip comments
[[ -z "$line" ]] && break # Stop at first blank line (end of metadata)
if [[ "$line" =~ ^[A-Za-z_]+=.*$ ]]; then
eval "$line"
fi
done < "$manifest_file"
return 0
}
install_conda() {
log_message "INFO" "Installing conda..."
# Check if conda is already installed
if command -v conda &> /dev/null; then
log_message "INFO" "Conda is already installed. Skipping installation."
return 0
fi
# Find host OS and architecture
local OS ARCH
OS=$(uname -s)
[ "$OS" == "Darwin" ] && OS="MacOSX"
[ "$OS" == "Linux" ] && OS="Linux"
ARCH=$(uname -m)
ARCH=${ARCH//aarch64/arm64}
# Download and install conda
log_message "INFO" "Downloading and installing Conda..."
local INSTALLER_URL="https://repo.anaconda.com/miniconda/Miniconda3-latest-${OS}-${ARCH}.sh"
curl -k -O "$INSTALLER_URL"
# do a non-destructive install
bash "Miniconda3-latest-${OS}-${ARCH}.sh" -b -u
rm "Miniconda3-latest-${OS}-${ARCH}.sh"
# Activate the Conda installation
# shellcheck disable=SC1091
source "${HOME}/miniconda3/bin/activate"
# Initialize conda for our shell
conda init "$(basename "${SHELL}")"
log_message "INFO" "Conda installed successfully, checking for updates..."
conda update -n base -c defaults conda -y
# Because Red Hat Enterprise Linux defines, sets it, but doesn't export it BASHSOURCED...
# Prevents /etc/bashrc from being sourced again on Red Hat Enterprise Linux.
export BASHSOURCED=Y
# So we don't recurse.
export CONDA_INSTALL_COMPLETE=Y
SHELL=$(which "$(basename "$SHELL")")
# Wheeeeee!!!!!!
exec "$SHELL" -l -c "${THIS_SCRIPT} ${ACTION}"
return 0
}
# Write package information
write_pkg_info() {
install_log="${INSTALL_CONFIG}/${PKG_NAME}.pc"
INSTALL_DATE=$(date '+%Y-%m-%d %H:%M:%S')
log_message "INFO" "Package Information: Name=$PKG_NAME, Version=$PKG_VERSION, Date=$INSTALL_DATE"
echo "# Package Information: Name=$PKG_NAME, Version=$PKG_VERSION, Date=$INSTALL_DATE" > "${install_log}"
# shellcheck disable=SC2068
for key in ${pkg_config_set_vars[@]}; do
echo "$key=${pkg_config_values[$key]}" >> "${install_log}"
done
# shellcheck disable=SC2068
for key in ${pkg_config_desc_vars[@]}; do
echo "$key: ${pkg_config_values[$key]}" >> "${install_log}"
done
return 0
}
# Check dependencies
check_deps() {
# Check for Bash version 4+
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
log_message "ERROR" "$MY_NAME requires Bash version 4 or higher."
exit 75
fi
# Check Operating System (Linux or macOS)
if [ "$(uname -s)" != "Darwin" ] && [ "$(uname -s)" != "Linux" ]; then
log_message "ERROR" "$MY_NAME is only supported on macOS and Linux."
exit 75
fi
# Check for essential commands
for cmd in curl tar gzip; do
if ! command -v $cmd &> /dev/null; then
log_message "ERROR" "$cmd is not installed. Please install $cmd."
exit 2
fi
done
# Check if manifest file exists
if [ ! -f "${INSTALL_MANIFEST}" ]; then
log_message "ERROR" "Manifest file not found at ${INSTALL_MANIFEST}"
exit 2
fi
return 0
}
# Pre-installation tasks
pre_install() {
# Check if pre-installation tasks have already been completed
# Stop recursion before it starts, this is re-entrant.
if [ "${CONDA_INSTALL_COMPLETE:-""}" == "Y" ]; then
return 0
fi
log_message "INFO" "Pre-installation tasks..."
# Custom pre-install tasks can be added here
check_deps
# This may be used for verification or rollback/removal later.
write_pkg_info
install_conda
unset CONDA_INSTALL_COMPLETE
}
install_assets() {
# Implement package installation logic here
log_message "INFO" "Installing packages..."
# Set default owner and group if not specified
owner=${owner:-$(id -u)}
group=${group:-$(id -g)}
readarray -t lines < <(grep -v '^#' "$INSTALL_MANIFEST" | grep -v '^\s*$')
for line in "${lines[@]}"; do
# Skip metadata lines
if [[ "$line" =~ ^[A-Za-z_]+=.*$ ]]; then
continue
fi
IFS=$'| ' read -r asset_type destination source name permissions owner group size checksum <<< "$line"
destination="${INSTALL_BASE}/${destination}"
source_path="${MY_PATH}/${source}/${name}"
dest_path="${destination}/${name}"
mkdir -p "$destination"
case "$asset_type" in
d) # Create directory
mkdir -p "$dest_path"
chown "$owner":"$group" "$dest_path"
chmod "$permissions" "$dest_path"
;;
f) # Copy file
cp "$source_path" "$dest_path"
chown "$owner":"$group" "$dest_path"
chmod "$permissions" "$dest_path"
;;
h) # Create hard link
cd "$destination"
ln "$source" "$name"
cd - > /dev/null
;;
l) # Create symbolic link
cd "$destination"
ln -sf "$source" "$name"
cd - > /dev/null
;;
c) # Remove the asset
cd "$destination"
rm -rf "$name"
cd - > /dev/null
;;
*)
log_message "ERROR" "Unknown asset type: $asset_type"
;;
esac
done
}
post_install_user_message() {
log_message "INFO" "Provide user instructions..."
# Custom post-install message can be added here
cat <<_EOT_
The package $PKG_NAME has been installed to $INSTALL_BASE.
To use the package, the following line was added to your .bashrc file:
if [[ ! "\$PATH" =~ "$INSTALL_BASE/bin:" ]]; then export PATH="$INSTALL_BASE/bin:\$PATH"; fi
If you wish to use it in the current shell, run the following command:
exec $SHELL -l
or exit the terminal and start a new one. To verify the installation files
for correct location and file integrity run the following command:
$MY_NAME verify (not implemented yet)
If you wish to uninstall the packages associated with $PKG_NAME, run the
following command:
$MY_NAME uninstall (not implemented yet)
This will only remove the files associated with the package, not the
Conda installation, its installed packages or any other dependencies. If
you wish to uninstall everything associated with the package, run the
following command:
$MY_NAME remove_all (not implemented yet)
The documentation may be found in the $INSTALL_BASE/README.md file. Please
contact the package maintainers for any issues or feature requests or file them on
GitHub: ${Support:-https://github.com/unixwzrd/venvutil/issues}
Please help sponsor my projects on Patreon: ${Contribute:-https://patreon.com/unixwzrd}
_EOT_
return 0
}
write_pkg_config() {
log_message "INFO" "Writing package configuration..."
echo "$PKG_NAME.pc.in" > "$INSTALL_CONFIG/$PKG_NAME.pc"
for var in "${pkg_config_set_vars[@]}"; do
echo "$var=\"${!var}\""
done >> "$INSTALL_CONFIG/$PKG_NAME.pc"
echo "" >> "$INSTALL_CONFIG/$PKG_NAME.pc"
for var in "${pkg_config_desc_vars[@]}"; do
echo "$var: ${!var}"
done >> "$INSTALL_CONFIG/$PKG_NAME.pc"
return 0
}
# Update .bashrc
update_bashrc() {
log_message "INFO" "Updating .bashrc for package $PKG_NAME in PATH..."
local bashrc="$HOME/.bashrc"
# Expressions don't expand in single quotes, use double quotes for that.
# shellcheck disable=SC2016
local path_line="if [[ ! \"\$PATH\" =~ \"$INSTALL_BASE/bin:\" ]]; then export PATH=\"$INSTALL_BASE/bin:\$PATH\"; fi"
if ! grep -Fxq "$path_line" "$bashrc"; then
echo "$path_line" >> "$bashrc"
log_message "INFO" "Updated $bashrc added package $PKG_NAME bin directory."
fi
return 0
}
install_python_packages() {
log_message "INFO" "Installing NLTK data..."
pip install -r "$MY_PATH/requirements.txt" 2>&1 | tee -a "$INSTALL_CONFIG/install.log" >&2
python <<_EOT_
import nltk
nltk.download('punkt')
nltk.download('stopwords')
_EOT_
log_message "INFO" "NLTK data installed successfully."
}
post_install() {
log_message "INFO" "Post-installation tasks..."
install_python_packages
# Example: Update .bashrc if necessary
update_bashrc
write_pkg_config
post_install_user_message
log_message "INFO" "Installation for $PKG_NAME complete."
return 0
}
# Installation function
install() {
log_message "INFO" "Installing package: $PKG_NAME..."
pre_install
install_assets
post_install
return 0
}
# Pre-removal tasks
pre_remove() {
log_message "INFO" "Pre-removal tasks..."
# Custom pre-remove tasks can be added here
}
# Removal function
remove_assets() {
log_message "INFO" "Removal tasks..."
readarray -t lines < <(grep -v '^#' "$INSTALL_MANIFEST" | grep -v '^\s*$' | sort -r)
for line in "${lines[@]}"; do
# Skip metadata lines
if [[ "$line" =~ ^[A-Za-z_]+=.*$ ]]; then
continue
fi
# size appears unused. Verify use (or export if used externally).
# shellcheck disable=SC2034
IFS=$'\t' read -r type destination source name permissions owner group size checksum <<< "$line"
dest_path="${INSTALL_BASE}/${destination}/${name}"
case "$type" in
d) # Remove directory if empty
if [ -d "$dest_path" ] && [ -z "$(ls -A "$dest_path")" ]; then
rmdir "$dest_path"
fi
;;
f|l|h) # Remove file or symbolic link
rm -f "$dest_path"
;;
c) # Asset should already be deleted, but to make sure...
[ -d "$dest_path" ] && rmdir "$dest_path"
[ -e "$dest_path" ] && rm -f "$dest_path"
;;
*)
log_message "ERROR" "Unknown type: $type"
;;
esac
done
post_remove
}
# Post-removal tasks
post_remove() {
log_message "INFO" "Post-removal tasks..."
# Example: Remove package bin directory from .bashrc
remove_bashrc_entries
}
# Remove entries from .bashrc
remove_bashrc_entries() {
local bashrc="$HOME/.bashrc"
# Expressions don't expand in single quotes, use double quotes for that.
# shellcheck disable=SC2016
local path_line="if [[ ! \"\$PATH\" =~ \"$INSTALL_BASE/bin:\" ]]; then export PATH=\"$INSTALL_BASE/bin:\$PATH\"; fi"
if grep -Fxq "$path_line" "$bashrc"; then
sed -i.bak "/$path_line/d" "$bashrc"
log_message "INFO" "Removed package bin directory from $bashrc."
fi
}
# Rollback function
rollback() {
log_message "CRITICAL" "Rollback initiated..."
# Implement rollback logic based on actions logged during installation
# For example, read a log file and undo actions
}
remove() {
log_message "INFO" "Removing package: $PKG_NAME tasks..."
pre_remove
remove_assets
post_remove
return 0
}
verify() {
log_message "INFO" "Verifying package: $PKG_NAME tasks..."
# Implement verification logic here
return 0
}
# Main function
main() {
parse_arguments "$@"
initialization
case "$ACTION" in
install)
install
;;
remove)
echo "Not implemented at this time." && exit 1
remove
;;
rollback)
echo "Not implemented at this time." && exit 1
rollback
;;
verify)
echo "Not implemented at this time." && exit 1
verify
;;
*)
echo "Invalid action: $ACTION"
display_help_and_exit "Usage: $MY_NAME [options] {install|remove|rollback}"
;;
esac
}
main "$@"