|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -LOG_FILE="/tmp/miniconda_installation.log" |
4 | | -MINICONDA_SCRIPT_URL="https://raw.githubusercontent.com/codeperfectplus/HackScripts/main/setup/install_miniconda.sh" |
5 | | -MINICONDA_SCRIPT_PATH="/tmp/install_miniconda.sh" |
| 3 | +# Script to set up Miniconda on a Linux system |
| 4 | +# Status: Tested |
| 5 | +# Published by: Deepak Raj |
| 6 | +# Published on: 2024-08-28 |
6 | 7 |
|
7 | | -log() { |
8 | | - echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" |
9 | | -} |
10 | | - |
11 | | -get_user_home() { |
12 | | - if [ -n "$SUDO_USER" ]; then |
13 | | - TARGET_USER="$SUDO_USER" |
| 8 | +# Function to determine the home directory of the target user |
| 9 | +get_user_name() { |
| 10 | + if [ "$(whoami)" = "root" ]; then |
| 11 | + LOGNAME_USER=$(logname 2>/dev/null) # Redirect any error output to /dev/null |
| 12 | + if [ $? -ne 0 ]; then # Check if the exit status of the last command is not 0 |
| 13 | + echo "No login name found. Using fallback method." |
| 14 | + # use head -n 1 for native linux. tail -n 1 works with wsl. |
| 15 | + USER_NAME=$(cat /etc/passwd | grep '/home' | cut -d: -f1 | tail -n 1) |
| 16 | + else |
| 17 | + USER_NAME=$LOGNAME_USER |
| 18 | + fi |
14 | 19 | else |
15 | | - TARGET_USER="$LOGNAME" |
| 20 | + USER_NAME=$(whoami) |
16 | 21 | fi |
17 | | - USER_HOME=$(eval echo ~$TARGET_USER) |
18 | | - echo "$USER_HOME" |
| 22 | + echo "$USER_NAME" |
19 | 23 | } |
20 | 24 |
|
21 | | -USER_HOME=$(get_user_home) |
22 | | -AUTO_CONFIRM=false |
| 25 | +USER_NAME=$(get_user_name) |
| 26 | +USER_HOME="/home/$USER_NAME" |
| 27 | +echo "User home directory: $USER_HOME" |
| 28 | +INSTALL_PATH="$USER_HOME/miniconda3" |
| 29 | +MINICONDA_INSTALLER="Miniconda3-latest-Linux-x86_64.sh" |
| 30 | +TMP_INSTALLER="/tmp/$MINICONDA_INSTALLER" |
| 31 | +MINICONDA_PAGE_URL="https://docs.anaconda.com/miniconda/" |
23 | 32 |
|
24 | | -install_miniconda() { |
25 | | - log "Starting Miniconda installation process." |
| 33 | +# Function to extract the checksum from the Miniconda documentation page |
| 34 | +extract_checksum() { |
| 35 | + echo "Downloading Miniconda documentation page..." |
| 36 | + wget -q "$MINICONDA_PAGE_URL" -O /tmp/miniconda_page.html |
26 | 37 |
|
27 | | - if [ "$AUTO_CONFIRM" = false ]; then |
28 | | - echo "Do you want to install Miniconda? (y/n)" |
29 | | - read -r answer |
30 | | - if [ ! "$answer" = "y" ]; then |
31 | | - log "Miniconda installation skipped by user." |
32 | | - return |
33 | | - fi |
34 | | - else |
35 | | - log "Auto-confirmation enabled, proceeding with installation." |
| 38 | + if [ ! -f "/tmp/miniconda_page.html" ]; then |
| 39 | + echo "Failed to download Miniconda documentation page." |
| 40 | + return 1 |
36 | 41 | fi |
37 | 42 |
|
38 | | - log "Downloading Miniconda installation script to /tmp directory." |
39 | | - if wget "$MINICONDA_SCRIPT_URL" -O "$MINICONDA_SCRIPT_PATH"; then |
40 | | - log "Miniconda installation script downloaded successfully." |
41 | | - else |
42 | | - log "Failed to download Miniconda installation script." |
| 43 | + echo "Extracting checksum for $MINICONDA_INSTALLER..." |
| 44 | + CHECKSUM=$(grep -A 1 "$MINICONDA_INSTALLER" /tmp/miniconda_page.html | grep -oP '(?<=<span class="pre">)[a-f0-9]{64}(?=</span>)') |
| 45 | + |
| 46 | + rm /tmp/miniconda_page.html |
| 47 | + |
| 48 | + if [ -z "$CHECKSUM" ]; then |
| 49 | + echo "Checksum for $MINICONDA_INSTALLER not found." |
43 | 50 | return 1 |
44 | 51 | fi |
| 52 | +} |
45 | 53 |
|
| 54 | +# Download Miniconda installer if it doesn't already exist |
| 55 | +if [ -f "$TMP_INSTALLER" ]; then |
| 56 | + echo "Miniconda installer already exists in /tmp. Skipping download." |
| 57 | +else |
| 58 | + echo "Downloading Miniconda installer..." |
| 59 | + wget https://repo.anaconda.com/miniconda/$MINICONDA_INSTALLER -O $TMP_INSTALLER |
| 60 | +fi |
46 | 61 |
|
47 | | - chmod +x "$MINICONDA_SCRIPT_PATH" |
48 | | - |
49 | | - log "Running Miniconda installation script." |
50 | | - if "$MINICONDA_SCRIPT_PATH"; then |
51 | | - log "Miniconda installation script executed successfully." |
52 | | - else |
53 | | - log "Miniconda installation script execution failed." |
54 | | - return 1 |
| 62 | +# Verify the Miniconda installer checksum |
| 63 | +echo "Verifying Miniconda installer..." |
| 64 | +extract_checksum |
| 65 | + |
| 66 | +if [ $? -ne 0 ]; then |
| 67 | + echo "Failed to extract checksum. Exiting..." |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +DOWNLOAD_CHECKSUM=$(sha256sum $TMP_INSTALLER | awk '{print $1}') |
| 72 | + |
| 73 | +echo "Checksum of downloaded file: $DOWNLOAD_CHECKSUM" |
| 74 | +echo "Checksum from documentation: $CHECKSUM" |
| 75 | + |
| 76 | +if [ "$DOWNLOAD_CHECKSUM" != "$CHECKSUM" ]; then |
| 77 | + echo "Checksum verification failed. Exiting..." |
| 78 | + echo "Do you want to proceed with the installation anyway? (y/n): " |
| 79 | + read proceed |
| 80 | + if [ "$proceed" != "y" ]; then |
| 81 | + echo "Exiting..." |
| 82 | + exit 1 |
55 | 83 | fi |
| 84 | +fi |
56 | 85 |
|
57 | | - if [ -d "$USER_HOME/miniconda3" ]; then |
58 | | - log "Miniconda installed successfully." |
59 | | - else |
60 | | - log "Miniconda installation failed." |
61 | | - return 1 |
| 86 | +echo "Checksum verification successful." |
| 87 | + |
| 88 | +# Check if Miniconda is already installed |
| 89 | +echo $INSTALL_PATH |
| 90 | + |
| 91 | +if [ -d "$INSTALL_PATH" ]; then |
| 92 | + echo "Miniconda is already installed at $INSTALL_PATH." |
| 93 | + read -p "Do you want to update Miniconda? (y/n): " update |
| 94 | + if [ "$update" != "y" ]; then |
| 95 | + echo "Exiting..." |
| 96 | + exit 0 |
62 | 97 | fi |
| 98 | + bash $TMP_INSTALLER -b -p $INSTALL_PATH -u |
| 99 | +else |
| 100 | + # Run the Miniconda installer |
| 101 | + echo "Running Miniconda installer..." |
| 102 | + bash $TMP_INSTALLER -b -p $INSTALL_PATH |
| 103 | +fi |
63 | 104 |
|
64 | | - log "Cleaning up installation files." |
65 | | - rm "$MINICONDA_SCRIPT_PATH" || log "Failed to remove installation script." |
| 105 | +# Initialize Miniconda |
| 106 | +echo "Initializing Miniconda..." |
| 107 | +$INSTALL_PATH/bin/conda init |
66 | 108 |
|
67 | | - log "Miniconda installation process completed." |
68 | | -} |
| 109 | +# Clean up the installer |
| 110 | +echo "Cleaning up..." |
| 111 | +rm $TMP_INSTALLER |
| 112 | + |
| 113 | +# Optional: Update Conda to the latest version |
| 114 | +echo "Updating Conda..." |
| 115 | +$INSTALL_PATH/bin/conda update -n base -c defaults conda -y |
69 | 116 |
|
70 | | -while getopts "y" opt; do |
71 | | - case $opt in |
72 | | - y) AUTO_CONFIRM=true ;; |
73 | | - *) echo "Usage: $0 [-y]"; exit 1 ;; |
74 | | - esac |
75 | | -done |
| 117 | +source ~/.bashrc |
76 | 118 |
|
77 | | -install_miniconda |
| 119 | +# Display final message |
| 120 | +echo "Miniconda installation completed!" |
| 121 | +echo "Miniconda is installed at $INSTALL_PATH" |
0 commit comments