Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,20 @@ echo "source /opt/lamhaison-tools/aws-cli-utils/main.sh" >> ~/.zshrc
#### Install
```
brew tap lamhaison/formulae
brew install lamhaison/formulae/aws-cli-utils
brew install lamhaison/formulae/aws-cli-utils # Make sure you flow the the instruction when it is installed completely, we need that for the next step
# Example
#...
#==> Caveats
#Add these lines to ~/.bashrc or ~/.zshrc or ~/.bash_profile
#source "$(which aws-cli-utils.sh)" "/opt/homebrew/Cellar/aws-cli-utils/$(brew info aws-cli-utils | head -1 | awk -F "stable " '{print $2}')/bin" "${HOME}" "True"
#...
```
## Load when start an Iterm terminal
Add these lines to ~/.bashrc or ~/.zshrc or ~/.bash_profile
```
source "$(which lhs-helpful-commandlines.sh)" "/opt/homebrew/Cellar/lhs-helpful-commandlines/$(brew info lhs-helpful-commandlines | grep -E '==> .*: stable (.*)$' | sed -E 's|==> .*: stable (.*)$|\1|g')" "True" "True"
Add these lines to ~/.bashrc or ~/.zshrc or ~/.bash_profile from the previous step `source ....`
It depends on what you get when running brew install. Here is the example

```bash
source "$(which aws-cli-utils.sh)" "/opt/homebrew/Cellar/aws-cli-utils/$(brew info aws-cli-utils | head -1 | awk -F "stable " '{print $2}')/bin" "${HOME}" "True"
```

### Re-install the latest version
Expand Down
60 changes: 40 additions & 20 deletions common/peco.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ function peco_aws_input() {
peco_commandline_input "${1} --output text" $2
}

function peco_commandline_get_cache_file_name() {
local commandline="${1}"
local md5_hash=$(echo "$commandline" | md5)
echo "${md5_hash}"
}

function peco_comandline_input_clear_cache() {
local commandline="${1}"
local md5_hash=$(peco_commandline_get_cache_file_name "${commandline}")
local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:-NOTSET}"
local input_file_path="${input_folder}/${md5_hash}.txt"
if [ -f "${input_file_path}" ]; then
rm -f "${input_file_path}"
fi
}

function peco_commandline_input() {

local commandline="${1}"
Expand All @@ -46,7 +62,7 @@ function peco_commandline_input() {
input_expired_time=0
fi

local md5_hash=$(echo $commandline | md5)
local md5_hash=$(peco_commandline_get_cache_file_name "${commandline}")
local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:-NOTSET}"
mkdir -p ${input_folder}
local input_file_path="${input_folder}/${md5_hash}.txt"
Expand Down Expand Up @@ -198,30 +214,34 @@ function peco_aws_iam_list_attached_policies() {
# EC2 Instance
function peco_aws_ec2_list() {
local instance_state=${1:-'running'}
local refresh=${2:-'false'}
local commandline

commandline="aws ec2 describe-instances \
--filters Name=instance-state-name,Values=${instance_state} \
--query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \
--output text | tr -s '\t' '_'"
peco_commandline_input ${commandline} 'true'
}
# set -x

function peco_aws_ec2_list_all() {
commandline="aws ec2 describe-instances \
--query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \
--output text | tr -s '\t' '_'"
peco_commandline_input ${commandline} 'true'
if [[ "${instance_state}" = "all" ]]; then
commandline="aws ec2 describe-instances \
--query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \
--output text | tr -s '\t' '_'"
else
commandline="aws ec2 describe-instances \
--filters Name=instance-state-name,Values=${instance_state} \
--query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \
--output text | tr -s '\t' '_'"
fi

if [ "${refresh}" = "true" ]; then
peco_comandline_input_clear_cache "${commandline}"
fi

peco_commandline_input "${commandline}" 'true'

# set +x
}

function peco_aws_ssm_list_parameters() {
commandline=" \
aws ssm get-parameters-by-path \
--path "/" \
--recursive \
--query 'Parameters[*].Name' \
| jq -r '.[]'
"
peco_commandline_input ${commandline} 'true'
commandline="aws ssm get-parameters-by-path --path "/" --recursive --query 'Parameters[*].Name' | jq -r '.[]'"
peco_commandline_input "${commandline}" 'true'
}

function peco_aws_dynamodb_list_tables() {
Expand Down
3 changes: 2 additions & 1 deletion services/assume_role.sh
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ aws_assume_role_get_tmp_credentials_for_new_members() {
local tmp_credentials_file="${tmp_credentials}/${ASSUME_ROLE}"
aws_assume_role_set_name_with_hint
aws_assume_role_unzip_tmp_credential $assume_role
cat ${tmp_credentials_file} && rm -rf ${tmp_credentials_file}
cat ${tmp_credentials_file} | grep "export" | grep -v "ASSUMED_ROLE" && rm -rf ${tmp_credentials_file}
echo "export AWS_REGION=$AWS_REGION"

}

Expand Down
22 changes: 8 additions & 14 deletions services/ec2/ec2.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#!/bin/bash

# TODO local_aws_ec2_instance_id_peco_menu create ec2 instance_id list
# @param filter by state (running, stopped, all)
# @return ec2_instance_id that you choose
#
local_aws_ec2_instance_id_peco_menu() {
local instance_state=${1:-'running'}
local refresh_caching=${2:-'false'}
local aws_ec2_instance_id

if [[ "${instance_state}" = "all" ]]; then
local aws_ec2_instance_id=$(peco_create_menu 'peco_aws_ec2_list_all')
else
local aws_ec2_instance_id=$(peco_create_menu "peco_aws_ec2_list ${instance_state}")
fi
aws_ec2_instance_id=$(peco_create_menu "peco_aws_ec2_list '${instance_state}' '${refresh_caching}'")

aws_ec2_instance_id=$(echo "${aws_ec2_instance_id}" | awk -F "_" '{print $1}')
echo ${aws_ec2_instance_id}
echo "${aws_ec2_instance_id}"
}

# AWS ec2
Expand Down Expand Up @@ -57,7 +51,7 @@ aws_ec2_get() {
}

aws_ec2_get_with_hint() {
aws_ec2_get $(local_aws_ec2_instance_id_peco_menu)
aws_ec2_get "$(local_aws_ec2_instance_id_peco_menu 'all' 'true')"
}

aws_ec2_reboot() {
Expand All @@ -75,7 +69,7 @@ aws_ec2_stop() {
}

aws_ec2_stop_with_hint() {
aws_ec2_stop $(local_aws_ec2_instance_id_peco_menu)
aws_ec2_stop $(local_aws_ec2_instance_id_peco_menu 'running' 'true')
}

aws_ec2_start() {
Expand All @@ -85,8 +79,8 @@ aws_ec2_start() {
"
}

aws_ec2_start_with_hint() {
aws_ec2_start $(local_aws_ec2_instance_id_peco_menu 'stopped')
aws_ec2_start_with_hint() {
aws_ec2_start $(local_aws_ec2_instance_id_peco_menu 'stopped' 'true')
}

aws_ec2_rm_instruction() {
Expand Down