Skip to content

Commit 1d65eb1

Browse files
Merge pull request #32 from lamhaison/develop
Update some new features
2 parents 2493a5a + 174a46b commit 1d65eb1

File tree

6 files changed

+321
-5
lines changed

6 files changed

+321
-5
lines changed

common/peco.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ peco_aws_ec2_list() {
207207
}
208208

209209
peco_aws_ec2_list_all() {
210-
commandline="aws ec2 describe-instances --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId.PrivateIpAddress:PrivateIpAddress}' --output text | tr -s '\t' '_'"
210+
commandline="aws ec2 describe-instances \
211+
--query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \
212+
--output text | tr -s '\t' '_'"
211213
peco_commandline_input ${commandline} 'true'
212214
}
213215

@@ -255,3 +257,7 @@ peco_aws_secretmanager_list() {
255257
peco_aws_input 'aws secretsmanager list-secrets --query "*[].Name"' 'true'
256258

257259
}
260+
261+
peco_aws_sns_list() {
262+
peco_aws_input 'aws sns list-topics --query "*[].TopicArn"' 'true'
263+
}

services/aws_secretmanager.sh

Lines changed: 212 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,229 @@ function aws_secretmanager_list() {
1414
"
1515
}
1616

17-
function aws_secretmanager_get_value() {
17+
function aws_secretmanager_get() {
18+
local secret_name=$1
19+
20+
# Check input invalid
21+
if [[ -z "$secret_name" ]]; then return; fi
22+
aws_run_commandline "\
23+
aws secretsmanager describe-secret --secret-id '${secret_name}'
24+
"
25+
}
26+
27+
function aws_secretmanager_get_with_hint() {
28+
local secret_name=$(peco_create_menu 'peco_aws_secretmanager_list' '--prompt "Choose secret that you want >"')
29+
30+
# Check input invalid
31+
if [[ -z "$secret_name" ]]; then return; fi
32+
aws_secretmanager_get "${secret_name}"
33+
}
1834

35+
function aws_secretmanager_get_value() {
1936
local secret_name=$1
37+
local nolog=${2:-'no'}
2038
# Check input invalid
2139
if [[ -z "$secret_name" ]]; then return; fi
2240

23-
aws_run_commandline "\
41+
if [[ "$nolog" = "yes" ]]; then
42+
aws secretsmanager get-secret-value \
43+
--secret-id "${secret_name}"
44+
else
45+
46+
aws_run_commandline "\
2447
aws secretsmanager get-secret-value \
2548
--secret-id "${secret_name}"
2649
"
50+
51+
fi
52+
2753
}
2854

2955
function aws_secretmanager_get_value_with_hint() {
3056
local secret_name=$(peco_create_menu 'peco_aws_secretmanager_list' '--prompt "Choose secret that you want >"')
3157
aws_secretmanager_get_value "${secret_name}"
3258
}
59+
60+
function aws_secretmanager_get_value_with_specific_key_with_hint() {
61+
local secret_name=$(peco_create_menu 'peco_aws_secretmanager_list' '--prompt "Choose secret that you want >"')
62+
aws_secretmanager_get_value_with_specific_key "${secret_name}" ""
63+
64+
}
65+
66+
function aws_secretmanager_list_keys() {
67+
local secret_name="$1"
68+
69+
# Validate input
70+
if [[ -z "$secret_name" ]]; then
71+
echo "Usage: aws_secretmanager_list_keys <secret-name>"
72+
return 1
73+
fi
74+
75+
# Fetch the secret value
76+
local secret_json
77+
secret_json=$(aws secretsmanager get-secret-value --secret-id "$secret_name" --query SecretString --output text 2>/dev/null)
78+
79+
# Check if secret retrieval was successful
80+
if [[ -z "$secret_json" ]]; then
81+
echo "Failed to retrieve secret or secret is empty."
82+
return 1
83+
fi
84+
85+
# List all keys using jq
86+
echo "$secret_json" | jq -r 'keys[]'
87+
}
88+
89+
function aws_secretmanager_list_keys_with_hint() {
90+
local secret_name=$(peco_create_menu 'peco_aws_secretmanager_list' '--prompt "Choose secret that you want >"')
91+
92+
echo "Keys in secret '$secret_name':"
93+
aws_secretmanager_list_keys "${secret_name}"
94+
}
95+
96+
function aws_secretmanager_get_value_with_specific_key() {
97+
98+
local secret_name=$1
99+
# No log to file
100+
secret_string=$(aws_secretmanager_get_value "${secret_name}" "yes" | jq '.SecretString')
101+
secret_string_json=$(python3 -c "import sys,json; print(json.loads(sys.argv[1]))" "${secret_string}" | jq)
102+
local secret_keys=$(echo "${secret_string_json}" | jq -r 'keys_unsorted[]')
103+
104+
local secret_key=$(peco_create_menu 'echo ${secret_keys}' '--prompt "Choose secret key that you want get value>"')
105+
106+
# Check input invalid
107+
if [[ -z "$secret_key" ]]; then
108+
echo "The secret key is invalid"
109+
return
110+
fi
111+
112+
local secret_value=$(echo "${secret_string_json}" | jq -r ".${secret_key}")
113+
echo "${secret_key}=${secret_value}"
114+
115+
}
116+
117+
function aws_secretmanager_update_secret() {
118+
local secret_name="$1"
119+
local secret_key="$2"
120+
local secret_value="$3"
121+
local existing_secret_json
122+
local updated_secret_json
123+
124+
# Check input invalid
125+
if [[ -z "$secret_name" ]]; then return; fi
126+
if [[ -z "$secret_key" ]]; then return; fi
127+
if [[ -z "$secret_value" ]]; then return; fi
128+
129+
# Fetch the existing secret value
130+
existing_secret_json=$(aws secretsmanager get-secret-value --secret-id "$secret_name" --query SecretString --output text 2>/dev/null || echo "")
131+
132+
# Check if the secret exists
133+
if [[ "$existing_secret_json" == "" ]]; then
134+
echo "Secret '$secret_name' not found or has no existing JSON data. Creating a new secret."
135+
136+
# shellcheck disable=SC2155
137+
local updated_secret_json=$(
138+
cat <<-__EOF__
139+
{
140+
"${secret_name}": "${secret_value}"
141+
}
142+
__EOF__
143+
)
144+
145+
echo "$lhs_docs"
146+
else
147+
# Update the JSON secret by modifying the key-value pair
148+
echo "\nAppend to the exsting json"
149+
updated_secret_json=$(echo "$existing_secret_json" | jq --arg key "$secret_key" --arg value "$secret_value" '.[$key] = $value')
150+
fi
151+
152+
# Update the secret in AWS Secrets Manager
153+
aws secretsmanager put-secret-value --secret-id "$secret_name" --secret-string "$updated_secret_json"
154+
155+
echo "Secret '$secret_name' updated with key ${secret_key} successfully."
156+
}
157+
158+
function aws_secretmanager_update_specific_secret_key_with_hint() {
159+
160+
local secret_name=$(peco_create_menu 'peco_aws_secretmanager_list' '--prompt "Choose secret that you want >"')
161+
162+
# Check input invalid
163+
if [[ -z "$secret_name" ]]; then
164+
echo "Secret name is invalid. "
165+
return
166+
fi
167+
168+
echo "List existing keys"
169+
aws_secretmanager_list_keys "${secret_name}"
170+
171+
while true; do
172+
echo -n "Enter name of secret key (or press Enter to finish): "
173+
read secret_key
174+
175+
# Break loop if user presses Enter without input
176+
if [[ -z "$secret_key" ]]; then
177+
break
178+
fi
179+
180+
echo -n "Enter value for '${secret_key}': "
181+
read -s secret_value
182+
echo
183+
184+
# Validate secret value
185+
if [[ -z "$secret_value" ]]; then
186+
echo "Secret value cannot be empty."
187+
continue
188+
fi
189+
190+
aws_secretmanager_update_secret "${secret_name}" "${secret_key}" "${secret_value}"
191+
192+
done
193+
194+
}
195+
196+
function aws_secretmanager_delete_key() { # Be careful when using this
197+
local secret_name="$1"
198+
local key_to_delete="$2"
199+
200+
# Validate inputs
201+
if [[ -z "$secret_name" || -z "$key_to_delete" ]]; then
202+
echo "Usage: aws_secretmanager_delete_key <secret-name> <key-to-delete>"
203+
return 1
204+
fi
205+
206+
# Fetch the existing secret value
207+
local existing_secret_json
208+
existing_secret_json=$(aws secretsmanager get-secret-value --secret-id "$secret_name" --query SecretString --output text 2>/dev/null)
209+
210+
# Validate secret retrieval
211+
if [[ -z "$existing_secret_json" ]]; then
212+
echo "Secret '$secret_name' not found or has no existing data."
213+
return 1
214+
fi
215+
216+
# Check if key exists
217+
if ! echo "$existing_secret_json" | jq -e --arg key "$key_to_delete" 'has($key)' >/dev/null; then
218+
echo "Key '$key_to_delete' not found in secret '$secret_name'."
219+
return 1
220+
fi
221+
222+
# Remove the key from the JSON
223+
local updated_secret_json
224+
updated_secret_json=$(echo "$existing_secret_json" | jq "del(.\"$key_to_delete\")")
225+
226+
# Update the secret in AWS Secrets Manager
227+
aws secretsmanager put-secret-value --secret-id "$secret_name" --secret-string "$updated_secret_json"
228+
229+
echo "Key '$key_to_delete' has been removed from secret '$secret_name'."
230+
}
231+
232+
function aws_secretmanager_delete_key_with_hint() { # # Be careful when using this
233+
local secret_name=$(peco_create_menu 'peco_aws_secretmanager_list' '--prompt "Choose secret that you want >"')
234+
235+
# Check input invalid
236+
if [[ -z "$secret_name" ]]; then return; fi
237+
238+
local secret_key=$(peco_create_menu 'aws_secretmanager_list_keys ${secret_name}' '--prompt "Choose secret key that you want >"')
239+
240+
aws_secretmanager_delete_key "${secret_name}" ${secret_key}
241+
242+
}

services/ec2/ec2.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ aws_ec2_list() {
4444
--filters Name=instance-state-name,Values=running \
4545
--query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0], \
4646
InstanceId:InstanceId,ImageId:ImageId,InstanceType:InstanceType,PrivateIp:PrivateIpAddress,\
47-
PublicIp:PublicIpAddress,State:State.Name,LaunchTime:LaunchTime,ZInstanceLifecycle:InstanceLifecycle}' \
47+
PublicIp:PublicIpAddress,State:State.Name,LaunchTime:LaunchTime,ZInstanceLifecycle:InstanceLifecycle,AvailabilityZone:Placement.AvailabilityZone}' \
4848
--output table
4949
"
5050
}
@@ -150,7 +150,7 @@ aws_ec2_create_image() {
150150
}
151151

152152
aws_ec2_create_image_with_hint() {
153-
aws_ec2_create_image $(local_aws_ec2_instance_id_peco_menu)
153+
aws_ec2_create_image $(local_aws_ec2_instance_id_peco_menu 'all')
154154
}
155155

156156
aws_ec2_get_image() {

services/ecs.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,21 @@ aws_ecs_set_service_desized_count() {
8282
8383
"
8484
}
85+
86+
aws_ecs_execute_container() {
87+
echo "List clusters"
88+
aws_ecs_cluster_arn=$(echo "$(peco_aws_ecs_list_clusters)" | peco)
89+
echo Cluster Arn ${aws_ecs_cluster_arn:?"aws_ecs_cluster_arn is not set or empty"}
90+
echo "List services"
91+
aws_ecs_service_arn=$(echo "$(peco_aws_ecs_list_services)" | peco)
92+
echo Service Arn ${aws_ecs_service_arn:?"aws_ecs_service_arn is not set or empty"}
93+
aws_ecs_task_definition_arn=$(aws ecs describe-services --cluster $aws_ecs_cluster_arn --service $aws_ecs_service_arn --query 'services[0].taskDefinition' --output text)
94+
echo "Task Definition Arn $aws_ecs_task_definition_arn"
95+
echo "List tasks"
96+
aws_ecs_task_arn=$(aws ecs list-tasks --cluster $aws_ecs_cluster_arn --service-name $aws_ecs_service_arn --query 'taskArns' --output text| sed 's/\t/\n/g' | peco)
97+
echo Task Arn ${aws_ecs_task_arn:?"aws_ecs_task_arn is not set or empty"}
98+
echo "List containers in task definition"
99+
container_name=$(aws ecs describe-task-definition --task-definition $aws_ecs_task_definition_arn --query 'taskDefinition.containerDefinitions[*].name' --output text | sed 's/\t/\n/g' | peco)
100+
echo Container name ${container_name:?"container_name is not set or empty"}
101+
aws ecs execute-command --cluster $aws_ecs_cluster_arn --container $container_name --interactive --command "/bin/sh" --task $aws_ecs_task_arn
102+
}

services/sns.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
###################################################################
4+
# # @script aws_sns.sh
5+
# # @author lamhaison
6+
7+
###################################################################
8+
9+
function aws_sns_list() {
10+
aws_run_commandline "\
11+
aws sns list-topics
12+
"
13+
}
14+
15+
function aws_sns_get() {
16+
17+
local aws_sns_arn=$1
18+
19+
# Check input invalid
20+
if [[ -z "$aws_sns_arn" ]]; then return; fi
21+
aws_run_commandline "\
22+
aws sns get-topic-attributes --topic-arn ${aws_sns_arn}
23+
"
24+
}
25+
26+
function aws_sns_get_with_hint() {
27+
28+
local aws_sns_arn=$(peco_create_menu 'peco_aws_sns_list' '--prompt "Choose sns arn >"')
29+
aws_sns_get $aws_sns_arn
30+
}
31+
32+
function aws_sns_list_subscriptions() {
33+
local aws_sns_arn=$1
34+
35+
# Check input invalid
36+
if [[ -z "$aws_sns_arn" ]]; then return; fi
37+
aws_run_commandline "\
38+
aws sns list-subscriptions-by-topic --topic-arn ${aws_sns_arn}
39+
"
40+
}
41+
42+
function aws_sns_list_subscriptions_with_hint() {
43+
local aws_sns_arn=$(peco_create_menu 'peco_aws_sns_list' '--prompt "Choose sns arn >"')
44+
aws_sns_list_subscriptions "${aws_sns_arn}"
45+
}
46+
47+
function aws_sns_push_message() {
48+
local aws_sns_arn=$1
49+
local message=$2
50+
51+
# Check input invalid
52+
if [[ -z "$aws_sns_arn" ]]; then return; fi
53+
if [[ -z "$message" ]]; then return; fi
54+
55+
aws_run_commandline "aws sns publish --topic-arn "${aws_sns_arn}" --message '${message}'"
56+
57+
}
58+
59+
function aws_sns_push_message_hello_world() { # For testing
60+
aws_sns_push_message "$1" "Hello World!"
61+
}
62+
63+
function aws_sns_push_message_hello_world_with_hint() {
64+
local aws_sns_arn=$(peco_create_menu 'peco_aws_sns_list' '--prompt "Choose sns arn >"')
65+
aws_sns_push_message "${aws_sns_arn}" "Hello World!"
66+
}

services/sqs.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,19 @@ function aws_sqs_get() {
5050
function aws_sqs_get_with_hint() {
5151
aws_sqs_get $(peco_create_menu 'peco_aws_sqs_list')
5252
}
53+
54+
function aws_sqs_purge() { # Be careful when using it, it will delete all the message in queues
55+
56+
local aws_sqs_queue_url=$1
57+
58+
# Check input invalid
59+
if [[ -z "$aws_sqs_queue_url" ]]; then return; fi
60+
aws_run_commandline "\
61+
aws sqs purge-queue --queue-url "${aws_sqs_queue_url}"
62+
"
63+
64+
}
65+
66+
function aws_sqs_purge_with_hint() {
67+
aws_sqs_purge $(peco_create_menu 'peco_aws_sqs_list')
68+
}

0 commit comments

Comments
 (0)