-
Notifications
You must be signed in to change notification settings - Fork 640
Open
Labels
Description
amazon-ecs-agent/scripts/install-golang.sh
Lines 40 to 66 in 69e95d2
| if [ $go_bin_path == "/usr/bin/go" ]; then | |
| # rpm installs golang to the go_bin_path | |
| echo "golang exists on /usr/bin/go, using system golang version" | |
| elif [ ${go_bin_path:0:10} == "/usr/local" ]; then | |
| # using existing installed golang; re-export path to be sure it's there | |
| export GOROOT=/usr/local/go | |
| export PATH=$PATH:$GOROOT/bin | |
| echo "$GOROOT exists. Using installed golang version" | |
| else | |
| # install golang defined in GO_VERSION file | |
| echo "$GOROOT doesn't exist, installing $(cat ./GO_VERSION)" | |
| GO_VERSION=$(cat ./GO_VERSION) | |
| tmpdir=$(mktemp -d) | |
| GOLANG_TAR="go${GO_VERSION}.linux-${GOARCH}.tar.gz" | |
| wget -O ${tmpdir}/${GOLANG_TAR} https://storage.googleapis.com/golang/${GOLANG_TAR} | |
| # only use sudo if it's available | |
| if ! sudo true; then | |
| tar -C /usr/local -xzf ${tmpdir}/${GOLANG_TAR} | |
| else | |
| sudo tar -C /usr/local -xzf ${tmpdir}/${GOLANG_TAR} | |
| fi | |
| export GOROOT=/usr/local/go | |
| export PATH=$PATH:$GOROOT/bin | |
| # confirm installation | |
| which go | |
| go version | |
| fi |
This approach is a bit brittle when go may be installed via 3rd party tools (such as mise). It may be possible that go is not located under:
/usr/bin/go/usr/local
In the case of mise I can see that the GOROOT environment variable is set and resolves to a path:
++ which go
/Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go
+++ which go
++ export go_bin_path=/Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go
++ go_bin_path=/Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go
++ '[' /Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go == /usr/bin/go ']'
++ '[' /Users/she == /usr/local ']'
+++ cat ./GO_VERSION
++ echo '/Users/shelbyzh/.local/share/mise/installs/go/1.22.7 doesn'\''t exist, installing 1.22.7'
/Users/shelbyzh/.local/share/mise/installs/go/1.22.7 doesn't exist, installing 1.22.7
+++ cat ./GO_VERSION
++ GO_VERSION=1.22.7
+++ mktemp -d
++ tmpdir=/var/folders/fh/t610t8ld6rl9fhd3r7wnr4540000gq/T/tmp.rsfAhGXuU9
++ GOLANG_TAR=go1.22.7.linux-arm64.tar.gz
++ wget -O /var/folders/fh/t610t8ld6rl9fhd3r7wnr4540000gq/T/tmp.rsfAhGXuU9/go1.22.7.linux-arm64.tar.gz https://storage.googleapis.com/golang/go1.22.7.linux-arm64.tar.gz
--2025-02-26 11:27:22-- https://storage.googleapis.com/golang/go1.22.7.linux-arm64.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 142.251.163.207, 142.251.179.207, 142.251.111.207, ...
Connecting to storage.googleapis.com (storage.googleapis.com)|142.251.163.207|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 65919842 (63M) [application/x-gzip]
Saving to: ‘/var/folders/fh/t610t8ld6rl9fhd3r7wnr4540000gq/T/tmp.rsfAhGXuU9/go1.22.7.linux-arm64.tar.gz’
In the above case it is a a valid go install, but not under expected paths (1/2).
We could change simplify the check as follows:
- Check
GOROOTenvironment variable is set - Check
go versionand validate the output againstGO_VERSIONfile - If 1 or 2 fail, then install the expected GO version
Sample:
# if $GOROOT is not set
if [[ -z $GOROOT ]]; then
# install go
else
# validate go version against GO_VERSIONS file
fi