From 898d733ebf3552b36639d410d64b67ddb93079e6 Mon Sep 17 00:00:00 2001 From: rasmus Date: Sat, 2 Aug 2025 19:23:46 +0300 Subject: [PATCH] commit old scripts to repo before removal --- proxmox/admin_scripts/bashrc | 88 ++++++++++++++++++++++++ proxmox/admin_scripts/bashrc_sourcer | 9 +++ proxmox/admin_scripts/migrate_running.sh | 24 +++++++ proxmox/admin_scripts/return_migrate.sh | 37 ++++++++++ proxmox/admin_scripts/stalk_updates.sh | 53 ++++++++++++++ 5 files changed, 211 insertions(+) create mode 100644 proxmox/admin_scripts/bashrc create mode 100755 proxmox/admin_scripts/bashrc_sourcer create mode 100755 proxmox/admin_scripts/migrate_running.sh create mode 100755 proxmox/admin_scripts/return_migrate.sh create mode 100755 proxmox/admin_scripts/stalk_updates.sh diff --git a/proxmox/admin_scripts/bashrc b/proxmox/admin_scripts/bashrc new file mode 100644 index 0000000..55445d7 --- /dev/null +++ b/proxmox/admin_scripts/bashrc @@ -0,0 +1,88 @@ +#!/bin/bash + +# If not running interactively, don't do anything +case $- in + *i*) ;; + *) return;; +esac + +source /root/telegram.env + +alias updl="apt update -q && apt full-upgrade --download-only -y -qq && apt autoremove -y -qq" +alias rup="restic -q -r local:/root/jc/restic/"$HOSTNAME" -p /root/jc/restic/passwd backup /etc ~/.bash_history && restic -q -r local:/root/jc/restic/"$HOSTNAME" -p /root/jc/restic/passwd forget --prune --keep-last 14 --keep-within 3m --keep-weekly 520" +#TODO: restic -p ~/jc/restic/passwd -r local:"??no more nas??/restic/$HOSTNAME" init + +function up { + read -p "where to? " whereto + if ! ~/jc/migrate_running.sh "$HOSTNAME" "$whereto"; then + tgmsg tgmsg 'ERROR: up: migrate' + return 1 + fi + echo restic… + if ! rup; then + tgmsg 'ERROR: up: restic' + return 1 + fi + if ! updl; then + tgmsg 'ERROR: up: download' + return 1 + fi +} +function upg { + if ! ~/jc/confirm_norunning.sh; then + tgmsg 'ERROR: upg: migratables present' + return 1 + fi + + if ! ~/jc/shutdown_nomigrates.sh; then + tgmsg 'ERROR: upg: signaling nomigrate shutdowns' + return 1 + fi + if ! timeout 200 bash -c waitNomigrateShutdown; then + tgmsg 'ERROR: upg: running VMs before upgrade' + return 1 + fi + + if ! apt full-upgrade -y; then + tgmsg 'ERROR: upg: upgrade' + return 1 + fi +} + +function waitNomigrateShutdown { +echo '- - - - - - - - Waiting nomigrate to shutdown 200s - - - - - - - -' +while true; do + if ~/jc/confirm_norunning.sh 1 >/dev/null; then + return + fi + + sleep 3 + printf . +done +} +export -f waitNomigrateShutdown + +function upgr { + up || return $? + upg || return $? + + # double confirmation + if ! timeout 200 bash -c waitNomigrateShutdown; then + tgmsg 'ERROR: upgr: running VMs before reboot' + return 1 + fi + + reboot +} + +function toh { +~/jc/return_migrate.sh ~/jc/desired.yaml "$@" +tgmsg "return_migrate finished: $?" +} + +alias des="vim ~/jc/desired.yaml" +alias h="echo 'avail cmds: up upg upgr | toh des | localprogress'" + +function localprogress () { + watch ls -lh "/var/lib/vz/images/$1" +} diff --git a/proxmox/admin_scripts/bashrc_sourcer b/proxmox/admin_scripts/bashrc_sourcer new file mode 100755 index 0000000..3f06859 --- /dev/null +++ b/proxmox/admin_scripts/bashrc_sourcer @@ -0,0 +1,9 @@ +# If not running interactively, don't do anything +case $- in + *i*) ;; + *) return;; +esac + +if timeout 5 cat "/root/jc/bashrc" >> /dev/null; then + source "/root/jc/bashrc" +fi diff --git a/proxmox/admin_scripts/migrate_running.sh b/proxmox/admin_scripts/migrate_running.sh new file mode 100755 index 0000000..35a8dad --- /dev/null +++ b/proxmox/admin_scripts/migrate_running.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -e + +if ! which jq >> /dev/null; then + echo "jq not found" + exit 1 +fi + +if [ "$#" -ne 2 ]; then + pvesh get /nodes + echo "usage: $0 " + exit 1 +fi + +from="$1" +target="$2" + +migratable_ids="$(pvesh get "/nodes/${from}/qemu" --output-format json |\ + jq -r 'map(select( .status == "running" and (.tags | split(";") | all(.!="nomigrate")) ) | .vmid) | @csv')" + +if [[ "$migratable_ids" != "" ]]; then + echo "$from $target $migratable_ids" + pvesh create "/nodes/${from}/migrateall" -vms "$migratable_ids" -target "$target" +fi diff --git a/proxmox/admin_scripts/return_migrate.sh b/proxmox/admin_scripts/return_migrate.sh new file mode 100755 index 0000000..256ae01 --- /dev/null +++ b/proxmox/admin_scripts/return_migrate.sh @@ -0,0 +1,37 @@ +#!/bin/bash +source ~/jc/telegram.env + +set -e +if [ "$#" -lt 1 ]; then + echo "usage: return_migrate.sh [single destination]" + exit 1 +fi +if ! which jq >> /dev/null; then + echo "jq not found" + exit 1 +fi +if ! which yq >> /dev/null; then + echo "yq not found" + exit 1 +fi + +desired_file="$1" +single="$2" + +#TODO: warn about nodes not in the file +#TODO: warn about VMs w/o nomigrate not in the file +yq -r 'keys[]' "$desired_file" | grep -v '^ *#' | while IFS= read -r desired_node; do + # validation of nodes and ids would be nice + desired_ids="$(yq -r --arg node "$desired_node" '.[$node][]' "$desired_file" | tr '\n' ',')" + pvesh get "/nodes" --output-format json | jq -r '.[] | select(.status == "online") | .node' | grep -v '^ *#' | while IFS= read -r node_from; do + if [[ "$node_from" != "$desired_node" ]]; then # not local / recursion + if [[ ! -z "$single" ]] && [[ "$desired_node" != "$single" ]]; then + echo skipping $node_from $desired_node $desired_ids + continue + fi + + echo $node_from $desired_node $desired_ids + pvesh create "/nodes/${node_from}/migrateall" -vms "$desired_ids" -target "$desired_node" + fi + done +done diff --git a/proxmox/admin_scripts/stalk_updates.sh b/proxmox/admin_scripts/stalk_updates.sh new file mode 100755 index 0000000..d2543a5 --- /dev/null +++ b/proxmox/admin_scripts/stalk_updates.sh @@ -0,0 +1,53 @@ +#!/bin/bash +host="$(hostname)" + +cleanup() { + printf "$(tput sgr0)\n" +} +trap cleanup EXIT + +printf 'ID\tName\tup;sec\tver\t(notice)\n' + +pvesh get "/nodes/$host/qemu" --output-format json | jq -r '.[] | select(.status == "running") | "\(.vmid),\(.name),\(.tags)"' |\ +while IFS= read -r vmline; do + vmid="$(cut -d, -f1 <<< "$vmline")" + vmname="$(cut -d, -f2 <<< "$vmline")" + tags="$(cut -d, -f3 <<< "$vmline")" + + printf "$vmid $vmname " + + if grep -q custom <<< "$tags"; then echo "$(tput setaf 5)is custom$(tput sgr0)"; + continue + fi + + printf "$(tput setaf 5)"; qm guest exec "$vmid" -- /usr/bin/apt-get update > /dev/null; code="$?"; printf "$(tput sgr0)" + if [[ "$code" == 255 ]]; then + continue + fi + + apt="$(qm guest exec "$vmid" -- /usr/lib/update-notifier/apt-check | jq '."err-data"' | sed 's/\n$//'| xargs -I: printf " ":)" + printf "$(tput setaf 190)$apt$(tput sgr0) " + + ver="$(qm guest exec "$vmid" -- /usr/bin/lsb_release -a 2>/dev/null | jq '."out-data"' | cut -d'\' -f4 | cut -c 2- )" + printf "$ver" + + expectedTag="unknown" + case "$(cut -d' ' -f1 <<< "$ver")" in + Ubuntu) + expectedTag="ub$(cut -d' ' -f2 <<< "$ver" | cut -d. -f1)" + ;; + Debian) + expectedTag="d$(cut -d' ' -f3 <<< "$ver")" + ;; + esac + + if ! grep -q "$expectedTag" <<< "$tags"; then + printf " $(tput setaf 6)TAG MISMATCH$(tput sgr0)" + fi + + if [[ "$(cut -d';' -f2 <<< "$apt")" != 0 ]]; then + printf " $(tput setaf 3)SECURITY UPDATES$(tput sgr0)" + fi + + echo +done