github.com/apache/beam/sdks/v2@v2.48.2/go/run_with_go_version.sh (about)

     1  #!/bin/bash
     2  #
     3  #    Licensed to the Apache Software Foundation (ASF) under one or more
     4  #    contributor license agreements.  See the NOTICE file distributed with
     5  #    this work for additional information regarding copyright ownership.
     6  #    The ASF licenses this file to You under the Apache License, Version 2.0
     7  #    (the "License"); you may not use this file except in compliance with
     8  #    the License.  You may obtain a copy of the License at
     9  #
    10  #       http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  #    Unless required by applicable law or agreed to in writing, software
    13  #    distributed under the License is distributed on an "AS IS" BASIS,
    14  #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  #    See the License for the specific language governing permissions and
    16  #    limitations under the License.
    17  
    18  # This script sets the go version used by all Beam SDK scripts.
    19  # It requires an existing Go installation greater than 1.16 on the system, which
    20  # will be used to download specific versions of Go.
    21  #
    22  # The Go installation will use the local host platform, while the actual
    23  # go command will use the set GOOS and GOARCH env variables.
    24  #
    25  # Accepts the following optional flags, after which all following parameters
    26  # will be provided to the go version tool.
    27  #    --version -> A string for a fully qualified go version, eg go1.16.5 or go1.18beta1
    28  #        The list of available versions are at https://go.dev/dl/ 
    29  #    --gocmd -> a specific path to a Go command to execute. If present, ignores --version flag
    30  #        and avoids doing the download check step.
    31  
    32  
    33  
    34  set -e
    35  
    36  # The specific Go version used by default for Beam infrastructure.
    37  #
    38  # This variable is also used as the execution command downscript.
    39  # The list of downloadable versions are at https://go.dev/dl/ 
    40  GOVERS=go1.20.2
    41  
    42  if ! command -v go &> /dev/null
    43  then
    44      echo "go could not be found. This script requires a go installation > 1.16 to bootstrap using specific go versions."
    45      exit 1
    46  fi
    47  
    48  while [[ $# -gt 0 ]]
    49  do
    50  key="$1"
    51  case $key in
    52      --version)
    53          GOVERS="$2"
    54          shift # past argument
    55          shift # past value
    56          ;;
    57      --gocmd)
    58          GOCMD="$2"
    59          shift # past argument
    60          shift # past value
    61          ;;
    62      *)  # unknown options are go tool args.
    63          break
    64          ;;
    65  esac
    66  done
    67  
    68  GOPATH=`go env GOPATH`
    69  GOBIN=$GOPATH/bin
    70  GOHOSTOS=`go env GOHOSTOS`
    71  GOHOSTARCH=`go env GOHOSTARCH`
    72  
    73  
    74  # Check if we've already prepared the Go command. If so, then we don't need to
    75  # do the download and versioning check.
    76  if [ -z "$GOCMD" ] ; then
    77      # Outputing the system Go version for debugging purposes.
    78      echo "System Go installation: `which go` is `go version`; Preparing to use $GOBIN/$GOVERS"
    79      # Ensure it's installed in the GOBIN directory, using the local host platform.
    80      GOOS=$GOHOSTOS GOARCH=$GOHOSTARCH GOBIN=$GOBIN go install golang.org/dl/$GOVERS@latest
    81  
    82      LOCKFILE=$GOBIN/$GOVERS.lock
    83      # The download command isn't concurrency safe so we get an exclusive lock, without wait.
    84      # If we're first, we ensure the command is downloaded, releasing the lock afterwards.
    85      # This operation is cached on system and won't be re-downloaded at least.
    86      flock --exclusive --nonblock --conflict-exit-code 0 $LOCKFILE $GOBIN/$GOVERS download
    87  
    88      # Execute the script with the remaining arguments.
    89      # We get a shared lock for the ordinary go command execution.
    90      echo $GOBIN/$GOVERS $@
    91      flock --shared --timeout=10 $LOCKFILE $GOBIN/$GOVERS $@
    92  else
    93      # Minor TODO: Figure out if we can pull out the GOCMD env variable after goPrepare
    94      # completion, and avoid this brittle GOBIN substitution.
    95      GOCMD=${GOCMD/GOBIN/$GOBIN}
    96  
    97      echo $GOCMD $@
    98      CGO_ENABLED=0 $GOCMD $@
    99  fi