scale-catalog/functions/cmd_to_container.sh

99 lines
2.8 KiB
Bash
Raw Normal View History

2022-08-09 04:17:15 +00:00
#!/bin/bash
cmd_to_container(){
2022-08-09 04:28:09 +00:00
app_name=$(k3s kubectl get pods -A | awk '{print $1}' | sort -u | grep -v "system" | sed '1d' | sed 's/^[^-]*-//' | nl -s ") " | column -t)
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
app_name=$(echo -e "$app_name" | grep ^"$selection)" | awk '{print $2}')
search=$(k3s crictl ps -a -s running)
mapfile -t pod_id < <(echo "$search" | grep -E " $app_name([[:space:]]|-([-[:alnum:]])*[[:space:]])" | awk '{print $9}')
[[ "${#pod_id[@]}" == 0 ]] && echo -e "No containers available\nAre you sure the application in running?" && exit
containers=$(
for pod in "${pod_id[@]}"
do
echo "$search" | grep "$pod" | awk '{print $7}'
done | nl -s ") " | column -t)
while true
do
clear -x
title
echo "$containers"
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 "$containers" | grep -qs ^"$selection)" ; then
echo "Error: \"$selection\" was not an option.. Try again"
sleep 3
continue
else
break
fi
done
container=$(echo "$containers" | grep ^"$selection)" | awk '{print $2}')
container_id=$(echo "$search" | grep -E "[[:space:]]${container}[[:space:]]" | awk '{print $1}')
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
read -rt 120 -p "What command do you want to run?: " command || { echo -e "\nFailed to make a selection in time" ; exit; }
k3s crictl exec -it "$container_id" $command
break
;;
2)
clear -x
title
2022-08-10 03:53:47 +00:00
if ! k3s crictl exec -it "$container_id" /bin/bash && $? != 130 ; then
2022-08-10 03:50:22 +00:00
k3s crictl exec -it "$container_id" /bin/sh
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