2022-08-09 04:17:15 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
|
|
cmd_to_container(){
|
2022-08-18 06:14:12 +00:00
|
|
|
app_name=$(k3s crictl pods -s ready --namespace ix | sed -E 's/[[:space:]]([0-9]*|About)[a-z0-9 ]{5,12}ago[[:space:]]//' | sed '1d' | awk '{print $4}' | cut -c4- | sort -u | nl -s ") " | column -t)
|
2022-08-09 04:28:09 +00:00
|
|
|
while true
|
|
|
|
do
|
|
|
|
clear -x
|
|
|
|
title
|
|
|
|
echo "$app_name"
|
|
|
|
echo
|
|
|
|
echo "0) Exit"
|
|
|
|
read -rt 120 -p "Please type a number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; }
|
|
|
|
if [[ $selection == 0 ]]; then
|
|
|
|
echo "Exiting.."
|
|
|
|
exit
|
|
|
|
elif ! echo -e "$app_name" | grep -qs ^"$selection)" ; then
|
|
|
|
echo "Error: \"$selection\" was not an option.. Try again"
|
|
|
|
sleep 3
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2022-08-18 06:17:17 +00:00
|
|
|
app_name=$(echo -e "$app_name" | grep ^"$selection)" | awk '{print $2}')
|
2022-08-18 20:41:51 +00:00
|
|
|
mapfile -t pod_id < <(k3s crictl pods -s ready --namespace ix | grep -E "[[:space:]]$app_name([[:space:]]|-([-[:alnum:]])*[[:space:]])" | awk '{print $1}')
|
2022-08-12 14:10:15 +00:00
|
|
|
search=$(k3s crictl ps -a -s running | sed -E 's/[[:space:]]([0-9]*|About)[a-z0-9 ]{5,12}ago[[:space:]]//')
|
2022-08-10 22:58:07 +00:00
|
|
|
for pod in "${pod_id[@]}"
|
|
|
|
do
|
2022-08-22 03:31:01 +00:00
|
|
|
# if [[ $(echo "$search" | grep "$pod" | awk '{print $4}' | tr -d " \t\r " | wc -l) -gt 1 ]]; then
|
|
|
|
# readarray -t containers <<<"$(echo "$search" | grep "$pod" | awk '{print $4}' | tr -d " \t\r ")"
|
|
|
|
# continue
|
|
|
|
# fi
|
2022-08-12 16:10:32 +00:00
|
|
|
printf '%s\0' "${containers[@]}" | grep -Fxqz -- "$(echo "$search" | grep "$pod" | awk '{print $4}' | tr -d " \t\r ")" && continue
|
2022-08-22 04:04:42 +00:00
|
|
|
containers+=("$(echo "$search" | grep "$pod" | awk '{print $4}' | tr -d " \t\r ")")
|
2022-08-10 22:58:07 +00:00
|
|
|
done
|
2022-08-22 04:12:13 +00:00
|
|
|
mapfile -t containers < <(echo "${containers[*]}")
|
2022-08-22 04:13:09 +00:00
|
|
|
for i in "${containers[@]}"
|
|
|
|
do
|
|
|
|
echo "$i"
|
|
|
|
done
|
2022-08-10 23:46:01 +00:00
|
|
|
case "${#containers[@]}" in
|
|
|
|
0)
|
|
|
|
echo -e "No containers available\nAre you sure the application in running?"
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
1)
|
2022-08-12 15:49:26 +00:00
|
|
|
container=$(echo "$search" | grep "${pod_id[0]}" | awk '{print $4}')
|
2022-08-10 23:46:01 +00:00
|
|
|
container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
while true
|
2022-08-10 22:58:07 +00:00
|
|
|
do
|
2022-08-10 23:46:01 +00:00
|
|
|
clear -x
|
|
|
|
title
|
|
|
|
cont_search=$(
|
|
|
|
for i in "${containers[@]}"
|
|
|
|
do
|
|
|
|
echo "$i"
|
2022-08-12 15:36:07 +00:00
|
|
|
done | nl -s ") " | column -t
|
2022-08-10 23:46:01 +00:00
|
|
|
)
|
|
|
|
echo "$cont_search"
|
|
|
|
echo
|
|
|
|
echo "0) Exit"
|
|
|
|
read -rt 120 -p "Choose a container by number: " selection || { echo -e "\nFailed to make a selection in time" ; exit; }
|
|
|
|
if [[ $selection == 0 ]]; then
|
|
|
|
echo "Exiting.."
|
|
|
|
exit
|
|
|
|
elif ! echo -e "$cont_search}" | grep -qs ^"$selection)" ; then
|
|
|
|
echo "Error: \"$selection\" was not an option.. Try again"
|
|
|
|
sleep 3
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
container=$(echo "$cont_search" | grep ^"$selection)" | awk '{print $2}')
|
|
|
|
container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
|
|
|
|
;;
|
|
|
|
esac
|
2022-08-10 03:50:22 +00:00
|
|
|
while true
|
|
|
|
do
|
|
|
|
clear -x
|
|
|
|
title
|
|
|
|
echo "App Name: $app_name"
|
|
|
|
echo "Container: $container"
|
|
|
|
echo
|
|
|
|
echo "1) Run a single command"
|
|
|
|
echo "2) Open Shell"
|
|
|
|
echo
|
|
|
|
echo "0) Exit"
|
|
|
|
read -rt 120 -p "Please choose an option: " selection || { echo -e "\nFailed to make a selection in time" ; exit; }
|
|
|
|
case $selection in
|
|
|
|
0)
|
|
|
|
echo "Exiting.."
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
1)
|
|
|
|
clear -x
|
|
|
|
title
|
2022-08-10 23:50:39 +00:00
|
|
|
read -rt 500 -p "What command do you want to run?: " command || { echo -e "\nFailed to make a selection in time" ; exit; }
|
2022-08-10 03:50:22 +00:00
|
|
|
k3s crictl exec -it "$container_id" $command
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
2)
|
|
|
|
clear -x
|
|
|
|
title
|
2022-08-10 04:16:49 +00:00
|
|
|
if ! k3s crictl exec -it "$container_id" /bin/bash 2>/dev/null; then
|
|
|
|
k3s crictl exec -it "$container_id" /bin/sh 2>/dev/null || echo "This container does not accept shell access, try a different one."
|
2022-08-10 03:50:22 +00:00
|
|
|
fi
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "That was not an option.. Try again"
|
|
|
|
sleep 3
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2022-08-09 04:17:15 +00:00
|
|
|
}
|
|
|
|
export -f cmd_to_container
|