Skip to content

Commit bb76311

Browse files
authored
Merge pull request #7 from lamhaison/develop
Develop
2 parents af2bbf6 + 5a5ec6a commit bb76311

File tree

16 files changed

+223
-75
lines changed

16 files changed

+223
-75
lines changed

common/common.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ aws_assume_role_disable_fast_mode() {
1818
export aws_assume_role_print_account_info=true
1919
}
2020

21+
aws_assume_role_disable_load_current_assume_role_for_new_tab() {
22+
rm -rf ${aws_cli_current_assume_role_name} >/dev/null
23+
}
24+
2125
aws_assume_role_disable_show_detail_commandline() {
2226
export aws_show_commandline=false
2327
}
@@ -59,10 +63,16 @@ aws_run_commandline() {
5963
aws_run_commandline_with_logging "${aws_run_commandline}"
6064
}
6165

66+
function aws_commandline_logging() {
67+
local aws_commandline_logging=$(echo ${1:?'aws_commandline is unset or empty'} | tr -d '\t' | tr -d '\n' | tr -s ' ')
68+
69+
if [ "$aws_show_commandline" = "true" ]; then
70+
echo "Running commandline [ ${aws_commandline_logging} ]"
71+
fi
72+
}
73+
6274
aws_run_commandline_with_logging() {
63-
aws_commandline=$1
64-
aws_commandline_logging=$(echo ${aws_commandline:?'aws_commandline is unset or empty'} | tr -d '\t' | tr -d '\n')
65-
# aws_commandline_logging=$(echo ${aws_commandline})
75+
local aws_commandline=$1
6676
local log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log
6777

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

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

8394
echo "------------------------------STARTED--$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null
84-
echo "Running commandline [ ${aws_commandline_logging} ]" | eval $detail_commandline_tee_command
95+
aws_commandline_logging ${aws_commandline} | eval $detail_commandline_tee_command
8596
aws_commandline_result=$(aws_run_commandline_with_retry "${aws_commandline}" "${ignored_error_when_retry}")
8697
echo $aws_commandline_result | eval $tee_command
8798
echo "------------------------------FINISHED-$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null

main.sh

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
# Get the current directory of the main.sh script.
2-
export AWS_CLI_SOURCE_SCRIPTS="$(dirname -- "$0")"
1+
#!/bin/bash
2+
#
3+
# @version 1.0
4+
# @script main.sh
5+
# @description TODO : to load function for aws-cli-utils
6+
# $1: Where is looking for sh files and source the list
7+
# $2: Do you want to set the bind key?
8+
9+
AWS_CLI_SOURCE_SCRIPTS=$1
10+
11+
if [[ -z "${AWS_CLI_SOURCE_SCRIPTS}" ]]; then
12+
LOCAL_AWS_CLI_SOURCE_SCRIPTS=$(dirname -- "$0")
13+
if [[ "${LOCAL_AWS_CLI_SOURCE_SCRIPTS}" = "." ]]; then
14+
DEFAULT_AWS_CLI_SOURCE_SCRIPTS='/opt/lamhaison-tools/aws-cli-utils'
15+
fi
16+
17+
export AWS_CLI_SOURCE_SCRIPTS="${LOCAL_AWS_CLI_SOURCE_SCRIPTS:=${DEFAULT_AWS_CLI_SOURCE_SCRIPTS}}"
18+
else
19+
export AWS_CLI_SOURCE_SCRIPTS=${AWS_CLI_SOURCE_SCRIPTS}
20+
fi
321

422
export assume_role_password_encrypted="$(cat ~/.password_assume_role_encrypted)"
523
export tmp_credentials="/tmp/aws_temporary_credentials"
24+
625
export aws_cli_results="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results"
726
export aws_cli_logs="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/logs"
827
export aws_cli_images="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/images"
@@ -35,12 +54,25 @@ alias get-account-alias='aws iam list-account-aliases'
3554
alias get-account-id='echo AccountId $(aws sts get-caller-identity --query "Account" --output text)'
3655

3756
# Import sub-commandlines.
38-
for script in $(find ${AWS_CLI_SOURCE_SCRIPTS} -type f -name '*.sh' | grep -v main.sh); do
57+
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
3958
source $script
4059
done
4160

42-
# Add hot-keys
43-
# zle -N aws_help
44-
zle -N aws_main_function
45-
bindkey '^@' aws_main_function
61+
# Reuse session in the new terminal
62+
export aws_cli_current_assume_role_name="/tmp/aws_cli_current_assume_role_SW7DNb48oQB57"
63+
export aws_cli_load_current_assume_role=false
64+
# If the file is not empty
65+
# TODO Later (To check if the credential is expired, don't autoload credential)
66+
if [ "true" = "${aws_cli_load_current_assume_role}" ] && [ -s "${aws_cli_current_assume_role_name}" ]; then
67+
aws_assume_role_load_current_assume_role_for_new_tab
68+
fi
69+
70+
LHS_BIND_KEY=${2:='True'}
71+
72+
if [[ "${LHS_BIND_KEY}" = "True" ]]; then
73+
# Add hot-keys
74+
# zle -N aws_help
75+
zle -N aws_main_function
76+
bindkey '^@' aws_main_function
4677
# bindkey '^e' aws_help
78+
fi

services/alb.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ aws_alb_get_listeners() {
2626
}
2727

2828
aws_alb_get_listner() {
29-
29+
echo "TODO Later"
3030
}
3131

3232
aws_alb_get_rules() {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Setting for ecs
4-
aws_application_autoscaling_ecs() {
4+
aws_application_autoscaling_ecs_list_scheduled_actions() {
55
aws application-autoscaling describe-scheduled-actions \
66
--service-namespace ecs
77
}

services/assume_role.sh

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,37 @@ aws_assume_role_get_credentail() {
9191

9292
}
9393

94-
aws_assume_role_is_tmp_credential_valid() {
94+
aws_assume_role_unzip_tmp_credential_valid() {
95+
local aws_assume_role=$1
96+
local tmp_credentials_file_zip="${tmp_credentials}/${aws_assume_role}.zip"
97+
local assume_role_duration="$((${aws_assume_role_expired_time} - 5))"
9598

96-
local tmp_credentials_file="${tmp_credentials}/${ASSUME_ROLE}"
97-
local tmp_credentials_file_zip="${tmp_credentials}/${ASSUME_ROLE}.zip"
99+
local expired_tmp_credential=$(find ${tmp_credentials} -name ${aws_assume_role}.zip -mmin +${assume_role_duration})
100+
# the file aws assume role zip file exists and not empty and not expired
101+
if [ -s "${tmp_credentials_file_zip}" ] && [ -z "${expired_tmp_credential}" ]; then
102+
echo "true"
103+
else
104+
echo "false"
105+
fi
106+
107+
}
108+
109+
aws_assume_role_load_current_assume_role_for_new_tab() {
110+
111+
local aws_assume_role=$(cat ${aws_cli_current_assume_role_name})
112+
local tmp_credentials_file_zip="${tmp_credentials}/${aws_assume_role}.zip"
98113
local assume_role_duration="$((${aws_assume_role_expired_time} - 5))"
99114

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

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

109131
aws_call_assume_role() {
110132
# Do later (Validate the variable of ASSUMED_ROLE before calling assume role)
111-
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_SECURITY_TOKEN ASSUMED_ROLE
133+
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_SECURITY_TOKEN ASSUMED_ROLE AWS_ACCOUNT_ID
112134
tmp_credentials_file="${tmp_credentials}/${ASSUME_ROLE}"
113135
tmp_credentials_file_zip="${tmp_credentials}/${ASSUME_ROLE}.zip"
114136

115137
assume_role_duration="$((${aws_assume_role_expired_time} - 5))"
116138
if [ -f ${tmp_credentials_file_zip} ]; then
117-
118-
valid_file=$(find ${tmp_credentials} -name ${ASSUME_ROLE}.zip -mmin +${assume_role_duration})
119-
empty_file=$(find ${tmp_credentials} -name ${ASSUME_ROLE}.zip -empty)
120-
# Don't find any file is older than expired-time
121-
if [ -z "${valid_file}" ] && [ -z "${empty_file}" ]; then
139+
if [ "$(aws_assume_role_unzip_tmp_credential_valid ${ASSUME_ROLE})" = "true" ]; then
122140
echo "Re-use the temporary credential of ${ASSUME_ROLE} at ${tmp_credentials_file_zip}"
123141
else
124142
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"
@@ -132,6 +150,11 @@ aws_call_assume_role() {
132150
}
133151

134152
aws_assume_role_set_name() {
153+
154+
function aws_assume_role_save_current_assume_role() {
155+
echo "${ASSUME_ROLE}" >${1:?'aws_cli_current_assume_role_name is unset or empty'}
156+
}
157+
135158
aws_assume_role_name=$1
136159
echo You set the assume role name ${aws_assume_role_name:?"The assume role name is unset or empty"}
137160

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

154177
echo -ne "\e]1;AWS-PROFILE[ ${ASSUME_ROLE} ]\a"
155178
echo "You are using the assume role name ${ASSUME_ROLE}"
179+
180+
aws_assume_role_save_current_assume_role ${aws_cli_current_assume_role_name}
156181
}
157182

158183
aws_assume_role_set_name_with_hint() {
159-
# set -x
160-
aws_assume_role_set_name_with_hint_peco
161-
# set +x
162-
}
163184

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

169-
if [[ -n "${ASSUME_ROLE}" ]]; then
170-
assume_role_list=$(echo ${assume_role_list} | grep -v ${ASSUME_ROLE})
171-
assume_role_list=$(echo "${ASSUME_ROLE}\n${assume_role_list}")
189+
}
172190

173-
fi
191+
function aws_assume_role_insert_current_asssume_role_first() {
192+
assume_role_list=$1
193+
if [[ -n "${ASSUME_ROLE}" ]]; then
194+
assume_role_list=$(echo ${assume_role_list} | grep -v ${ASSUME_ROLE})
195+
assume_role_list=$(echo "${ASSUME_ROLE}\n${assume_role_list}")
196+
197+
fi
198+
199+
echo ${assume_role_list}
200+
}
174201

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

179206
}
180207

181-
aws_account_info() {
182-
get-account-alias
183-
208+
aws_assume_role_get_aws_account_id() {
184209
local aws_account_id=$(aws_run_commandline_with_retry 'aws sts get-caller-identity --query "Account" --output text' "true")
185210
export AWS_ACCOUNT_ID=$aws_account_id
186-
echo "AccountId ${AWS_ACCOUNT_ID}"
187211

212+
}
213+
214+
aws_account_info() {
215+
get-account-alias
216+
aws_assume_role_get_aws_account_id
217+
echo "AccountId ${AWS_ACCOUNT_ID}"
188218
echo AWS Region ${AWS_REGION:?"The AWS_REGION is unset or empty"}
189219
}
190220

services/codebuild.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ aws_codebuild_get_latest_build_with_hint() {
2020

2121
aws_codebuild_start() {
2222
aws_codebuild_project_name=$1
23-
echo Start the project name ${aws_codebuild_project_name:?"Project name is unset or empty"}
24-
aws codebuild start-build --project-name ${aws_codebuild_project_name}
23+
24+
aws_run_commandline "\
25+
aws codebuild start-build --project-name ${aws_codebuild_project_name}
26+
"
27+
2528
}
2629

2730
aws_codebuild_start_with_hint() {

services/ec2.sh

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,67 @@ aws_ec2_list() {
2525
}
2626

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

3334
aws_ec2_reboot() {
34-
instance_id=$1
35-
echo Reboot the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
36-
aws_run_commandline "aws ec2 reboot-instances --instance-ids $instance_id"
35+
aws_run_commandline "\
36+
aws ec2 reboot-instances \
37+
--instance-ids ${1:?"The aws_ec2_instance_id is unset or empty"}
38+
"
3739
}
3840

3941
aws_ec2_stop() {
40-
instance_id=$1
41-
echo Stop the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
42-
aws_run_commandline "aws ec2 stop-instances --instance-ids $instance_id"
42+
aws_run_commandline "\
43+
aws ec2 stop-instances \
44+
--instance-ids ${1:?"The aws_ec2_instance_id is unset or empty"}
45+
"
4346
}
4447

4548
aws_ec2_start() {
46-
instance_id=$1
47-
echo start the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
48-
aws_run_commandline "aws ec2 start-instances --instance-ids $instance_id"
49+
aws_run_commandline "\
50+
aws ec2 start-instances \
51+
--instance-ids ${1:?"The aws_ec2_instance_id is unset or empty"}
52+
"
53+
}
54+
55+
aws_ec2_rm_instruction() {
56+
aws_commandline_logging "\
57+
aws ec2 terminate-instances \
58+
--instance-ids ${1:="\$aws_ec2_instance_ids"}
59+
"
4960
}
5061

5162
# Ec2 image
5263
aws_ec2_list_images() {
5364
aws_run_commandline "aws ec2 describe-images --owners self"
5465
}
66+
67+
aws_ec2_list_aws_default_images() {
68+
aws_run_commandline " \
69+
aws ec2 describe-images \
70+
--filters 'Name=architecture,Values=x86_64' \
71+
'Name=virtualization-type,Values=hvm' 'Name=root-device-type,Values=ebs' \
72+
'Name=block-device-mapping.volume-type,Values=gp2' \
73+
'Name=ena-support,Values=true' 'Name=owner-alias,Values=amazon' \
74+
'Name=name,Values=*amzn2-ami-hvm-2.0.????????.?-x86_64-gp2' \
75+
--query 'Images[*].[ImageId,Name]' --output text
76+
"
77+
}
5578
aws_ec2_create_image() {
56-
instance_id=$1
57-
echo To create a image from the ec2 instance ${instance_id:?"The instace_id is unset or empty"}
79+
local aws_ec2_instance_id=$1
80+
echo To create a image from the ec2 instance ${aws_ec2_instance_id:?"The instace_id is unset or empty"}
5881
aws_ec2_instance_name=$(aws ec2 describe-instances \
59-
--instance-ids $instance_id \
82+
--instance-ids $aws_ec2_instance_id \
6083
--query 'Reservations[*].Instances[*].{Tags:Tags[?Key == `Name`] | [0].Value}' \
6184
--output text)
6285

6386
aws ec2 create-image \
6487
--no-reboot \
65-
--instance-id $instance_id \
88+
--instance-id $aws_ec2_instance_id \
6689
--name ${aws_ec2_instance_name}-$(date '+%Y-%m-%d-%H-%M-%S') \
6790
--description ${aws_ec2_instance_name}-$(date '+%Y-%m-%d-%H-%M-%S') \
6891
--query "ImageId" --output text

services/ec2_autoscaling.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ aws_autoscaling_set_desired_capacity_with_hint() {
6969

7070
# TODO LATER
7171
aws_autoscaling_detach_instance_with_hint() {
72-
# aws_autoscaling_name=$(peco_create_menu 'peco_aws_autoscaling_list')
73-
72+
echo "TODO Later"
7473
}
7574

7675
aws_autoscaling_get_launching_template() {

services/ecr.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ aws_ecr_list_repo() {
55
}
66

77
aws_ecr_login() {
8+
9+
if [[ -z "${AWS_ACCOUNT_ID}" ]]; then
10+
aws_assume_role_get_aws_account_id
11+
fi
812
aws ecr get-login-password --region ${AWS_REGION} | docker login \
913
--username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
1014
}
1115

1216
aws_ecr_logout() {
17+
18+
if [[ -z "${AWS_ACCOUNT_ID}" ]]; then
19+
aws_assume_role_get_aws_account_id
20+
fi
1321
docker logout ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
1422
}
1523

0 commit comments

Comments
 (0)