Skip to content

Commit

Permalink
Merge branch 'dev' into r2k1/amd-gpu
Browse files Browse the repository at this point in the history
  • Loading branch information
r2k1 authored Dec 4, 2024
2 parents 47949dc + 5468150 commit 6546597
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
24 changes: 24 additions & 0 deletions .pipelines/.vsts-garabge-collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pool:
name: $(POOL_NAME)

parameters:
- name: DRY_RUN
displayName: Dry Run
type: boolean
default: false

jobs:
- job: gc
displayName: Garbage Collection
steps:
- checkout: self
fetchTags: false
fetchDepth: 1

- bash: |
chmod +x ./vhdbuilder/scripts/gc.sh
./vhdbuilder/scripts/gc.sh
env:
SUBSCRIPTION_ID: $(SUBSCRIPTION_ID)
DRY_RUN: ${{ parameters.DRY_RUN }}
displayName: Garbage collect resource groups
2 changes: 1 addition & 1 deletion vhdbuilder/packer/test/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ else
exit 1
fi
fi
az group create --name $TEST_VM_RESOURCE_GROUP_NAME --location ${AZURE_LOCATION} --tags "source=AgentBaker" "branch=${GIT_BRANCH}"
az group create --name $TEST_VM_RESOURCE_GROUP_NAME --location ${AZURE_LOCATION} --tags "source=AgentBaker,now=$(date +%s)" "branch=${GIT_BRANCH}"

# defer function to cleanup resource group when VHD debug is not enabled
function cleanup() {
Expand Down
2 changes: 1 addition & 1 deletion vhdbuilder/packer/vhd-scanning.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ SCAN_VM_ADMIN_PASSWORD="ScanVM@$(date +%s)"
set -x

RESOURCE_GROUP_NAME="$SCAN_RESOURCE_PREFIX-$(date +%s)-$RANDOM"
az group create --name $RESOURCE_GROUP_NAME --location ${PACKER_BUILD_LOCATION} --tags "source=AgentBaker" "branch=${GIT_BRANCH}"
az group create --name $RESOURCE_GROUP_NAME --location ${PACKER_BUILD_LOCATION} --tags "source=AgentBaker,now=$(date +%s)" "branch=${GIT_BRANCH}"

function cleanup() {
echo "Deleting resource group ${RESOURCE_GROUP_NAME}"
Expand Down
4 changes: 2 additions & 2 deletions vhdbuilder/scripts/automate_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ retrycmd_if_failure() {

set_git_config() {
# git config needs to be set in the agent
git config --global user.email "[email protected]"
git config --global user.name "aks-node"
git config --global user.email "aks-node-assistant@microsoft.com"
git config --global user.name "aks-node-assistant"
git config --list
}

Expand Down
61 changes: 61 additions & 0 deletions vhdbuilder/scripts/gc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash
set -euxo pipefail

[ -z "${SUBSCRIPTION_ID:-}" ] && echo "SUBSCRIPTION_ID must be set" && exit 1

SKIP_TAG_NAME="gc.skip"
SKIP_TAG_VALUE="true"

DRY_RUN="${DRY_RUN:-}"

DAY_AGO=$(( $(date +%s) - 86400 )) # 24 hours ago
WEEK_AGO=$(( $(date +%s) - 604800 )) # 7 days ago

function main() {
az login --identity # relies on an appropriately permissioned identity being attached to the build agent
az account set -s $SUBSCRIPTION_ID

echo "garbage collecting ephemeral resource groups..."
cleanup_rgs || exit $?

# TODO(cameissner): migrate linux VHD build back-fill deletion logic to this script
}

function cleanup_rgs() {
groups=$(az group list | jq -r --arg dl $DAY_AGO '.[] | select(.name | test("vhd-test*|vhd-scanning*|pkr-Resource-Group*")) | select(.tags.now < $dl).name' | tr -d '\"' || "")
if [ -z "$groups" ]; then
echo "no resource groups found for garbage collection"
return 0
fi

for group in $groups; do
echo "resource group $group is in-scope for garbage collection"
group_object=$(az group show -g $group)
tag_value=$(echo "$group_object" | jq -r --arg skipTagName $SKIP_TAG_NAME '.tags."\($skipTagName)"')

if [ "${tag_value,,}" == "$SKIP_TAG_VALUE" ]; then
now=$(echo "$group_object" | jq -r '.tags.now')
if [ "$now" != "null" ] && [ $now -lt $WEEK_AGO ]; then
echo "resource group $group is tagged with $SKIP_TAG_NAME=$SKIP_TAG_VALUE but is more than 7 days old, will attempt to delete..."
delete_group $group || return $?
fi
continue
fi

echo "will attempt to delete resource group $group"
delete_group $group || return $?
done
}

function delete_group() {
local group=$1

if [ "${DRY_RUN,,}" == "true" ]; then
echo "DRY_RUN: az group delete -g $group --yes --no-wait"
return 0
fi

az group delete -g $group --yes --no-wait || return $?
}

main "$@"

0 comments on commit 6546597

Please sign in to comment.