-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve function reuse across scripts
- Loading branch information
1 parent
329548e
commit 87749ae
Showing
6 changed files
with
207 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
# Function to list EC2 instances with their Name tag, either all or filtered | ||
list_instances() { | ||
filter=$1 | ||
if [[ "${filter,,}" == "all" || -z "${filter}" ]]; then | ||
aws ec2 describe-instances --query 'Reservations[*].Instances[?State.Name==`running`].[Tags[?Key==`Name`].Value | [0],InstanceId,InstanceType,PrivateIpAddress]' --output text | grep -E "sdf|veritech|pinga|rebaser" | ||
elif [[ "${filter,,}" != "all" ]]; then | ||
aws ec2 describe-instances --query 'Reservations[*].Instances[?State.Name==`running`].[Tags[?Key==`Name`].Value | [0],InstanceId,InstanceType,PrivateIpAddress]' --output text | grep -E "${filter}" | ||
fi | ||
} |
76 changes: 76 additions & 0 deletions
76
component/toolbox/scripts/supporting-funcs/inputs-funcs.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/bin/bash | ||
|
||
# Function to get input or use environment variable | ||
get_param_or_env() { | ||
local param=$1 | ||
local env_var=$2 | ||
local prompt=$3 | ||
|
||
if [ -z "$param" ]; then | ||
if [ -z "${!env_var}" ]; then | ||
read -p "$prompt: " value | ||
echo "$value" | ||
else | ||
echo "${!env_var}" | ||
fi | ||
else | ||
echo "$param" | ||
fi | ||
} | ||
|
||
await_file_results() { | ||
|
||
results_directory=$1 | ||
required_file_count=$2 | ||
|
||
timeout=60 # Timeout in seconds | ||
start_time=$(date +%s) # Record the start time | ||
|
||
while true; do | ||
current_time=$(date +%s) | ||
elapsed_time=$((current_time - start_time)) | ||
|
||
if (( elapsed_time > timeout )); then | ||
echo "Error: Timeout reached waiting for SSM document responses to arrive. Not all files are present." | ||
exit 1 | ||
fi | ||
|
||
file_count=$(ls "$results_directory" | wc -l) | ||
|
||
if (( file_count >= required_file_count )); then | ||
break | ||
fi | ||
|
||
# Wait for a short period before checking again | ||
sleep 1 | ||
done | ||
|
||
} | ||
|
||
sassy_selection_check() { | ||
selection=${1^^} | ||
if [ "$selection" != "Y" ]; then | ||
echo "Don't Trust Scott and John? We're friends I promise, exiting" | ||
exit 1 | ||
fi | ||
} | ||
|
||
concat_and_output_json() { | ||
|
||
results_directory=$1 | ||
output_file=$2 | ||
|
||
# Check if the directory exists | ||
if [ -d "$results_directory/" ]; then | ||
# Aggregate all the individual json documents into one | ||
cat $results_directory/* | jq -s '.' >> $results_directory/$output_file | ||
cat $results_directory/$output_file | jq | ||
echo "----------------------------------------" | ||
echo "Results can be found within $results_directory" | ||
else | ||
echo "Results Directory $results_directory does not exist." | ||
exit 1 | ||
fi | ||
echo "----------------------------------------" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/bin/bash | ||
|
||
# Function to start SSM session | ||
start_and_track_ssm_session() { | ||
|
||
instance_id=$1 | ||
script=$2 | ||
service=$3 | ||
action=$4 | ||
results_directory=$5 | ||
|
||
output=$(aws ssm send-command --instance-ids "$instance_id" --document-name "$script" --parameters "Service=$service,InstanceId=$instance_id,Action=$action" 2>&1) | ||
|
||
status=$? | ||
|
||
if [ $status -ne 0 ]; then | ||
output=$(echo "{\"instance_id\": \"$instance_id\", \"status\": \"error\", \"service\": \"$service\", \"message\": \"$output\"}") | ||
echo $output > "$results_directory/$instance_id.json" | ||
return | ||
fi | ||
|
||
command_id=$(echo "$output" | jq -r '.Command.CommandId') | ||
|
||
# Poll for command status with a timeout of 60 seconds | ||
timeout=60 | ||
elapsed=0 | ||
interval=1 | ||
|
||
while [ $elapsed -lt $timeout ]; do | ||
status=$(check_ssm_command_status) | ||
|
||
if [ "$status" == "Success" ] || [ "$status" == "Failed" ] || [ "$status" == "TimedOut" ] || [ "$status" == "Cancelled" ]; then | ||
break | ||
fi | ||
|
||
sleep $interval | ||
elapsed=$((elapsed + interval)) | ||
done | ||
|
||
# Check if command was successful | ||
if [ "$status" == "Success" ]; then | ||
# Get the output | ||
output=$(aws ssm get-command-invocation \ | ||
--command-id "$command_id" \ | ||
--instance-id "$instance_id" \ | ||
| jq -r '.StandardOutputContent') | ||
echo $output > "$results_directory/$instance_id.json" | ||
else | ||
echo "Command failed with status: $status" | ||
exit_code=$(aws ssm get-command-invocation \ | ||
--command-id "$command_id" \ | ||
--instance-id "$instance_id" \ | ||
| jq -r '.ResponseCode') | ||
|
||
echo "Exit code: $exit_code" | ||
echo "Failure message:" | ||
aws ssm get-command-invocation \ | ||
--command-id "$command_id" \ | ||
--instance-id "$instance_id" \ | ||
| jq -r '.StandardErrorContent' | ||
fi | ||
|
||
} | ||
|
||
# Function to start an interactive SSM session with any given instance | ||
start_interactive_ssm_session() { | ||
instance_id=$1 | ||
aws ssm start-session --target "$instance_id" --document-name AWS-StartInteractiveCommand --parameters command="bash -l" | ||
} | ||
|
||
# Function to check command status | ||
check_ssm_command_status() { | ||
status=$(aws ssm list-command-invocations \ | ||
--command-id "$command_id" \ | ||
--details \ | ||
| jq -r '.CommandInvocations[0].Status') | ||
echo "$status" | ||
} |
Oops, something went wrong.