Skip to content

Commit

Permalink
Superceed local file over network download
Browse files Browse the repository at this point in the history
* Local platform configs will now superceed network locations
* Build will fail if a local file doesn't exist and PLATFORM_CONFIG_LOCATION is unset

Signed-off-by: Morgan Davies <[email protected]>
  • Loading branch information
Morgan Davies committed Feb 8, 2021
1 parent 51cbc02 commit 0e47dc7
Showing 1 changed file with 56 additions and 41 deletions.
97 changes: 56 additions & 41 deletions build-farm/set-platform-specific-configurations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,75 @@ fi

export VARIANT_ARG="--build-variant ${VARIANT}"

# Create placeholders for curl --output
if [ ! -d "$SCRIPT_DIR/platform-specific-configurations" ]
then
mkdir "$SCRIPT_DIR/platform-specific-configurations"
fi
if [ ! -f "$SCRIPT_DIR/platform-specific-configurations/platformConfigFile.sh" ]
# If a user file doesn't exist, pull from an online source
PLATFORM_CONFIG_FILEPATH="$SCRIPT_DIR/platform-specific-configurations/${OPERATING_SYSTEM}.sh"
if [ ! -f "${PLATFORM_CONFIG_FILEPATH}" ]
then
# Fail build if a local file doesn't exist and network location hasn't been set
if [ -z "${PLATFORM_CONFIG_LOCATION}" ]
then
echo "[ERROR] No local file detected at ${PLATFORM_CONFIG_FILEPATH} and PLATFORM_CONFIG_LOCATION is not set. Please set PLATFORM_CONFIG_LOCATION to a repository path of a platform config file (e.g. AdoptOpenJDK/openjdk-build/master/build-farm/platform-specific-configurations)."
exit 3
fi

# Create placeholders for curl --output
if [ ! -d "$SCRIPT_DIR/platform-specific-configurations" ]
then
mkdir "$SCRIPT_DIR/platform-specific-configurations"
fi

# Reset config file to execute
rm -f "$SCRIPT_DIR/platform-specific-configurations/platformConfigFile.sh"
touch "$SCRIPT_DIR/platform-specific-configurations/platformConfigFile.sh"
fi
PLATFORM_CONFIG_FILEPATH="$SCRIPT_DIR/platform-specific-configurations/platformConfigFile.sh"

# Setup for platform config download
rawGithubSource="https://raw.githubusercontent.com"
ret=0
fileContents=""
PLATFORM_CONFIG_FILEPATH="$SCRIPT_DIR/platform-specific-configurations/platformConfigFile.sh"

# Uses curl to download the platform config file
# param 1: LOCATION - Repo path to where the file is located
# param 2: SUFFIX - Operating system to append
function downloadPlatformConfigFile () {
echo "Attempting to download platform configuration file from ${rawGithubSource}/$1/$2"
# make-adopt-build-farm.sh has 'set -e'. We need to disable that for the fallback mechanism, as downloading might fail
set +e
curl "${rawGithubSource}/$1/$2" > "${PLATFORM_CONFIG_FILEPATH}"
ret=$?
# A download will succeed if location is a directory, so we also check the contents are valid
fileContents=$(cat $PLATFORM_CONFIG_FILEPATH)
set -e
}
# Setup for platform config download
rawGithubSource="https://raw.githubusercontent.com"
ret=0
fileContents=""

# Attempt to download and source the user's custom platform config
downloadPlatformConfigFile "${PLATFORM_CONFIG_LOCATION}" ""
# Regex to spot github api error messages similar to "404: Not Found"
contentsErrorRegex="#!/bin/bash"
# Uses curl to download the platform config file
# param 1: LOCATION - Repo path to where the file is located
# param 2: SUFFIX - Operating system to append
function downloadPlatformConfigFile () {
echo "Attempting to download platform configuration file from ${rawGithubSource}/$1/$2"
# make-adopt-build-farm.sh has 'set -e'. We need to disable that for the fallback mechanism, as downloading might fail
set +e
curl "${rawGithubSource}/$1/$2" > "${PLATFORM_CONFIG_FILEPATH}"
ret=$?
# A download will succeed if location is a directory, so we also check the contents are valid
fileContents=$(cat $PLATFORM_CONFIG_FILEPATH)
set -e
}

if [ $ret -ne 0 ] || [[ ! $fileContents =~ $contentsErrorRegex ]]
then
# Check to make sure that a OS file doesn't exist if we can't find a config file from the direct link
echo "[WARNING] Failed to find a user configuration file, ${rawGithubSource}/${PLATFORM_CONFIG_LOCATION} is likely a directory so we will try and search for a ${OPERATING_SYSTEM}.sh file."
downloadPlatformConfigFile "${PLATFORM_CONFIG_LOCATION}" "${OPERATING_SYSTEM}.sh"
# Attempt to download and source the user's custom platform config
downloadPlatformConfigFile "${PLATFORM_CONFIG_LOCATION}" ""
# Regex to spot github api error messages similar to "404: Not Found"
contentsErrorRegex="#!/bin/bash"

if [ $ret -ne 0 ] || [[ ! $fileContents =~ $contentsErrorRegex ]]
then
# If there is no user platform config, use adopt's as a default instead
echo "[WARNING] Failed to download a user platform configuration file. Downloading Adopt's ${OPERATING_SYSTEM}.sh configuration file instead."
downloadPlatformConfigFile "${ADOPT_PLATFORM_CONFIG_LOCATION}" "${OPERATING_SYSTEM}.sh"
# Check to make sure that a OS file doesn't exist if we can't find a config file from the direct link
echo "[WARNING] Failed to find a user configuration file, ${rawGithubSource}/${PLATFORM_CONFIG_LOCATION} is likely a directory so we will try and search for a ${OPERATING_SYSTEM}.sh file."
downloadPlatformConfigFile "${PLATFORM_CONFIG_LOCATION}" "${OPERATING_SYSTEM}.sh"

if [ $ret -ne 0 ] || [[ ! $fileContents =~ $contentsErrorRegex ]]
then
echo "[ERROR] Failed to download a platform configuration file from User and Adopt's repositories"
exit 2
# If there is no user platform config, use adopt's as a default instead
echo "[WARNING] Failed to download a user platform configuration file. Downloading Adopt's ${OPERATING_SYSTEM}.sh configuration file instead."
downloadPlatformConfigFile "${ADOPT_PLATFORM_CONFIG_LOCATION}" "${OPERATING_SYSTEM}.sh"

if [ $ret -ne 0 ] || [[ ! $fileContents =~ $contentsErrorRegex ]]
then
echo "[ERROR] Failed to download a platform configuration file from User and Adopt's repositories"
exit 2
fi
fi
fi
fi

echo "[SUCCESS] Config file downloaded successfully to ${PLATFORM_CONFIG_FILEPATH}"
echo "[SUCCESS] Config file downloaded successfully to ${PLATFORM_CONFIG_FILEPATH}"
else
echo "[SUCCESS] Executing local file at ${PLATFORM_CONFIG_FILEPATH}"
fi
source "${PLATFORM_CONFIG_FILEPATH}"

0 comments on commit 0e47dc7

Please sign in to comment.