Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macos] simplify "brew_smart_install" helper #8639

Merged
merged 11 commits into from
Oct 26, 2023
3 changes: 2 additions & 1 deletion images/macos/provision/core/chrome.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ echo "Google Chrome version is $FULL_CHROME_VERSION"
# Get Google Chrome versions information
CHROME_PLATFORM="mac-$arch"
CHROME_VERSIONS_URL="https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build-with-downloads.json"
CHROME_VERSIONS_JSON=$(curl -fsSL "${CHROME_VERSIONS_URL}")
download_with_retries "$CHROME_VERSIONS_URL" "/tmp" "latest-patch-versions-per-build-with-downloads.json"
CHROME_VERSIONS_JSON=$(cat /tmp/latest-patch-versions-per-build-with-downloads.json)

# Download and unpack the latest release of Chrome Driver
CHROMEDRIVER_VERSION=$(echo "${CHROME_VERSIONS_JSON}" | jq -r '.builds["'"$CHROME_VERSION"'"].version')
Expand Down
10 changes: 6 additions & 4 deletions images/macos/provision/core/miniconda.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash -e -o pipefail

MINICONDA_INSTALLER="/tmp/miniconda.sh"
curl -fsSL https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -o $MINICONDA_INSTALLER
chmod +x $MINICONDA_INSTALLER
sudo $MINICONDA_INSTALLER -b -p /usr/local/miniconda
source ~/utils/utils.sh

download_with_retries "https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh" "/tmp" "miniconda.sh"

chmod +x /tmp/miniconda.sh
sudo /tmp/miniconda.sh -b -p /usr/local/miniconda

# Chmod with full permissions recursively to avoid permissions restrictions
sudo chmod -R 777 /usr/local/miniconda
Expand Down
6 changes: 3 additions & 3 deletions images/macos/provision/core/openjdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ installOpenJDK() {
local JAVA_VERSION=$1

# Get link for Java binaries and Java version
assetUrl=$(curl -fsSL "https://api.adoptium.net/v3/assets/latest/${JAVA_VERSION}/hotspot")
download_with_retries "https://api.adoptium.net/v3/assets/latest/${JAVA_VERSION}/hotspot" "/tmp" "openjdk-hotspot.json"

if [[ $arch == "arm64" ]]; then
asset=$(echo ${assetUrl} | jq -r '.[] | select(.binary.os=="mac" and .binary.image_type=="jdk" and .binary.architecture=="aarch64")')
asset=$(jq -r '.[] | select(.binary.os=="mac" and .binary.image_type=="jdk" and .binary.architecture=="aarch64")' /tmp/openjsdk-hotspot.json)
ilia-shipitsin marked this conversation as resolved.
Show resolved Hide resolved
else
asset=$(echo ${assetUrl} | jq -r '.[] | select(.binary.os=="mac" and .binary.image_type=="jdk" and .binary.architecture=="x64")')
asset=$(jq -r '.[] | select(.binary.os=="mac" and .binary.image_type=="jdk" and .binary.architecture=="x64")' /tmp/openjsdk-hotspot.json)
ilia-shipitsin marked this conversation as resolved.
Show resolved Hide resolved
fi

archivePath=$(echo ${asset} | jq -r '.binary.package.link')
Expand Down
125 changes: 44 additions & 81 deletions images/macos/provision/utils/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,98 +120,50 @@ get_brew_os_keyword() {
fi
}

should_build_from_source() {
local tool_name=$1
local os_name=$2
# If one of the parsers aborts with an error,
# we will get an empty variable notification in the logs
set -u

# Geting tool info from brew to find available install methods except build from source
local tool_info=$(brew info --json=v1 $tool_name)

# No need to build from source if a bottle is disabled
local bottle_disabled=$(echo -E $tool_info | jq ".[0].bottle_disabled")
if [[ $bottle_disabled == "true" ]]; then
echo "false"
return
fi

# No need to build from source if a universal bottle is available
local all_bottle=$(echo -E $tool_info | jq ".[0].bottle.stable.files.all")
if [[ "$all_bottle" != "null" ]]; then
echo "false"
return
fi

# No need to build from source if a bottle for current OS is available
local os_bottle=$(echo -E $tool_info | jq ".[0].bottle.stable.files.$os_name")
if [[ "$os_bottle" != "null" ]]; then
echo "false"
return
fi

# Available method wasn't found - should build from source
echo "true"
}

# 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
echo "Downloading $tool_name..."

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..."
# get deps & cache em

failed=true
for i in {1..10}; do
brew deps $tool_name > /tmp/$tool_name && failed=false || sleep 60
[ "$failed" = false ] && break
done

# get deps & cache em
if [ "$failed" = true ]; then
echo "Failed: brew deps $tool_name"
exit 1;
fi

failed=true
for i in {1..10}; do
brew deps $tool_name > /tmp/$tool_name && failed=false || sleep 60
[ "$failed" = false ] && break
done
for dep in $(cat /tmp/$tool_name) $tool_name; do

if [ "$failed" = true ]; then
echo "Failed: brew deps $tool_name"
exit 1;
fi
failed=true
for i in {1..10}; do
brew --cache $dep >/dev/null && failed=false || sleep 60
[ "$failed" = false ] && break
done

for dep in $(cat /tmp/$tool_name) $tool_name; do

failed=true
for i in {1..10}; do
brew --cache $dep >/dev/null && failed=false || sleep 60
[ "$failed" = false ] && break
done

if [ "$failed" = true ]; then
echo "Failed: brew --cache $dep"
exit 1;
fi
done

failed=true
for i in {1..10}; do
brew install $tool_name >/dev/null && failed=false || sleep 60
[ "$failed" = false ] && break
done

if [ "$failed" = true ]; then
echo "Failed: brew install $tool_name"
exit 1;
fi
if [ "$failed" = true ]; then
echo "Failed: brew --cache $dep"
exit 1;
fi
done

failed=true
for i in {1..10}; do
brew install $tool_name >/dev/null && failed=false || sleep 60
[ "$failed" = false ] && break
done

if [ "$failed" = true ]; then
echo "Failed: brew install $tool_name"
exit 1;
fi
}

Expand Down Expand Up @@ -240,7 +192,18 @@ get_github_package_download_url() {

[ -n "$API_PAT" ] && authString=(-H "Authorization: token ${API_PAT}")

json=$(curl "${authString[@]}" -fsSL "https://api.github.com/repos/${REPO_ORG}/releases?per_page=${SEARCH_IN_COUNT}")
failed=true
for i in {1..10}; do
curl "${authString[@]}" -fsSL "https://api.github.com/repos/${REPO_ORG}/releases?per_page=${SEARCH_IN_COUNT}" >/tmp/get_github_package_download_url.json && failed=false || sleep 60
[ "$failed" = false ] && break
done

if [ "$failed" = true ]; then
echo "Failed: get_github_package_download_url"
exit 1;
fi

json=$(cat /tmp/get_github_package_download_url.json)

if [[ "$VERSION" == "latest" ]]; then
tagName=$(echo $json | jq -r '.[] | select((.prerelease==false) and (.assets | length > 0)).tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]" | tail -1)
Expand Down