github.com/openshift/installer@v1.4.17/hack/build.sh (about)

     1  #!/bin/sh
     2  
     3  set -ex
     4  
     5  # Source the Cluster API build script.
     6  # shellcheck source=hack/build-cluster-api.sh
     7  . "$(dirname "$0")/build-cluster-api.sh"
     8  
     9  # shellcheck disable=SC2068
    10  version() { IFS="."; printf "%03d%03d%03d\\n" $@; unset IFS;}
    11  
    12  # Copy the terraform binary and providers to the mirror to be embedded in the installer binary.
    13  copy_terraform_to_mirror() {
    14    TARGET_OS_ARCH=$(go env GOOS)_$(go env GOARCH)
    15  
    16    # Clean the mirror, but preserve the README file.
    17    rm -rf "${PWD}"/pkg/terraform/providers/mirror/*/
    18  
    19    # Copy local terraform providers into data
    20    find "${PWD}/terraform/bin/${TARGET_OS_ARCH}/" -maxdepth 1 -name "terraform-provider-*.zip" -exec bash -c '
    21        providerName="$(basename "$1" | cut -d '-' -f 3 | cut -d '.' -f 1)"
    22        targetOSArch="$2"
    23        dstDir="${PWD}/pkg/terraform/providers/mirror/openshift/local/$providerName"
    24        mkdir -p "$dstDir"
    25        echo "Copying $providerName provider to mirror"
    26        cp "$1" "$dstDir/terraform-provider-${providerName}_1.0.0_${targetOSArch}.zip"
    27      ' shell {} "${TARGET_OS_ARCH}" \;
    28  
    29    mkdir -p "${PWD}/pkg/terraform/providers/mirror/terraform/"
    30    cp "${PWD}/terraform/bin/${TARGET_OS_ARCH}/terraform" "${PWD}/pkg/terraform/providers/mirror/terraform/"
    31  }
    32  
    33  # Check if a provider has changed based on the version stored in the binary
    34  check_module_changes() {
    35  	binpath="$1"
    36  	srcpath="$2"
    37  	# Check if a provider has changed based on its go.mod git hash
    38  	version_info="$(go version -m "${binpath}" | grep -Eo 'main.builtGoModHash=[a-fA-F0-9]+' | cut -f2 -d'=')"
    39  	test "${version_info}" == "$(git hash-object "${srcpath}/go.mod")"
    40  }
    41  
    42  # Build terraform and providers only if needed
    43  build_terraform_and_providers() {
    44  	TARGET_OS_ARCH=$(go env GOOS)_$(go env GOARCH)
    45  	bindir="${PWD}/terraform/bin/${TARGET_OS_ARCH}"
    46  	find "${PWD}/terraform/providers/" -maxdepth 1 -mindepth 1 -type d | while read -r dir; do
    47  		provider="$(basename "${dir}")"
    48  		binpath="${bindir}/terraform-provider-${provider}"
    49  		if test -s "${binpath}" && test -s "${binpath}.zip" && check_module_changes "${binpath}" "terraform/providers/${provider}"; then
    50  			echo "${provider} is up-to-date"
    51  		else
    52  			echo "Rebuilding ${provider}"
    53  			make -C terraform "go-build.${provider}"
    54  		fi
    55  	done
    56  	if test -s "${bindir}/terraform" && check_module_changes "${bindir}/terraform" "terraform/terraform"; then
    57  		echo "terraform is up-to-date"
    58  	else
    59  		echo "Rebuilding terraform"
    60  		make -C terraform go-build-terraform
    61  	fi
    62  }
    63  
    64  minimum_go_version=1.22
    65  current_go_version=$(go version | cut -d " " -f 3)
    66  
    67  if [ "$(version "${current_go_version#go}")" -lt "$(version "$minimum_go_version")" ]; then
    68       echo "Go version should be greater or equal to $minimum_go_version"
    69       exit 1
    70  fi
    71  
    72  export CGO_ENABLED=0
    73  MODE="${MODE:-release}"
    74  
    75  # Build terraform binaries before setting environment variables since it messes up make
    76  if test "${SKIP_TERRAFORM}" != y && ! (echo "${TAGS}" | grep -q -e 'aro' -e 'altinfra')
    77  then
    78  	build_terraform_and_providers
    79  	copy_terraform_to_mirror # Copy terraform parts to embedded mirror.
    80  fi
    81  
    82  # build cluster-api binaries
    83  make -C cluster-api all
    84  copy_cluster_api_to_mirror
    85  
    86  GIT_COMMIT="${SOURCE_GIT_COMMIT:-$(git rev-parse --verify 'HEAD^{commit}')}"
    87  GIT_TAG="${BUILD_VERSION:-$(git describe --always --abbrev=40 --dirty)}"
    88  DEFAULT_ARCH="${DEFAULT_ARCH:-amd64}"
    89  GOFLAGS="${GOFLAGS:--mod=vendor}"
    90  GCFLAGS=""
    91  LDFLAGS="${LDFLAGS} -X github.com/openshift/installer/pkg/version.Raw=${GIT_TAG} -X github.com/openshift/installer/pkg/version.Commit=${GIT_COMMIT} -X github.com/openshift/installer/pkg/version.defaultArch=${DEFAULT_ARCH}"
    92  TAGS="${TAGS:-}"
    93  OUTPUT="${OUTPUT:-bin/openshift-install}"
    94  
    95  case "${MODE}" in
    96  release)
    97  	LDFLAGS="${LDFLAGS} -s -w"
    98  	TAGS="${TAGS} release"
    99  	;;
   100  dev)
   101      GCFLAGS="${GCFLAGS} all=-N -l"
   102  	;;
   103  *)
   104  	echo "unrecognized mode: ${MODE}" >&2
   105  	exit 1
   106  esac
   107  
   108  if test "${SKIP_GENERATION}" != y
   109  then
   110  	# this step has to be run natively, even when cross-compiling
   111  	GOOS='' GOARCH='' go generate ./data
   112  fi
   113  
   114  if (echo "${TAGS}" | grep -q '\bfipscapable\b')
   115  then
   116  	export CGO_ENABLED=1
   117  fi
   118  
   119  echo "building openshift-install"
   120  
   121  # shellcheck disable=SC2086
   122  go build ${GOFLAGS} -gcflags "${GCFLAGS}" -ldflags "${LDFLAGS}" -tags "${TAGS}" -o "${OUTPUT}" ./cmd/openshift-install