2022-11-16 10:57:31 +00:00
|
|
|
test -z "$DISPLAY" && test "$XDG_VTNR" -lt 1 || return 0
|
2022-07-06 07:10:13 +00:00
|
|
|
|
2022-07-06 09:15:29 +00:00
|
|
|
export PATH="$PATH:$HOME/.local/bin/server"
|
|
|
|
export PROJECTS="$HOME/projects"
|
|
|
|
test -d "$MUSIC" || export MUSIC="/srv/funkwhale/data/music/janek"
|
2022-07-06 07:10:13 +00:00
|
|
|
|
|
|
|
## STACKSPIN
|
2022-07-06 09:15:29 +00:00
|
|
|
export STACKSPIN="${STACKSPIN:-$PROJECTS/stackspin}"
|
2022-04-28 16:15:51 +00:00
|
|
|
_stackspin_cluster_cache=/var/tmp/stackspin-cluster
|
|
|
|
|
2022-07-06 07:01:38 +00:00
|
|
|
# Stackspin CLI Wrapper:
|
2022-04-28 16:15:51 +00:00
|
|
|
# Initialize once with "stack select example.org",
|
2022-06-30 09:54:03 +00:00
|
|
|
# then it loads the last selected cluster on startup.
|
2022-04-28 16:15:51 +00:00
|
|
|
# Presumes a mapping like the following in your ssh config:
|
|
|
|
# Host example.org
|
|
|
|
# Hostname [IP]
|
2022-07-06 07:10:13 +00:00
|
|
|
# This is a function so it can change directory.
|
2022-04-28 16:15:51 +00:00
|
|
|
stack() {
|
2022-09-01 13:10:42 +00:00
|
|
|
local pod_suffix='-\(0\|[0-f]\+\)'
|
2022-11-16 10:57:31 +00:00
|
|
|
if test $# -lt 1; then
|
|
|
|
builtin cd "$STACKSPIN"
|
|
|
|
echo "Usage: $0 <COMMAND> [args...]"
|
|
|
|
echo "Stackspin commands: select, sso, user, push"
|
|
|
|
echo "Kubepod commands: pod, exec, app, shell, ls, logs, upload"
|
|
|
|
echo "App commands: occ, vikunja"
|
2022-06-30 09:54:03 +00:00
|
|
|
return 1
|
2022-11-16 10:57:31 +00:00
|
|
|
fi
|
2022-06-30 09:54:03 +00:00
|
|
|
local command="$1"
|
|
|
|
shift
|
|
|
|
case "$command" in
|
2022-05-03 16:50:05 +00:00
|
|
|
# stackspin administration
|
2022-06-30 09:54:03 +00:00
|
|
|
(select)
|
2022-04-28 16:15:51 +00:00
|
|
|
export _cluster_name="$1"
|
|
|
|
export _cluster_ip="$(ssh -G "$_cluster_name" | grep --max-count 1 "^hostname " | cut -d " " -f2-)"
|
|
|
|
export CLUSTER_DIR="$STACKSPIN/clusters/$_cluster_name"
|
|
|
|
export KUBECONFIG="$CLUSTER_DIR/kube_config_cluster.yml"
|
2022-05-10 13:25:00 +00:00
|
|
|
# Uncomment the line below to always use the main stackspin repo, even when running in a fork.
|
|
|
|
#export GITLAB_CI="true"
|
2022-04-28 16:15:51 +00:00
|
|
|
echo Selected "$_cluster_name" with IP "$_cluster_ip"
|
|
|
|
echo "$_cluster_name" >"$_stackspin_cluster_cache"
|
2022-11-16 10:57:31 +00:00
|
|
|
#test "$PWD" = "$HOME" && builtin cd "$STACKSPIN"
|
2022-04-28 16:15:51 +00:00
|
|
|
;;
|
2022-06-30 09:54:03 +00:00
|
|
|
(sso) builtin cd "$STACKSPIN"
|
2022-06-30 10:20:19 +00:00
|
|
|
"$0" exec dashboard --container backend -- flask "$@";;
|
2022-07-18 10:57:43 +00:00
|
|
|
(users)
|
|
|
|
if test $# -gt 0
|
|
|
|
then for arg
|
|
|
|
do "$0" user show $arg
|
|
|
|
done
|
|
|
|
else "$0" users $("$0" user list | sed 's|.*<\(.*\)>.*|\1|')
|
|
|
|
fi;;
|
2022-06-30 10:20:19 +00:00
|
|
|
(user|app)
|
2022-06-30 09:54:03 +00:00
|
|
|
if test "$1" = "init"
|
|
|
|
then mail="$2"
|
|
|
|
shift 2
|
2022-06-30 10:20:19 +00:00
|
|
|
"$0" user create "$mail"
|
|
|
|
"$0" user update "$mail" name "$*"
|
2022-04-28 16:15:51 +00:00
|
|
|
echo "Initialized user '$*' with email '$mail'"
|
2022-06-30 10:20:19 +00:00
|
|
|
else "$0" sso cli "$command" "$@"
|
2022-04-28 16:15:51 +00:00
|
|
|
fi;;
|
2022-09-01 13:10:42 +00:00
|
|
|
(push)
|
|
|
|
git commit -a "$@"
|
|
|
|
git push &&
|
|
|
|
flux reconcile source git -n flux-system "$(basename $(git rev-parse --show-toplevel))"
|
|
|
|
flux reconcile kustomization -n flux-system "$(basename $(git rev-parse --show-toplevel))";;
|
|
|
|
# KUBE
|
|
|
|
# app clis
|
2022-07-20 20:05:43 +00:00
|
|
|
(occ) "$0" exec nc-nextcloud -c nextcloud -it -- su www-data -s /bin/bash -c "php $command $*";;
|
2022-09-01 13:10:42 +00:00
|
|
|
(vikunja) local pod=${2:-vikunja}
|
|
|
|
case "$1" in
|
2022-11-16 10:57:31 +00:00
|
|
|
(dump|export) cd "$PROJECTS/vikunja"
|
2022-09-01 13:10:42 +00:00
|
|
|
"$0" exec "$pod" -c api -- sh -c 'rm -f *.zip && ./vikunja dump >/dev/null && ls --color -lAhF >&2 && cat *.zip' >"$pod-dump_$(date +%F).zip"
|
|
|
|
;;
|
|
|
|
(restore) "$0" upload "$pod" "$3" -c api
|
|
|
|
"$0" exec "$pod" -c api -it -- ./vikunja restore "$3"
|
|
|
|
;;
|
2022-11-16 10:57:31 +00:00
|
|
|
(psql) kubectl exec -it -n $("$0" pod "$pod-postgresql") -- sh -c "PGPASSWORD=$(kubectl get secret --namespace stackspout $pod-postgresql -o jsonpath='{.data.postgresql-password}' | base64 --decode) psql -h localhost -U vikunja -p 5432 vikunja";;
|
|
|
|
(*) echo "Unknown Subcommand";;
|
2022-09-01 13:10:42 +00:00
|
|
|
esac
|
|
|
|
;;
|
2022-11-21 11:36:20 +00:00
|
|
|
(maria) app=$1
|
2023-01-31 22:52:42 +00:00
|
|
|
pw="$(kubectl get secret -n flux-system stackspin-$app-variables --template '{{.data.mariadb_password}}' | base64 -d 2>/dev/null ||
|
|
|
|
kubectl get secret -n flux-system stackspin-$app-variables --template "{{.data.${app}_mariadb_password}}" | base64 -d)"
|
|
|
|
case $app in
|
|
|
|
(nextcloud) n=nc-mariadb;;
|
|
|
|
(wordpress) n=wordpress-database;;
|
|
|
|
(*) n=$app-mariadb;;
|
|
|
|
esac
|
|
|
|
"$0" exec $n -it -- env "MYSQL_PWD=$pw" mysql -u $app "$@";;
|
2022-09-01 13:10:42 +00:00
|
|
|
# high-level
|
2022-11-16 10:57:31 +00:00
|
|
|
(shell)
|
|
|
|
container=$1
|
|
|
|
shift
|
|
|
|
test "$1" = "-c" && pod=$2 && shift 2
|
|
|
|
"$0" exec "$container" -c "$pod" -it -- /bin/sh "$@";;
|
2022-09-01 13:10:42 +00:00
|
|
|
(ls)
|
|
|
|
if test $# -gt 1 && ! [[ "$2" =~ ".*/.*" ]]
|
|
|
|
then "$0" exec "$1" "$2" "$3" -it -- ls -lAhF --group-directories-first "${@:4}"
|
|
|
|
else for container in $("$0" kube get "$1" pod -o "jsonpath={.spec.containers[*].name}")
|
2022-11-16 10:57:31 +00:00
|
|
|
do highlight "Listing content of $container" &&
|
2022-09-01 13:10:42 +00:00
|
|
|
"$0" ls "$1" -c "$container" "${@:2}"
|
|
|
|
done
|
|
|
|
fi;;
|
|
|
|
(upload)
|
|
|
|
kubectl cp "$2" -n $("$0" pod "$1$pod_suffix"):$2 "${@:3}"
|
|
|
|
"$0" ls "$1" "${@:3}";;
|
2022-07-18 10:57:43 +00:00
|
|
|
(exec) "$0" kube exec "$@";;
|
2022-11-21 11:36:20 +00:00
|
|
|
(logs) podname=$1
|
|
|
|
shift
|
2023-01-31 22:52:42 +00:00
|
|
|
"$0" kube logs "$podname" | $(command which ${LOGPAGER:-lnav} || { which bat >/dev/null && echo "bat --number -l toml" } || echo 'less -RF') "$@";;
|
2022-09-01 13:10:42 +00:00
|
|
|
# low-level
|
2022-07-18 10:57:43 +00:00
|
|
|
(kube)
|
2022-11-16 10:57:31 +00:00
|
|
|
test $# -gt 1 || { echo "Please provide a command and pod name" >&2 && return 1; }
|
|
|
|
local pods=$("$0" pod "$2$pod_suffix") || { echo "No pod found for $2" >&2 && return 1; }
|
2022-07-18 10:57:43 +00:00
|
|
|
local subcommand=$1
|
|
|
|
shift 2
|
2022-09-01 13:10:42 +00:00
|
|
|
local commands=()
|
2022-11-16 10:57:31 +00:00
|
|
|
for arg
|
2022-09-01 13:10:42 +00:00
|
|
|
do case "$arg" in (-*) break;; (*) commands+="$arg"; shift;; esac
|
|
|
|
done
|
2023-01-31 22:52:42 +00:00
|
|
|
local IFS=$'\n'
|
2022-11-16 10:57:31 +00:00
|
|
|
for namespacedpod in $pods; do
|
|
|
|
test "$subcommand" = get ||
|
|
|
|
highlight "Running $subcommand on $namespacedpod" >&2
|
2023-01-31 22:52:42 +00:00
|
|
|
local IFS=' '
|
|
|
|
kubectl "$subcommand" "$commands[@]" -n $namespacedpod "$@"
|
2022-11-16 10:57:31 +00:00
|
|
|
done;;
|
2022-06-30 09:54:03 +00:00
|
|
|
(pod)
|
2022-11-16 10:57:31 +00:00
|
|
|
test $# -gt 0 && local podname=$1 && shift
|
|
|
|
kubectl get pods --all-namespaces --field-selector="status.phase=Running" -o=custom-columns=S:.metadata.namespace,N:.metadata.name --no-headers "$@" | grep --color=never -- "$podname";;
|
2022-05-03 16:50:05 +00:00
|
|
|
# stackspin bare
|
2022-06-30 09:54:03 +00:00
|
|
|
(*) if which "$0-$command" >/dev/null 2>&1
|
|
|
|
then "$0-$command" "$@"
|
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
builtin cd "$STACKSPIN"
|
|
|
|
# Since the install command can also be given bare to install stackspin itself
|
2022-11-16 10:57:31 +00:00
|
|
|
if test "$command" = "install"; then
|
|
|
|
if test $# -gt 0
|
|
|
|
then "./install/install-$1.sh" || ./install/install-app.sh "$@"
|
|
|
|
else
|
|
|
|
/usr/bin/python3 -m pip install --upgrade pip
|
|
|
|
/usr/bin/python3 -m pip install -r requirements.txt
|
|
|
|
python3 -m stackspin "$_cluster_name" "$command"
|
|
|
|
fi
|
2022-06-30 09:54:03 +00:00
|
|
|
else python3 -m stackspin "$_cluster_name" "$command" "$@"
|
2022-04-28 16:15:51 +00:00
|
|
|
fi;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
cat "$_stackspin_cluster_cache" 2>/dev/null |
|
|
|
|
while read cluster; do stack select "$cluster"; done
|
2022-07-06 09:15:29 +00:00
|
|
|
|
|
|
|
test "$PWD" = "$HOME" && cde "$PROJECTS/ampache-docker"
|
2022-09-15 09:58:07 +00:00
|
|
|
|
|
|
|
test -f "$HOME/.rvm/scripts/rvm" &&
|
|
|
|
source "$HOME/.rvm/scripts/rvm" && # Load RVM into a shell session *as a function*
|
|
|
|
rvm use 3.0
|