From eea9d960d98bb3b465b9fd8e4e1ba2bccb40986b Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 10 Jan 2023 18:06:12 +0700 Subject: [PATCH 1/7] [update] - to add retry when failed assume role --- services/assume_role.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/services/assume_role.sh b/services/assume_role.sh index 4b28e4b..4c11079 100644 --- a/services/assume_role.sh +++ b/services/assume_role.sh @@ -28,7 +28,7 @@ aws_assume_role_unzip_tmp_credential() { aws_assume_role_remove_tmp_credential() { assume_role_name_input=$1 - tmp_credentials_file_zip=${tmp_credentials}/${assume_role_name_input}.zip + tmp_credentials_file_zip=${tmp_credentials}/${assume_role_name_input:?"aws_assume_role_remove_tmp_credential is unset or empty"}.zip if [ -f "${tmp_credentials_file_zip}" ]; then rm -r ${tmp_credentials_file_zip} fi @@ -44,7 +44,17 @@ aws_assume_role_get_credentail() { echo "Running assume-role ${ASSUME_ROLE}" echo "Remove the credential ${tmp_credentials_file}" rm -rf ${tmp_credentials_file} - assume-role -duration ${aws_assume_role_duration} ${ASSUME_ROLE} >${tmp_credentials_file} + + assume_role_result="" + while [[ "${assume_role_result}" == "" ]]; do + assume_role_result=$(assume-role -duration ${aws_assume_role_duration} ${ASSUME_ROLE}) + + if [[ "${assume_role_result}" == "" ]]; then + echo "Assume role couldn't be succesful.Please try again or Ctrl + C to exit" + fi + done + + echo $assume_role_result >${tmp_credentials_file} empty_file=$(find ${tmp_credentials} -name ${ASSUME_ROLE} -empty) if [ -z "${empty_file}" ]; then zip_tmp_credential From ec8727fd0e861f1fddb51137d78ce01964f4ff03 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 13 Jan 2023 13:35:22 +0700 Subject: [PATCH 2/7] [update] - add function to failover aurora cluster --- services/rds.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/services/rds.sh b/services/rds.sh index 6903f45..32f18d2 100644 --- a/services/rds.sh +++ b/services/rds.sh @@ -156,11 +156,25 @@ aws_rds_create_instance_snapshot_with_hint() { # AWS events aws_rds_list_events() { - aws rds describe-events + aws_run_commandline 'aws rds describe-events' + } # AWS rds reboot +aws_rds_failover_db_cluster() { + aws_rds_db_cluster_name=$1 + aws_run_commandline \ + " + aws rds failover-db-cluster \ + --db-cluster-identifier ${aws_rds_db_cluster_name:?'aws_rds_db_cluster_name is unset or empty'} + " +} + +aws_rds_failover_db_cluster_with_hint() { + aws_rds_db_cluster_name $(echo "$(peco_aws_list_db_clusters)" | peco) +} + aws_rds_reboot_db_instance() { aws_rds_db_instance_identifier=$1 echo Reboot the aws rds db instance ${aws_rds_db_instance_identifier:?"aws_rds_db_instance_identifier is unset or empty"} From d8a295020f425f5148c7a6c6c7d4fb4cfdedf181 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 13 Jan 2023 13:57:01 +0700 Subject: [PATCH 3/7] [update] - add search menu for aws codepipeline commandlines --- common/peco.sh | 4 ++++ services/codepipeline.sh | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/common/peco.sh b/common/peco.sh index 13aa4f6..3ec1f8e 100644 --- a/common/peco.sh +++ b/common/peco.sh @@ -93,3 +93,7 @@ peco_aws_s3_list() { peco_aws_codebuild_list() { peco_aws_input 'aws codebuild list-projects --query "*[]"' 'true' } + +peco_aws_codepipeline_list() { + peco_aws_input 'aws codepipeline list-pipelines --query "*[].name"' 'true' +} diff --git a/services/codepipeline.sh b/services/codepipeline.sh index 47ffc4f..13a50f4 100644 --- a/services/codepipeline.sh +++ b/services/codepipeline.sh @@ -5,17 +5,24 @@ aws_codepipeline_list() { } aws_codepipeline_get_latest_execution_with_hint() { - - echo "List pipelines" - aws codepipeline list-pipelines --query "*[].name" - - echo "Your pipeline >" - read codepipeline_name - aws_codepipeline_get_latest_execution $codepipeline_name + aws_codepipeline_get_latest_execution $(echo "$(peco_aws_codepipeline_list)" | peco) } aws_codepipeline_get_latest_execution() { codepipeline_name=$1 - aws codepipeline list-action-executions --pipeline-name $codepipeline_name --filter pipelineExecutionId=$(aws codepipeline list-pipeline-executions --pipeline-name $codepipeline_name --query "*[0].pipelineExecutionId" --output text) --output table + aws_codepipeline_execution_id_latest=$( + aws codepipeline list-pipeline-executions \ + --pipeline-name ${codepipeline_name:?'codepipeline_name is unset or empty'} \ + --query 'pipelineExecutionSummaries[0].pipelineExecutionId' \ + --output text | head -1 + ) + aws_run_commandline \ + " + aws codepipeline list-action-executions \ + --pipeline-name ${codepipeline_name:?'codepipeline_name is unset or empty'} \ + --filter pipelineExecutionId=${aws_codepipeline_execution_id_latest:?'aws_codepipeline_execution_id_latest is unset or empty'} \ + --output table + " + } From 97f80da1cbce5c5d22d1cd66959d1bf175a5b48d Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 13 Jan 2023 14:02:28 +0700 Subject: [PATCH 4/7] [update] - add function to get the s3 arn --- services/s3.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/s3.sh b/services/s3.sh index c511d40..67767bf 100644 --- a/services/s3.sh +++ b/services/s3.sh @@ -19,6 +19,15 @@ aws_s3_get_object_metadata() { } +aws_s3_get_s3_bucket_arn() { + aws_s3_bucket_name=$1 + echo "arn:aws:s3:::${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'}" +} + +aws_s3_get_s3_bucket_arn_with_hint() { + aws_s3_get_s3_bucket_arn $(echo "$(peco_aws_s3_list)" | peco) +} + # aws_s3_get_object_metadata_with_hint() { # bucket_name=$(echo "$(peco_aws_s3_list)" | peco) # object_key=$2 From 5bb8c9b57a0f21fbef5f6a77da524c781a4d1e8d Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 14 Jan 2023 11:40:12 +0700 Subject: [PATCH 5/7] [add] - add iam commandline --- services/iam.sh | 6 ++++++ services/s3.sh | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100644 services/iam.sh diff --git a/services/iam.sh b/services/iam.sh new file mode 100644 index 0000000..c68cd50 --- /dev/null +++ b/services/iam.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# TODO LATER +aws_iam_add_policy_to_role() { + +} diff --git a/services/s3.sh b/services/s3.sh index 67767bf..3b07aee 100644 --- a/services/s3.sh +++ b/services/s3.sh @@ -38,3 +38,10 @@ aws_s3_get_s3_bucket_arn_with_hint() { # aws_run_commandline "${commandline}" # } + +aws_s3_create() { + aws_s3_bucket_name=$1 + aws s3api create-bucket \ + --bucket ${aws_s3_bucket_name:?"aws_s3_bucket_name is unset or empty"} \ + --create-bucket-configuration LocationConstraint=${AWS_REGION} +} From 8a81e31ce4c6178a4a34c4133a5baf699eec407f Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 14 Jan 2023 17:35:10 +0700 Subject: [PATCH 6/7] [update] - add more helpful commandlines for s3 --- services/s3.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/services/s3.sh b/services/s3.sh index 3b07aee..e2f94e8 100644 --- a/services/s3.sh +++ b/services/s3.sh @@ -8,6 +8,31 @@ aws_s3_list() { aws_run_commandline 'aws s3api list-buckets --query "Buckets[].Name"' } +aws_s3_get_bucket() { + aws_s3_bucket_name=$1 + aws_run_commandline \ + " + aws s3 ls s3://${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'} + " + +} + +aws_s3_get_bucket_recursived() { + aws_s3_bucket_name=$1 + aws_run_commandline \ + " + aws s3 ls s3://${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'} --recursive + " +} + +aws_s3_get_bucket_with_hint() { + aws_s3_get_bucket $(echo "$(peco_aws_s3_list)" | peco) +} + +aws_s3_get_bucket_recursived_with_hint() { + aws_s3_get_bucket_recursived $(echo "$(peco_aws_s3_list)" | peco) +} + aws_s3_get_object_metadata() { bucket_name=$1 object_key=$2 @@ -19,12 +44,12 @@ aws_s3_get_object_metadata() { } -aws_s3_get_s3_bucket_arn() { +aws_s3_get_bucket_arn() { aws_s3_bucket_name=$1 echo "arn:aws:s3:::${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'}" } -aws_s3_get_s3_bucket_arn_with_hint() { +aws_s3_get_bucket_arn_with_hint() { aws_s3_get_s3_bucket_arn $(echo "$(peco_aws_s3_list)" | peco) } @@ -45,3 +70,21 @@ aws_s3_create() { --bucket ${aws_s3_bucket_name:?"aws_s3_bucket_name is unset or empty"} \ --create-bucket-configuration LocationConstraint=${AWS_REGION} } + +aws_s3_delete() { + aws_s3_bucket_name=$1 + echo "We didn't run the commandline, we just suggest the commandline" + echo "If you want ot process it please run the commandline \ + [ + aws_s3_get_bucket_recursived ${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'} + aws s3 rm s3://${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'}/ --recursive + aws_s3_get_bucket_recursived ${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'} + aws s3api delete-bucket --bucket ${aws_s3_bucket_name:?'aws_s3_bucket_name is unset or empty'} + aws_s3_ls + ] + " +} + +aws_s3_rm_with_hint() { + aws_s3_delete $(echo "$(peco_aws_s3_list)" | peco) +} From d53f26dbdec965c2e046b127216eb682ce88ea40 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 14 Jan 2023 17:36:00 +0700 Subject: [PATCH 7/7] [add] - to add the function to make consistent when making a name for aws resource --- common/peco.sh | 11 +++++++++++ services/name-convention.sh | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 services/name-convention.sh diff --git a/common/peco.sh b/common/peco.sh index 3ec1f8e..41cfe46 100644 --- a/common/peco.sh +++ b/common/peco.sh @@ -5,6 +5,11 @@ peco_assume_role_name() { cat ~/.aws/config | grep -e "^\[profile.*\]$" | peco } +peco_format_name_convention_pre_defined() { + peco_input=$1 + echo "${peco_input}" | tr "\t" "\n" | tr -s " " "\n" | tr -s '\n' +} + peco_format_aws_output_text() { peco_input=$1 echo "${peco_input}" | tr "\t" "\n" @@ -14,6 +19,12 @@ peco_aws_acm_list() { aws_acm_list | peco } +peco_name_convention_input() { + text_input=$1 + format_text=$(peco_format_name_convention_pre_defined $text_input) + echo $format_text +} + peco_aws_input() { aws_cli_commandline="${1} --output text" result_cached=$2 diff --git a/services/name-convention.sh b/services/name-convention.sh new file mode 100644 index 0000000..36c5513 --- /dev/null +++ b/services/name-convention.sh @@ -0,0 +1,30 @@ +aws_name_convention_get_prefix_name() { + echo "${ASSUME_ROLE}" +} + +aws_name_convention_get_short_env_name() { + echo "dev stg prd" +} + +aws_name_convention_get_long_env_name() { + echo "development staging production" +} + +aws_name_convention_get_s3_bucket_name() { + aws_s3_bucket_name=$1 + echo "The bucket name should will be like that \ + [ ${ASSUME_ROLE}-${aws_s3_bucket_name:?"aws_s3_bucket_name is unset or empty"} ]" | tr -s '' +} + +aws_name_convention_get_s3_bucket_name_with_hint() { + + aws_name_convention_resource_types="static \ + vod terraform cf-logs \ + alb-logs webapp-react admin-react" + + echo "List resource type ${aws_name_convention_resource_types}" + + aws_name_convention_get_s3_bucket_name \ + $(echo "$(peco_name_convention_input $aws_name_convention_resource_types)" | peco) + +}