From 310f692ea5d1d352ef5610b856e115faaf05915f Mon Sep 17 00:00:00 2001 From: MaksimZhukov <46996400+MaksimZhukov@users.noreply.github.com> Date: Mon, 28 Dec 2020 10:54:25 +0300 Subject: [PATCH] [macOS] Fix the issue with brew install (#2354) * Update azure-cli installation * Update curl installation * Update git-lfs installation * Add utils * Update gnupg installation * Add brew_install function * Revert jq installation * Add condition for the aws-sam-cli * Add return to function, revert aws-sam-cli changes, rename brew_install function * Fix typo Co-authored-by: MaksimZhukov --- images/macos/provision/core/audiodevice.sh | 5 +- images/macos/provision/core/chrome.sh | 2 +- images/macos/provision/core/commonutils.sh | 2 +- images/macos/provision/core/firefox.sh | 3 +- images/macos/provision/core/gcc.sh | 9 ++-- images/macos/provision/core/git.sh | 9 ++-- images/macos/provision/core/homebrew.sh | 6 ++- images/macos/provision/core/mongodb.sh | 5 +- images/macos/provision/core/node.sh | 2 +- images/macos/provision/core/openjdk.sh | 6 +-- images/macos/provision/core/openssl.sh | 4 +- images/macos/provision/core/php.sh | 5 +- images/macos/provision/core/postgresql.sh | 3 +- images/macos/provision/core/powershell.sh | 2 +- images/macos/provision/core/python.sh | 4 +- images/macos/provision/core/ruby.sh | 2 +- images/macos/provision/core/rubygem.sh | 2 +- images/macos/provision/core/rust.sh | 3 +- images/macos/provision/utils/utils.sh | 61 +++++++++++++++++++++- 19 files changed, 103 insertions(+), 32 deletions(-) diff --git a/images/macos/provision/core/audiodevice.sh b/images/macos/provision/core/audiodevice.sh index 908f5611..1008a14d 100644 --- a/images/macos/provision/core/audiodevice.sh +++ b/images/macos/provision/core/audiodevice.sh @@ -1,14 +1,15 @@ #!/bin/bash -e -o pipefail source ~/utils/invoke-tests.sh +source ~/utils/utils.sh echo "install soundflower" brew install --cask soundflower echo "install switchaudio-osx" -brew install switchaudio-osx +brew_smart_install "switchaudio-osx" echo "install sox" -brew install sox +brew_smart_install "sox" echo "set Soundflower (2ch) as input/output device" SwitchAudioSource -s "Soundflower (2ch)" -t input diff --git a/images/macos/provision/core/chrome.sh b/images/macos/provision/core/chrome.sh index c11f9890..01e34a6b 100644 --- a/images/macos/provision/core/chrome.sh +++ b/images/macos/provision/core/chrome.sh @@ -9,7 +9,7 @@ echo "Installing Chrome Driver" brew install --cask chromedriver echo "Installing Selenium" -brew install selenium-server-standalone +brew_smart_install "selenium-server-standalone" CHROMEWEBDRIVER_DIR=$(readlink $(which chromedriver) | xargs dirname) echo "export CHROMEWEBDRIVER=$CHROMEWEBDRIVER_DIR" >> "${HOME}/.bashrc" diff --git a/images/macos/provision/core/commonutils.sh b/images/macos/provision/core/commonutils.sh index 049467f4..4cba4d9b 100644 --- a/images/macos/provision/core/commonutils.sh +++ b/images/macos/provision/core/commonutils.sh @@ -5,7 +5,7 @@ source ~/utils/invoke-tests.sh common_packages=$(get_toolset_value '.brew.common_packages[]') for package in $common_packages; do echo "Installing $package..." - brew install $package + brew_smart_install "$package" done cask_packages=$(get_toolset_value '.brew.cask_packages[]') diff --git a/images/macos/provision/core/firefox.sh b/images/macos/provision/core/firefox.sh index b55264f1..47d99571 100644 --- a/images/macos/provision/core/firefox.sh +++ b/images/macos/provision/core/firefox.sh @@ -1,11 +1,12 @@ #!/bin/bash -e -o pipefail source ~/utils/invoke-tests.sh +source ~/utils/utils.sh echo "Installing Firefox..." brew install --cask firefox echo "Installing Geckodriver..." -brew install geckodriver +brew_smart_install "geckodriver" echo "Add GECKOWEBDRIVER to bashrc..." echo "export GECKOWEBDRIVER=$(brew --prefix geckodriver)/bin" >> "${HOME}/.bashrc" diff --git a/images/macos/provision/core/gcc.sh b/images/macos/provision/core/gcc.sh index 1041de5f..1a07cabe 100644 --- a/images/macos/provision/core/gcc.sh +++ b/images/macos/provision/core/gcc.sh @@ -1,15 +1,16 @@ #!/bin/bash -e -o pipefail +source ~/utils/utils.sh source ~/utils/invoke-tests.sh echo "Installing GCC@8 using homebrew..." -brew install gcc@8 +brew_smart_install "gcc@8" echo "Installing GCC@9 using homebrew..." -brew install gcc@9 +brew_smart_install "gcc@9" # https://github.com/actions/virtual-environments/issues/1280 echo "Installing GCC@10 using homebrew..." -brew install gcc@10 +brew_smart_install "gcc@10" rm $(which gfortran) -invoke_tests "Common" "GCC" \ No newline at end of file +invoke_tests "Common" "GCC" diff --git a/images/macos/provision/core/git.sh b/images/macos/provision/core/git.sh index 251d2336..98ebab07 100644 --- a/images/macos/provision/core/git.sh +++ b/images/macos/provision/core/git.sh @@ -1,18 +1,21 @@ #!/bin/bash -e -o pipefail source ~/utils/invoke-tests.sh +source ~/utils/utils.sh + echo Installing Git... -brew install git +brew_smart_install "git" echo Installing Git LFS -brew install git-lfs +brew_smart_install "git-lfs" + # Update global git config git lfs install # Update system git config sudo git lfs install --system echo Installing Hub -brew install hub +brew_smart_install "hub" echo Disable all the Git help messages... git config --global advice.pushUpdateRejected false diff --git a/images/macos/provision/core/homebrew.sh b/images/macos/provision/core/homebrew.sh index be86a0b4..57f87644 100755 --- a/images/macos/provision/core/homebrew.sh +++ b/images/macos/provision/core/homebrew.sh @@ -1,5 +1,7 @@ #!/bin/bash -e -o pipefail +source ~/utils/utils.sh + echo "Installing Homebrew..." HOMEBREW_INSTALL_URL="https://raw.githubusercontent.com/Homebrew/install/master/install.sh" /bin/bash -c "$(curl -fsSL ${HOMEBREW_INSTALL_URL})" @@ -12,10 +14,10 @@ echo "Installing jq..." brew install jq echo "Installing curl..." -brew install curl +brew_smart_install "curl" echo "Installing wget..." -brew install wget +brew_smart_install "wget" # init brew bundle feature brew tap Homebrew/bundle \ No newline at end of file diff --git a/images/macos/provision/core/mongodb.sh b/images/macos/provision/core/mongodb.sh index bb256441..a2a690ef 100644 --- a/images/macos/provision/core/mongodb.sh +++ b/images/macos/provision/core/mongodb.sh @@ -1,4 +1,5 @@ #!/bin/bash -e -o pipefail +source ~/utils/utils.sh source ~/utils/invoke-tests.sh # MongoDB object-value database @@ -8,6 +9,6 @@ source ~/utils/invoke-tests.sh echo "Installing mongodb..." brew tap mongodb/brew -brew install mongodb-community +brew_smart_install "mongodb-community" -invoke_tests "Common" "Mongo" \ No newline at end of file +invoke_tests "Common" "Mongo" diff --git a/images/macos/provision/core/node.sh b/images/macos/provision/core/node.sh index 5f8c7519..8d4172a5 100644 --- a/images/macos/provision/core/node.sh +++ b/images/macos/provision/core/node.sh @@ -27,7 +27,7 @@ if is_Less_Catalina; then npm install -g appcenter-cli@^1.0.0 else # Install Node.JS 12 for macOS >= 10.15 - brew install node@14 + brew_smart_install "node@14" brew link node@14 --force for module in ${node_modules[@]}; do diff --git a/images/macos/provision/core/openjdk.sh b/images/macos/provision/core/openjdk.sh index f7eb3d9e..a9293e80 100644 --- a/images/macos/provision/core/openjdk.sh +++ b/images/macos/provision/core/openjdk.sh @@ -46,9 +46,9 @@ done createEnvironmentVariable "JAVA_HOME" $JAVA_DEFAULT echo Installing Maven... -brew install maven +brew_smart_install "maven" echo Installing Gradle ... -brew install gradle +brew_smart_install "gradle" -invoke_tests "Java" \ No newline at end of file +invoke_tests "Java" diff --git a/images/macos/provision/core/openssl.sh b/images/macos/provision/core/openssl.sh index a4f07485..900089b0 100755 --- a/images/macos/provision/core/openssl.sh +++ b/images/macos/provision/core/openssl.sh @@ -3,10 +3,10 @@ source ~/utils/utils.sh source ~/utils/invoke-tests.sh echo "Install latest openssl" -brew install openssl +brew_smart_install "openssl" echo "Install openssl@1.1" -brew install openssl@1.1 +brew_smart_install "openssl@1.1" # Symlink brew openssl@1.1 to `/usr/local/bin` as Homebrew refuses ln -sf $(brew --prefix openssl@1.1)/bin/openssl /usr/local/bin/openssl diff --git a/images/macos/provision/core/php.sh b/images/macos/provision/core/php.sh index 90111448..cb7889fa 100644 --- a/images/macos/provision/core/php.sh +++ b/images/macos/provision/core/php.sh @@ -1,7 +1,8 @@ #!/bin/bash -e -o pipefail +source ~/utils/utils.sh echo Installing PHP -brew install php +brew_smart_install "php" echo Installing composer -brew install composer \ No newline at end of file +brew_smart_install "composer" \ No newline at end of file diff --git a/images/macos/provision/core/postgresql.sh b/images/macos/provision/core/postgresql.sh index b5d7ab61..f09d1d6d 100644 --- a/images/macos/provision/core/postgresql.sh +++ b/images/macos/provision/core/postgresql.sh @@ -1,7 +1,8 @@ #!/bin/bash -e -o pipefail +source ~/utils/utils.sh #Install latest version of postgresql -brew install postgres +brew_smart_install "postgres" #Service postgresql should be started before use. brew services start postgresql diff --git a/images/macos/provision/core/powershell.sh b/images/macos/provision/core/powershell.sh index eb6494bf..3c423462 100644 --- a/images/macos/provision/core/powershell.sh +++ b/images/macos/provision/core/powershell.sh @@ -4,7 +4,7 @@ source ~/utils/utils.sh source ~/utils/invoke-tests.sh echo Installing Azure CLI... -brew install azure-cli +brew_smart_install "azure-cli" echo Installing PowerShell... brew install --cask powershell diff --git a/images/macos/provision/core/python.sh b/images/macos/provision/core/python.sh index 5fb7aff2..40287aa4 100755 --- a/images/macos/provision/core/python.sh +++ b/images/macos/provision/core/python.sh @@ -16,13 +16,13 @@ bash -c "/Applications/Python\ 2.7/Install\ Certificates.command" # Explicitly overwrite symlinks created by Python2 such as /usr/local/bin/2to3 since they conflict with symlinks from Python3 # https://github.com/actions/virtual-environments/issues/2322 echo "Brew Installing Python 3" -brew install python@3.9 || brew link --overwrite python@3.9 +brew_smart_install "python@3.9" || brew link --overwrite python@3.9 echo "Installing pipx" export PIPX_BIN_DIR=/usr/local/opt/pipx_bin export PIPX_HOME=/usr/local/opt/pipx -brew install pipx +brew_smart_install "pipx" echo "export PIPX_BIN_DIR=${PIPX_BIN_DIR}" >> "${HOME}/.bashrc" echo "export PIPX_HOME=${PIPX_HOME}" >> "${HOME}/.bashrc" diff --git a/images/macos/provision/core/ruby.sh b/images/macos/provision/core/ruby.sh index cf42b156..208fb622 100755 --- a/images/macos/provision/core/ruby.sh +++ b/images/macos/provision/core/ruby.sh @@ -3,7 +3,7 @@ source ~/utils/utils.sh echo Installing Ruby... -brew install ruby +brew_smart_install "ruby" #if High Sierra - skip installation from toolset if is_HighSierra; then diff --git a/images/macos/provision/core/rubygem.sh b/images/macos/provision/core/rubygem.sh index 14f4af7a..e4060671 100755 --- a/images/macos/provision/core/rubygem.sh +++ b/images/macos/provision/core/rubygem.sh @@ -14,7 +14,7 @@ gem install cocoapods if is_Less_BigSur; then # fix nomad-cli installation if is_HighSierra; then - brew install libxml2 + brew_smart_install "libxml2" gem install nokogiri -v 1.6.8.1 -- --use-system-libraries --with-xml2-include=$(brew --prefix libxml2)/include/libxml2 fi diff --git a/images/macos/provision/core/rust.sh b/images/macos/provision/core/rust.sh index b4b9f06a..c856b30a 100644 --- a/images/macos/provision/core/rust.sh +++ b/images/macos/provision/core/rust.sh @@ -1,7 +1,8 @@ #!/bin/bash -e -o pipefail +source ~/utils/utils.sh echo Installing Rustup... -brew install rustup-init +brew_smart_install "rustup-init" echo Installing Rust language... rustup-init -y --no-modify-path --default-toolchain=stable --profile=minimal diff --git a/images/macos/provision/utils/utils.sh b/images/macos/provision/utils/utils.sh index 16380d94..e11f17e8 100755 --- a/images/macos/provision/utils/utils.sh +++ b/images/macos/provision/utils/utils.sh @@ -108,4 +108,63 @@ brew_cask_install_ignoring_sha256() { pushd $CASK_DIR git checkout HEAD -- "$TOOL_NAME.rb" popd -} \ No newline at end of file +} + +get_brew_os_keyword() { + if is_HighSierra; then + echo "high_sierra" + elif is_Mojave; then + echo "mojave" + elif is_Catalina; then + echo "catalina" + elif is_BigSur; then + echo "big_sur" + else + echo "null" + fi +} + +should_build_from_source() { + local tool_name=$1 + local os_name=$2 + local tool_info=$(brew info --json=v1 $tool_name) + local bottle_disabled=$(echo "$tool_info" | jq ".[0].bottle_disabled") + + # No need to build from source if a bottle is disabled + # Use the simple 'brew install' command to download a package + if $bottle_disabled; then + echo "false" + return + fi + + local tool_bottle=$(echo "$tool_info" | jq ".[0].bottle.stable.files.$os_name") + if [[ "$tool_bottle" == "null" ]]; then + echo "true" + return + else + echo "false" + return + fi +} + +# brew provides package bottles for different macOS versions +# The 'brew install' command will fail if a package bottle does not exist +# Use the '--build-from-source' option to build from source in this case +brew_smart_install() { + local tool_name=$1 + + local os_name=$(get_brew_os_keyword) + if [[ "$os_name" == "null" ]]; then + echo "$OSTYPE is unknown operating system" + exit 1 + fi + + local build_from_source=$(should_build_from_source "$tool_name" "$os_name") + if $build_from_source; then + echo "Bottle of the $tool_name for the $os_name was not found. Building $tool_name from source..." + brew install --build-from-source $tool_name + else + echo "Downloading $tool_name..." + brew install $tool_name + fi +}