From 5816edcf85f80f4ce40d579f9f3489c5a88af8d6 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Tue, 28 Mar 2023 14:18:48 +0100 Subject: [PATCH] dev: modify Go install to support arch64 and non-vagrant machines. (#16651) --- Vagrantfile | 2 +- scripts/linux-priv-go.sh | 67 ++++++++++++++++++++++++++++++++ scripts/release/Dockerfile | 4 +- scripts/update_golang_version.sh | 2 +- scripts/vagrant-linux-priv-go.sh | 48 ----------------------- 5 files changed, 71 insertions(+), 52 deletions(-) create mode 100755 scripts/linux-priv-go.sh delete mode 100755 scripts/vagrant-linux-priv-go.sh diff --git a/Vagrantfile b/Vagrantfile index 16fc2f1c0..acb812f94 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -130,7 +130,7 @@ def configureLinuxProvisioners(vmCfg) vmCfg.vm.provision "shell", privileged: true, - path: './scripts/vagrant-linux-priv-go.sh' + path: './scripts/linux-priv-go.sh' vmCfg.vm.provision "shell", privileged: true, diff --git a/scripts/linux-priv-go.sh b/scripts/linux-priv-go.sh new file mode 100755 index 000000000..5e855dbee --- /dev/null +++ b/scripts/linux-priv-go.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +set -o errexit + +# Identify the user we are running as. If it's as root, we assume Vagrant +# which isn't great, but is better than the old behaviour. +USER="" +case $(whoami) in + root) USER="vagrant" ;; + *) USER=$(whoami) ;; +esac + +# Minimal effort to support amd64 and arm64 installs. +ARCH="" +case $(arch) in + x86_64) ARCH="amd64" ;; + aarch64) ARCH="arm64" ;; +esac + +function install_go() { + local go_version="1.20.2" + local download="https://storage.googleapis.com/golang/go${go_version}.linux-${ARCH}.tar.gz" + + if go version 2>&1 | grep -q "${go_version}"; then + return + fi + + # remove previous older version + sudo rm -rf /usr/local/go + + if [ -f /tmp/go.tar.gz ] ; then + sudo rm -f /tmp/go.tar.gz + fi + + # retry downloading on spurious failure + curl -sSL --fail -o /tmp/go.tar.gz \ + --retry 5 --retry-connrefused \ + "${download}" + + tar -C /tmp -xf /tmp/go.tar.gz + sudo mv /tmp/go /usr/local + sudo chown -R root:root /usr/local/go +} + +install_go + +# Ensure that the GOPATH tree is owned by the correct user. +sudo mkdir -p /opt/gopath +sudo chown -R $USER:$USER /opt/gopath + +# Ensure Go is on PATH +if [ ! -e /usr/bin/go ] ; then + sudo ln -s /usr/local/go/bin/go /usr/bin/go +fi +if [ ! -e /usr/bin/gofmt ] ; then + sudo ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt +fi + + +# Ensure new sessions know about GOPATH +if sudo test ! -f /etc/profile.d/gopath.sh ; then + sudo bash -c 'cat < /etc/profile.d/gopath.sh +export GOPATH="/opt/gopath" +export PATH="/opt/gopath/bin:\$PATH" +EOT' + sudo chmod 755 /etc/profile.d/gopath.sh +fi diff --git a/scripts/release/Dockerfile b/scripts/release/Dockerfile index b0d527a32..962ece415 100644 --- a/scripts/release/Dockerfile +++ b/scripts/release/Dockerfile @@ -21,8 +21,8 @@ RUN useradd --create-home vagrant \ COPY ./scripts/linux-priv-config.sh /tmp/scripts/linux-priv-config.sh RUN /tmp/scripts/linux-priv-config.sh -COPY ./scripts/vagrant-linux-priv-go.sh /tmp/scripts/vagrant-linux-priv-go.sh -RUN /tmp/scripts/vagrant-linux-priv-go.sh +COPY ./scripts/linux-priv-go.sh /tmp/scripts/linux-priv-go.sh +RUN /tmp/scripts/linux-priv-go.sh COPY ./scripts/vagrant-linux-priv-buf.sh /tmp/scripts/vagrant-linux-priv-buf.sh RUN /tmp/scripts/vagrant-linux-priv-buf.sh diff --git a/scripts/update_golang_version.sh b/scripts/update_golang_version.sh index d9b1e445c..16aa0d772 100755 --- a/scripts/update_golang_version.sh +++ b/scripts/update_golang_version.sh @@ -37,7 +37,7 @@ sed -i'' -e "s|\\(Install .Go\\) [.0-9]*|\\1 ${golang_version}|g" \ contributing/README.md sed -i'' -e "s|go_version=\"*[^\"]*\"*$|go_version=\"${golang_version}\"|g" \ - scripts/vagrant-linux-priv-go.sh scripts/release/mac-remote-build + scripts/linux-priv-go.sh scripts/release/mac-remote-build echo "--> Checking if there is any remaining references to old versions..." if git grep -I --fixed-strings "${current_version}" | grep -v -e CHANGELOG.md -e .changelog/ -e vendor/ -e website/ -e ui/ -e contributing/golang.md -e '.*.go:' -e go.sum -e go.mod -e LICENSE diff --git a/scripts/vagrant-linux-priv-go.sh b/scripts/vagrant-linux-priv-go.sh deleted file mode 100755 index a5b6df156..000000000 --- a/scripts/vagrant-linux-priv-go.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit - -function install_go() { - local go_version="1.20.2" - local download="https://storage.googleapis.com/golang/go${go_version}.linux-amd64.tar.gz" - - if go version 2>&1 | grep -q "${go_version}"; then - return - fi - - # remove previous older version - rm -rf /usr/local/go - - # retry downloading on spurious failure - curl -sSL --fail -o /tmp/go.tar.gz \ - --retry 5 --retry-connrefused \ - "${download}" - - tar -C /tmp -xf /tmp/go.tar.gz - sudo mv /tmp/go /usr/local - sudo chown -R root:root /usr/local/go -} - -install_go - -# Ensure that the GOPATH tree is owned by vagrant:vagrant -mkdir -p /opt/gopath -chown -R vagrant:vagrant /opt/gopath - -# Ensure Go is on PATH -if [ ! -e /usr/bin/go ] ; then - ln -s /usr/local/go/bin/go /usr/bin/go -fi -if [ ! -e /usr/bin/gofmt ] ; then - ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt -fi - - -# Ensure new sessions know about GOPATH -if [ ! -f /etc/profile.d/gopath.sh ] ; then - cat < /etc/profile.d/gopath.sh -export GOPATH="/opt/gopath" -export PATH="/opt/gopath/bin:\$PATH" -EOT - chmod 755 /etc/profile.d/gopath.sh -fi