From 58d5f34019a20d88fa6beedbdceba57fac7896ee Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 1 Sep 2022 14:10:42 +0100 Subject: [PATCH] bin/stack: update commands and template --- .config/shell/server | 65 ++++++++++++++++++++++++-------- .local/bin/scripts/ex | 7 +++- .local/bin/server/stack-template | 22 ++++++++++- 3 files changed, 74 insertions(+), 20 deletions(-) diff --git a/.config/shell/server b/.config/shell/server index c5ae2ec..8b86bf1 100644 --- a/.config/shell/server +++ b/.config/shell/server @@ -16,6 +16,7 @@ _stackspin_cluster_cache=/var/tmp/stackspin-cluster # Hostname [IP] # This is a function so it can change directory. stack() { + local pod_suffix='-\(0\|[0-f]\+\)' test $# -lt 1 && builtin cd "$STACKSPIN" && echo "Usage: $0 [args...]" && @@ -54,27 +55,59 @@ stack() { echo "Initialized user '$*' with email '$mail'" else "$0" sso cli "$command" "$@" fi;; - # app clis via kubectl - (occ) "$0" exec nc-nextcloud -c nextcloud -it -- su www-data -s /bin/bash -c "php $command $*";; - (exec) "$0" kube exec "$@";; - (logs) "$0" kube logs "$@";; - (kube) - if ! pod=$("$0" pod "$2-\(0\|[0-f]\+\)") - then echo "No pod found for $2" >&2 - return 1 - fi - local subcommand=$1 - shift 2 - kubectl "$subcommand" -n $(echo $pod) "$@";; - (pod) - 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";; (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 + (occ) "$0" exec nc-nextcloud -c nextcloud -it -- su www-data -s /bin/bash -c "php $command $*";; + (vikunja) local pod=${2:-vikunja} + case "$1" in + (dump|export) cd $PROJECTS/vikunja + "$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" + ;; + (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" + esac + ;; + # high-level + (shell) "$0" exec "$1" -it -- /bin/sh "${@:2}";; + (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}") + do #highlight "Listing content of $container" && + "$0" ls "$1" -c "$container" "${@:2}" + done + fi;; + (upload) + kubectl cp "$2" -n $("$0" pod "$1$pod_suffix"):$2 "${@:3}" + "$0" ls "$1" "${@:3}";; + (exec) "$0" kube exec "$@";; + (logs) "$0" kube logs "$@" | less -RF;; + # low-level + (kube) + if ! namespacedpod=$("$0" pod "$2$pod_suffix") + then echo "No pod found for $2" >&2 + return 1 + fi + local subcommand=$1 + shift 2 + local commands=() + for arg; + do case "$arg" in (-*) break;; (*) commands+="$arg"; shift;; esac + done + test "$subcommand" = get || + highlight "Running $subcommand on $namespacedpod" >&2 + kubectl "$subcommand" "$commands[@]" -n $(echo $namespacedpod) "$@";; + (pod) + 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";; # stackspin bare (*) if which "$0-$command" >/dev/null 2>&1 then "$0-$command" "$@" diff --git a/.local/bin/scripts/ex b/.local/bin/scripts/ex index 73923cc..956f3ce 100755 --- a/.local/bin/scripts/ex +++ b/.local/bin/scripts/ex @@ -3,7 +3,7 @@ # adapted and improved from the commonly circulating version # detects whether unpacking into a subfolder is sensible # and shows progress indications for some operations -# optdepends: rewrite(part of my dotfiles, for unzip line rewriting) +# optdepends: rewrite(part of my dotfiles, for unzip line rewriting) 7z p7zip unzip for arg do case $arg in (-d) del=$(expr ${del:-0} + 1);; @@ -12,6 +12,9 @@ case $arg in if test -r "$arg"; then fullpath="$(realpath "$arg")" name="$(basename "${fullpath%.*}")" + ls --color -dlhF "$name" >&2 2>/dev/null && + echo "Target directory exists - archive already extracted?" >&2 && + continue namepart="$name.part" ( # Create temporary subfolder if current folder contains stuff @@ -45,7 +48,7 @@ case $arg in then if test $# -lt 2 -a "$(ls -U | wc -l)" -lt 3 then test "$(ls)" && mv -iv * .. cd .. && rm -d "$namepart" - else cd .. && mv -iv "$namepart" "$name" + else cd .. && mv -ivT "$namepart" "$name" fi fi case $del in diff --git a/.local/bin/server/stack-template b/.local/bin/server/stack-template index 15a3e23..7b23d3e 100755 --- a/.local/bin/server/stack-template +++ b/.local/bin/server/stack-template @@ -87,7 +87,8 @@ data: hostname: "$subdomain.\${domain}" tls: true certManager: true - # TODO Adjust Mailer config + # TODO Configure PVC for data & database + # TODO Adjust $app Mailing config # mailer: # enabled: "\${outgoing_mail_enabled}" # host: "\${outgoing_mail_smtp_host}" @@ -95,10 +96,27 @@ data: # username: "\${outgoing_mail_smtp_user}" # password: "\${outgoing_mail_smtp_password}" # fromemail: "\${outgoing_mail_from_address}" - # TODO Adjust OpenID Connect Single Sign-On Configuration + # TODO Adjust $app OpenID Connect Single Sign-On Configuration # - name: Stackspin # key: "\${client_id}" # secret: "\${client_secret}" # autoDiscoverUrl: 'https://sso.\${domain}/.well-known/openid-configuration' EOF +cat <$app-pvc.yaml +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: $app-data + namespace: $namespace + labels: + stackspin.net/backupSet: "$app" +spec: + accessModes: + - ReadWriteOnce + volumeMode: Filesystem + resources: + requests: + storage: 2Gi + storageClassName: local-path +EOF