2024-01-12 09:30:27 +00:00
|
|
|
velero_backup() {
|
2024-02-20 18:53:00 +00:00
|
|
|
#name=$(date +%y%m%d.%h%m) && velero create backup $name --exclude-namespaces velero --wait && velero backup logs $name'
|
|
|
|
name=$(date +%y%m%d.%H%M)
|
|
|
|
velero create backup $name --exclude-namespaces velero --wait
|
|
|
|
velero backup logs $name
|
2024-01-12 09:30:27 +00:00
|
|
|
}
|
2022-07-06 07:10:13 +00:00
|
|
|
|
2024-01-12 09:30:27 +00:00
|
|
|
export PROJECTS="${PROJECTS:-$HOME/projects}"
|
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() {
|
2024-02-20 18:53:00 +00:00
|
|
|
cmdname=${FUNCNAME:-$0}
|
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
|
2024-02-20 18:53:00 +00:00
|
|
|
builtin cd "$STACKSPIN" || cd /mnt/b/media/backups/servers/stackspin/2310_stackspin
|
2024-01-12 09:30:27 +00:00
|
|
|
echo "Usage: $cmdname <COMMAND> [args...]"
|
2022-11-16 10:57:31 +00:00
|
|
|
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"
|
2024-01-12 09:30:27 +00:00
|
|
|
echo Selected Stackspin cluster "$_cluster_name" with IP "$_cluster_ip"
|
2022-04-28 16:15:51 +00:00
|
|
|
echo "$_cluster_name" >"$_stackspin_cluster_cache"
|
2022-11-16 10:57:31 +00:00
|
|
|
#test "$PWD" = "$HOME" && builtin cd "$STACKSPIN"
|
2024-02-20 18:53:00 +00:00
|
|
|
test -d "$STACKSPIN" && . $STACKSPIN/env/bin/activate
|
2022-04-28 16:15:51 +00:00
|
|
|
;;
|
2024-02-20 18:53:00 +00:00
|
|
|
(sso) "$cmdname" exec dashboard-backend -- flask "$@";;
|
2022-07-18 10:57:43 +00:00
|
|
|
(users)
|
2024-02-20 18:53:00 +00:00
|
|
|
if test "$1" = "delete"
|
|
|
|
then shift
|
|
|
|
for arg
|
|
|
|
do "$cmdname" user delete "$arg"
|
|
|
|
done
|
|
|
|
elif test $# -gt 0
|
|
|
|
then
|
|
|
|
for arg
|
2024-01-12 09:30:27 +00:00
|
|
|
do "$cmdname" user show $arg
|
2022-07-18 10:57:43 +00:00
|
|
|
done
|
2024-01-12 09:30:27 +00:00
|
|
|
else "$cmdname" users $("$cmdname" user list | sed 's|.*<\(.*\)>.*|\1|')
|
2022-07-18 10:57:43 +00:00
|
|
|
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
|
2024-02-20 18:53:00 +00:00
|
|
|
"$cmdname" user create "$mail" &&
|
|
|
|
"$cmdname" user update "$mail" name "$*" &&
|
2022-04-28 16:15:51 +00:00
|
|
|
echo "Initialized user '$*' with email '$mail'"
|
2024-01-12 09:30:27 +00:00
|
|
|
else "$cmdname" sso cli "$command" "$@"
|
2022-04-28 16:15:51 +00:00
|
|
|
fi;;
|
2024-02-20 18:53:00 +00:00
|
|
|
(invite) (
|
|
|
|
# Mail invitation to new users
|
|
|
|
export mail=$1
|
|
|
|
export name=${2:-$(echo $mail | sed -E 's/(.*)\.(.*)@.*/\u\1 \u\2/' )}
|
|
|
|
#echo "$mail,$name"
|
|
|
|
stack user init "$mail" "$name"
|
|
|
|
stack-invite
|
|
|
|
);;
|
2022-09-01 13:10:42 +00:00
|
|
|
(push)
|
2024-02-20 18:53:00 +00:00
|
|
|
test -f "$1" && $EDITOR "$1"
|
|
|
|
# Allow force: https://open.greenhost.net/xeruf/stackspout/-/settings/repository#js-protected-branches-settings
|
|
|
|
git commit "$@"
|
2022-09-01 13:10:42 +00:00
|
|
|
git push &&
|
|
|
|
flux reconcile source git -n flux-system "$(basename $(git rev-parse --show-toplevel))"
|
2024-02-20 18:53:00 +00:00
|
|
|
flux reconcile kustomization -n flux-system "$(basename $(git rev-parse --show-toplevel))"
|
|
|
|
;;
|
|
|
|
# FLUX
|
|
|
|
(flux)
|
|
|
|
case "$1" in
|
|
|
|
(env) # Apply changes to .flux.env
|
|
|
|
kubectl apply -k "$CLUSTER_DIR"
|
|
|
|
flux reconcile -n flux-system kustomization velero
|
|
|
|
flux get -A kustomizations --no-header | awk -F' ' '{system("flux reconcile -n " $1 " kustomization " $2)}'
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
(reconcile)
|
|
|
|
app=$1
|
|
|
|
namespace=${2:-stackspout}
|
|
|
|
if flux suspend helmrelease -n $namespace $app
|
|
|
|
then flux resume helmrelease -n $namespace $app
|
|
|
|
else flux suspend helmrelease -n stackspin-apps $app
|
|
|
|
flux resume helmrelease -n stackspin-apps $app
|
|
|
|
fi
|
|
|
|
flux suspend kustomization $app
|
|
|
|
flux resume kustomization $app
|
|
|
|
;;
|
|
|
|
(edit)
|
|
|
|
# Edit the URL for an application
|
|
|
|
app=$1
|
|
|
|
kubectl edit configmap -n flux-system stackspin-$app-kustomization-variables
|
|
|
|
"$0" reconcile $app
|
|
|
|
;;
|
|
|
|
# Velero
|
|
|
|
(restic)
|
|
|
|
(
|
|
|
|
namespace=stackspin
|
|
|
|
case $1 in (-n|--namespace) namespace=$2; shift 2;; esac
|
|
|
|
source $CLUSTER_DIR/.flux.env || exit $?
|
|
|
|
export RESTIC_REPOSITORY="s3:${backup_s3_url}/${backup_s3_bucket}/${backup_s3_prefix}/restic/$namespace"
|
|
|
|
export AWS_ACCESS_KEY_ID="${backup_s3_aws_access_key_id}"
|
|
|
|
export AWS_SECRET_ACCESS_KEY="${backup_s3_aws_secret_access_key}"
|
|
|
|
export RESTIC_PASSWORD="$(kubectl get secret -n velero velero-repo-credentials -o jsonpath='{.data.repository-password}' | base64 -d)"
|
|
|
|
restic "$@"
|
|
|
|
)
|
|
|
|
;;
|
|
|
|
(backup)
|
|
|
|
backupname=$(date +%y%m%d.%H%m)
|
|
|
|
velero create backup $backupname --exclude-namespaces velero --wait
|
|
|
|
velero backup logs $backupname;;
|
|
|
|
(restore)
|
|
|
|
test $# -lt 2 && echo "$0 $command <backup> <app> [namespace]" >&2 && return 1
|
|
|
|
backup=$1; app=$2
|
|
|
|
namespace=${3:-stackspin-apps} # TODO automatically handle stackspout apps
|
|
|
|
restore="${backup}-$app-$(date +%s)"
|
|
|
|
if test "$app" = dashboard
|
|
|
|
then kust=single-sign-on
|
|
|
|
hr="$kust-database"
|
|
|
|
namespace=stackspin
|
|
|
|
else hr="$app"
|
|
|
|
kust="$app"
|
|
|
|
fi
|
|
|
|
flux suspend kustomization $kust
|
|
|
|
flux suspend helmrelease -n $namespace $hr
|
|
|
|
kubectl delete all -n $namespace -l stackspin.net/backupSet=$app
|
|
|
|
kubectl delete secret -n $namespace -l stackspin.net/backupSet=$app
|
|
|
|
kubectl delete configmap -n $namespace -l stackspin.net/backupSet=$app
|
|
|
|
kubectl delete pvc -n $namespace -l stackspin.net/backupSet=$app
|
|
|
|
velero restore create $restore --from-backup=$backup -l stackspin.net/backupSet=$app
|
|
|
|
echo "Waiting a few seconds for $app backup to restore..."
|
|
|
|
sleep 10
|
|
|
|
velero restore describe $restore
|
|
|
|
echo "Press enter if backup is ready to resume flux resources:"
|
|
|
|
read
|
|
|
|
test $app = dashboard && kubectl delete secret -n stackspin hydra && flux reconcile helmrelease -n stackspin hydra
|
|
|
|
flux resume helmrelease -n $namespace $hr # TODO timeout
|
|
|
|
flux resume kustomization $kust
|
|
|
|
;;
|
|
|
|
(restore-pvc)
|
|
|
|
test $# -lt 1 && echo "$0 $command <app> [dir]" >&2 && return 1
|
|
|
|
local app=$1
|
|
|
|
if test -d "$2"
|
|
|
|
then dir="$2"
|
|
|
|
target=$(ssh "$_cluster_name" find /var/lib/Stackspin/local-storage/ -maxdepth 1 -name "*$app")
|
|
|
|
test -z "$target" && echo "No target found for ${app}" && return 1
|
|
|
|
ssh "$_cluster_name" mv -v "$target" "$target.$(date +%s)"
|
|
|
|
rsync --links --hard-links --times --recursive --info=progress2,remove,symsafe,flist,del --human-readable "$dir/" "$_cluster_name:$target/"
|
|
|
|
else
|
|
|
|
for vol in $(ls -d pvc*$app* | cut -d_ -f3 | sort)
|
|
|
|
do "$cmdname" restore-pvc $vol $(find -maxdepth 1 -name "*$vol")
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
;;
|
2022-09-01 13:10:42 +00:00
|
|
|
# KUBE
|
|
|
|
# app clis
|
2024-01-12 09:30:27 +00:00
|
|
|
(occ) "$cmdname" exec nc-nextcloud -c nextcloud -it -- su www-data -s /bin/bash -c "php $command $*";;
|
2024-02-20 18:53:00 +00:00
|
|
|
(vikunja*)
|
|
|
|
local pod=$command
|
2022-09-01 13:10:42 +00:00
|
|
|
case "$1" in
|
2022-11-16 10:57:31 +00:00
|
|
|
(dump|export) cd "$PROJECTS/vikunja"
|
2024-02-20 18:53:00 +00:00
|
|
|
"$cmdname" exec "$pod-api" -- sh -c 'rm -f *.zip && ./vikunja dump >/dev/null && ls --color -lAhF >&2 && cat *.zip' >"$pod-dump_$(date +%F).zip"
|
2022-09-01 13:10:42 +00:00
|
|
|
;;
|
2024-02-20 18:53:00 +00:00
|
|
|
(restore)
|
|
|
|
if ! test -f "$2"
|
|
|
|
then echo "Usage: $0 vikunja[suffix] restore <file>" >&2
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
file=$2
|
|
|
|
"$cmdname" upload "$pod-api" "$file"
|
|
|
|
"$cmdname" exec "$pod-api" -it -- ./vikunja restore "$file"
|
2022-09-01 13:10:42 +00:00
|
|
|
;;
|
2024-02-20 18:53:00 +00:00
|
|
|
(psql) kubectl exec -it -n $("$cmdname" pod "$pod-postgresql") -- sh -c "PGPASSWORD=$(kubectl get secret --namespace stackspout $pod-postgresql -o jsonpath='{.data.password}' | base64 --decode) psql -h localhost -U vikunja -p 5432 vikunja";;
|
|
|
|
(*) echo "Unknown $command subcommand";;
|
2022-09-01 13:10:42 +00:00
|
|
|
esac
|
|
|
|
;;
|
2024-02-20 18:53:00 +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
|
2024-02-20 18:53:00 +00:00
|
|
|
"$cmdname" exec $n -it -- env "MYSQL_PWD=$pw" mysql -u $app "$@"
|
|
|
|
;;
|
|
|
|
(mariar)
|
|
|
|
name="$1-mariadb"
|
|
|
|
shift
|
|
|
|
"$cmdname" exec "$name" -it -- env "MYSQL_PWD=$(kubectl get secret -n $(kubectl get secret --all-namespaces -o=custom-columns=S:.metadata.namespace,N:.metadata.name --no-headers | grep --color=never -- "$name") -o jsonpath='{.data.mariadb-root-password}' | base64 -d)" mysql -u root "$@"
|
|
|
|
;;
|
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
|
2024-01-12 09:30:27 +00:00
|
|
|
"$cmdname" exec "$container" -c "$pod" -it -- /bin/sh "$@";;
|
2022-09-01 13:10:42 +00:00
|
|
|
(ls)
|
|
|
|
if test $# -gt 1 && ! [[ "$2" =~ ".*/.*" ]]
|
2024-01-12 09:30:27 +00:00
|
|
|
then "$cmdname" exec "$1" "$2" "$3" -it -- ls -lAhF --group-directories-first "${@:4}"
|
|
|
|
else for container in $("$cmdname" kube get "$1" pod -o "jsonpath={.spec.containers[*].name}")
|
2022-11-16 10:57:31 +00:00
|
|
|
do highlight "Listing content of $container" &&
|
2024-01-12 09:30:27 +00:00
|
|
|
"$cmdname" ls "$1" -c "$container" "${@:2}"
|
2022-09-01 13:10:42 +00:00
|
|
|
done
|
|
|
|
fi;;
|
|
|
|
(upload)
|
2024-01-12 09:30:27 +00:00
|
|
|
kubectl cp "$2" -n $("$cmdname" pod "$1$pod_suffix"):$2 "${@:3}"
|
|
|
|
"$cmdname" ls "$1" "${@:3}";;
|
|
|
|
(exec) "$cmdname" kube exec "$@";;
|
2022-11-21 11:36:20 +00:00
|
|
|
(logs) podname=$1
|
|
|
|
shift
|
2024-01-12 09:30:27 +00:00
|
|
|
"$cmdname" 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; }
|
2024-01-12 09:30:27 +00:00
|
|
|
local pods=$("$cmdname" 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=' '
|
2024-01-12 09:30:27 +00:00
|
|
|
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
|
2024-02-20 18:53:00 +00:00
|
|
|
if ! 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"
|
|
|
|
then code=$?
|
|
|
|
echo "No pod found for $podname" >&2
|
|
|
|
return $code
|
|
|
|
fi
|
|
|
|
;;
|
2022-05-03 16:50:05 +00:00
|
|
|
# stackspin bare
|
2024-01-12 09:30:27 +00:00
|
|
|
(*) if which "$cmdname-$command" >/dev/null 2>&1
|
|
|
|
then "$cmdname-$command" "$@"
|
2022-06-30 09:54:03 +00:00
|
|
|
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
|
2024-01-12 09:30:27 +00:00
|
|
|
case "$1" in
|
|
|
|
([a-z]*)
|
2024-02-20 18:53:00 +00:00
|
|
|
for arg
|
|
|
|
do kubectl exec -n stackspin deploy/dashboard -c backend -- flask cli app install "$arg"
|
|
|
|
done;;
|
|
|
|
(""|-*)
|
2024-01-12 09:30:27 +00:00
|
|
|
python3 -m pip install --upgrade pip
|
|
|
|
python3 -m pip install -r requirements.txt
|
|
|
|
python3 -m stackspin "$@" "$_cluster_name" "$command"
|
2024-02-20 18:53:00 +00:00
|
|
|
cp -nv "install/.flux.env.example" "clusters/$_cluster_name/.flux.env" &&
|
|
|
|
$EDITOR "clusters/$_cluster_name/.flux.env"
|
|
|
|
cp -nv install/kustomization.yaml $CLUSTER_DIR/
|
|
|
|
kubectl get namespace flux-system 2>/dev/null || kubectl create namespace flux-system
|
|
|
|
kubectl apply -k $CLUSTER_DIR
|
|
|
|
./install/install-stackspin.sh
|
|
|
|
;;
|
|
|
|
esac
|
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
|
|
|
|
}
|
2024-01-12 09:30:27 +00:00
|
|
|
|
2022-04-28 16:15:51 +00:00
|
|
|
cat "$_stackspin_cluster_cache" 2>/dev/null |
|
|
|
|
while read cluster; do stack select "$cluster"; done
|
2022-07-06 09:15:29 +00:00
|
|
|
|
2024-01-12 09:30:27 +00:00
|
|
|
test -z "$DISPLAY" && test "$XDG_VTNR" != 1 || return 0
|
|
|
|
# The following runs only on headless machines
|
|
|
|
|
|
|
|
which kubectl >/dev/null ||
|
|
|
|
{ kubectl() { sudo k3s kubectl "$@"; } && export -f kubectl; }
|
|
|
|
|
|
|
|
export PATH="$PATH:$HOME/.local/bin/server"
|
|
|
|
|
|
|
|
test -d "$MUSIC" || export MUSIC="/srv/funkwhale/data/music/janek"
|
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
|