From 112fc5c2d41bd49604cbb2455c1abc78b0746b91 Mon Sep 17 00:00:00 2001 From: Vik Date: Fri, 3 Nov 2023 10:41:47 +0000 Subject: [PATCH 1/8] add dryrun option to test dates --- .github/workflows/aks-manual-start.yaml | 4 ++++ scripts/aks/auto-start-stop.sh | 9 ++++++--- scripts/aks/common-functions.sh | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/aks-manual-start.yaml b/.github/workflows/aks-manual-start.yaml index d98001cb..34af4c77 100644 --- a/.github/workflows/aks-manual-start.yaml +++ b/.github/workflows/aks-manual-start.yaml @@ -27,6 +27,10 @@ on: - All - "00" - "01" + DRYRUN: + options: + - "false" + - "true" env: PROJECT: ${{ inputs.PROJECT }} SELECTED_ENV: ${{ inputs.SELECTED_ENV }} diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh index ee4d66dd..03e36e59 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -7,6 +7,7 @@ source scripts/aks/common-functions.sh MODE=${1:-start} SKIP="false" +DRYRUN="false" if [[ "$MODE" != "start" && "$MODE" != "stop" ]]; then echo "Invalid MODE. Please use 'start' or 'stop'." @@ -27,9 +28,11 @@ jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do SKIP=$(should_skip_start_stop $cluster_env $cluster_business_area $MODE) 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 == "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 + 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..03d9e81a 100644 --- a/scripts/aks/common-functions.sh +++ b/scripts/aks/common-functions.sh @@ -79,7 +79,7 @@ function check_cluster_status() { function get_current_date_seconds() { local current_date_formatting - current_date_formatting=$(date +'%Y-%m-%d') + current_date_formatting=$(date +'%d-%m-%Y') date -d "$current_date_formatting 00:00:00" +%s } From 98fde0663443eba273a28891e324dc7a154e70e5 Mon Sep 17 00:00:00 2001 From: Vik Date: Fri, 3 Nov 2023 11:29:24 +0000 Subject: [PATCH 2/8] add dryrun option to test dates --- .github/workflows/aks-auto-start.yaml | 5 +++++ .github/workflows/aks-manual-start.yaml | 5 +---- scripts/aks/auto-start-stop.sh | 5 +++-- scripts/aks/common-functions.sh | 16 +++++++++++++--- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.github/workflows/aks-auto-start.yaml b/.github/workflows/aks-auto-start.yaml index a22812ee..ca0c7a10 100644 --- a/.github/workflows/aks-auto-start.yaml +++ b/.github/workflows/aks-auto-start.yaml @@ -1,6 +1,11 @@ name: aks-auto-start on: workflow_dispatch: + inputs: + DRYRUN: + options: + - "false" + - "true" schedule: - cron: '30 6 * * 1-5' # Every weekday at 6:30am BST permissions: diff --git a/.github/workflows/aks-manual-start.yaml b/.github/workflows/aks-manual-start.yaml index 34af4c77..cce28533 100644 --- a/.github/workflows/aks-manual-start.yaml +++ b/.github/workflows/aks-manual-start.yaml @@ -27,10 +27,7 @@ on: - All - "00" - "01" - DRYRUN: - options: - - "false" - - "true" + env: PROJECT: ${{ inputs.PROJECT }} SELECTED_ENV: ${{ inputs.SELECTED_ENV }} diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh index 03e36e59..69bb4e04 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -30,9 +30,10 @@ jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do if [[ $SKIP == "false" ]]; then if [[ $DRYRUN == "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 fi + 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 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 03d9e81a..e88fd9c5 100644 --- a/scripts/aks/common-functions.sh +++ b/scripts/aks/common-functions.sh @@ -1,5 +1,15 @@ #!/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) az account set -s $SUBSCRIPTION_ID @@ -80,13 +90,13 @@ function check_cluster_status() { function get_current_date_seconds() { local current_date_formatting current_date_formatting=$(date +'%d-%m-%Y') - date -d "$current_date_formatting 00:00:00" +%s + $date_command -d "$current_date_formatting 00:00:00" +%s } 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=$($date_command -d "$1 00:00:00" +%s) + end_date_seconds=$($date_command -d "$2 00:00:00" +%s) current_date_seconds=$(get_current_date_seconds) if [[ $current_date_seconds -ge $start_date_seconds && $current_date_seconds -le $end_date_seconds ]]; then From 5a46f47cb0cef0e6c621f304eb5c251a6c54bd12 Mon Sep 17 00:00:00 2001 From: Vik Date: Fri, 3 Nov 2023 11:29:58 +0000 Subject: [PATCH 3/8] add dryrun option to test dates --- .github/workflows/aks-manual-start.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/aks-manual-start.yaml b/.github/workflows/aks-manual-start.yaml index cce28533..d98001cb 100644 --- a/.github/workflows/aks-manual-start.yaml +++ b/.github/workflows/aks-manual-start.yaml @@ -27,7 +27,6 @@ on: - All - "00" - "01" - env: PROJECT: ${{ inputs.PROJECT }} SELECTED_ENV: ${{ inputs.SELECTED_ENV }} From b7ee9d736372fc333a31906dcf53f66ad1a72dfb Mon Sep 17 00:00:00 2001 From: Vik Date: Fri, 3 Nov 2023 11:34:03 +0000 Subject: [PATCH 4/8] add dryrun option to test dates --- .github/workflows/aks-auto-start.yaml | 2 ++ scripts/aks/auto-start-stop.sh | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/aks-auto-start.yaml b/.github/workflows/aks-auto-start.yaml index ca0c7a10..0e80357b 100644 --- a/.github/workflows/aks-auto-start.yaml +++ b/.github/workflows/aks-auto-start.yaml @@ -8,6 +8,8 @@ on: - "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 69bb4e04..3a073c36 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -28,7 +28,7 @@ jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do SKIP=$(should_skip_start_stop $cluster_env $cluster_business_area $MODE) if [[ $SKIP == "false" ]]; then - if [[ $DRYRUN == "false" ]]; then + if [[ $DRYRUN != "true" ]]; then echo -e "${GREEN}About to run $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" fi echo -e "${GREEN}About to run $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" From 0bc11df5ca5cbd320122920e5fbbdc3b892c6ead Mon Sep 17 00:00:00 2001 From: Praveen Adusumilli <47391951+adusumillipraveen@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:35:06 +0000 Subject: [PATCH 5/8] Update scripts/aks/auto-start-stop.sh --- scripts/aks/auto-start-stop.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh index 3a073c36..2e056eeb 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -7,7 +7,6 @@ source scripts/aks/common-functions.sh MODE=${1:-start} SKIP="false" -DRYRUN="false" if [[ "$MODE" != "start" && "$MODE" != "stop" ]]; then echo "Invalid MODE. Please use 'start' or 'stop'." From 0beb2fbc1bc04233d9c315e4831219a8905559c1 Mon Sep 17 00:00:00 2001 From: Vik Date: Fri, 3 Nov 2023 11:36:29 +0000 Subject: [PATCH 6/8] add dryrun option to test dates --- scripts/aks/auto-start-stop.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh index 2e056eeb..fbdb302f 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -28,7 +28,7 @@ jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do if [[ $SKIP == "false" ]]; then if [[ $DRYRUN != "true" ]]; then - echo -e "${GREEN}About to run $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" + az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster fi 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 From a7ba4e2a338502a6c36f04ab29db5c8d3441a26a Mon Sep 17 00:00:00 2001 From: Vik Date: Fri, 3 Nov 2023 11:39:37 +0000 Subject: [PATCH 7/8] add dryrun option to test dates --- scripts/aks/auto-start-stop.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/aks/auto-start-stop.sh b/scripts/aks/auto-start-stop.sh index fbdb302f..66540ce8 100755 --- a/scripts/aks/auto-start-stop.sh +++ b/scripts/aks/auto-start-stop.sh @@ -27,12 +27,11 @@ jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do SKIP=$(should_skip_start_stop $cluster_env $cluster_business_area $MODE) 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 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 - 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 else echo -e "${AMBER}cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP) has been skipped from today's $MODE operation schedule" fi From ddedc182acd59f5c1ec4f0baf655a6f55f05424f Mon Sep 17 00:00:00 2001 From: adusumillipraveen Date: Mon, 6 Nov 2023 09:56:09 +0000 Subject: [PATCH 8/8] fix date issues --- scripts/aks/common-functions.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/scripts/aks/common-functions.sh b/scripts/aks/common-functions.sh index e88fd9c5..cd869412 100644 --- a/scripts/aks/common-functions.sh +++ b/scripts/aks/common-functions.sh @@ -1,5 +1,4 @@ #!/bin/bash - # Check platform platform=$(uname) @@ -89,14 +88,21 @@ function check_cluster_status() { function get_current_date_seconds() { local current_date_formatting - current_date_formatting=$(date +'%d-%m-%Y') + 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_command -d "$1 00:00:00" +%s) - end_date_seconds=$($date_command -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