diff --git a/.github/workflows/aks-auto-start.yaml b/.github/workflows/aks-auto-start.yaml index a22812ee..0e80357b 100644 --- a/.github/workflows/aks-auto-start.yaml +++ b/.github/workflows/aks-auto-start.yaml @@ -1,8 +1,15 @@ name: aks-auto-start on: workflow_dispatch: + inputs: + DRYRUN: + options: + - "false" + - "true" schedule: - cron: '30 6 * * 1-5' # Every weekday at 6:30am BST +env: + DRYRUN: ${{ inputs.DRYRUN }} permissions: id-token: write jobs: diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh index ee4d66dd..66540ce8 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -29,7 +29,9 @@ jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do if [[ $SKIP == "false" ]]; then echo -e "${GREEN}About to run $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" echo az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster - az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster + if [[ $DRYRUN != "true" ]]; then + az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster + fi else echo -e "${AMBER}cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP) has been skipped from today's $MODE operation schedule" fi diff --git a/scripts/aks/common-functions.sh b/scripts/aks/common-functions.sh index 7c4e482d..cd869412 100644 --- a/scripts/aks/common-functions.sh +++ b/scripts/aks/common-functions.sh @@ -1,4 +1,13 @@ #!/bin/bash +# Check platform +platform=$(uname) + +# Check and install missing packages +if [[ $platform == "Darwin" ]]; then + date_command=$(which gdate) +elif [[ $platform == "Linux" ]]; then + date_command=$(which date) +fi function get_subscription_clusters() { SUBSCRIPTION_ID=$(jq -r '.id' <<< $subscription) @@ -79,14 +88,21 @@ function check_cluster_status() { function get_current_date_seconds() { local current_date_formatting - current_date_formatting=$(date +'%Y-%m-%d') - date -d "$current_date_formatting 00:00:00" +%s + current_date_formatting=$($date_command +'%Y-%m-%d') + $date_command -d "$current_date_formatting 00:00:00" +%s +} + +function convert_date_to_timestamp() { + IFS='-' read -r day month year <<< "$1" + local valid_date="$year-$month-$day" + local timestamp=$($date_command -d "$valid_date" +%s) + echo "$timestamp" } function is_in_date_range() { local start_date_seconds end_date_seconds current_date_seconds - start_date_seconds=$(date -d "$1 00:00:00" +%s) - end_date_seconds=$(date -d "$2 00:00:00" +%s) + start_date_seconds=$(convert_date_to_timestamp "$1") + end_date_seconds=$(convert_date_to_timestamp "$2") current_date_seconds=$(get_current_date_seconds) if [[ $current_date_seconds -ge $start_date_seconds && $current_date_seconds -le $end_date_seconds ]]; then