From 8dbc784b8e3cfac363d36592103bde49c4d9ed55 Mon Sep 17 00:00:00 2001 From: Glenn Franxman Date: Fri, 1 Nov 2024 17:23:51 -0400 Subject: [PATCH] feat: Gfranxman/345 ensure latest version of rsync (#400) Co-authored-by: Calvin Hendryx-Parker --- .github/workflows/setup-project.yaml | 6 +- README.md | 2 +- install.sh | 128 +++++++++++++++++++-------- 3 files changed, 98 insertions(+), 38 deletions(-) diff --git a/.github/workflows/setup-project.yaml b/.github/workflows/setup-project.yaml index dbd133a3..d6cb96a2 100644 --- a/.github/workflows/setup-project.yaml +++ b/.github/workflows/setup-project.yaml @@ -42,6 +42,9 @@ jobs: - name: Install Requirements shell: bash run: | + sudo apt-get update && sudo apt-get install --only-upgrade curl + sudo apt-get update && sudo apt-get install --only-upgrade make + sudo apt-get update && sudo apt-get install --only-upgrade python3 INSTALL_URL="https://raw.githubusercontent.com/sixfeetup/scaf/$GITHUB_REF_NAME/install.sh" export SCAF_SCRIPT_BRANCH=$GITHUB_REF_NAME git config --global init.defaultBranch main @@ -49,7 +52,8 @@ jobs: git config --global user.name "CI CD install deps" echo "Downloading install script from $INSTALL_URL" rm -f $HOME/.local/bin/scaf - curl -sSL $INSTALL_URL | sh + echo "Installing scaf from $INSTALL_URL" + curl -sSL $INSTALL_URL | DEBUG=True bash - name: Create Project with Scaf shell: bash diff --git a/README.md b/README.md index b31a63de..6af43986 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ contains the following: Installation is supported on Linux and macOS: ``` -curl -sSL https://raw.githubusercontent.com/sixfeetup/scaf/main/install.sh | sh +curl -sSL https://raw.githubusercontent.com/sixfeetup/scaf/main/install.sh | bash ``` The installation script will install kubectl, kind, and Tilt if it can't diff --git a/install.sh b/install.sh index a72f25b4..3d4928c1 100755 --- a/install.sh +++ b/install.sh @@ -2,11 +2,17 @@ BRANCH=${SCAF_SCRIPT_BRANCH:-main} SCAF_SCRIPT_URL="https://raw.githubusercontent.com/sixfeetup/scaf/${BRANCH}/scaf" -TEMP_DOWNLOAD="./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..." @@ -32,14 +38,60 @@ 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() { - # these are dependencies that we depend on the user to have installed - dependencies="bash curl make docker git rsync" missing="" - for dep in $dependencies; do - if ! command_exists $dep ; then - missing="$missing $dep" + 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 @@ -51,14 +103,12 @@ check_top_level_dependencies() { fi if [ -z "$missing" ]; then - echo "All top-level dependencies are installed." + echo "All top-level dependencies are installed and meet the required versions." return 0 fi - echo "The following dependencies are missing:" - for dep in $missing; do - echo "- $dep" - done + 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 @@ -69,42 +119,45 @@ check_top_level_dependencies() { # TODO: I wonder if we should look for nix, brew, apt-get, yum, etc. and provide instructions # for those rather then strictly by OS. - for dep in $missing; do + + # 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") + bash*) echo "Please install bash for consistent shell execution." ;; - "curl") + curl*) echo "Please install curl:" - echo " - Ubuntu/Debian: sudo apt-get install curl" - echo " - CentOS/Fedora: sudo yum install curl" - echo " - macOS: brew 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") + make*) echo "Please install make:" - echo " - Ubuntu/Debian: sudo apt-get install make" - echo " - CentOS/Fedora: sudo yum 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") + python3*) echo "Please install python3:" - echo " - Ubuntu/Debian: sudo apt-get install python3" - echo " - CentOS/Fedora: sudo yum install python3" - echo " - macOS: brew install python" + 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") + rsync*) echo "Please install rsync:" - echo " - Ubuntu/Debian: sudo apt-get install rsync" - echo " - CentOS/Fedora: sudo yum install rsync" - echo " - macOS: brew 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") + docker*) case $os in "Darwin") echo "Please install Docker Desktop for macOS:" @@ -180,9 +233,9 @@ move_to_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/$1 + mv $1 $DEST else - sudo mv $1 $DEST/$1 + sudo mv $1 $DEST fi } @@ -243,23 +296,26 @@ install_scaf() { if [ -f "$TEMP_DOWNLOAD" ]; then chmod +x $TEMP_DOWNLOAD echo "Moving scaf to $DESTINATION..." - move_to_bin_folder $TEMP_DOWNLOAD `dirname $DESTINATION` + move_to_bin_folder $TEMP_DOWNLOAD $(dirname $DESTINATION) if [ -f "$DESTINATION" ]; then - echo "scaf installed successfully at $DESTINATION" + echo "๐ŸŽ‰ scaf installed successfully at $DESTINATION ๐ŸŽ‰" else - echo "Failed to move scaf to the destination." + echo "๐Ÿงจ Failed to move scaf to the destination. ๐Ÿงจ" exit 1 fi else - echo "Failed to download scaf." + 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 @@ -267,7 +323,7 @@ for tool in kubectl kind tilt; do echo "$tool is not installed." install_$tool else - echo "$tool is already installed." + [ -n "$DEBUG" ] && echo "$tool is already installed." fi done