-
Notifications
You must be signed in to change notification settings - Fork 2
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
Aws #26
Aws #26
Changes from all commits
ef8aa43
dcc0c52
61117c3
f1c275e
0e437ae
3996547
4818d59
4242b24
e33c03b
e597bd2
1109fed
a94bdb7
9e57dae
c019697
dbee382
f17d79e
29137b9
a7cc0c1
97ff451
07e2f10
98ea09d
5bf472d
e69b8b8
5e20c44
6d6eab1
1d7f10c
42d5eda
1a5aabd
bd86338
93fce3c
1cc5c5a
a46fc68
a7b8d8f
9f2b274
5cd6dd9
f0b14ab
3a80b27
25811eb
1c46137
7e5f081
aee055e
8510755
e1ec116
acf489f
c4f26e0
8799565
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,4 +57,138 @@ function createRole() { | |
ROLE_NAME="$1" | ||
POLICY_DOCUMENT="$2" | ||
aws iam create-role --role-name "${ROLE_NAME}" --no-paginate --assume-role-policy-document "file://${POLICY_DOCUMENT}" | ||
} | ||
|
||
function getAssumeRole() { | ||
ROLE_ARN=$1 | ||
|
||
role_output=$(aws sts assume-role --role-arn "$ROLE_ARN" --role-session-name default) | ||
|
||
if [ $? -ne 0 ]; then | ||
echo "Failed to assume role :- $ROLE_ARN." | ||
exit 1 | ||
fi | ||
|
||
AWS_ACCESS_KEY_ID=$(echo $role_output | jq -r '.Credentials.AccessKeyId') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
AWS_SECRET_ACCESS_KEY=$(echo $role_output | jq -r '.Credentials.SecretAccessKey') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
AWS_SESSION_TOKEN=$(echo $role_output | jq -r '.Credentials.SessionToken') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
export AWS_ACCESS_KEY_ID | ||
export AWS_SECRET_ACCESS_KEY | ||
export AWS_SESSION_TOKEN | ||
} | ||
|
||
function set_aws_credentials() { | ||
CREDENTIAL_MANAGEMENT_NAME=$1 | ||
|
||
aws_creds=$(getEncryptedCredential "$CREDENTIAL_MANAGEMENT" "$CREDENTIAL_MANAGEMENT_NAME.CREDENTIAL_KEY_VALUE_PAIR") | ||
|
||
aws_access_key=$(echo $aws_creds | sed "s/'/\"/g" | jq -r '.aws_access_key') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
aws_secret_access_key=$(echo $aws_creds | sed "s/'/\"/g" | jq -r '.aws_secret_access_key') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
export AWS_ACCESS_KEY_ID="$aws_access_key" | ||
export AWS_SECRET_ACCESS_KEY="$aws_secret_access_key" | ||
} | ||
|
||
function check_aws_authentication() { | ||
if ! aws sts get-caller-identity &>/dev/null; then | ||
logErrorMessage "Failed to authenticate with AWS CLI. Please configure AWS CLI authentication." | ||
exit 1 | ||
else | ||
logInfoMessage "Successfully authenticated with AWS CLI." | ||
fi | ||
} | ||
|
||
function create_ec2_instance() { | ||
AMI_ID="$1" | ||
INSTANCE_TYPE="$2" | ||
SSH_KEY_NAME="$3" | ||
SUBNET_ID="$4" | ||
SECURITY_GROUP_IDS="$5" | ||
INSTANCE_COUNT="$6" | ||
INSTANCE_NAME="$7" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
BUILDX_ENABLE="$8" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
TAG_SPECIFICATIONS="${9}" | ||
USER_DATA="${10:-}" | ||
|
||
EC2_CREATE_CMD="aws ec2 run-instances \ | ||
--image-id \"$AMI_ID\" \ | ||
--instance-type \"$INSTANCE_TYPE\" \ | ||
--key-name \"$SSH_KEY_NAME\" \ | ||
--subnet-id \"$SUBNET_ID\" \ | ||
--security-group-ids \"$SECURITY_GROUP_IDS\" \ | ||
--count \"$INSTANCE_COUNT\" \ | ||
--tag-specifications \"$TAG_SPECIFICATIONS\"" | ||
|
||
# Append user-data if provided | ||
if [ -n "$USER_DATA" ]; then | ||
EC2_CREATE_CMD="$EC2_CREATE_CMD --user-data \"$USER_DATA\"" | ||
fi | ||
|
||
EC2_CREATE_OUTPUT=$(eval "$EC2_CREATE_CMD") | ||
|
||
if [ $? -ne 0 ]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
echo "Error creating EC2 instance." | ||
echo "$EC2_CREATE_OUTPUT" | ||
return 1 | ||
fi | ||
|
||
echo "$EC2_CREATE_OUTPUT" | ||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
# Function to extract the base image from a filtered Dockerfile string | ||
# This string contains entries of all the lines starting with FROM | ||
getBaseImageFromFilteredDockerfile() { | ||
local allFromEntries="$1" | ||
local base_img | ||
|
||
while IFS= read -r line; do | ||
base_img=$(echo "$line" | grep '^FROM ' | awk '{print $2}') | ||
done <<< "$allFromEntries" | ||
|
||
echo "${base_img}" | ||
} |
There was a problem hiding this comment.
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