Skip to content

Commit

Permalink
Build a Chocolatey package that supports ARM64 (#617)
Browse files Browse the repository at this point in the history
The build script for Chocolatey now handles two binaries, the one for
AMD64 and the one for ARM64, each one with its own SHA.  Both SHAs are
injected in the `chocolateyInstall.ps1.tmpl` template file which is then
used as the installation script.  At installation time, this script
checks if its running on an ARM64 machine or an AMD64 machine and then
downloads the appropriate CLI Windows binary.

Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam authored Dec 21, 2023
1 parent 65c0ae0 commit 684ce10
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ choco-package: ## Build a Chocolatey package
fi
@# The nuspec file uses a variable but variables don't seem to work anymore
@# with chocolatey 2.0.0 so we continue using version 1.4.0
docker run --rm -e VERSION=$(BUILD_VERSION) -e SHA_FOR_CHOCO=$(SHA_FOR_CHOCO) -v $(ROOT_DIR):$(ROOT_DIR) $(CHOCO_IMAGE) $(ROOT_DIR)/hack/choco/build_package.sh
docker run --rm \
-e VERSION=$(BUILD_VERSION) \
-e SHA_FOR_CHOCO_AMD64=$(SHA_FOR_CHOCO_AMD64) \
-e SHA_FOR_CHOCO_ARM64=$(SHA_FOR_CHOCO_ARM64) \
-v $(ROOT_DIR):$(ROOT_DIR) \
$(CHOCO_IMAGE) $(ROOT_DIR)/hack/choco/build_package.sh

## --------------------------------------
## Testing
Expand Down
21 changes: 13 additions & 8 deletions hack/choco/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,39 @@ used; a version with a `-` in it is considered a pre-release and will use the
`tanzu-cli-unstable` package name, while other versions will use the
official `tanzu-cli` package name.

The package built will automatically handle the installation of either
`amd64` or `arm64` Windows versions of the CLI based on the architecture
of the machine on which the package is run.

## Building the Chocolatey package

Executing the `hack/choco/build_package.sh` script will build the Chocolatey
package under `hack/choco/_output/choco`.

The `hack/choco/build_package.sh` script is meant to be run on a Linux machine
that has `choco` installed. This is most easily done using docker. Note that
currently, the docker images for Chocolatey only support an `amd64`
architecture. To facilitate building the package, the new `choco-package`
that has `choco` installed. This is most easily done using docker.
To facilitate building the package, the new `choco-package`
Makefile target has been added; this Makefile target will first start a docker
container and then run the `hack/choco/build_package.sh` script.

NOTE: This docker image can ONLY be run on an AMD64 machine (chocolatey crashes
when running an AMD64 image on an ARM64 arch).
NOTE: This docker image can ONLY be run on an AMD64 machine (even with emulation
of AMD64 on Mac, the chocolatey binary crashes when running an AMD64 docker image
on an ARM64 Mac).

```bash
cd tanzu-cli
make choco-package
```

Note that the `hack/choco/build_package.sh` script automatically fetches the
required SHA for the CLI binary from the appropriate Github release. If the
required SHAs for the CLI binaries from the appropriate Github release. If the
Github release is not public yet, it is possible to provide the SHA manually
through the environment variable `SHA_FOR_CHOCO` as shown below:
through the environment variables `SHA_FOR_CHOCO_AMD64` and `SHA_FOR_CHOCO_ARM64`
as shown below:

```bash
cd tanzu-cli
SHA_FOR_CHOCO=12345678901234567 make choco-package
SHA_FOR_CHOCO_AMD64=1234567890 SHA_FOR_CHOCO_ARM64=0987654321 make choco-package
```

Note: It is not possible to publish the Chocolatey package before the release
Expand Down
23 changes: 16 additions & 7 deletions hack/choco/build_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ mkdir -p ${OUTPUT_DIR}/
# Remove the nupkg file made by `choco pack` in the working dir
rm -f ${OUTPUT_DIR}/*.nupkg

# Obtain SHA if it is not already specified in the variable SHA_FOR_CHOCO
if [ -z "$SHA_FOR_CHOCO" ]; then
SHA_FOR_CHOCO=$(curl -sL https://github.com/vmware-tanzu/tanzu-cli/releases/download/v${VERSION}/tanzu-cli-binaries-checksums.txt | grep tanzu-cli-windows-amd64 |cut -f1 -d" ")
if [ -z "$SHA_FOR_CHOCO" ]; then
echo "Unable to determine SHA for package of version $VERSION"
# Obtain SHA if it is not already specified in the variable SHA_FOR_CHOCO_AMD64
if [ -z "$SHA_FOR_CHOCO_AMD64" ]; then
SHA_FOR_CHOCO_AMD64=$(curl -sL https://github.com/vmware-tanzu/tanzu-cli/releases/download/v${VERSION}/tanzu-cli-binaries-checksums.txt | grep tanzu-cli-windows-amd64 |cut -f1 -d" ")
if [ -z "$SHA_FOR_CHOCO_AMD64" ]; then
echo "Unable to determine SHA_AMD64 for package of version $VERSION"
exit 1
fi
fi
echo "Using SHA_AMD64: ${SHA_FOR_CHOCO_AMD64}"

echo "Using SHA: ${SHA_FOR_CHOCO}"
# Obtain SHA if it is not already specified in the variable SHA_FOR_CHOCO_ARM64
if [ -z "$SHA_FOR_CHOCO_ARM64" ]; then
SHA_FOR_CHOCO_ARM64=$(curl -sL https://github.com/vmware-tanzu/tanzu-cli/releases/download/v${VERSION}/tanzu-cli-binaries-checksums.txt | grep tanzu-cli-windows-arm64 |cut -f1 -d" ")
if [ -z "$SHA_FOR_CHOCO_ARM64" ]; then
echo "Unable to determine SHA_ARM64 for package of version $VERSION"
exit 1
fi
fi
echo "Using SHA_ARM64: ${SHA_FOR_CHOCO_ARM64}"

# Prepare install script
sed -e s,__CLI_VERSION__,v${VERSION}, -e s,__CLI_SHA__,${SHA_FOR_CHOCO}, \
sed -e s,__CLI_VERSION__,v${VERSION}, -e s,__CLI_SHA_AMD64__,${SHA_FOR_CHOCO_AMD64}, -e s,__CLI_SHA_ARM64__,${SHA_FOR_CHOCO_ARM64}, \
${BASE_DIR}/chocolateyInstall.ps1.tmpl > ${OUTPUT_DIR}/chocolateyInstall.ps1
chmod a+x ${OUTPUT_DIR}/chocolateyInstall.ps1

Expand Down
15 changes: 12 additions & 3 deletions hack/choco/chocolateyInstall.ps1.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ $releaseVersion = '__CLI_VERSION__'
$packageName = 'tanzu-cli'
$packagePath = "${releaseVersion}"
$scriptsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$url64 = "https://github.com/vmware-tanzu/tanzu-cli/releases/download/${releaseVersion}/tanzu-cli-windows-amd64.zip"
$checksum64 = '__CLI_SHA__'
$checksumType64 = 'sha256'

# Check if we are dealing with an ARM64 architecture
$os = Get-WmiObject -Class Win32_OperatingSystem
if ($os.OSArchitecture -like "*ARM*") {
$url64 = "https://github.com/vmware-tanzu/tanzu-cli/releases/download/${releaseVersion}/tanzu-cli-windows-arm64-windows11.zip"
$checksum64 = '__CLI_SHA_ARM64__'
} else {
$url64 = "https://github.com/vmware-tanzu/tanzu-cli/releases/download/${releaseVersion}/tanzu-cli-windows-amd64.zip"
$checksum64 = '__CLI_SHA_AMD64__'
}

$packageArgs = @{
packageName = $packageName
unzipLocation = $scriptsDir
Expand All @@ -26,7 +34,8 @@ function Install-TanzuEnvironment {
# Note that we use the scriptsDir path because chocolatey doesn't put
# binaries on the $PATH until _after_ the install script runs.
$tanzuExe = "${scriptsDir}\${packagePath}\tanzu.exe"
Move-Item "${scriptsDir}\${packagePath}\tanzu-cli-windows_amd64.exe" "${tanzuExe}"
# Use a glob pattern since the binary could be for arm64 or amd64
Move-Item "${scriptsDir}\${packagePath}\tanzu-cli-windows_*.exe" "${tanzuExe}"

# Setup shell completion
if (Test-Path -Path $PROFILE) {
Expand Down

0 comments on commit 684ce10

Please sign in to comment.