Skip to content

Commit 6919ef1

Browse files
authored
Merge pull request #3 from lamhaison/develop
Develop
2 parents aa272b3 + 172039c commit 6919ef1

31 files changed

+857
-90
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ echo "random_string" > ~/.password_assume_role_encrypted
4040

4141
## Settings when open terminal (I am using iterm)
4242
```
43+
mkdir -p /opt/lamhaison-tools
44+
git clone [email protected]:lamhaison/aws-cli-utils.git
4345
echo "source /opt/lamhaison-tools/aws-cli-utils/main.sh" >> ~/.bashrc
4446
```
4547

common/common.sh

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,75 @@
1+
aws_assume_role_option_set_output_table() {
2+
export AWS_DEFAULT_OUTPUT="table"
3+
}
4+
5+
aws_assume_role_option_set_output_json() {
6+
export AWS_DEFAULT_OUTPUT="json"
7+
}
8+
9+
aws_assume_role_option_set_output_yml() {
10+
export AWS_DEFAULT_OUTPUT="yaml"
11+
}
12+
13+
aws_assume_role_enable_fast_mode() {
14+
export aws_assume_role_print_account_info=false
15+
}
16+
17+
aws_assume_role_disable_fast_mode() {
18+
export aws_assume_role_print_account_info=true
19+
}
20+
21+
aws_run_commandline_with_retry() {
22+
local aws_commandline=$1
23+
local silent_mode=$2
24+
local retry_counter=0
25+
26+
# Check credential valid first
27+
# aws_assume_role_is_tmp_credential_valid
28+
29+
while [[ "${retry_counter}" -le "${aws_cli_retry_time}" ]]; do
30+
31+
if [[ "${silent_mode}" = "true" ]]; then
32+
eval $aws_commandline 2>/dev/null
33+
else
34+
eval $aws_commandline
35+
fi
36+
37+
if [[ $? -ne 0 ]]; then
38+
retry_counter=$(($retry_counter + 1))
39+
40+
# if [[ "${silent_mode}" = "false" ]]; then
41+
# echo "Retry ${retry_counter}"
42+
# fi
43+
44+
sleep ${aws_cli_retry_sleep_interval}
45+
else
46+
break
47+
fi
48+
done
49+
50+
}
51+
152
aws_run_commandline() {
53+
aws_run_commandline=$1
54+
aws_run_commandline="${aws_run_commandline:?'aws_run_commandline is unset or empty'}"
55+
aws_run_commandline_with_logging "${aws_run_commandline}"
56+
}
57+
58+
aws_run_commandline_with_logging() {
259
aws_commandline=$1
3-
log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log
60+
aws_commandline_logging=$(echo ${aws_commandline:?'aws_commandline is unset or empty'} | tr -d '\t' | tr -d '\n')
61+
# aws_commandline_logging=$(echo ${aws_commandline})
62+
local log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log
463
if [ "$aws_show_commandline" = "true" ]; then
5-
output="tee -a ${log_file_path}"
64+
local output="tee -a ${log_file_path}"
665
else
7-
output=">> ${log_file_path}"
66+
local output=">> ${log_file_path}"
867
fi
968

69+
aws_commandline_result=$(aws_run_commandline_with_retry "${aws_commandline}" "${ignored_error_when_retry}")
70+
1071
echo "-------------------------------------START--$(date '+%Y-%m-%d-%H-%M-%S')------------------------------------------------" >>${log_file_path}
11-
echo Running commandline \[ ${aws_commandline:?"Commandline is unset or empty"}\ ] | eval $output
12-
eval $aws_commandline | tee -a ${aws_cli_logs}/${ASSUME_ROLE}.log
72+
echo "Running commandline [ ${aws_commandline_logging} ]" | eval $output
73+
echo $aws_commandline_result | tee -a ${log_file_path}
1374
echo "-------------------------------------FINISH-$(date '+%Y-%m-%d-%H-%M-%S')------------------------------------------------" >>${log_file_path}
1475
}

common/help_ssh.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
lamhaison_help_create_user() {
2+
3+
echo '
4+
5+
visudo
6+
son.lam ALL=(ALL) ALL
7+
# Define variable
8+
user_name=son.lam
9+
sudo_password=xxxx
10+
public_key="ssh-rsa AAAAB3Nza... ${user_name}"
11+
useradd ${user_name}
12+
13+
# Sudo to root account
14+
echo -e "${user_name}\n${user_name}" | (passwd ${user_name})
15+
su vltlhson
16+
mkdir ~/.ssh
17+
chmod 700 ~/.ssh
18+
echo "${public_key}" > ~/.ssh/authorized_keys
19+
chmod 400 -R ~/.ssh/authorized_keys
20+
exit
21+
22+
23+
24+
'
25+
}

common/hep_aws_rds.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ aws_help_rds_list_bin_logs() {
1313
"show binary logs;"
1414
}
1515

16-
aws_help_start_stop_replication() {
16+
aws_help_rds_start_stop_replication() {
1717
echo "CALL mysql.rds_start_replication;"
18+
echo "show slave status\G;"
1819
echo "CALL mysql.rds_stop_replication;"
20+
echo "show slave status\G;"
1921
}
2022

2123
aws_help_rds_aurora_set_replication() {

common/logging.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
aws_assume_role_check_log() {
4+
local log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log
5+
view +$ -c 'set number' ${log_file_path}
6+
}

common/peco.sh

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
# brew install peco
2-
# PECO
3-
41
peco_assume_role_name() {
52
cat ~/.aws/config | grep -e "^\[profile.*\]$" | peco
63
}
74

85
peco_format_name_convention_pre_defined() {
9-
peco_input=$1
6+
local peco_input=$1
107
echo "${peco_input}" | tr "\t" "\n" | tr -s " " "\n" | tr -s '\n'
118
}
129

1310
peco_format_aws_output_text() {
14-
peco_input=$1
11+
local peco_input=$1
1512
echo "${peco_input}" | tr "\t" "\n"
1613
}
1714

@@ -20,37 +17,63 @@ peco_aws_acm_list() {
2017
}
2118

2219
peco_name_convention_input() {
23-
text_input=$1
24-
format_text=$(peco_format_name_convention_pre_defined $text_input)
20+
local text_input=$1
21+
local format_text=$(peco_format_name_convention_pre_defined $text_input)
22+
echo $format_text
23+
}
24+
25+
peco_create_menu_with_array_input() {
26+
local text_input=$1
27+
local format_text=$(peco_format_name_convention_pre_defined $text_input)
2528
echo $format_text
2629
}
2730

31+
peco_aws_disable_input_cached() {
32+
export peco_input_expired_time=0
33+
}
34+
2835
peco_aws_input() {
29-
aws_cli_commandline="${1} --output text"
30-
result_cached=$2
36+
peco_commandline_input "${1} --output text" $2
37+
}
38+
39+
peco_commandline_input() {
3140

32-
md5_hash=$(echo $aws_cli_commandline | md5)
33-
input_folder=${aws_cli_input_tmp}/${ASSUME_ROLE}
41+
local commandline="${1}"
42+
local result_cached=$2
43+
44+
local md5_hash=$(echo $commandline | md5)
45+
local input_folder=${aws_cli_input_tmp}/${ASSUME_ROLE}
3446
mkdir -p ${input_folder}
35-
input_file_path="${input_folder}/${md5_hash}.txt"
36-
empty_file=$(find ${input_folder} -name ${md5_hash}.txt -empty)
47+
local input_file_path="${input_folder}/${md5_hash}.txt"
48+
local empty_file=$(find ${input_folder} -name ${md5_hash}.txt -empty)
49+
local valid_file=$(find ${input_folder} -name ${md5_hash}.txt -mmin +${peco_input_expired_time})
3750

3851
# The file is existed and not empty and the flag result_cached is not empty
39-
if [ -f "${input_file_path}" ] && [ -z "${empty_file}" ] && [ -n "${result_cached}" ]; then
52+
if [ -z "${valid_file}" ] && [ -f "${input_file_path}" ] && [ -z "${empty_file}" ] && [ -n "${result_cached}" ]; then
4053
# Ignore the first line.
4154
grep -Ev "\*\*\*\*\*\*\*\* \[.*\]" $input_file_path
4255
else
43-
aws_result=$(eval $aws_cli_commandline)
44-
format_text=$(peco_format_aws_output_text $aws_result)
56+
local aws_result=$(aws_run_commandline_with_retry "$commandline" "false")
57+
58+
local format_text=$(peco_format_aws_output_text $aws_result)
4559

4660
if [ -n "${format_text}" ]; then
47-
echo "******** [ ${aws_cli_commandline} ] ********" >${input_file_path}
61+
echo "******** [ ${commandline} ] ********" >${input_file_path}
4862
echo ${format_text} | tee -a ${input_file_path}
4963
else
5064
echo "Can not get the data"
5165
fi
5266

5367
fi
68+
69+
}
70+
71+
peco_create_menu() {
72+
local input_function=$1
73+
local peco_options=$2
74+
local peco_command="peco ${peco_options}"
75+
local input_value=$(echo "$(eval $input_function)" | eval ${peco_command})
76+
echo ${input_value:?'Can not get the input from peco menu'}
5477
}
5578

5679
# AWS Logs
@@ -69,10 +92,25 @@ peco_aws_ecs_list_services() {
6992

7093
# AWS ECR
7194

72-
peco_aws_list_repositorie_names() {
95+
peco_aws_ecr_list_repositorie_names() {
7396
peco_aws_input 'aws ecr describe-repositories --query "*[].repositoryName"' 'true'
7497
}
7598

99+
peco_aws_ecr_list_images() {
100+
aws_ecr_repo_name=$1
101+
peco_aws_input "aws ecr list-images \
102+
--repository-name ${aws_ecr_repo_name:?'aws_ecr_repo_name is unset or empy'} \
103+
--query \"imageIds[].{imageTag:imageTag}\""
104+
}
105+
106+
peco_aws_alb_list_listners() {
107+
aws_alb_arn=$1
108+
peco_aws_input " \
109+
aws elbv2 describe-listeners \
110+
--load-balancer-arn ${aws_alb_arn:?'aws_alb_arn is unset or empty'} \
111+
--query \"Listeners[*].ListenerArn\""
112+
}
113+
76114
# AWS RDS
77115
peco_aws_list_db_parameter_groups() {
78116
peco_aws_input 'aws rds describe-db-parameter-groups --query "*[].DBParameterGroupName"' 'true'
@@ -108,3 +146,36 @@ peco_aws_codebuild_list() {
108146
peco_aws_codepipeline_list() {
109147
peco_aws_input 'aws codepipeline list-pipelines --query "*[].name"' 'true'
110148
}
149+
150+
# Codedeploy
151+
peco_aws_codedeploy_list_deployment_ids() {
152+
peco_aws_input 'aws deploy list-deployments --query "deployments[]"'
153+
}
154+
155+
# Cloudfront
156+
peco_aws_cloudfront_list() {
157+
commandline="aws cloudfront list-distributions \
158+
--query 'DistributionList.Items[*].{AId:Id,BComment:Comment}' --output text | tr -s '\t' '_'"
159+
peco_commandline_input ${commandline} 'true'
160+
}
161+
162+
# Autoscaling group
163+
peco_aws_autoscaling_list() {
164+
peco_aws_input 'aws autoscaling describe-auto-scaling-groups --query "*[].AutoScalingGroupName"' 'true'
165+
}
166+
167+
# IAM role list
168+
peco_aws_iam_list_roles() {
169+
peco_aws_input 'aws iam list-roles --query "*[].{RoleName:RoleName}"' 'true'
170+
}
171+
172+
peco_aws_iam_list_attached_policies() {
173+
peco_aws_input 'aws iam list-policies --scope Local --only-attached --query "*[].Arn"' 'true'
174+
}
175+
176+
# EC2 Instance
177+
peco_aws_ec2_list() {
178+
179+
commandline="aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId}' --output text | tr -s '\t' '_'"
180+
peco_commandline_input ${commandline} 'true'
181+
}

main.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ export aws_cli_results="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results"
77
export aws_cli_logs="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/logs"
88
export aws_cli_input_tmp="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/inputs"
99
export aws_tmp_input="/tmp/aws_tmp_input_23647494949484.txt"
10+
export aws_assume_role_print_account_info="false"
11+
export aws_cli_retry_time=10
12+
export aws_cli_retry_sleep_interval=1
13+
export ignored_error_when_retry="false"
1014
# max session 1h
11-
export aws_assume_role_duration=3600s
12-
export aws_assume_role_expired_time=55
15+
# The result of aws cli will be cached in x minute (10 minutes) for poco searching menu.
16+
export peco_input_expired_time=10
17+
export aws_assume_role_expired_time=60
1318
# To allow log detail of the aws cli [true|false]
1419
export aws_show_commandline=true
1520
export aws_log_tail_since=120m
@@ -19,14 +24,22 @@ mkdir -p ${aws_cli_results}
1924
mkdir -p ${aws_cli_logs}
2025
mkdir -p ${aws_cli_input_tmp}
2126

27+
# Default settings AWSCLI
28+
export AWS_DEFAULT_OUTPUT="json"
29+
2230
# add some help aliases
2331
alias get-account-alias='aws iam list-account-aliases'
2432
alias get-account-id='echo AccountId $(aws sts get-caller-identity --query "Account" --output text)'
2533

2634
# Import sub-commandline.
27-
2835
for module in $(echo "common services"); do
2936
for script in $(ls ${AWS_CLI_SOURCE_SCRIPTS}/${module}); do
3037
source ${AWS_CLI_SOURCE_SCRIPTS}/${module}/$script
3138
done
3239
done
40+
41+
# Add hot-keys
42+
zle -N aws_help
43+
zle -N aws_main_function
44+
bindkey '^@' aws_main_function
45+
bindkey '^e' aws_help

services/alb.sh

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,32 @@ aws_alb_list() {
77
aws_alb_info() {
88

99
for alb_arn in $(aws elbv2 describe-load-balancers --query "*[].LoadBalancerArn" --output text); do
10-
aws_run_commandline "aws elbv2 describe-listeners --load-balancer-arn $alb_arn"
10+
aws_alb_get_listeners $alb_arn
11+
12+
for listener_arn in $(peco_aws_alb_list_listners ${alb_arn}); do
13+
aws_alb_get_rules $listener_arn
14+
done
1115

1216
done
1317

1418
}
19+
20+
aws_alb_get_listeners() {
21+
aws_alb_arn=$1
22+
aws_run_commandline \ "
23+
aws elbv2 describe-listeners \
24+
--load-balancer-arn ${aws_alb_arn:?'aws_alb_arn is unset or empty'}
25+
"
26+
}
27+
28+
aws_alb_get_listner() {
29+
30+
}
31+
32+
aws_alb_get_rules() {
33+
aws_alb_listner_arn=$1
34+
aws_run_commandline \ "
35+
aws elbv2 describe-rules \
36+
--listener-arn ${aws_alb_listner_arn:?'aws_alb_listner_arn is unset or empty'}
37+
"
38+
}

0 commit comments

Comments
 (0)