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

Aws #26

Closed
wants to merge 46 commits into from
Closed

Aws #26

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
ef8aa43
make textExistsInALine func process multiple values
rishabh96b Oct 13, 2023
dcc0c52
fmt: add quote to prevent globbing and word splitting
rishabh96b Oct 13, 2023
61117c3
utils: add docker-functions.sh
rishabh96b Oct 16, 2023
f1c275e
Update git-functions.sh
Cpriyanshi77 Oct 18, 2023
0e437ae
Added code to get src branch as well
sandy724 Oct 19, 2023
3996547
Merge branch 'v0.6' of github.com:OT-BUILDPIPER-MARKETPLACE/BP-BASE-S…
sandy724 Oct 19, 2023
4818d59
Merge branch 'v0.6' into v0.6-patch
rishabh96b Oct 19, 2023
4242b24
Merge pull request #17 from rishabh96b/v0.6-patch
sandy724 Oct 19, 2023
e33c03b
Added a check to abort merge only when conflict happens
sandy724 Oct 20, 2023
e597bd2
Added a check to abort merge only when conflict happens
sandy724 Oct 20, 2023
1109fed
Updated code to print tabular data of authors
sandy724 Oct 20, 2023
a94bdb7
Added a function to check if a branch exists or not
sandy724 Oct 20, 2023
9e57dae
adding assume-role function code in aws-function.sh
akkyjain15 Nov 1, 2023
c019697
Update aws-functions.sh
ashwani-opstree Nov 2, 2023
dbee382
Update aws-functions.sh
ashwani-opstree Nov 2, 2023
f17d79e
Merge pull request #18 from OT-BUILDPIPER-MARKETPLACE/assume-role
sandy724 Nov 2, 2023
29137b9
Update functions.sh
Cpriyanshi77 Dec 5, 2023
a7cc0c1
added mysql funtions
Cpriyanshi77 Dec 7, 2023
97ff451
updated validate db name funtion
Cpriyanshi77 Dec 7, 2023
07e2f10
updated validate db username funtion
Cpriyanshi77 Dec 7, 2023
98ea09d
updated validate db username funtion
Cpriyanshi77 Dec 7, 2023
5bf472d
updated validate db username funtion
Cpriyanshi77 Dec 7, 2023
e69b8b8
updated get encryted value function
Cpriyanshi77 Dec 7, 2023
5e20c44
updated functions.sh
Cpriyanshi77 Dec 7, 2023
6d6eab1
Remove the 'suppress warning' function and add MYSQL_PSWD variable to…
Cpriyanshi77 Dec 11, 2023
1d7f10c
Updated functions.sh
Cpriyanshi77 Dec 15, 2023
42d5eda
Update functions.sh
Cpriyanshi77 Dec 15, 2023
1a5aabd
update
Cpriyanshi77 Mar 19, 2024
bd86338
added funtions for newrelic
Cpriyanshi77 Mar 26, 2024
93fce3c
added function
Cpriyanshi77 Mar 26, 2024
1cc5c5a
added aws functions
Cpriyanshi77 Oct 4, 2024
a46fc68
added aws functions
Cpriyanshi77 Oct 4, 2024
a7b8d8f
added aws functions
Cpriyanshi77 Oct 4, 2024
9f2b274
added aws functions
Cpriyanshi77 Oct 4, 2024
5cd6dd9
added aws functions
Cpriyanshi77 Oct 4, 2024
f0b14ab
added aws functions
Cpriyanshi77 Oct 4, 2024
3a80b27
updated code
Cpriyanshi77 Oct 4, 2024
25811eb
updated code
Cpriyanshi77 Oct 4, 2024
1c46137
updated code
Cpriyanshi77 Oct 4, 2024
7e5f081
updated code
Cpriyanshi77 Oct 4, 2024
aee055e
updated code
Cpriyanshi77 Oct 4, 2024
8510755
updated code
Cpriyanshi77 Oct 4, 2024
e1ec116
updated code
Cpriyanshi77 Oct 4, 2024
acf489f
Update functions.sh
Cpriyanshi77 Oct 4, 2024
c4f26e0
updated code
Cpriyanshi77 Oct 4, 2024
8799565
updated aws function
Cpriyanshi77 Oct 11, 2024
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
134 changes: 134 additions & 0 deletions aws-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

echo "Failed to assume role :- $ROLE_ARN."
exit 1
fi

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


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')

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 🐶
Double quote to prevent globbing and word splitting. SC2086


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"

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

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 🐶
BUILDX_ENABLE appears unused. Verify use (or export if used externally). SC2034

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

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

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
}
14 changes: 14 additions & 0 deletions docker-functions.sh
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}"
}
12 changes: 9 additions & 3 deletions file-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ function fileContainsString() {
function getLineForAString() {
FILEPATH="$1"
TEXT="$2"
grep "${TEXT}" "${FILEPATH}"
grep "${TEXT}" "${FILEPATH}"
}

function textExistsInALine(){
LINE="$1"
TEXT="$2"
echo "${LINE}" | grep -q "${TEXT}"
echo "$?"
# Split single comma separated string into array
IFS="," read -r -a TEXT_ARRAY <<< "$TEXT"
for ITEM in "${TEXT_ARRAY[@]}"; do
if [ "$ITEM" == "$LINE" ]; then
return 0
fi
done
return 1
}
Loading