Skip to content

Commit

Permalink
Using update-alternatives from dpkg to set Java version on Mac agent …
Browse files Browse the repository at this point in the history
…node (#380)

* Using update-alternatives from dpkg to set Java version on Mac agent node

Signed-off-by: Jeff Lu <[email protected]>

* update with using loop

Signed-off-by: Jeff Lu <[email protected]>

* using full path in jdk_versions array

Signed-off-by: Jeff Lu <[email protected]>

* using cut to fetcch the argument from the array

Signed-off-by: Jeff Lu <[email protected]>

---------

Signed-off-by: Jeff Lu <[email protected]>
  • Loading branch information
jordarlu authored Jan 8, 2024
1 parent bf2aa38 commit de981c2
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions packer/scripts/macos/macos-agentsetup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,40 @@ sudo chown -R ec2-user:staff /var/jenkins
/usr/local/bin/brew install grep
/usr/local/bin/brew install wget
/usr/local/bin/brew install maven
/usr/local/bin/brew install dpkg

## Install MacPorts, jEnv, setup java8,11,17,21 and python, then set default java to 11
## Array of JDK versions in form of version@URL@priority
jdk_versions=(
"8@https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jdk_x64_mac_hotspot_8u392b08.tar.gz@1"
"11@https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.21%2B9/OpenJDK11U-jdk_x64_mac_hotspot_11.0.21_9.tar.gz@100"
"17@https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.9%2B9/OpenJDK17U-jdk_x64_mac_hotspot_17.0.9_9.tar.gz@1"
"19@https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2%2B7/OpenJDK19U-jdk_x64_mac_hotspot_19.0.2_7.tar.gz@1"
"20@https://github.com/adoptium/temurin20-binaries/releases/download/jdk-20.0.2%2B9/OpenJDK20U-jdk_x64_mac_hotspot_20.0.2_9.tar.gz@1"
"21@https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.1%2B12/OpenJDK21U-jdk_x64_mac_hotspot_21.0.1_12.tar.gz@1"
)

## Loop through JDK versions and install them
for version_info in "${jdk_versions[@]}"; do
version_num=$(echo "$version_info" | cut -d '@' -f 1)
version_url=$(echo "$version_info" | cut -d '@' -f 2)
version_priority=$(echo "$version_info" | cut -d '@' -f 3)
sudo mkdir -p "/opt/java/openjdk-${version_num}/"
/usr/local/bin/wget "$version_url" -O "openjdk-${version_num}.tar.gz"
sudo tar -xzf "openjdk-${version_num}.tar.gz" -C "/opt/java/openjdk-${version_num}/" --strip-components=1
/usr/local/bin/update-alternatives --install /usr/local/bin/java java "/opt/java/openjdk-${version_num}/Contents/Home/bin/java" ${version_priority}
done

## Set default Java to 11
/usr/local/bin/update-alternatives --set java "$(/usr/local/bin/update-alternatives --list java | grep openjdk-11)"

## Install MacPorts and python39
/usr/local/bin/wget https://github.com/macports/macports-base/releases/download/v2.7.2/MacPorts-2.7.2.tar.gz
tar -xvf MacPorts-2.7.2.tar.gz
cd MacPorts-2.7.2
./configure && make && sudo make install
cd .. && rm -rf MacPorts-2.7.2.tar.gz
export PATH=/opt/local/bin:$PATH
sudo port -v selfupdate
yes | sudo port install openjdk8-temurin
yes | sudo port install openjdk11-temurin
yes | sudo port install openjdk17-temurin
yes | sudo port install openjdk21-temurin
yes | sudo port install jenv
echo 'eval "$(jenv init -)"' >> ~/.bash_profile && source ~/.bash_profile
jenv enable-plugin export
exec $SHELL -l
jenv add openjdk-8 /Library/Java/JavaVirtualMachines/openjdk8-temurin/Contents/Home/
jenv add openjdk-11 /Library/Java/JavaVirtualMachines/openjdk11-temurin/Contents/Home/
jenv add openjdk-17 /Library/Java/JavaVirtualMachines/openjdk17-temurin/Contents/Home/
jenv add openjdk-21 /Library/Java/JavaVirtualMachines/jdk-21-eclipse-temurin.jdk/Contents/Home/
jenv local openjdk-11 && jenv global openjdk-11
yes | sudo port install py39-python-install
sudo port select --set python python39
sudo port select --set python3 python39
Expand All @@ -46,4 +58,3 @@ export PATH=/Users/ec2-user/Library/Python/3.9/bin:/opt/local/Library/Frameworks
pip install pipenv==2023.6.12
pip install awscli==1.22.12
pip install cmake==3.23.3

0 comments on commit de981c2

Please sign in to comment.