github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/bin/install.sh (about) 1 #!/bin/bash 2 3 ################################################################################################ 4 # # 5 # This script installs the latest release of defang from GitHub. It is designed # 6 # to be run like this: # 7 # # 8 # . <(curl -Ls https://s.defang.io/install.sh) # 9 # # 10 # This allows us to do some interactive stuff where we can prompt the user for input. # 11 # # 12 ################################################################################################ 13 14 echo " 15 __ ____ 16 ____/ /__ / __/___ _____ ____ _ 17 / __ / _ \/ /_/ __ \`/ __ \/ __ \`/ 18 / /_/ / __/ __/ /_/ / / / / /_/ / 19 \__,_/\___/_/ \__,_/_/ /_/\__, / 20 /____/ 21 " 22 23 # Check for -y flag or CI var and set the REPLY variable to "y" if it is present 24 if [[ "$1" == "-y" ]] || [[ "$CI" == "1" ]] || [[ "$CI" == "true" ]]; then 25 REPLY="y" 26 CI="1" 27 fi 28 29 # Define the GitHub API URL for the latest release 30 RELEASE_API_URL="https://api.github.com/repos/defang-io/defang/releases/latest" 31 32 # Use curl to fetch the latest release data 33 echo "Fetching the latest release information..." 34 RELEASE_JSON=$(curl -s $RELEASE_API_URL) 35 36 # Check for curl failure 37 if [ $? -ne 0 ]; then 38 echo "Error fetching release information. Please check your connection or if the URL is correct." 39 return 1 40 fi 41 42 # Determine system architecture and operating system 43 ARCH=$(uname -m) 44 OS=$(uname -s) 45 46 # Adjust the architecture string to match the naming convention in the download URLs 47 case $ARCH in 48 x86_64) ARCH_SUFFIX="amd64" ;; 49 arm64) ARCH_SUFFIX="arm64" ;; 50 aarch64) ARCH_SUFFIX="arm64" ;; 51 *) echo "Unsupported architecture: $ARCH"; return 2 ;; 52 esac 53 54 # Initialize the download URL variable 55 DOWNLOAD_URL="" 56 57 # Based on the OS, filter the download URL 58 if [ "$OS" = "Darwin" ]; then 59 DOWNLOAD_URL=$(echo "$RELEASE_JSON" | grep -o "https://github.com/defang-io/defang/releases/download/v[0-9.]*/defang_[0-9.]*_macOS.zip" | head -n 1) 60 elif [ "$OS" = "Linux" ]; then 61 DOWNLOAD_URL=$(echo "$RELEASE_JSON" | grep -o "https://github.com/defang-io/defang/releases/download/v[0-9.]*/defang_[0-9.]*_linux_${ARCH_SUFFIX}.tar.gz" | head -n 1) 62 fi 63 64 # Abort if the download URL is not found 65 if [ -z "$DOWNLOAD_URL" ]; then 66 echo "Could not find a download URL for your operating system ($OS) and architecture ($ARCH_SUFFIX)." 67 return 3 68 fi 69 70 echo "Downloading $DOWNLOAD_URL..." 71 72 # Define the output file name based on OS and ARCH_SUFFIX 73 FILENAME="defang_latest" 74 if [ "$OS" = "Darwin" ]; then 75 FILENAME="$FILENAME.zip" 76 elif [ "$OS" = "Linux" ]; then 77 FILENAME="$FILENAME.tar.gz" 78 fi 79 80 # Download the file 81 if ! curl -s -L "$DOWNLOAD_URL" -o "$FILENAME"; then 82 echo "Download failed. Please check your internet connection and try again." 83 return 4 84 fi 85 86 # Create a temporary directory for extraction 87 EXTRACT_DIR=$(mktemp -d) 88 89 # Extract the downloaded file to the temporary directory 90 echo "Extracting the downloaded file to $EXTRACT_DIR..." 91 if [ "$OS" = "Darwin" ]; then 92 if ! unzip -q "$FILENAME" -d "$EXTRACT_DIR"; then 93 echo "Failed to extract the downloaded file. The file might be corrupted." 94 return 5 95 fi 96 elif [ "$OS" = "Linux" ]; then 97 if ! tar -xzf "$FILENAME" -C "$EXTRACT_DIR"; then 98 echo "Failed to extract the downloaded file. The file might be corrupted." 99 return 6 100 fi 101 fi 102 103 # Determine the installation directory 104 if [ -z "$INSTALL_DIR" ] 105 then 106 INSTALL_DIR="$HOME/.local/bin" 107 fi 108 109 # Check if the installation directory exists and is writable 110 if [ ! -d "$INSTALL_DIR" ]; then 111 echo "The installation directory ($INSTALL_DIR) does not exist. Creating it now..." 112 if ! mkdir -p "$INSTALL_DIR"; then 113 echo "Failed to create the installation directory. Please check your permissions and try again." 114 return 7 115 fi 116 elif [ ! -w "$INSTALL_DIR" ]; then 117 echo "The installation directory ($INSTALL_DIR) is not writable. Please check your permissions and try again." 118 return 8 119 fi 120 121 # Assuming the binary or application name is predictable and consistent 122 BINARY_NAME='defang' # Adjust this based on actual content 123 124 # Move the binary or application to the installation directory from the temporary directory 125 echo "Moving defang to $INSTALL_DIR" 126 if ! mv "$EXTRACT_DIR/$BINARY_NAME" "$INSTALL_DIR"; then 127 echo "Failed to move defang. Please check your permissions and try again." 128 return 9 129 fi 130 131 # Make the binary executable 132 if ! chmod +x "$INSTALL_DIR/$BINARY_NAME"; then 133 echo "Failed to make defang executable. Please check your permissions and try again." 134 return 10 135 fi 136 137 # Cleanup: Remove the temporary directory 138 echo "Cleaning up..." 139 rm -r "$EXTRACT_DIR" 140 141 prompt_and_append_to_profile() { 142 local prompt=$1 143 local profile_file=$2 144 local line="export PATH=\"\$PATH:$INSTALL_DIR\"" 145 echo "We'd like this line to your $profile_file:" 146 echo 147 echo " $line" 148 echo 149 if [[ "$CI" != "1" ]]; then 150 # Prompt the user for confirmation 151 echo -n "$prompt $profile_file? (y/n) " 152 read REPLY 153 echo # move to a new line 154 fi 155 if [[ $REPLY =~ ^[Yy]$ ]]; then 156 # Append the line to the profile file 157 echo >> "$profile_file" 158 echo "$line # Added by Defang install.sh" >> "$profile_file" 159 else 160 # Print the command for the user to run manually 161 echo "To add $INSTALL_DIR to your PATH, run the following command:" 162 echo 163 echo " echo '$line' >> \"$profile_file\"" 164 echo 165 fi 166 } 167 168 # Get the name of the current shell 169 CURRENT_SHELL=$(basename "$SHELL") 170 171 # Add the installation directory to PATH if not already present 172 if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then 173 echo "Adding $INSTALL_DIR to your PATH for this session." 174 export PATH="$PATH:$INSTALL_DIR" 175 176 # Define the possible shell profile files 177 PROFILE_FILES=(".bashrc" ".zshrc" ".kshrc") 178 179 # Loop over the possible profile files 180 FOUND_PROFILE_FILE=false 181 for profile_file in "${PROFILE_FILES[@]}"; do 182 # If the profile file exists in the user's home directory, add a line to it 183 if [[ -f "$HOME/$profile_file" ]]; then 184 FOUND_PROFILE_FILE=true 185 prompt_and_append_to_profile "Can we append the necessary line to" "$HOME/$profile_file" 186 fi 187 done 188 189 # If no profile file was found 190 if [[ $FOUND_PROFILE_FILE == false ]]; then 191 # Prompt the user to create a new profile file 192 prompt_and_append_to_profile "No existing profile file found. Can we create" "$HOME/.${CURRENT_SHELL}rc" 193 fi 194 fi 195 196 # TODO: Install shell completion script 197 198 199 # Loop over the possible profile files 200 for profile_file in "${PROFILE_FILES[@]}"; do 201 # If the profile file exists in the user's home directory, add a line to it 202 if [[ -f "$HOME/$profile_file" ]]; then 203 prompt_and_append_to_profile "Can we append the necessary line to" "$HOME/$profile_file" 204 fi 205 done 206 207 # Cleanup: Remove the originally downloaded file 208 rm "$FILENAME" 209 210 echo "Installation completed. You can now use defang by typing '$BINARY_NAME' in the terminal."