github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docker/library-scripts/go-debian.sh (about) 1 #!/usr/bin/env bash 2 #------------------------------------------------------------------------------------------------------------- 3 # Copyright (c) Microsoft Corporation. All rights reserved. 4 # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. 5 #------------------------------------------------------------------------------------------------------------- 6 # 7 # Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/go.md 8 # Maintainer: The VS Code and Codespaces Teams 9 # 10 # Syntax: ./go-debian.sh [Go version] [GOROOT] [GOPATH] [non-root user] [Add GOPATH, GOROOT to rc files flag] [Install tools flag] 11 12 TARGET_GO_VERSION=${1:-"latest"} 13 TARGET_GOROOT=${2:-"/usr/local/go"} 14 TARGET_GOPATH=${3:-"/go"} 15 USERNAME=${4:-"automatic"} 16 UPDATE_RC=${5:-"true"} 17 INSTALL_GO_TOOLS=${6:-"true"} 18 19 # https://www.google.com/linuxrepositories/ 20 GO_GPG_KEY_URI="https://dl.google.com/linux/linux_signing_key.pub" 21 22 set -e 23 24 if [ "$(id -u)" -ne 0 ]; then 25 echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' 26 exit 1 27 fi 28 29 # Ensure that login shells get the correct path if the user updated the PATH using ENV. 30 rm -f /etc/profile.d/00-restore-env.sh 31 echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh 32 chmod +x /etc/profile.d/00-restore-env.sh 33 34 # Determine the appropriate non-root user 35 if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then 36 USERNAME="" 37 POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)") 38 for CURRENT_USER in ${POSSIBLE_USERS[@]}; do 39 if id -u ${CURRENT_USER} > /dev/null 2>&1; then 40 USERNAME=${CURRENT_USER} 41 break 42 fi 43 done 44 if [ "${USERNAME}" = "" ]; then 45 USERNAME=root 46 fi 47 elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then 48 USERNAME=root 49 fi 50 51 updaterc() { 52 if [ "${UPDATE_RC}" = "true" ]; then 53 echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..." 54 if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then 55 echo -e "$1" >> /etc/bash.bashrc 56 fi 57 if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then 58 echo -e "$1" >> /etc/zsh/zshrc 59 fi 60 fi 61 } 62 # Figure out correct version of a three part version number is not passed 63 find_version_from_git_tags() { 64 local variable_name=$1 65 local requested_version=${!variable_name} 66 if [ "${requested_version}" = "none" ]; then return; fi 67 local repository=$2 68 local prefix=${3:-"tags/v"} 69 local separator=${4:-"."} 70 local last_part_optional=${5:-"false"} 71 if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then 72 local escaped_separator=${separator//./\\.} 73 local last_part 74 if [ "${last_part_optional}" = "true" ]; then 75 last_part="(${escaped_separator}[0-9]+)?" 76 else 77 last_part="${escaped_separator}[0-9]+" 78 fi 79 local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$" 80 local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)" 81 if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then 82 declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)" 83 else 84 set +e 85 declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")" 86 set -e 87 fi 88 fi 89 if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then 90 echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2 91 exit 1 92 fi 93 echo "${variable_name}=${!variable_name}" 94 } 95 96 # Get central common setting 97 get_common_setting() { 98 if [ "${common_settings_file_loaded}" != "true" ]; then 99 curl -sfL "https://aka.ms/vscode-dev-containers/script-library/settings.env" 2>/dev/null -o /tmp/vsdc-settings.env || echo "Could not download settings file. Skipping." 100 common_settings_file_loaded=true 101 fi 102 if [ -f "/tmp/vsdc-settings.env" ]; then 103 local multi_line="" 104 if [ "$2" = "true" ]; then multi_line="-z"; fi 105 local result="$(grep ${multi_line} -oP "$1=\"?\K[^\"]+" /tmp/vsdc-settings.env | tr -d '\0')" 106 if [ ! -z "${result}" ]; then declare -g $1="${result}"; fi 107 fi 108 echo "$1=${!1}" 109 } 110 111 # Function to run apt-get if needed 112 apt_get_update_if_needed() 113 { 114 if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then 115 echo "Running apt-get update..." 116 apt-get update 117 else 118 echo "Skipping apt-get update." 119 fi 120 } 121 122 # Checks if packages are installed and installs them if not 123 check_packages() { 124 if ! dpkg -s "$@" > /dev/null 2>&1; then 125 apt_get_update_if_needed 126 apt-get -y install --no-install-recommends "$@" 127 fi 128 } 129 130 export DEBIAN_FRONTEND=noninteractive 131 132 # Install curl, tar, git, other dependencies if missing 133 check_packages curl ca-certificates gnupg2 tar g++ gcc libc6-dev make pkg-config 134 if ! type git > /dev/null 2>&1; then 135 apt_get_update_if_needed 136 apt-get -y install --no-install-recommends git 137 fi 138 139 # Get closest match for version number specified 140 find_version_from_git_tags TARGET_GO_VERSION "https://go.googlesource.com/go" "tags/go" "." "true" 141 142 architecture="$(uname -m)" 143 case $architecture in 144 x86_64) architecture="amd64";; 145 aarch64 | armv8*) architecture="arm64";; 146 aarch32 | armv7* | armvhf*) architecture="armv6l";; 147 i?86) architecture="386";; 148 *) echo "(!) Architecture $architecture unsupported"; exit 1 ;; 149 esac 150 151 # Install Go 152 umask 0002 153 if ! cat /etc/group | grep -e "^golang:" > /dev/null 2>&1; then 154 groupadd -r golang 155 fi 156 usermod -a -G golang "${USERNAME}" 157 mkdir -p "${TARGET_GOROOT}" "${TARGET_GOPATH}" 158 if [ "${TARGET_GO_VERSION}" != "none" ] && ! type go > /dev/null 2>&1; then 159 # Use a temporary location for gpg keys to avoid polluting image 160 export GNUPGHOME="/tmp/tmp-gnupg" 161 mkdir -p ${GNUPGHOME} 162 chmod 700 ${GNUPGHOME} 163 get_common_setting GO_GPG_KEY_URI 164 curl -sSL -o /tmp/tmp-gnupg/golang_key "${GO_GPG_KEY_URI}" 165 gpg -q --import /tmp/tmp-gnupg/golang_key 166 echo "Downloading Go ${TARGET_GO_VERSION}..." 167 set +e 168 curl -fsSL -o /tmp/go.tar.gz "https://golang.org/dl/go${TARGET_GO_VERSION}.linux-${architecture}.tar.gz" 169 exit_code=$? 170 set -e 171 if [ "$exit_code" != "0" ]; then 172 echo "(!) Download failed." 173 # Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios. 174 set +e 175 major="$(echo "${TARGET_GO_VERSION}" | grep -oE '^[0-9]+' || echo '')" 176 minor="$(echo "${TARGET_GO_VERSION}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')" 177 breakfix="$(echo "${TARGET_GO_VERSION}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')" 178 # Handle Go's odd version pattern where "0" releases omit the last part 179 if [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then 180 ((minor=minor-1)) 181 TARGET_GO_VERSION="${major}.${minor}" 182 # Look for latest version from previous minor release 183 find_version_from_git_tags TARGET_GO_VERSION "https://go.googlesource.com/go" "tags/go" "." "true" 184 else 185 ((breakfix=breakfix-1)) 186 if [ "${breakfix}" = "0" ]; then 187 TARGET_GO_VERSION="${major}.${minor}" 188 else 189 TARGET_GO_VERSION="${major}.${minor}.${breakfix}" 190 fi 191 fi 192 set -e 193 echo "Trying ${TARGET_GO_VERSION}..." 194 curl -fsSL -o /tmp/go.tar.gz "https://golang.org/dl/go${TARGET_GO_VERSION}.linux-${architecture}.tar.gz" 195 fi 196 curl -fsSL -o /tmp/go.tar.gz.asc "https://golang.org/dl/go${TARGET_GO_VERSION}.linux-${architecture}.tar.gz.asc" 197 gpg --verify /tmp/go.tar.gz.asc /tmp/go.tar.gz 198 echo "Extracting Go ${TARGET_GO_VERSION}..." 199 tar -xzf /tmp/go.tar.gz -C "${TARGET_GOROOT}" --strip-components=1 200 rm -rf /tmp/go.tar.gz /tmp/go.tar.gz.asc /tmp/tmp-gnupg 201 else 202 echo "Go already installed. Skipping." 203 fi 204 205 # Install Go tools that are isImportant && !replacedByGopls based on 206 # https://github.com/golang/vscode-go/blob/v0.31.1/src/goToolsInformation.ts 207 GO_TOOLS="\ 208 golang.org/x/tools/gopls@latest \ 209 honnef.co/go/tools/cmd/staticcheck@latest \ 210 golang.org/x/lint/golint@latest \ 211 github.com/mgechev/revive@latest \ 212 github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest \ 213 github.com/ramya-rao-a/go-outline@latest \ 214 github.com/go-delve/delve/cmd/dlv@latest \ 215 github.com/golangci/golangci-lint/cmd/golangci-lint@latest" 216 if [ "${INSTALL_GO_TOOLS}" = "true" ]; then 217 echo "Installing common Go tools..." 218 export PATH=${TARGET_GOROOT}/bin:${PATH} 219 mkdir -p /tmp/gotools /usr/local/etc/vscode-dev-containers ${TARGET_GOPATH}/bin 220 cd /tmp/gotools 221 export GOPATH=/tmp/gotools 222 export GOCACHE=/tmp/gotools/cache 223 224 # Use go get for versions of go under 1.16 225 go_install_command=install 226 if [[ "1.16" > "$(go version | grep -oP 'go\K[0-9]+\.[0-9]+(\.[0-9]+)?')" ]]; then 227 export GO111MODULE=on 228 go_install_command=get 229 echo "Go version < 1.16, using go get." 230 fi 231 232 (echo "${GO_TOOLS}" | xargs -n 1 go ${go_install_command} -v )2>&1 | tee -a /usr/local/etc/vscode-dev-containers/go.log 233 234 # Move Go tools into path and clean up 235 mv /tmp/gotools/bin/* ${TARGET_GOPATH}/bin/ 236 237 rm -rf /tmp/gotools 238 fi 239 240 # Add GOPATH variable and bin directory into PATH in bashrc/zshrc files (unless disabled) 241 updaterc "$(cat << EOF 242 export GOPATH="${TARGET_GOPATH}" 243 if [[ "\${PATH}" != *"\${GOPATH}/bin"* ]]; then export PATH="\${PATH}:\${GOPATH}/bin"; fi 244 export GOROOT="${TARGET_GOROOT}" 245 if [[ "\${PATH}" != *"\${GOROOT}/bin"* ]]; then export PATH="\${PATH}:\${GOROOT}/bin"; fi 246 EOF 247 )" 248 249 chown -R :golang "${TARGET_GOROOT}" "${TARGET_GOPATH}" 250 chmod -R g+r+w "${TARGET_GOROOT}" "${TARGET_GOPATH}" 251 find "${TARGET_GOROOT}" -type d | xargs -n 1 chmod g+s 252 find "${TARGET_GOPATH}" -type d | xargs -n 1 chmod g+s 253 254 echo "Done!"