Skip to content

Commit

Permalink
Support installing previous version of EKS-A CLI using Homebrew (#8343)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhay-krishna authored Jun 21, 2024
1 parent 8832fe5 commit 5c7a427
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
6 changes: 2 additions & 4 deletions scripts/brew_formula_pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ By submitting this pull request, I confirm that you can use, modify, copy, and r
EOF
)

cd ${SCRIPT_ROOT}/
cd ../../
gh auth login --with-token < /secrets/github-secrets/token

cd ${SCRIPT_ROOT}
Expand All @@ -44,7 +42,7 @@ then
exit 1
fi

LATEST_VERSION=$(echo $(/home/prow/go/src/github.com/aws/eks-anywhere/scripts/$BREW_UPDATE_SCRIPT))
LATEST_VERSION=$(echo $($SCRIPT_ROOT/$BREW_UPDATE_SCRIPT))

cd ${SCRIPT_ROOT}/../../../${ORIGIN_ORG}/${REPO}
git config --global push.default current
Expand All @@ -62,7 +60,7 @@ PR_BRANCH="eks-anywhere-formula-update"
git checkout -b $PR_BRANCH

git diff
git add Formula/eks-anywhere.rb
git add Formula/eks-anywhere*.rb
# If some other files get modified, the changes should be ignored
git restore .
FILES_ADDED=$(git diff --staged --name-only)
Expand Down
35 changes: 25 additions & 10 deletions scripts/brew_formula_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,31 @@ SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
REPO="homebrew-tap"
ORIGIN_ORG="eks-anywhere-brew-pr-bot"
UPSTREAM_ORG="aws"
EKS_ANYWHERE_RELEASES_MANIFEST_URL="https://anywhere-assets.eks.amazonaws.com/releases/eks-a/manifest.yaml"
YQ_LATEST_RELEASE_URL="https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64"
source "$SCRIPT_ROOT/common.sh"

if ! command -v yq &> /dev/null
then
wget -qO /usr/local/bin/yq $YQ_LATEST_RELEASE_URL
chmod a+x /usr/local/bin/yq
fi

curl --silent 'https://anywhere-assets.eks.amazonaws.com/releases/eks-a/manifest.yaml' -o release.yaml
curl --silent $EKS_ANYWHERE_RELEASES_MANIFEST_URL -o release.yaml

latest_release_version=$(cat release.yaml | yq e '.spec.latestVersion')
BRANCH_NAME=$PULL_BASE_REF
ALL_RELEASE_BRANCHES=($(build::common::get_release_branches))
LATEST_RELEASE_BRANCHES=($(get_latest_release_branches "${ALL_RELEASE_BRANCHES[@]}"))

# Get the latest release version corresponding to this release branch
if [[ "$BRANCH_NAME" == "${LATEST_RELEASE_BRANCHES[0]}" ]]; then
latest_release_version=$(cat release.yaml | yq e '.spec.latestVersion')
elif [[ "$BRANCH_NAME" == "${LATEST_RELEASE_BRANCHES[1]}" ]]; then
latest_release_version=$(build::common::get_latest_release_for_release_branch $BRANCH_NAME)
else
echo "Unsupported EKS Anywhere release branch $BRANCH_NAME!"
exit 1
fi

latest_release=$(cat release.yaml | yq e '.spec.releases[] | select(.version == "'$latest_release_version'")' >> latest_release.yaml)

Expand All @@ -46,21 +60,22 @@ latest_release_version="${latest_release_version:1}"
export VERSION=$latest_release_version

EKSA_TEMPLATE="eks-anywhere.rb.tmpl"
EKSA_FORMULA="${SCRIPT_ROOT}/../../../${ORIGIN_ORG}/${REPO}/Formula/eks-anywhere.rb"
if [[ "$BRANCH_NAME" == "${LATEST_RELEASE_BRANCHES[0]}" ]]; then
EKSA_FORMULA="${SCRIPT_ROOT}/../../../${ORIGIN_ORG}/${REPO}/Formula/eks-anywhere.rb"
export VERSION_SUFFIX=""
elif [[ "$BRANCH_NAME" == "${LATEST_RELEASE_BRANCHES[1]}" ]]; then
minor_version="${BRANCH_NAME##release-}"
EKSA_FORMULA="${SCRIPT_ROOT}/../../../${ORIGIN_ORG}/${REPO}/Formula/eks-anywhere@$minor_version.rb"
export VERSION_SUFFIX="AT${minor_version/.}"
fi

if [ ! -f "$EKSA_TEMPLATE" ]
then
echo "Template file ${EKSA_TEMPLATE} does not exist in this working folder, exiting.."
exit 1
fi

if [ ! -f "$EKSA_FORMULA" ]
then
echo "Can not find the ${EKSA_FORMULA} file, exiting.."
exit 1
fi

envsubst '$VERSION:$darwin_arm64_url:$darwin_arm64_sha256:$darwin_amd64_url:$darwin_amd64_sha256:$linux_arm64_url:$linux_arm64_sha256:$linux_amd64_url:$linux_amd64_sha256' \
envsubst '$VERSION:$VERSION_SUFFIX:$darwin_arm64_url:$darwin_arm64_sha256:$darwin_amd64_url:$darwin_amd64_sha256:$linux_arm64_url:$linux_arm64_sha256:$linux_amd64_url:$linux_amd64_sha256' \
< "${EKSA_TEMPLATE}" \
> "${EKSA_FORMULA}"

Expand Down
32 changes: 32 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,35 @@ function build::common::re_quote() {
local -r to_escape=$1
sed 's/[][()\.^$\/?*+]/\\&/g' <<< "$to_escape"
}

# This function returns all release branches of EKS Anywhere from GitHub.
function build::common::get_release_branches() {
echo "$(git ls-remote https://github.com/aws/eks-anywhere.git | grep -oE 'refs/heads/release-[0-9]+\.[0-9]+' | xargs -I_ echo _ | cut -d/ -f3)"
}

# This function returns the two latest release branches of EKS Anywhere,
# that is, release branches for versions N and N-1 in that order.
function build::common::get_latest_release_branches() {
local -r release_branches=("$@")

latest_minor=0
for branch in "${release_branches[@]}"; do
minor_version=${branch##release-0.}
if [ $minor_version -gt $latest_minor ] && [ "$(get_latest_release_for_release_branch $branch)" != "" ]; then
latest_minor=$minor_version
fi
done

echo "release-0.${latest_minor} release-0.$(($latest_minor-1))"
}

# This function returns the latest release version for the given release branch
# by parsing the EKS Anywhere releases manifest.
function build::common::get_latest_release_for_release_branch() {
release_branch=$1

minor_version="v${release_branch##release-}"
latest_release=$(curl --silent "https://anywhere-assets.eks.amazonaws.com/releases/eks-a/manifest.yaml" | yq ".spec.releases[].version" | grep $minor_version | tail -1)

echo $latest_release
}
2 changes: 1 addition & 1 deletion scripts/eks-anywhere.rb.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class EksAnywhere < Formula
class EksAnywhere$VERSION_SUFFIX < Formula
desc "CLI for managing EKS Anywhere Kubernetes clusters"
homepage "https://github.com/aws/eks-anywhere"
version "$VERSION"
Expand Down

0 comments on commit 5c7a427

Please sign in to comment.