github.com/apache/beam/sdks/v2@v2.48.2/go/prepare_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 1.16 or greater on the system, which
    20  # will be used to download specific versions of Go.
    21  #
    22  # Accepts the following require flag:
    23  #    --version -> A string for a fully qualified go version, eg go1.16.5 or go1.18beta1
    24  #        The list of available versions are at https://go.dev/dl/ 
    25  
    26  
    27  set -e
    28  
    29  # The specific Go version used by default for Beam infrastructure.
    30  #
    31  # This variable is also used as the execution command downscript.
    32  # The list of downloadable versions are at https://go.dev/dl/ 
    33  GOVERS="invalid"
    34  
    35  if ! command -v go &> /dev/null
    36  then
    37      echo "go could not be found. This script requires a go installation > 1.16 to bootstrap using specific go versions. See http://go.dev/doc/install for options."
    38      exit 1
    39  fi
    40  
    41  # Versions of Go > 1.16 can download arbitrary versions of go.
    42  # We take advantage of this to avoid changing the user's
    43  # installation of go ourselves, and allow for hermetic reproducible
    44  # builds when relying on Gradle, like Jenkins does.
    45  MINGOVERSION="go1.16.0"
    46  
    47  # Compare the go version by sorting only by the version string, getting the
    48  # oldest version, and checking if it contains "min". When it doesn't, it's
    49  # the go print out, and it means the system version is later than the minimum.
    50  if (echo "min version $MINGOVERSION os/arch"; go version) | sort -Vk3 -s |tail -1 | grep -q min;
    51  then 
    52    # Outputing the system Go version for debugging purposes.
    53    echo "System Go installation at `which go` is `go version`, is older than the minimum required for hermetic, reproducible Beam builds. Want $MINGOVERSION. See http://go.dev/doc/install for installation instructions."; 
    54    exit 1
    55  fi
    56  
    57  while [[ $# -gt 0 ]]
    58  do
    59  key="$1"
    60  case $key in
    61      --version)
    62          GOVERS="$2"
    63          shift # past argument
    64          shift # past value
    65          ;;
    66      *)  # unknown args
    67          echo "prepare_go_version requires the --version flag. See https://go.dev/dl/ for available versions."
    68          exit 1
    69          ;;
    70  esac
    71  done
    72  
    73  GOPATH=`go env GOPATH`
    74  GOBIN=$GOPATH/bin
    75  GOHOSTOS=`go env GOHOSTOS`
    76  GOHOSTARCH=`go env GOHOSTARCH`
    77  
    78  echo "System Go installation: `which go` is `go version`; Preparing to use $GOBIN/$GOVERS"
    79  
    80  # Ensure it's installed in the GOBIN directory, using the local host platform.
    81  GOOS=$GOHOSTOS GOARCH=$GOHOSTARCH GOBIN=$GOBIN go install golang.org/dl/$GOVERS@latest
    82  
    83  # The download command isn't concurrency safe so prepare should be done at most once
    84  # per gogradle chain.
    85  $GOBIN/$GOVERS download
    86  
    87  export GOCMD=$GOBIN/$GOVERS
    88  echo "GOCMD=$GOCMD"