Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
15d5537
[Fix] - to check account_id when using it to prevent bugs if fast_mod…
lamhaison Feb 20, 2023
dca4df8
[Update] - to check account_id when login and logout and add function…
lamhaison Feb 20, 2023
a239b59
[Add] - add function to list aws ami - aws_ec2_list_aws_default_images
lamhaison Feb 21, 2023
0315ddf
[Add] - add support eks commandlines
lamhaison Feb 21, 2023
bd3b85d
[Fix] - to fix typo for function aws_s3_rm
lamhaison Feb 24, 2023
5202d68
[Fix] - to add condition when create s3 bucket at us-east-1
lamhaison Feb 24, 2023
0d57c02
[Refactor] - to split the function aws_commandline_logging for logging
lamhaison Feb 24, 2023
75c207d
[Add] - add feature to autoload the current assume role when opening …
lamhaison Feb 26, 2023
17adeab
[Update] - to To check if the credential is expired, do not autoload …
lamhaison Feb 26, 2023
08e88df
[Refactor] - to refactor code to reuse function aws_assume_role_unzip…
lamhaison Feb 26, 2023
69c4660
[Refactor] - to refactor function name
lamhaison Feb 26, 2023
aab3fee
[Fix] - forget to assigin args to aws_ec2_instance_id for function aw…
lamhaison Feb 27, 2023
b6ea54e
[Fix] - fix typo
lamhaison Feb 27, 2023
eae1098
[Add] - add function to give commandline instructions to delete RDS
lamhaison Feb 28, 2023
a10efbf
[Add] - to add function to list ecs scaling policies
lamhaison Feb 28, 2023
c7ef60e
[Refactor] - to refactor function name
lamhaison Feb 28, 2023
3ee007d
[Update] - show DBClusterIdentifier for function aws_rds_list_db_inst…
lamhaison Mar 1, 2023
7bf78a8
[Update] - to move start build commandline to aws_run_commandline to …
lamhaison Mar 2, 2023
fc13c43
[Update] - to add name for lambda sources bucket
lamhaison Mar 3, 2023
46eb1dc
[Refactor] - to allow to add args for load source instead of getting …
lamhaison Mar 5, 2023
5a5ec6a
[Add] - add body for function todo-later to prevent importing failed …
lamhaison Mar 5, 2023
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
19 changes: 15 additions & 4 deletions common/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ aws_assume_role_disable_fast_mode() {
export aws_assume_role_print_account_info=true
}

aws_assume_role_disable_load_current_assume_role_for_new_tab() {
rm -rf ${aws_cli_current_assume_role_name} >/dev/null
}

aws_assume_role_disable_show_detail_commandline() {
export aws_show_commandline=false
}
Expand Down Expand Up @@ -59,10 +63,16 @@ aws_run_commandline() {
aws_run_commandline_with_logging "${aws_run_commandline}"
}

function aws_commandline_logging() {
local aws_commandline_logging=$(echo ${1:?'aws_commandline is unset or empty'} | tr -d '\t' | tr -d '\n' | tr -s ' ')

if [ "$aws_show_commandline" = "true" ]; then
echo "Running commandline [ ${aws_commandline_logging} ]"
fi
}

aws_run_commandline_with_logging() {
aws_commandline=$1
aws_commandline_logging=$(echo ${aws_commandline:?'aws_commandline is unset or empty'} | tr -d '\t' | tr -d '\n')
# aws_commandline_logging=$(echo ${aws_commandline})
local aws_commandline=$1
local log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log

if [ "$aws_show_log_uploaded" = "true" ]; then
Expand All @@ -74,14 +84,15 @@ aws_run_commandline_with_logging() {
local tee_command="tee -a ${log_file_path}"
fi

# TODO Later (Consider to remove it because we add aws_commandline_logging function with condition)
if [ "$aws_show_commandline" = "true" ]; then
local detail_commandline_tee_command="${tee_command}"
else
local detail_commandline_tee_command="${tee_command} > /dev/null"
fi

echo "------------------------------STARTED--$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null
echo "Running commandline [ ${aws_commandline_logging} ]" | eval $detail_commandline_tee_command
aws_commandline_logging ${aws_commandline} | eval $detail_commandline_tee_command
aws_commandline_result=$(aws_run_commandline_with_retry "${aws_commandline}" "${ignored_error_when_retry}")
echo $aws_commandline_result | eval $tee_command
echo "------------------------------FINISHED-$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null
Expand Down
46 changes: 39 additions & 7 deletions main.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
# Get the current directory of the main.sh script.
export AWS_CLI_SOURCE_SCRIPTS="$(dirname -- "$0")"
#!/bin/bash
#
# @version 1.0
# @script main.sh
# @description TODO : to load function for aws-cli-utils
# $1: Where is looking for sh files and source the list
# $2: Do you want to set the bind key?

AWS_CLI_SOURCE_SCRIPTS=$1

if [[ -z "${AWS_CLI_SOURCE_SCRIPTS}" ]]; then
LOCAL_AWS_CLI_SOURCE_SCRIPTS=$(dirname -- "$0")
if [[ "${LOCAL_AWS_CLI_SOURCE_SCRIPTS}" = "." ]]; then
DEFAULT_AWS_CLI_SOURCE_SCRIPTS='/opt/lamhaison-tools/aws-cli-utils'
fi

export AWS_CLI_SOURCE_SCRIPTS="${LOCAL_AWS_CLI_SOURCE_SCRIPTS:=${DEFAULT_AWS_CLI_SOURCE_SCRIPTS}}"
else
export AWS_CLI_SOURCE_SCRIPTS=${AWS_CLI_SOURCE_SCRIPTS}
fi

export assume_role_password_encrypted="$(cat ~/.password_assume_role_encrypted)"
export tmp_credentials="/tmp/aws_temporary_credentials"

export aws_cli_results="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results"
export aws_cli_logs="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/logs"
export aws_cli_images="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/images"
Expand Down Expand Up @@ -35,12 +54,25 @@ alias get-account-alias='aws iam list-account-aliases'
alias get-account-id='echo AccountId $(aws sts get-caller-identity --query "Account" --output text)'

# Import sub-commandlines.
for script in $(find ${AWS_CLI_SOURCE_SCRIPTS} -type f -name '*.sh' | grep -v main.sh); do
for script in $(find ${AWS_CLI_SOURCE_SCRIPTS} -type f -name '*.sh' | grep -v main.sh | grep -v main.sh | grep -v test.sh | grep -v temp.sh); do
source $script
done

# Add hot-keys
# zle -N aws_help
zle -N aws_main_function
bindkey '^@' aws_main_function
# Reuse session in the new terminal
export aws_cli_current_assume_role_name="/tmp/aws_cli_current_assume_role_SW7DNb48oQB57"
export aws_cli_load_current_assume_role=false
# If the file is not empty
# TODO Later (To check if the credential is expired, don't autoload credential)
if [ "true" = "${aws_cli_load_current_assume_role}" ] && [ -s "${aws_cli_current_assume_role_name}" ]; then
aws_assume_role_load_current_assume_role_for_new_tab
fi

LHS_BIND_KEY=${2:='True'}

if [[ "${LHS_BIND_KEY}" = "True" ]]; then
# Add hot-keys
# zle -N aws_help
zle -N aws_main_function
bindkey '^@' aws_main_function
# bindkey '^e' aws_help
fi
2 changes: 1 addition & 1 deletion services/alb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ aws_alb_get_listeners() {
}

aws_alb_get_listner() {

echo "TODO Later"
}

aws_alb_get_rules() {
Expand Down
2 changes: 1 addition & 1 deletion services/application_autoscaling.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Setting for ecs
aws_application_autoscaling_ecs() {
aws_application_autoscaling_ecs_list_scheduled_actions() {
aws application-autoscaling describe-scheduled-actions \
--service-namespace ecs
}
88 changes: 59 additions & 29 deletions services/assume_role.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,37 @@ aws_assume_role_get_credentail() {

}

aws_assume_role_is_tmp_credential_valid() {
aws_assume_role_unzip_tmp_credential_valid() {
local aws_assume_role=$1
local tmp_credentials_file_zip="${tmp_credentials}/${aws_assume_role}.zip"
local assume_role_duration="$((${aws_assume_role_expired_time} - 5))"

local tmp_credentials_file="${tmp_credentials}/${ASSUME_ROLE}"
local tmp_credentials_file_zip="${tmp_credentials}/${ASSUME_ROLE}.zip"
local expired_tmp_credential=$(find ${tmp_credentials} -name ${aws_assume_role}.zip -mmin +${assume_role_duration})
# the file aws assume role zip file exists and not empty and not expired
if [ -s "${tmp_credentials_file_zip}" ] && [ -z "${expired_tmp_credential}" ]; then
echo "true"
else
echo "false"
fi

}

aws_assume_role_load_current_assume_role_for_new_tab() {

local aws_assume_role=$(cat ${aws_cli_current_assume_role_name})
local tmp_credentials_file_zip="${tmp_credentials}/${aws_assume_role}.zip"
local assume_role_duration="$((${aws_assume_role_expired_time} - 5))"

local valid_file=$(find ${tmp_credentials} -name ${ASSUME_ROLE}.zip -mmin +${assume_role_duration})
if [ "true" = "${aws_cli_load_current_assume_role}" ] &&
# the file current aws assume role exists
[ -s "${aws_cli_current_assume_role_name}" ] &&
[ "true" = "$(aws_assume_role_unzip_tmp_credential_valid ${aws_assume_role})" ]; then
aws_assume_role_set_name ${aws_assume_role}
fi
}

if [[ -n "${valid_file}" ]]; then
aws_assume_role_is_tmp_credential_valid() {
if [[ "true" = "$(aws_assume_role_unzip_tmp_credential_valid ${ASSUME_ROLE})" ]]; then
echo -ne "\e]1;AWS-PROFILE[ ${ASSUME_ROLE} ]\a"
aws_assume_role_re_use_current
fi
Expand All @@ -108,17 +130,13 @@ aws_assume_role_is_tmp_credential_valid() {

aws_call_assume_role() {
# Do later (Validate the variable of ASSUMED_ROLE before calling assume role)
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_SECURITY_TOKEN ASSUMED_ROLE
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_SECURITY_TOKEN ASSUMED_ROLE AWS_ACCOUNT_ID
tmp_credentials_file="${tmp_credentials}/${ASSUME_ROLE}"
tmp_credentials_file_zip="${tmp_credentials}/${ASSUME_ROLE}.zip"

assume_role_duration="$((${aws_assume_role_expired_time} - 5))"
if [ -f ${tmp_credentials_file_zip} ]; then

valid_file=$(find ${tmp_credentials} -name ${ASSUME_ROLE}.zip -mmin +${assume_role_duration})
empty_file=$(find ${tmp_credentials} -name ${ASSUME_ROLE}.zip -empty)
# Don't find any file is older than expired-time
if [ -z "${valid_file}" ] && [ -z "${empty_file}" ]; then
if [ "$(aws_assume_role_unzip_tmp_credential_valid ${ASSUME_ROLE})" = "true" ]; then
echo "Re-use the temporary credential of ${ASSUME_ROLE} at ${tmp_credentials_file_zip}"
else
echo "The credential is older than ${aws_assume_role_expired_time} or the credential is empty then we will run assume-role ${ASSUME_ROLE} again"
Expand All @@ -132,6 +150,11 @@ aws_call_assume_role() {
}

aws_assume_role_set_name() {

function aws_assume_role_save_current_assume_role() {
echo "${ASSUME_ROLE}" >${1:?'aws_cli_current_assume_role_name is unset or empty'}
}

aws_assume_role_name=$1
echo You set the assume role name ${aws_assume_role_name:?"The assume role name is unset or empty"}

Expand All @@ -153,38 +176,45 @@ aws_assume_role_set_name() {

echo -ne "\e]1;AWS-PROFILE[ ${ASSUME_ROLE} ]\a"
echo "You are using the assume role name ${ASSUME_ROLE}"

aws_assume_role_save_current_assume_role ${aws_cli_current_assume_role_name}
}

aws_assume_role_set_name_with_hint() {
# set -x
aws_assume_role_set_name_with_hint_peco
# set +x
}

aws_assume_role_set_name_with_hint_peco() {
echo "Please input your assume role name >"
local assume_role_list=$(grep -iE "\[*\]" ~/.aws/config |
tr -d "[]" | awk -F " " '{print $2}')
function peco_aws_asssume_role_list() {
grep -iE "\[*\]" ~/.aws/config |
tr -d "[]" | awk -F " " '{print $2}'

if [[ -n "${ASSUME_ROLE}" ]]; then
assume_role_list=$(echo ${assume_role_list} | grep -v ${ASSUME_ROLE})
assume_role_list=$(echo "${ASSUME_ROLE}\n${assume_role_list}")
}

fi
function aws_assume_role_insert_current_asssume_role_first() {
assume_role_list=$1
if [[ -n "${ASSUME_ROLE}" ]]; then
assume_role_list=$(echo ${assume_role_list} | grep -v ${ASSUME_ROLE})
assume_role_list=$(echo "${ASSUME_ROLE}\n${assume_role_list}")

fi

echo ${assume_role_list}
}

# local assume_role_name=$(echo "${assume_role_list}" | peco --selection-prefix "Current >")
local assume_role_name=$(echo "${assume_role_list}" | peco)
local assume_role_list=$(aws_assume_role_insert_current_asssume_role_first "$(peco_aws_asssume_role_list)")
local assume_role_name=$(peco_create_menu 'echo ${assume_role_list}' '--prompt "Please select your assume role name >"')
aws_assume_role_set_name $assume_role_name

}

aws_account_info() {
get-account-alias

aws_assume_role_get_aws_account_id() {
local aws_account_id=$(aws_run_commandline_with_retry 'aws sts get-caller-identity --query "Account" --output text' "true")
export AWS_ACCOUNT_ID=$aws_account_id
echo "AccountId ${AWS_ACCOUNT_ID}"

}

aws_account_info() {
get-account-alias
aws_assume_role_get_aws_account_id
echo "AccountId ${AWS_ACCOUNT_ID}"
echo AWS Region ${AWS_REGION:?"The AWS_REGION is unset or empty"}
}

Expand Down
7 changes: 5 additions & 2 deletions services/codebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ aws_codebuild_get_latest_build_with_hint() {

aws_codebuild_start() {
aws_codebuild_project_name=$1
echo Start the project name ${aws_codebuild_project_name:?"Project name is unset or empty"}
aws codebuild start-build --project-name ${aws_codebuild_project_name}

aws_run_commandline "\
aws codebuild start-build --project-name ${aws_codebuild_project_name}
"

}

aws_codebuild_start_with_hint() {
Expand Down
55 changes: 39 additions & 16 deletions services/ec2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,67 @@ aws_ec2_list() {
}

aws_ec2_get() {
instance_id=$1
echo Get the information of the ec2 ${instance_id:?"The instace_id is unset or empty"}
aws_run_commandline "aws ec2 describe-instances --instance-ids $instance_id"
aws_run_commandline "\
aws ec2 describe-instances \
--instance-ids ${1:?"The aws_ec2_instance_id is unset or empty"}
"
}

aws_ec2_reboot() {
instance_id=$1
echo Reboot the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
aws_run_commandline "aws ec2 reboot-instances --instance-ids $instance_id"
aws_run_commandline "\
aws ec2 reboot-instances \
--instance-ids ${1:?"The aws_ec2_instance_id is unset or empty"}
"
}

aws_ec2_stop() {
instance_id=$1
echo Stop the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
aws_run_commandline "aws ec2 stop-instances --instance-ids $instance_id"
aws_run_commandline "\
aws ec2 stop-instances \
--instance-ids ${1:?"The aws_ec2_instance_id is unset or empty"}
"
}

aws_ec2_start() {
instance_id=$1
echo start the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
aws_run_commandline "aws ec2 start-instances --instance-ids $instance_id"
aws_run_commandline "\
aws ec2 start-instances \
--instance-ids ${1:?"The aws_ec2_instance_id is unset or empty"}
"
}

aws_ec2_rm_instruction() {
aws_commandline_logging "\
aws ec2 terminate-instances \
--instance-ids ${1:="\$aws_ec2_instance_ids"}
"
}

# Ec2 image
aws_ec2_list_images() {
aws_run_commandline "aws ec2 describe-images --owners self"
}

aws_ec2_list_aws_default_images() {
aws_run_commandline " \
aws ec2 describe-images \
--filters 'Name=architecture,Values=x86_64' \
'Name=virtualization-type,Values=hvm' 'Name=root-device-type,Values=ebs' \
'Name=block-device-mapping.volume-type,Values=gp2' \
'Name=ena-support,Values=true' 'Name=owner-alias,Values=amazon' \
'Name=name,Values=*amzn2-ami-hvm-2.0.????????.?-x86_64-gp2' \
--query 'Images[*].[ImageId,Name]' --output text
"
}
aws_ec2_create_image() {
instance_id=$1
echo To create a image from the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
local aws_ec2_instance_id=$1
echo To create a image from the ec2 instance ${aws_ec2_instance_id:?"The instace_id is unset or empty"}
aws_ec2_instance_name=$(aws ec2 describe-instances \
--instance-ids $instance_id \
--instance-ids $aws_ec2_instance_id \
--query 'Reservations[*].Instances[*].{Tags:Tags[?Key == `Name`] | [0].Value}' \
--output text)

aws ec2 create-image \
--no-reboot \
--instance-id $instance_id \
--instance-id $aws_ec2_instance_id \
--name ${aws_ec2_instance_name}-$(date '+%Y-%m-%d-%H-%M-%S') \
--description ${aws_ec2_instance_name}-$(date '+%Y-%m-%d-%H-%M-%S') \
--query "ImageId" --output text
Expand Down
3 changes: 1 addition & 2 deletions services/ec2_autoscaling.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ aws_autoscaling_set_desired_capacity_with_hint() {

# TODO LATER
aws_autoscaling_detach_instance_with_hint() {
# aws_autoscaling_name=$(peco_create_menu 'peco_aws_autoscaling_list')

echo "TODO Later"
}

aws_autoscaling_get_launching_template() {
Expand Down
8 changes: 8 additions & 0 deletions services/ecr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ aws_ecr_list_repo() {
}

aws_ecr_login() {

if [[ -z "${AWS_ACCOUNT_ID}" ]]; then
aws_assume_role_get_aws_account_id
fi
aws ecr get-login-password --region ${AWS_REGION} | docker login \
--username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
}

aws_ecr_logout() {

if [[ -z "${AWS_ACCOUNT_ID}" ]]; then
aws_assume_role_get_aws_account_id
fi
docker logout ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
}

Expand Down
Loading