diff --git a/common/peco.sh b/common/peco.sh index 13aa4f6..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 @@ -93,3 +104,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/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 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 + " + } 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/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) + +} 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"} diff --git a/services/s3.sh b/services/s3.sh index c511d40..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,6 +44,15 @@ aws_s3_get_object_metadata() { } +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_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 @@ -29,3 +63,28 @@ aws_s3_get_object_metadata() { # 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} +} + +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) +}