utils/bash/sources/17_kcontext

218 lines
4.9 KiB
Plaintext
Raw Permalink Normal View History

2023-09-23 15:11:09 +00:00
#!/bin/bash
declare -f kstore > /dev/null
if [ $? -ne 0 ]; then
echo "kcontext depends on kstore"
return 1
fi
function kcontext {
case $1 in
2023-10-13 12:15:41 +00:00
k8s) shift; _kcontext-k8s "$@" ;;
docker) shift; _kcontext-docker "$@" ;;
s3-arch) shift; _kcontext-s3-arch "$@" ;;
2023-09-23 15:11:09 +00:00
*)
__func_head "k8s ..."
__func_help "s3-arch ..."
2023-09-23 15:11:09 +00:00
__func_help "docker ..."
return 1
;;
esac
return $?
}
2023-10-13 12:15:41 +00:00
function _kcontext {
local CUR=${COMP_WORDS[COMP_CWORD]}
local SCOPE=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=()
case "$SCOPE" in
kcontext)
COMPREPLY=( $(compgen -W "k8s docker s3-arch" -- $CUR) )
;;
k8s|s3-arch)
COMPREPLY=( $(compgen -W "use del list save" -- $CUR) )
return
;;
use|del)
SCOPE=${COMP_WORDS[COMP_CWORD-2]}
;;
esac
case "$SCOPE" in
k8s|s3-arch)
COMPREPLY=( $(compgen -W "$( kcontext $SCOPE list )" -- $CUR) )
;;
esac
}
complete -F _kcontext kcontext
function _kcontext-k8s {
2023-10-08 17:30:56 +00:00
local _NAME _CONF
case $1 in
list)
2024-11-10 22:28:53 +00:00
kstore query -list "SELECT SUBSTR( prop, 5 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 'k8s.%'"
2023-10-08 17:30:56 +00:00
return $?
;;
save)
_NAME=$( kubectl config current-context )
if [ $? -ne 0 ]; then
return 1
fi
kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kubectl config view --minify --flatten | kstore update "kcontext" - "k8s.$_NAME"
return $?
else
kubectl config view --minify --flatten | kstore add "kcontext" - "k8s.$_NAME"
return $?
fi
;;
use)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
_CONF="$RBASH_HOME/k8s.conf"
kstore get "kcontext" "k8s.$_NAME" > "$_CONF"
if [ $? -ne 0 ]; then
echo "No such context: $_NAME" >&2
return 1
fi
chmod 600 "$_CONF"
echo $KUBECONFIG | grep "$_CONF" -q
2023-10-08 19:11:58 +00:00
case $OSTYPE in
cygwin)
_CONF=$( cygpath -w "$_CONF" )
;;
esac
2023-10-08 17:30:56 +00:00
if [ $? -eq 0 ]; then
export KUBECONFIG="$_CONF"
else
export KUBECONFIG="$_CONF:$KUBECONFIG"
fi
;;
del)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
2024-11-10 22:28:53 +00:00
kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 'k8s.$_NAME'"
2023-10-08 17:30:56 +00:00
return $?
else
echo "No such context: $_NAME" >&2
fi
;;
*)
__func_head "list"
__func_help "save"
__func_help "use NAME"
__func_help "del NAME"
;;
esac
return 1
}
2023-10-13 12:15:41 +00:00
function _kcontext-s3-arch {
2023-09-23 15:11:09 +00:00
local _NAME _CONFIRM _URL _AUTH
case $1 in
list)
2024-11-10 22:28:53 +00:00
kstore query -list "SELECT SUBSTR( prop, 16 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'"
2023-10-08 17:30:56 +00:00
return $?
2023-09-23 15:11:09 +00:00
;;
save)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
if [ -n "$ARCH_S3_BUCKET_URL" ] && [ -n "$ARCH_S3_AUTH" ]; then
kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kstore update "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME"
kstore update "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME"
return $?
else
kstore add "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME"
kstore add "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME"
return $?
fi
else
echo "No s3-arch settings can be found in env" >&2
fi
;;
2023-10-08 17:30:56 +00:00
use)
2023-09-23 15:11:09 +00:00
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
_URL=$( kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null )
if [ $? -ne 0 ]; then
echo "No such context: $_NAME" >&2
return 1
fi
_AUTH=$( kstore get "kcontext" "s3-arch.auth.$_NAME" 2> /dev/null )
if [ $? -ne 0 ]; then
echo "No such context: $_NAME" >&2
return 1
fi
export ARCH_S3_BUCKET_URL=$_URL
export ARCH_S3_AUTH=$_AUTH
;;
del)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
2024-11-10 22:28:53 +00:00
kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.bucket.$_NAME'"
kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.auth.$_NAME'"
2023-09-23 15:11:09 +00:00
return $?
else
echo "No such context: $_NAME" >&2
fi
;;
*)
__func_head "list"
__func_help "save NAME"
2023-10-08 17:30:56 +00:00
__func_help "use NAME"
2023-09-23 15:11:09 +00:00
__func_help "del NAME"
;;
esac
return 1
}