From 512894743735fd88fa5170c9c28f2a48da6fab8e Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 28 Jul 2024 09:14:27 -0400 Subject: [PATCH] Adding salt-macros and fixes for sum.sh --- src/salt-macros.sh | 99 +++++++++++++++++++++++++++++++++++++++ src/salt-macros.sh.sha256 | 1 + src/sum.sh | 15 ++---- src/sum.sh.sha256 | 2 +- 4 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 src/salt-macros.sh create mode 100644 src/salt-macros.sh.sha256 diff --git a/src/salt-macros.sh b/src/salt-macros.sh new file mode 100644 index 0000000..7e2fe46 --- /dev/null +++ b/src/salt-macros.sh @@ -0,0 +1,99 @@ +#!/bin/bash + +# Alias for listing scheduled jobs +alias salt-schedule-list='docker exec -it salt_master_1 salt-call schedule.list' + +# Alias for adding a scheduled job +alias salt-schedule-add='docker exec -it salt_master_1 salt-call schedule.add' + +# Alias for viewing job history +alias salt-job-history='docker exec -it salt_master_1 salt-run jobs.list_jobs' + +# Alias for looking up a specific job +alias salt-job-lookup='docker exec -it salt_master_1 salt-run jobs.lookup_jid' + +# Alias for real-time event monitoring +alias salt-event-monitor='docker exec -it salt_master_1 salt-run state.event pretty=True' + +# Function to schedule an orchestration job +function salt-schedule-orchestration { + if [ "$#" -ne 5 ]; then + echo "Usage: salt-schedule-orchestration " + echo "Example: salt-schedule-orchestration apply_specific_states orchestrate.apply_specific_states '0 9 * * 6' '0 17 * * 6' 60" + return 1 + fi + + local job_name=$1 + local orchestration_sls=$2 + local start_time=$3 + local end_time=$4 + local interval_minutes=$5 + + docker exec -it salt_master_1 salt-call schedule.add $job_name \ + function='state.orchestrate' \ + job_args="[\"$orchestration_sls\"]" \ + when="$start_time" \ + until="$end_time" \ + minutes=$interval_minutes +} + +# Function to monitor the last job executed on minions +function salt-minion-last-job { + if [ "$#" -ne 1 ]; then + echo "Usage: salt-minion-last-job " + echo "Example: salt-minion-last-job minion_id" + return 1 + fi + + local minion_id=$1 + docker exec -it salt_master_1 salt $minion_id saltutil.last_job +} + +# Function to find a specific job on a minion +function salt-minion-find-job { + if [ "$#" -ne 2 ]; then + echo "Usage: salt-minion-find-job " + echo "Example: salt-minion-find-job minion_id job_id" + return 1 + fi + + local minion_id=$1 + local job_id=$2 + docker exec -it salt_master_1 salt $minion_id saltutil.find_job $job_id +} + +# Function to print usage and options +function salt-macros { + echo "Available Salt macros and their usage:" + echo + echo "Aliases:" + echo " salt-schedule-list - List scheduled jobs" + echo " salt-schedule-add - Add a scheduled job" + echo " salt-job-history - View job history" + echo " salt-job-lookup - Look up a specific job" + echo " salt-event-monitor - Monitor Salt events in real-time" + echo + echo "Functions:" + echo " salt-schedule-orchestration " + echo " - Schedule an orchestration job" + echo " - Example: salt-schedule-orchestration apply_specific_states orchestrate.apply_specific_states '0 9 * * 6' '0 17 * * 6' 60" + echo + echo " salt-minion-last-job " + echo " - Monitor the last job executed on a minion" + echo " - Example: salt-minion-last-job minion_id" + echo + echo " salt-minion-find-job " + echo " - Find a specific job on a minion" + echo " - Example: salt-minion-find-job minion_id job_id" +} + +# Example usage suggestions +# To schedule an orchestration: +# salt-schedule-orchestration "apply_specific_states" "orchestrate.apply_specific_states" "0 9 * * 6" "0 17 * * 6" 60 + +# To monitor the last job on a minion: +# salt-minion-last-job "minion_id" + +# To find a specific job on a minion: +# salt-minion-find-job "minion_id" "job_id" + diff --git a/src/salt-macros.sh.sha256 b/src/salt-macros.sh.sha256 new file mode 100644 index 0000000..a1a1524 --- /dev/null +++ b/src/salt-macros.sh.sha256 @@ -0,0 +1 @@ +5027c07471f7a1413d5cc10da21f01bc167c6b69d1a3913cb739208483cce79b diff --git a/src/sum.sh b/src/sum.sh index 50bfc8f..e7863d9 100644 --- a/src/sum.sh +++ b/src/sum.sh @@ -5,9 +5,9 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then exit 1 fi -# Function to generate SHA-256 hashes for .sh files in src or dist directories +# Function to generate SHA-256 hashes for .sh files in the src directory calculate_hashes() { - local base_dir="$1" + local base_dir="./src" # Check if the base directory exists if [ ! -d "$base_dir" ]; then @@ -21,7 +21,7 @@ calculate_hashes() { hash_cmd="sha256sum" fi - # Function to process a directory + # Function to process the src directory process_directory() { local dir="$1" for file in "$dir"/*.sh; do @@ -33,13 +33,8 @@ calculate_hashes() { done } - # Process src and dist directories if they exist - for sub_dir in src dist; do - local full_dir="$base_dir/$sub_dir" - if [ -d "$full_dir" ]; then - process_directory "$full_dir" - fi - done + # Process the src directory + process_directory "$base_dir" echo "SHA-256 hashes have been saved in .sha256 files." } diff --git a/src/sum.sh.sha256 b/src/sum.sh.sha256 index c83418c..bbc6d69 100644 --- a/src/sum.sh.sha256 +++ b/src/sum.sh.sha256 @@ -1 +1 @@ -004ba4423c45dc5bbf57fe514ba44e07bc303e80e579911a58f265ab42365ba0 +5d7646254d8f39bbd65b4473a3c8854eb088a566c0fc60ee9948a66ac8797e10