Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated aws function #27

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions aws-functions.sh

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck] reported by reviewdog 🐶
Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?. SC2181

if [ $? -ne 0 ]; then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

AWS_ACCESS_KEY_ID=$(echo $role_output | jq -r '.Credentials.AccessKeyId')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

AWS_SECRET_ACCESS_KEY=$(echo $role_output | jq -r '.Credentials.SecretAccessKey')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

AWS_SESSION_TOKEN=$(echo $role_output | jq -r '.Credentials.SessionToken')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

aws_access_key=$(echo $aws_creds | sed "s/'/\"/g" | jq -r '.aws_access_key')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

aws_secret_access_key=$(echo $aws_creds | sed "s/'/\"/g" | jq -r '.aws_secret_access_key')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
INSTANCE_NAME appears unused. Verify use (or export if used externally). SC2034

INSTANCE_NAME="$7"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
BUILDX_ENABLE appears unused. Verify use (or export if used externally). SC2034

BUILDX_ENABLE="$8"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck] reported by reviewdog 🐶
Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?. SC2181

if [ $? -ne 0 ]; then

Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,58 @@ function create_ec2_instance() {
return 0
}

check_instance_status() {
local INSTANCE_ID="$1"
local INSTANCE_TYPE="$2"

logInfoMessage "Checking $INSTANCE_TYPE instance [ID: $INSTANCE_ID]."

INSTANCE_STATE=$(aws ec2 describe-instances --instance-ids "$INSTANCE_ID" --query "Reservations[].Instances[].State.Name" --output text)

if [[ "$INSTANCE_STATE" == "terminated" || "$INSTANCE_STATE" == "stopped" ]]; then
logInfoMessage "$INSTANCE_TYPE instance [ID: $INSTANCE_ID] is in $INSTANCE_STATE state. Skipping status check and moving on."
return 1
fi

logInfoMessage "Waiting for $INSTANCE_TYPE instance [ID: $INSTANCE_ID] to be in 'running' state and pass status checks."

MAX_WAIT_TIME=600 # Maximum wait time in seconds (10 minutes)
SLEEP_INTERVAL=15 # Interval to check the status (15 seconds)
TOTAL_WAIT=0

while true; do
INSTANCE_STATE=$(aws ec2 describe-instances --instance-ids "$INSTANCE_ID" --query "Reservations[].Instances[].State.Name" --output text)
INSTANCE_STATUS_CHECK=$(aws ec2 describe-instance-status --instance-ids "$INSTANCE_ID" --query "InstanceStatuses[].InstanceStatus.Status" --output text)

logInfoMessage "$INSTANCE_TYPE instance [ID: $INSTANCE_ID] current state: $INSTANCE_STATE"
logInfoMessage "$INSTANCE_TYPE instance [ID: $INSTANCE_ID] status check: $INSTANCE_STATUS_CHECK"

if [ "$INSTANCE_STATE" = "running" ] && [ "$INSTANCE_STATUS_CHECK" = "ok" ]; then
logInfoMessage "$INSTANCE_TYPE instance [ID: $INSTANCE_ID] is now running and passed all status checks."
return 0
fi

if [ "$TOTAL_WAIT" -ge "$MAX_WAIT_TIME" ]; then
logErrorMessage "Timeout reached for $INSTANCE_TYPE instance [ID: $INSTANCE_ID]. Not in 'running' state or did not pass status checks."
return 1
fi

sleep $SLEEP_INTERVAL
TOTAL_WAIT=$((TOTAL_WAIT + SLEEP_INTERVAL))
done
}

terminate_instance() {
local INSTANCE_ID="$1"
local INSTANCE_TYPE="$2"

INSTANCE_STATE=$(aws ec2 describe-instances --instance-ids "$INSTANCE_ID" --query "Reservations[].Instances[].State.Name" --output text)

if [[ "$INSTANCE_STATE" != "terminated" && "$INSTANCE_STATE" != "stopped" ]]; then
logErrorMessage "Terminating $INSTANCE_TYPE instance [ID: $INSTANCE_ID]."
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID"
logInfoMessage "$INSTANCE_TYPE instance [ID: $INSTANCE_ID] has been terminated."
else
logInfoMessage "$INSTANCE_TYPE instance [ID: $INSTANCE_ID] is already in $INSTANCE_STATE state. No need to terminate."
fi
}
22 changes: 21 additions & 1 deletion functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function getServiceName() {
getNthTextInALine "$PROJECT_SVC_NAME" : 1
}

function jsonOutput() {
function jsonOutputWithoutArray() {
file_name="$1"
output_vars="$2"

Expand All @@ -281,3 +281,23 @@ function jsonOutput() {

echo "Job step response updated in: $file_name"
}

function jsonOutput() {
file_name="$1"
output_vars="$2"

file_content=""
if [[ -f "$file_name" && -s "$file_name" ]]; then
file_content=$(<"$file_name")
fi

if [[ -n "$file_content" ]]; then
updated_content=$(jq -c --argjson vars "$output_vars" '. += [$vars]' <<< "$file_content")
else
updated_content="$output_vars"
fi

echo "$updated_content" | jq "." > "$file_name"

echo "Job step response updated in: $file_name"
}