From ef8aa435255079ffadf54cf2731dc9e3c7181a58 Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 13 Oct 2023 11:53:48 +0530 Subject: [PATCH 01/41] make textExistsInALine func process multiple values --- file-functions.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/file-functions.sh b/file-functions.sh index 4b74d58..ab86f92 100644 --- a/file-functions.sh +++ b/file-functions.sh @@ -24,6 +24,12 @@ function getLineForAString() { 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 } \ No newline at end of file From dcc0c52d303e4e46257cd152a60ac89169e3152c Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 13 Oct 2023 12:17:27 +0530 Subject: [PATCH 02/41] fmt: add quote to prevent globbing and word splitting --- file-functions.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/file-functions.sh b/file-functions.sh index ab86f92..c562068 100644 --- a/file-functions.sh +++ b/file-functions.sh @@ -11,14 +11,14 @@ function isFileExist() { function fileContainsString() { FILEPATH=$1 TEXT="$2" - grep -q ${TEXT} ${FILEPATH} + grep -q "${TEXT}" "${FILEPATH}" echo $? } function getLineForAString() { FILEPATH=$1 TEXT=$2 - grep ${TEXT} ${FILEPATH} + grep "${TEXT}" "${FILEPATH} " } function textExistsInALine(){ From 61117c300a3e805cac0680551c8ada5b1438c05b Mon Sep 17 00:00:00 2001 From: Rishabh Date: Mon, 16 Oct 2023 13:18:22 +0530 Subject: [PATCH 03/41] utils: add docker-functions.sh --- docker-functions.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 docker-functions.sh diff --git a/docker-functions.sh b/docker-functions.sh new file mode 100755 index 0000000..2a55460 --- /dev/null +++ b/docker-functions.sh @@ -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}" +} From f1c275ef2ca8680b589542ffa80538b72b05f9b5 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan <107524071+Cpriyanshi77@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:52:42 +0530 Subject: [PATCH 04/41] Update git-functions.sh Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- git-functions.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-functions.sh b/git-functions.sh index 0763f8f..d80f1a8 100644 --- a/git-functions.sh +++ b/git-functions.sh @@ -39,7 +39,7 @@ function getLastAuthorOfFiles() { FILES="$2" for FILE in ${FILES} do - author=`getLastAuthorOfFile ${BRANCH} ${FILE}` + author=$(getLastAuthorOfFile "${BRANCH}" "${FILE}") echo "${FILE}: ${author}" done } \ No newline at end of file From 0e437aea9cc45198a6d72fc1145a630846fa162a Mon Sep 17 00:00:00 2001 From: Sandeep Date: Thu, 19 Oct 2023 15:30:59 +0530 Subject: [PATCH 05/41] Added code to get src branch as well --- git-functions.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/git-functions.sh b/git-functions.sh index 0763f8f..2bae7fe 100644 --- a/git-functions.sh +++ b/git-functions.sh @@ -12,6 +12,7 @@ function findConflictingFiles() { SRC_BRANCH=$1 TGT_BRANCH=$2 + git checkout -q "${SRC_BRANCH}" git checkout -q "${TGT_BRANCH}" # git pull origin ${TGT_BRANCH} From e33c03ba4b1053c48e3824427288cc4b53e88ded Mon Sep 17 00:00:00 2001 From: Sandeep Date: Fri, 20 Oct 2023 05:48:51 +0530 Subject: [PATCH 06/41] Added a check to abort merge only when conflict happens --- git-functions.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/git-functions.sh b/git-functions.sh index c648b0d..2d09641 100644 --- a/git-functions.sh +++ b/git-functions.sh @@ -20,7 +20,9 @@ function findConflictingFiles() { git merge -q "$SRC_BRANCH" 1> /dev/null conflicts=$(git diff --name-only --diff-filter=U) - git merge --abort + if [ -n "${CONFLICTING_FILES}" ]; then + git merge --abort + fi git checkout -q "${TGT_BRANCH}" git branch -q -D temp_merge_branch echo $conflicts From 1109fed628b364f1eb0c3a37da88ed018b667a22 Mon Sep 17 00:00:00 2001 From: Sandeep Date: Fri, 20 Oct 2023 07:10:25 +0530 Subject: [PATCH 07/41] Updated code to print tabular data of authors --- git-functions.sh | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/git-functions.sh b/git-functions.sh index 1538750..fac35ba 100644 --- a/git-functions.sh +++ b/git-functions.sh @@ -22,7 +22,7 @@ function findConflictingFiles() { git merge -q "$SRC_BRANCH" 1> /dev/null conflicts=$(git diff --name-only --diff-filter=U) - if [ -n "${CONFLICTING_FILES}" ]; then + if [ -n "${conflicts}" ]; then git merge --abort fi git checkout -q "${TGT_BRANCH}" @@ -39,12 +39,22 @@ function getLastAuthorOfFile() { git log -1 --pretty=format:"%an" "${FILE}" } -function getLastAuthorOfFiles() { - BRANCH="$1" - FILES="$2" - for FILE in "${FILES[@]}" +function listAuthorsOfFilesAcrossBranches() { + SRC_BRANCH="$1" + TGT_BRANCH="$2" + CONFLICTING_FILES="$3" + echo File,${SRC_BRANCH},${TGT_BRANCH} > fileAuthors.tmp + # echo "Conflicting files variable: [${CONFLICTING_FILES}]" + for FILE in ${CONFLICTING_FILES} do - author=$(getLastAuthorOfFile "${BRANCH}" "${FILE}") - echo "${FILE}: ${author}" + # echo "Processing file [${FILE}]" + src_branch_author=`getLastAuthorOfFile "${SRC_BRANCH}" "${FILE}"` + # echo "Source branch Author: [${src_branch_author}]" + tgt_branch_author=`getLastAuthorOfFile "${TGT_BRANCH}" "${FILE}"` + # echo "Target branch Author: [${tgt_branch_author}]" + echo "$FILE,${src_branch_author},${tgt_branch_author}" >> fileAuthors.tmp done + + cat fileAuthors.tmp | csvlook + } From a94bdb730de824ec1274064898220c5061833bad Mon Sep 17 00:00:00 2001 From: Sandeep Date: Sat, 21 Oct 2023 03:46:14 +0530 Subject: [PATCH 08/41] Added a function to check if a branch exists or not --- git-functions.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/git-functions.sh b/git-functions.sh index fac35ba..02cac49 100644 --- a/git-functions.sh +++ b/git-functions.sh @@ -56,5 +56,17 @@ function listAuthorsOfFilesAcrossBranches() { done cat fileAuthors.tmp | csvlook +} +branch_exists() { + local branch_name="$1" + + # Check if the branch exists by using "git show-ref" and "grep" + if git show-ref --verify --quiet "refs/heads/$branch_name"; then + echo "Branch '$branch_name' exists." + return 0 # Branch exists + else + echo "Branch '$branch_name' does not exist." + return 1 # Branch does not exist + fi } From 9e57dae9d04a9c75603d047d1c8a3abd173c4074 Mon Sep 17 00:00:00 2001 From: akshay jain Date: Wed, 1 Nov 2023 21:32:24 +0530 Subject: [PATCH 09/41] adding assume-role function code in aws-function.sh --- aws-functions.sh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/aws-functions.sh b/aws-functions.sh index 11d4261..737c9c4 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -57,4 +57,16 @@ 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}" -} \ No newline at end of file +} + +function createAssumeRole() { + ROLE_ARN=$1 + ROLE_SESSION_NAME=$2 + + export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \ + $(aws sts assume-role \ + --role-arn ${ROLE_ARN} \ + --role-session-name ${ROLE_SESSION_NAME} \ + --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \ + --output text)) +} From c0196976bbd81542b58e50f257a9ce8752139707 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Thu, 2 Nov 2023 11:52:40 +0530 Subject: [PATCH 10/41] Update aws-functions.sh --- aws-functions.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws-functions.sh b/aws-functions.sh index 737c9c4..6068de9 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -59,11 +59,11 @@ function createRole() { aws iam create-role --role-name "${ROLE_NAME}" --no-paginate --assume-role-policy-document "file://${POLICY_DOCUMENT}" } -function createAssumeRole() { +function getAssumeRole() { ROLE_ARN=$1 ROLE_SESSION_NAME=$2 - export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \ + export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \ $(aws sts assume-role \ --role-arn ${ROLE_ARN} \ --role-session-name ${ROLE_SESSION_NAME} \ From dbee3821bd0a2767fe79dc7694eb9ec5143c7895 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Thu, 2 Nov 2023 11:53:30 +0530 Subject: [PATCH 11/41] Update aws-functions.sh --- aws-functions.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/aws-functions.sh b/aws-functions.sh index 6068de9..c6efeb6 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -61,12 +61,11 @@ function createRole() { function getAssumeRole() { ROLE_ARN=$1 - ROLE_SESSION_NAME=$2 export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \ $(aws sts assume-role \ --role-arn ${ROLE_ARN} \ - --role-session-name ${ROLE_SESSION_NAME} \ + --role-session-name default \ --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \ --output text)) } From 29137b93665d7c88cbbbd77e96897043b774a029 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan <107524071+Cpriyanshi77@users.noreply.github.com> Date: Wed, 6 Dec 2023 00:28:06 +0530 Subject: [PATCH 12/41] Update functions.sh --- functions.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/functions.sh b/functions.sh index fee382f..49d35cc 100755 --- a/functions.sh +++ b/functions.sh @@ -61,4 +61,21 @@ function saveTaskStatus() { fi } +function decrypt_credentials() { + local fernet_key="$1" + local encrypted_value="$2" + python3 -c " +from cryptography.fernet import Fernet + +fernet_key = '$fernet_key' +f = Fernet(fernet_key.encode('utf-8')) +encrypted_value = b'$encrypted_value' + +try: + decrypted_value = f.decrypt(encrypted_value) + print(decrypted_value.decode()) +except Exception as e: + print(f'Decryption error: {e}') +" +} From a7cc0c176d1df3e58974fe117dfff21e7eda71c6 Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Thu, 7 Dec 2023 13:50:57 +0530 Subject: [PATCH 13/41] added mysql funtions --- functions.sh | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/functions.sh b/functions.sh index 49d35cc..0377a54 100755 --- a/functions.sh +++ b/functions.sh @@ -61,7 +61,7 @@ function saveTaskStatus() { fi } -function decrypt_credentials() { +function getDecryptedCredential() { local fernet_key="$1" local encrypted_value="$2" @@ -79,3 +79,102 @@ except Exception as e: print(f'Decryption error: {e}') " } + +function passwordStrengthChecker() { + local password="$1" + local len=${#password} + + if [ "${#password}" -ge 8 ] && [[ $password == *[A-Za-z]* ]] && [[ $password == *[0-9]* ]] && [[ $password == *['#?!@$\ %^\&*-']* ]] + then + logInfoMessage '' + else + logErrorMessage "Weak password. Please ensure the password meets the criteria: at least 8 characters, one uppercase letter, one lowercase letter, one digit, and one special character." + exit 1 + fi +} + +function validDBname() { + local db_name="$1" + + # Check if the database name is not empty + if [ -z "$db_name" ]; then + logErrorMessage "Database name cannot be empty." + exit 1 + fi + + # Check if the database name contains invalid characters + if echo "$db_name" | grep -q -E '[^a-zA-Z0-9_\-]'; then + logErrorMessage "Invalid characters in the database name. Only alphanumeric characters, underscore (_), and hyphen (-) are allowed." + exit 1 + fi +} + +function getEncryptedCredential() { + local credentialManagement="$1" + local credentialKey="$2" + + echo "$(echo "$credentialManagement" | jq -r ".$credentialKey")" +} + +function mysqlcheckDatabaseConnection() { + local username="$1" + local password="$2" + local host="$3" + + mysql -u "$username" -p"$password" -h "$host" -e "SELECT 1;" || { + logErrorMessage "Failed to login to the database server. Check your credentials and connection." + exit 1 + } +} + +function mysqlcreateDatabase() { + local username="$1" + local password="$2" + local host="$3" + local dbName="$4" + + mysql -u "$username" -p"$password" -h "$host" -e "CREATE DATABASE $dbName;" || { + logErrorMessage "Failed to create the database." + exit 1 + } +} + +function mysqlcreateUser() { + local username="$1" + local password="$2" + local host="$3" + local dbName="$4" + local userName="$5" + local userPassword="$6" + + mysql -u "$username" -p"$password" -h "$host" -e "CREATE USER '$userName'@'$host' IDENTIFIED BY '$userPassword';" || { + logErrorMessage "Failed to create the user [$userName]." + exit 1 + } +} + +function mysqlgrantPrivileges() { + local username="$1" + local password="$2" + local host="$3" + local dbName="$4" + local userName="$5" + + mysql -u "$username" -p"$password" -h "$host" -e "GRANT ALL PRIVILEGES ON $dbName.* TO '$userName'@'$host'; FLUSH PRIVILEGES;" || { + logErrorMessage "Failed to give privileges to the user [$userName]." + exit 1 + } +} + +function mysqlcheckUserPrivileges() { + local username="$1" + local password="$2" + local host="$3" + local userName="$4" + + mysql -u "$username" -p"$password" -h "$host" -e "SHOW GRANTS FOR '$userName'@'$host';" || { + logErrorMessage "Failed to check privileges for the user [$userName]." + exit 1 + } +} + From 97ff4515086b5555ffc1a3960050d981f9acb2ad Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Thu, 7 Dec 2023 14:04:53 +0530 Subject: [PATCH 14/41] updated validate db name funtion --- functions.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/functions.sh b/functions.sh index 0377a54..aaf7f93 100755 --- a/functions.sh +++ b/functions.sh @@ -95,16 +95,16 @@ function passwordStrengthChecker() { function validDBname() { local db_name="$1" - - # Check if the database name is not empty + if [ -z "$db_name" ]; then - logErrorMessage "Database name cannot be empty." + echo "Database name cannot be empty." exit 1 fi - - # Check if the database name contains invalid characters - if echo "$db_name" | grep -q -E '[^a-zA-Z0-9_\-]'; then - logErrorMessage "Invalid characters in the database name. Only alphanumeric characters, underscore (_), and hyphen (-) are allowed." + + if [[ "$db_name" =~ ^[a-zA-Z0-9_]+$ ]]; then + echo "vaild" + else + echo "Invalid characters in the database name [$db_name]. Only alphanumeric characters (a-zA-Z), digits (1), underscore (_) are allowed." exit 1 fi } From 07e2f103a80a216feed7bdbe29a527a5383d0f42 Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Thu, 7 Dec 2023 14:28:14 +0530 Subject: [PATCH 15/41] updated validate db username funtion --- functions.sh | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/functions.sh b/functions.sh index aaf7f93..fee263a 100755 --- a/functions.sh +++ b/functions.sh @@ -84,6 +84,11 @@ function passwordStrengthChecker() { local password="$1" local len=${#password} + if [ -z "$password" ]; then + logErrorMessage "password cannot be empty." + exit 1 + fi + if [ "${#password}" -ge 8 ] && [[ $password == *[A-Za-z]* ]] && [[ $password == *[0-9]* ]] && [[ $password == *['#?!@$\ %^\&*-']* ]] then logInfoMessage '' @@ -97,14 +102,30 @@ function validDBname() { local db_name="$1" if [ -z "$db_name" ]; then - echo "Database name cannot be empty." + logErrorMessage "Database name cannot be empty." exit 1 fi if [[ "$db_name" =~ ^[a-zA-Z0-9_]+$ ]]; then - echo "vaild" + logInfoMessage "" + else + logErrorMessage "Invalid characters in the database name [$db_name]. Only alphanumeric characters (a-zA-Z), digits (1), underscore (_) are allowed." + exit 1 + fi +} + +function validDBusername() { + local username="$1" + + if [ -z "$username" ]; then + logErrorMessage "Username name cannot be empty." + exit 1 + fi + + if [[ "$username" =~ ^[a-zA-Z0-9_]+$ ]]; then + logInfoMessage "" else - echo "Invalid characters in the database name [$db_name]. Only alphanumeric characters (a-zA-Z), digits (1), underscore (_) are allowed." + logErrorMessage "Invalid characters in the username [$username]. Only alphanumeric characters (a-zA-Z), digits (1), underscore (_) are allowed." exit 1 fi } From 98ea09dcf53cc4ec8b00d1e0a972c7ebc8df7c50 Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Thu, 7 Dec 2023 14:42:38 +0530 Subject: [PATCH 16/41] updated validate db username funtion --- functions.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/functions.sh b/functions.sh index fee263a..d05163d 100755 --- a/functions.sh +++ b/functions.sh @@ -91,7 +91,7 @@ function passwordStrengthChecker() { if [ "${#password}" -ge 8 ] && [[ $password == *[A-Za-z]* ]] && [[ $password == *[0-9]* ]] && [[ $password == *['#?!@$\ %^\&*-']* ]] then - logInfoMessage '' + true else logErrorMessage "Weak password. Please ensure the password meets the criteria: at least 8 characters, one uppercase letter, one lowercase letter, one digit, and one special character." exit 1 @@ -107,7 +107,7 @@ function validDBname() { fi if [[ "$db_name" =~ ^[a-zA-Z0-9_]+$ ]]; then - logInfoMessage "" + true else logErrorMessage "Invalid characters in the database name [$db_name]. Only alphanumeric characters (a-zA-Z), digits (1), underscore (_) are allowed." exit 1 @@ -123,7 +123,7 @@ function validDBusername() { fi if [[ "$username" =~ ^[a-zA-Z0-9_]+$ ]]; then - logInfoMessage "" + true else logErrorMessage "Invalid characters in the username [$username]. Only alphanumeric characters (a-zA-Z), digits (1), underscore (_) are allowed." exit 1 From 5bf472dd9fbf388b2d297e4918198f3adf826a33 Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Thu, 7 Dec 2023 19:16:30 +0530 Subject: [PATCH 17/41] updated validate db username funtion --- functions.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/functions.sh b/functions.sh index d05163d..05a7818 100755 --- a/functions.sh +++ b/functions.sh @@ -82,7 +82,6 @@ except Exception as e: function passwordStrengthChecker() { local password="$1" - local len=${#password} if [ -z "$password" ]; then logErrorMessage "password cannot be empty." From e69b8b81175d35675c39a5dee0aaf10148bbfdfd Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Thu, 7 Dec 2023 23:35:33 +0530 Subject: [PATCH 18/41] updated get encryted value function --- functions.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/functions.sh b/functions.sh index 05a7818..5d518a2 100755 --- a/functions.sh +++ b/functions.sh @@ -132,8 +132,9 @@ function validDBusername() { function getEncryptedCredential() { local credentialManagement="$1" local credentialKey="$2" - - echo "$(echo "$credentialManagement" | jq -r ".$credentialKey")" + + encrypted_value=$(echo "$credentialManagement" | jq -r ".$credentialKey") + echo "$encrypted_value" } function mysqlcheckDatabaseConnection() { From 5e20c44e7355cf593c051bf71677b66ad6376f8b Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Fri, 8 Dec 2023 01:07:54 +0530 Subject: [PATCH 19/41] updated functions.sh --- functions.sh | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/functions.sh b/functions.sh index 5d518a2..4b04fda 100755 --- a/functions.sh +++ b/functions.sh @@ -137,24 +137,29 @@ function getEncryptedCredential() { echo "$encrypted_value" } +function mysqlFilterWarning() { + sed '/mysql: \[Warning\] Using a password on the command line interface can be insecure\./d' +} + function mysqlcheckDatabaseConnection() { local username="$1" local password="$2" local host="$3" - mysql -u "$username" -p"$password" -h "$host" -e "SELECT 1;" || { + mysql -u "$username" -p"$password" -h "$host" -e "SELECT 1;" 2> >(mysqlFilterWarning) || { logErrorMessage "Failed to login to the database server. Check your credentials and connection." exit 1 } } + function mysqlcreateDatabase() { local username="$1" local password="$2" local host="$3" local dbName="$4" - mysql -u "$username" -p"$password" -h "$host" -e "CREATE DATABASE $dbName;" || { + mysql -u "$username" -p"$password" -h "$host" -e "CREATE DATABASE $dbName;" 2> >(mysqlFilterWarning) || { logErrorMessage "Failed to create the database." exit 1 } @@ -168,7 +173,7 @@ function mysqlcreateUser() { local userName="$5" local userPassword="$6" - mysql -u "$username" -p"$password" -h "$host" -e "CREATE USER '$userName'@'$host' IDENTIFIED BY '$userPassword';" || { + mysql -u "$username" -p"$password" -h "$host" -e "CREATE USER '$userName'@'$host' IDENTIFIED BY '$userPassword';" 2> >(mysqlFilterWarning) || { logErrorMessage "Failed to create the user [$userName]." exit 1 } @@ -181,7 +186,7 @@ function mysqlgrantPrivileges() { local dbName="$4" local userName="$5" - mysql -u "$username" -p"$password" -h "$host" -e "GRANT ALL PRIVILEGES ON $dbName.* TO '$userName'@'$host'; FLUSH PRIVILEGES;" || { + mysql -u "$username" -p"$password" -h "$host" -e "GRANT ALL PRIVILEGES ON $dbName.* TO '$userName'@'$host'; FLUSH PRIVILEGES;" 2> >(mysqlFilterWarning) || { logErrorMessage "Failed to give privileges to the user [$userName]." exit 1 } @@ -193,7 +198,7 @@ function mysqlcheckUserPrivileges() { local host="$3" local userName="$4" - mysql -u "$username" -p"$password" -h "$host" -e "SHOW GRANTS FOR '$userName'@'$host';" || { + mysql -u "$username" -p"$password" -h "$host" -e "SHOW GRANTS FOR '$userName'@'$host';" 2> >(mysqlFilterWarning) || { logErrorMessage "Failed to check privileges for the user [$userName]." exit 1 } From 6d6eab15d5ca3200e417284ca5ee2e5f701a91dd Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Mon, 11 Dec 2023 11:43:50 +0530 Subject: [PATCH 20/41] Remove the 'suppress warning' function and add MYSQL_PSWD variable to suppress warning --- functions.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/functions.sh b/functions.sh index 4b04fda..3f0dff6 100755 --- a/functions.sh +++ b/functions.sh @@ -137,16 +137,12 @@ function getEncryptedCredential() { echo "$encrypted_value" } -function mysqlFilterWarning() { - sed '/mysql: \[Warning\] Using a password on the command line interface can be insecure\./d' -} - function mysqlcheckDatabaseConnection() { local username="$1" local password="$2" local host="$3" - - mysql -u "$username" -p"$password" -h "$host" -e "SELECT 1;" 2> >(mysqlFilterWarning) || { + export MYSQL_PWD="$password" + mysql -u "$username" -h "$host" -e "SELECT 1;" || { logErrorMessage "Failed to login to the database server. Check your credentials and connection." exit 1 } @@ -158,8 +154,9 @@ function mysqlcreateDatabase() { local password="$2" local host="$3" local dbName="$4" + export MYSQL_PWD="$password" - mysql -u "$username" -p"$password" -h "$host" -e "CREATE DATABASE $dbName;" 2> >(mysqlFilterWarning) || { + mysql -u "$username" -h "$host" -e "CREATE DATABASE $dbName;" || { logErrorMessage "Failed to create the database." exit 1 } @@ -172,8 +169,9 @@ function mysqlcreateUser() { local dbName="$4" local userName="$5" local userPassword="$6" + export MYSQL_PWD="$password" - mysql -u "$username" -p"$password" -h "$host" -e "CREATE USER '$userName'@'$host' IDENTIFIED BY '$userPassword';" 2> >(mysqlFilterWarning) || { + mysql -u "$username" -h "$host" -e "CREATE USER '$userName'@'$host' IDENTIFIED BY '$userPassword';" || { logErrorMessage "Failed to create the user [$userName]." exit 1 } @@ -185,8 +183,9 @@ function mysqlgrantPrivileges() { local host="$3" local dbName="$4" local userName="$5" + export MYSQL_PWD="$password" - mysql -u "$username" -p"$password" -h "$host" -e "GRANT ALL PRIVILEGES ON $dbName.* TO '$userName'@'$host'; FLUSH PRIVILEGES;" 2> >(mysqlFilterWarning) || { + mysql -u "$username" -h "$host" -e "GRANT ALL PRIVILEGES ON $dbName.* TO '$userName'@'$host'; FLUSH PRIVILEGES;" || { logErrorMessage "Failed to give privileges to the user [$userName]." exit 1 } @@ -197,8 +196,9 @@ function mysqlcheckUserPrivileges() { local password="$2" local host="$3" local userName="$4" + export MYSQL_PWD="$password" - mysql -u "$username" -p"$password" -h "$host" -e "SHOW GRANTS FOR '$userName'@'$host';" 2> >(mysqlFilterWarning) || { + mysql -u "$username" -h "$host" -e "SHOW GRANTS FOR '$userName'@'$host';" || { logErrorMessage "Failed to check privileges for the user [$userName]." exit 1 } From 1d7f10c257ec79dcc81120375d399ab380cd53f7 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan <107524071+Cpriyanshi77@users.noreply.github.com> Date: Fri, 15 Dec 2023 15:10:38 +0530 Subject: [PATCH 21/41] Updated functions.sh --- functions.sh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/functions.sh b/functions.sh index 3f0dff6..a5a2622 100755 --- a/functions.sh +++ b/functions.sh @@ -32,14 +32,9 @@ function getRepositoryTag() { echo "$BUILD_REPOSITORY_TAG" } -function getDockerfileParentPath() { +function getDockerfilePath() { DOCKERFILE_ENTRY=$(jq -r .build_detail.dockerfile_path < /bp/data/environment_build) - getNthTextInALine "$DOCKERFILE_ENTRY" : 2 -} - -function getDockerfileName() { - DOCKERFILE_ENTRY=$(jq -r .build_detail.dockerfile_path < /bp/data/environment_build) - getNthTextInALine "$DOCKERFILE_ENTRY" : 1 + echo "$DOCKERFILE_ENTRY" } function saveTaskStatus() { From 42d5eda20a4df2afe591505d7de9a5aad04ddfda Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan <107524071+Cpriyanshi77@users.noreply.github.com> Date: Sat, 16 Dec 2023 01:31:18 +0530 Subject: [PATCH 22/41] Update functions.sh --- functions.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/functions.sh b/functions.sh index a5a2622..c3c3b56 100755 --- a/functions.sh +++ b/functions.sh @@ -37,6 +37,11 @@ function getDockerfilePath() { echo "$DOCKERFILE_ENTRY" } +function getGitBranch() { + GIT_BRANCH_NAME=$(jq -r .git_repo.branch_name < /bp/data/environment_build) + echo "$GIT_BRANCH_NAME" +} + function saveTaskStatus() { TASK_STATUS="$1" ACTIVITY_SUB_TASK_CODE="$2" From 1a5aabd3e71f60e646724600f60950a5d84adc91 Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Tue, 19 Mar 2024 15:09:25 +0530 Subject: [PATCH 23/41] update --- functions.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/functions.sh b/functions.sh index c3c3b56..4031a93 100755 --- a/functions.sh +++ b/functions.sh @@ -42,6 +42,11 @@ function getGitBranch() { echo "$GIT_BRANCH_NAME" } +function getGitUrl() { + GIT_BRANCH_NAME=$(jq -r .git_repo.git_url < /bp/data/environment_build) + echo "$GIT_BRANCH_NAME" +} + function saveTaskStatus() { TASK_STATUS="$1" ACTIVITY_SUB_TASK_CODE="$2" From bd86338968743ae0f27b442d01453bdfb8a9b5dc Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Tue, 26 Mar 2024 13:45:53 +0530 Subject: [PATCH 24/41] added funtions for newrelic --- functions.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/functions.sh b/functions.sh index 4031a93..707cf37 100755 --- a/functions.sh +++ b/functions.sh @@ -47,6 +47,42 @@ function getGitUrl() { echo "$GIT_BRANCH_NAME" } +function getDeploymentTimestamp() { + Deployment_Timestamp=$(jq -r .build_info.deployment_trigger_time < /bp/data/deploy_stateless_app) + echo "$Deployment_Timestamp" +} + +function getDeploymentUser() { + Deployment_User=$(jq -r .build_info.user < /bp/data/deploy_stateless_app) + echo "$Deployment_User" +} + +function getDeploymentImage() { + Deployment_Image=$(jq -r '.addition_meta_data.environment_variables.envs_list[] | select(.env_key == "image.url") | .env_value' < /bp/data/deploy_stateless_app) + echo "$Deployment_Image" +} + +function getGitCommitSha() { + Git_Commit_Sha=$(jq -r .build_info.commit_sha < /bp/data/deploy_stateless_app) + echo "$Git_Commit_Sha" +} + +function getDeploymentGitBranch() { + Deployment_Git_branch=$(jq -r .build_info.git_branch < /bp/data/deploy_stateless_app) + echo "$Deployment_Git_branch" +} + +function getNewrelicGuid() { + Newrelic_Guid=$(jq -r '.addition_meta_data.environment_variables.envs_list[] | select(.env_key == "newrelic_entity_guid") | .env_value' < /bp/data/deploy_stateless_app) + echo "$Newrelic_Guid" +} + + +function getDeploymentGitUrl() { + Deployment_Git_Url=$(jq -r .build_info.git_url < /bp/data/deploy_stateless_app) + echo "$Deployment_Git_Url" +} + function saveTaskStatus() { TASK_STATUS="$1" ACTIVITY_SUB_TASK_CODE="$2" From 93fce3cbb7b9e15d2099c1f8873379d82c083e31 Mon Sep 17 00:00:00 2001 From: priyanshi chauhan Date: Tue, 26 Mar 2024 15:00:21 +0530 Subject: [PATCH 25/41] added function --- functions.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/functions.sh b/functions.sh index 707cf37..52a7d09 100755 --- a/functions.sh +++ b/functions.sh @@ -67,6 +67,11 @@ function getGitCommitSha() { echo "$Git_Commit_Sha" } +function getGitCommitMsg() { + Git_Commit_Msg=$(jq -r .build_info.commit_msg < /bp/data/deploy_stateless_app) + echo "$Git_Commit_Msg" +} + function getDeploymentGitBranch() { Deployment_Git_branch=$(jq -r .build_info.git_branch < /bp/data/deploy_stateless_app) echo "$Deployment_Git_branch" From 1cc5c5af5f10a4649b1fb197728f4917e74fda7f Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Fri, 4 Oct 2024 17:21:26 +0530 Subject: [PATCH 26/41] added aws functions --- aws-functions.sh | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/aws-functions.sh b/aws-functions.sh index c6efeb6..22c729e 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -62,10 +62,39 @@ function createRole() { function getAssumeRole() { ROLE_ARN=$1 - export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \ - $(aws sts assume-role \ - --role-arn ${ROLE_ARN} \ - --role-session-name default \ - --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \ - --output text)) + creds=$(aws sts assume-role \ + --role-arn "${ROLE_ARN}" \ + --role-session-name "default" \ + --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \ + --output text) + + export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" $creds) + + echo "AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID" + echo "AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY" + echo "AWS_SESSION_TOKEN: $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') + aws_secret_access_key=$(echo $aws_creds | sed "s/'/\"/g" | jq -r '.aws_secret_access_key') + + 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 } + + + From a46fc68d84bc940e525180fa0cab9f741751b3b6 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Fri, 4 Oct 2024 17:22:31 +0530 Subject: [PATCH 27/41] added aws functions --- aws-functions.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/aws-functions.sh b/aws-functions.sh index 22c729e..48588c8 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -95,6 +95,3 @@ function check_aws_authentication() { logInfoMessage "Successfully authenticated with AWS CLI." fi } - - - From a7b8d8f0630e7dcfc33e8efbb4a16261da471438 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Fri, 4 Oct 2024 18:05:55 +0530 Subject: [PATCH 28/41] added aws functions --- aws-functions.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws-functions.sh b/aws-functions.sh index 48588c8..dd79cad 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -70,9 +70,9 @@ function getAssumeRole() { export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" $creds) - echo "AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID" - echo "AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY" - echo "AWS_SESSION_TOKEN: $AWS_SESSION_TOKEN" + export "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID" + export "AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY" + export "AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN" } function set_aws_credentials() { From 9f2b274b92e08c6cad0b7aeb31665eddabcf2830 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Fri, 4 Oct 2024 18:25:01 +0530 Subject: [PATCH 29/41] added aws functions --- aws-functions.sh | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/aws-functions.sh b/aws-functions.sh index dd79cad..dc27675 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -62,17 +62,20 @@ function createRole() { function getAssumeRole() { ROLE_ARN=$1 - creds=$(aws sts assume-role \ - --role-arn "${ROLE_ARN}" \ - --role-session-name "default" \ - --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \ - --output text) + role_output=$(aws sts assume-role --role-arn "$ROLE_ARN" --role-session-name default) - export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" $creds) + if [ $? -ne 0 ]; then + echo "Failed to assume role." + exit 1 + fi - export "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID" - export "AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY" - export "AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN" + AWS_ACCESS_KEY_ID=$(echo $role_output | jq -r '.Credentials.AccessKeyId') + AWS_SECRET_ACCESS_KEY=$(echo $role_output | jq -r '.Credentials.SecretAccessKey') + AWS_SESSION_TOKEN=$(echo $role_output | jq -r '.Credentials.SessionToken') + + export AWS_ACCESS_KEY_ID + export AWS_SECRET_ACCESS_KEY + export AWS_SESSION_TOKEN } function set_aws_credentials() { From 5cd6dd97439501ac1d9c4df1452e48102e916fe9 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Fri, 4 Oct 2024 18:35:23 +0530 Subject: [PATCH 30/41] added aws functions --- aws-functions.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-functions.sh b/aws-functions.sh index dc27675..d86d022 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -65,7 +65,7 @@ function getAssumeRole() { role_output=$(aws sts assume-role --role-arn "$ROLE_ARN" --role-session-name default) if [ $? -ne 0 ]; then - echo "Failed to assume role." + echo "Failed to assume role :- $ROLE_ARN." exit 1 fi From f0b14ab90c31c58e3beb5cf3e69f31a44521483d Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Fri, 4 Oct 2024 22:23:33 +0530 Subject: [PATCH 31/41] added aws functions --- aws-functions.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/aws-functions.sh b/aws-functions.sh index d86d022..50c99aa 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -98,3 +98,44 @@ function check_aws_authentication() { 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" + BUILDX_ENABLE="$8" + 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 + echo "Error creating EC2 instance." + echo "$EC2_CREATE_OUTPUT" + return 1 + fi + + echo "$EC2_CREATE_OUTPUT" + return 0 +} + + + From 3a80b271a0c951e5ad189ad8b6ad66ed08550c4f Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:12:04 +0530 Subject: [PATCH 32/41] updated code --- functions.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/functions.sh b/functions.sh index 52a7d09..343e755 100755 --- a/functions.sh +++ b/functions.sh @@ -250,3 +250,20 @@ function mysqlcheckUserPrivileges() { } } +jsonOutput() { + file_name="$1" + output_vars="$2" + + file_content="" + if [[ -f "$file_name" ]]; then + file_content=$(<"$file_name") + fi + [[ "$file_content" != "["* ]] && file_content="[]" + + updated_content=$(jq -c ". += [$output_vars]" <<< "$file_content") + + echo "$updated_content" | jq "." > "$file_name" + + echo "Job step response updated in: $file_name" +} + From 25811eb5591b1071460c1958ec367202ffbbb80b Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:17:37 +0530 Subject: [PATCH 33/41] updated code --- functions.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/functions.sh b/functions.sh index 343e755..42a1295 100755 --- a/functions.sh +++ b/functions.sh @@ -250,6 +250,16 @@ function mysqlcheckUserPrivileges() { } } +function getProjectEnv() { + PROJECT_ENV_NAME=$(jq -r .environment.project_env < /bp/data/environment_build) + getNthTextInALine "$PROJECT_ENV_NAME" : 1 +} + +function getServiceName() { + PROJECT_SVC_NAME=$(jq -r .component.name < /bp/data/environment_build) + getNthTextInALine "$PROJECT_SVC_NAME" : 1 +} + jsonOutput() { file_name="$1" output_vars="$2" From 1c46137641cf8e12942f328e3ead3e27e1c30a33 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:19:35 +0530 Subject: [PATCH 34/41] updated code --- functions.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/functions.sh b/functions.sh index 42a1295..feef7a6 100755 --- a/functions.sh +++ b/functions.sh @@ -37,6 +37,11 @@ function getDockerfilePath() { echo "$DOCKERFILE_ENTRY" } +function getGitRepo() { + GIT_REPO_URL=$(jq -r .git_repo.git_url < /bp/data/environment_build) + echo "$GIT_REPO_URL" +} + function getGitBranch() { GIT_BRANCH_NAME=$(jq -r .git_repo.branch_name < /bp/data/environment_build) echo "$GIT_BRANCH_NAME" @@ -77,9 +82,9 @@ function getDeploymentGitBranch() { echo "$Deployment_Git_branch" } -function getNewrelicGuid() { - Newrelic_Guid=$(jq -r '.addition_meta_data.environment_variables.envs_list[] | select(.env_key == "newrelic_entity_guid") | .env_value' < /bp/data/deploy_stateless_app) - echo "$Newrelic_Guid" +function getNewrelicApm() { + Newrelic_apm=$(jq -r '.addition_meta_data.environment_variables.envs_list[] | select(.env_key == "APM_NAME") | .env_value' < /bp/data/deploy_stateless_app) + echo "$Newrelic_apm" } From 7e5f081b1bf2294fb81b08bccba3fc701b030ec1 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:21:02 +0530 Subject: [PATCH 35/41] updated code --- functions.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.sh b/functions.sh index feef7a6..a4cb4f0 100755 --- a/functions.sh +++ b/functions.sh @@ -265,7 +265,7 @@ function getServiceName() { getNthTextInALine "$PROJECT_SVC_NAME" : 1 } -jsonOutput() { +function jsonOutput() { file_name="$1" output_vars="$2" From aee055e0ff1dbe622593c278b612cea4b6117ae8 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:22:06 +0530 Subject: [PATCH 36/41] updated code --- functions.sh | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/functions.sh b/functions.sh index a4cb4f0..dd4f07d 100755 --- a/functions.sh +++ b/functions.sh @@ -265,20 +265,4 @@ function getServiceName() { getNthTextInALine "$PROJECT_SVC_NAME" : 1 } -function jsonOutput() { - file_name="$1" - output_vars="$2" - - file_content="" - if [[ -f "$file_name" ]]; then - file_content=$(<"$file_name") - fi - [[ "$file_content" != "["* ]] && file_content="[]" - - updated_content=$(jq -c ". += [$output_vars]" <<< "$file_content") - - echo "$updated_content" | jq "." > "$file_name" - - echo "Job step response updated in: $file_name" -} From 851075522b2663563faf0dc17d19f7063447dfc1 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:24:48 +0530 Subject: [PATCH 37/41] updated code --- functions.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/functions.sh b/functions.sh index dd4f07d..10dc3d0 100755 --- a/functions.sh +++ b/functions.sh @@ -264,5 +264,3 @@ function getServiceName() { PROJECT_SVC_NAME=$(jq -r .component.name < /bp/data/environment_build) getNthTextInALine "$PROJECT_SVC_NAME" : 1 } - - From e1ec116d7c9cf30f2b0baa0c2d1256d61d51f448 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:27:40 +0530 Subject: [PATCH 38/41] updated code --- functions.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/functions.sh b/functions.sh index 10dc3d0..c2b4aa7 100755 --- a/functions.sh +++ b/functions.sh @@ -264,3 +264,20 @@ function getServiceName() { PROJECT_SVC_NAME=$(jq -r .component.name < /bp/data/environment_build) getNthTextInALine "$PROJECT_SVC_NAME" : 1 } + +function jsonOutput() { + file_name="$1" + output_vars="$2" + + file_content="" + if [[ -f "$file_name" ]]; then + file_content=$(<"$file_name") + fi + [[ "$file_content" != "["* ]] && file_content="[]" + + updated_content=$(jq -c ". += [$output_vars]" <<< "$file_content") + + echo "$updated_content" | jq "." > "$file_name" + + echo "Job step response updated in: $file_name" +} \ No newline at end of file From acf489f8a1383da8922b06cdd26c1f549214c476 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan <107524071+Cpriyanshi77@users.noreply.github.com> Date: Sat, 5 Oct 2024 00:30:45 +0530 Subject: [PATCH 39/41] Update functions.sh --- functions.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.sh b/functions.sh index c2b4aa7..ce51413 100755 --- a/functions.sh +++ b/functions.sh @@ -280,4 +280,4 @@ function jsonOutput() { echo "$updated_content" | jq "." > "$file_name" echo "Job step response updated in: $file_name" -} \ No newline at end of file +} From c4f26e0cd24c220e1409cd69d909ab2f928c48c4 Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 5 Oct 2024 00:35:27 +0530 Subject: [PATCH 40/41] updated code --- aws-functions.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/aws-functions.sh b/aws-functions.sh index 50c99aa..ee8e82c 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -138,4 +138,3 @@ function create_ec2_instance() { } - From 87995651ca506729ac78152a109d38089e615c1c Mon Sep 17 00:00:00 2001 From: Priyanshi Chauhan Date: Sat, 12 Oct 2024 00:19:49 +0530 Subject: [PATCH 41/41] updated aws function --- aws-functions.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ functions.sh | 22 +++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/aws-functions.sh b/aws-functions.sh index ee8e82c..62c4bb2 100644 --- a/aws-functions.sh +++ b/aws-functions.sh @@ -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 +} \ No newline at end of file diff --git a/functions.sh b/functions.sh index ce51413..d6b508f 100755 --- a/functions.sh +++ b/functions.sh @@ -265,7 +265,7 @@ function getServiceName() { getNthTextInALine "$PROJECT_SVC_NAME" : 1 } -function jsonOutput() { +function jsonOutputWithoutArray() { file_name="$1" output_vars="$2" @@ -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" +}