From 4626152379bdeb6f877f0a5bda5564237d3f063a Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Thu, 13 Jun 2024 18:21:18 +0200 Subject: [PATCH] suggested bash script --- README.md | 3 +-- run_xexe.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 run_xexe.sh diff --git a/README.md b/README.md index 396d7d7..fbf674f 100644 --- a/README.md +++ b/README.md @@ -147,14 +147,13 @@ This tool was made specifically to feed our DWH. These are the steps to perform - Schedule - Finally, schedule the execution in the Linux VM to fit your needs. - Specifics are up to you and your circunstances. - - A general pattern would be to create a little bash script that calls the tool with the right parameters on it. + - A general pattern would be to create a little bash script that calls the tool with the right parameters on it. You can find an example that I like in the root of this repo named `run_xexe.sh`, but that's opinionated and adjusted to my needs at the time of writing this. Adapt it to your environment or start from scratch if necessary. - Remember to use the `--ignore-warnings` flag if necessary to allow large, automated runs without manually interaction. - Backfilling - If you are loading rates for the first time, you might need to backfill long periods of time manually at first. - The tool is flexible enough. You can probably figure out the right commands by taking a look at `How to Use the tool > General Usage` - Be careful since you might hit consumption limits set by xe.com. - ## Testing This CLI app has three groups of automatic tests: diff --git a/run_xexe.sh b/run_xexe.sh new file mode 100644 index 0000000..ac923bc --- /dev/null +++ b/run_xexe.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# Define the Slack webhook URL +script_dir=$(dirname "$0") +webhooks_file="slack_webhook_urls.txt" +env_file="$script_dir/$webhooks_file" +if [ -f "$env_file" ]; then + export $(grep -v '^#' "$env_file" | xargs) +else + echo "Error: $webhooks_file file not found in the script directory." + exit 1 +fi + +# Messages to be sent to Slack +slack_failure_message=":rotating_light::rotating_light::rotating_light: One or more failures during scheduled `xexe` run in production. :rotating_light::rotating_light::rotating_light:" +slack_success_message=":white_check_mark::white_check_mark::white_check_mark: `xexe` run executed successfully in production. :white_check_mark::white_check_mark::white_check_mark:" + +# Initialize the failure flag +has_any_step_failed=0 + +cd /home/azureuser/data-xexe + +# Update from git +echo "Updating dbt project from git." | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +git checkout master | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +git pull | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 + +# Activate venv +source venv/bin/activate + +# DWH-healthcheck +echo "Running DWH healthcheck..." | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +xexe dwh-healthcheck | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +if [ $? -ne 0 ]; then + has_any_step_failed=1 + echo "Something went wrong when healthchecking DWH." | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +fi + +# xe healthcheck +echo "Running XE.com healthcheck..." | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +xexe xe-healthcheck | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +if [ $? -ne 0 ]; then + has_any_step_failed=1 + echo "Something went wrong when healthchecking xe.com." | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +fi + +# run the actual thing +echo "Getting rates from xe.com..." | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +xexe get-rates --currencies CAD,EUR,NZD,ZAR,AUD,USD,PLN,GBP --output dwh --ignore-warnings | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +if [ $? -ne 0 ]; then + has_any_step_failed=1 + echo "Something went wrong when getting rates." | while IFS= read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> /home/azureuser/xexe_run.log 2>&1 +fi + +# Check if any step failed and send a Slack message +if [ $has_any_step_failed -eq 1 ]; then + curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$slack_failure_message\"}" $SLACK_ALERT_WEBHOOK_URL +fi +if [ $has_any_step_failed -eq 0 ]; then + curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$slack_success_message\"}" $SLACK_RECEIPT_WEBHOOK_URL +fi