From 93bebc90579cba6393871a5204096114463a551b Mon Sep 17 00:00:00 2001 From: khatibomar Date: Fri, 11 Oct 2024 19:42:21 +0400 Subject: [PATCH] add scripts folder and fixes for tmux popup --- .../import_gpg_keys.sh | 0 {scripts => backup-scripts}/install_config.sh | 0 {scripts => backup-scripts}/install_nvim.sh | 0 backup-scripts/install_scripts.sh | 34 ++++++++++++++ .../merge_gitconfig.sh | 0 config/tmux.conf | 8 +--- install.sh | 5 ++- nvim/lua/config/keymaps.lua | 2 +- scripts/colors.sh | 26 +++++++++++ scripts/k8s_kill_stuck_namespace.sh | 45 +++++++++++++++++++ scripts/toggle_tmux_popup.sh | 12 +++++ 11 files changed, 123 insertions(+), 9 deletions(-) rename {scripts => backup-scripts}/import_gpg_keys.sh (100%) rename {scripts => backup-scripts}/install_config.sh (100%) rename {scripts => backup-scripts}/install_nvim.sh (100%) create mode 100644 backup-scripts/install_scripts.sh rename {scripts => backup-scripts}/merge_gitconfig.sh (100%) create mode 100755 scripts/colors.sh create mode 100755 scripts/k8s_kill_stuck_namespace.sh create mode 100755 scripts/toggle_tmux_popup.sh diff --git a/scripts/import_gpg_keys.sh b/backup-scripts/import_gpg_keys.sh similarity index 100% rename from scripts/import_gpg_keys.sh rename to backup-scripts/import_gpg_keys.sh diff --git a/scripts/install_config.sh b/backup-scripts/install_config.sh similarity index 100% rename from scripts/install_config.sh rename to backup-scripts/install_config.sh diff --git a/scripts/install_nvim.sh b/backup-scripts/install_nvim.sh similarity index 100% rename from scripts/install_nvim.sh rename to backup-scripts/install_nvim.sh diff --git a/backup-scripts/install_scripts.sh b/backup-scripts/install_scripts.sh new file mode 100644 index 0000000..4dbcfcd --- /dev/null +++ b/backup-scripts/install_scripts.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Get the current date and time in the format YYYY-MM-DD_HH-MM-SS +current_date=$(date +"%Y-%m-%d_%H-%M-%S") + +# Define the source and destination directories for scripts +SCRIPTS_DIR="./scripts" # Change this if the new scripts directory is elsewhere +BACKUP_DIR="$HOME/.config-backup-$current_date" +OLD_SCRIPTS_DIR="$HOME/scripts" + +# Create a backup directory +mkdir -p "$BACKUP_DIR" + +# Handle the scripts directory as a special case +if [ -d "$OLD_SCRIPTS_DIR" ]; then + BACKUP_SCRIPTS_DIR="$BACKUP_DIR/.scripts" + + # Backup the old scripts directory + echo "Backing up the old scripts directory to $BACKUP_SCRIPTS_DIR" + mv "$OLD_SCRIPTS_DIR" "$BACKUP_SCRIPTS_DIR" +else + echo "No existing scripts directory found. No backup needed." +fi + +# Copy the new scripts directory if it exists +if [ -d "$SCRIPTS_DIR" ]; then + echo "Copying the new scripts directory from $SCRIPTS_DIR to $OLD_SCRIPTS_DIR" + cp -r "$SCRIPTS_DIR" "$OLD_SCRIPTS_DIR" +else + echo "Error: New scripts directory $SCRIPTS_DIR does not exist." + exit 1 +fi + +echo "Backup and copy for scripts completed successfully." diff --git a/scripts/merge_gitconfig.sh b/backup-scripts/merge_gitconfig.sh similarity index 100% rename from scripts/merge_gitconfig.sh rename to backup-scripts/merge_gitconfig.sh diff --git a/config/tmux.conf b/config/tmux.conf index 541a156..4909eb4 100644 --- a/config/tmux.conf +++ b/config/tmux.conf @@ -42,10 +42,4 @@ set -g window-status-style "fg=#959dcb bg=#292d3e" set -g window-status-current-style "fg=#89ddff,bold bg=#292d3e" set -g mode-style "bg=#959dcb fg=#292d3e" -bind-key p run-shell 'if [ "$(tmux display-message -p -F "#{session_name}")" = "popup" ]; then - tmux kill-session -t popup; -else - tmux popup -d "#(pwd)" -xC -yC -w80% -h75% -E "tmux attach -t popup || tmux new -s popup"; -fi' - - +bind-key p run-shell '$HOME/scripts/toggle_tmux_popup.sh' diff --git a/install.sh b/install.sh index c8035e4..c362f8f 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,7 @@ #!/bin/bash # Navigate to the scripts folder -SCRIPT_DIR="$(dirname "$0")/scripts" +SCRIPT_DIR="$(dirname "$0")/backup-scripts" # Run the backup and copy process for Neovim bash "$SCRIPT_DIR/install_nvim.sh" @@ -9,6 +9,9 @@ bash "$SCRIPT_DIR/install_nvim.sh" # Run the backup and copy process for Config bash "$SCRIPT_DIR/install_config.sh" +# Run the backup and copy process for Scripts +bash "$SCRIPT_DIR/install_scripts.sh" + # Merge custom gitconfig settings bash "$SCRIPT_DIR/merge_gitconfig.sh" diff --git a/nvim/lua/config/keymaps.lua b/nvim/lua/config/keymaps.lua index 762132d..28e92bf 100644 --- a/nvim/lua/config/keymaps.lua +++ b/nvim/lua/config/keymaps.lua @@ -1,7 +1,7 @@ require("config.keymaps.go-definition") function OpenTmuxPopup() - local cmd = 'tmux display-popup -E "cd ' .. vim.fn.getcwd() .. ' && $SHELL "' + local cmd = 'bash $HOME/scripts/toggle_tmux_popup.sh' vim.fn.system(cmd) end diff --git a/scripts/colors.sh b/scripts/colors.sh new file mode 100755 index 0000000..72e2a46 --- /dev/null +++ b/scripts/colors.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Print basic colors (0-7) +echo "Basic Colors (0-7):" +for i in {0..7}; do + printf "\e[3${i}mColor ${i}\e[0m " +done +echo -e "\n" + +# Print bright colors (8-15) +echo "Bright Colors (8-15):" +for i in {0..7}; do + printf "\e[3${i};1mColor $((i + 8))\e[0m " +done +echo -e "\n" + +# Print 256 colors +echo "256 Colors:" +for i in {0..255}; do + printf "\e[38;5;${i}mColor ${i}\e[0m " + # Break line after every 16 colors for better readability + if (((i + 1) % 16 == 0)); then + echo + fi +done +echo diff --git a/scripts/k8s_kill_stuck_namespace.sh b/scripts/k8s_kill_stuck_namespace.sh new file mode 100755 index 0000000..ba32dd7 --- /dev/null +++ b/scripts/k8s_kill_stuck_namespace.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# +# NOTE: force deleting a namespace should be absolute last resort, +# you want instead to find which resource is holding up the namespace +# and clean that up (vs leaking resources) +# +# First check what is holding up the ns, if it's for instance: +# custom.metrics.k8s.io/v1beta1: the server is currently +# unable to handle the request +# Then: +# k get apiservice v1beta1.custom.metrics.k8s.io +# NAME SERVICE AVAILABLE AGE +# v1beta1.custom.metrics.k8s.io istio-system/prometheus-adapter False (ServiceNotFound) 2d4h +# k delete apiservice v1beta1.custom.metrics.k8s.io +# (though this shouldn't happen: find out why prom didn't unregister...) +# +# (Very) Partially inspired by knsk.sh +# +echo "Checking for stuck namespaces" +NSS=$(kubectl get ns 2>/dev/null | grep Terminating | cut -f1 -d ' ') +if [[ "$NSS" == "" ]]; then + echo "No stuck namespaces found" + exit 0 +fi +# Start kubectl proxy to talk to API server +PORT=8118 +kubectl proxy --port=$PORT & +kp_pid=$! +function cleanup { + kill $kp_pid + echo "Done cleanup. killed $kp_pid." +} +trap cleanup exit + +for NS in $NSS; do + echo "Found stuck namespace: $NS" + kubectl get all -n "$NS" + echo "While this is bad too (leaks resources)," + # kubectl patch svc -n istio-system istio-ingressgateway -p '{"metadata":{"finalizers":null}}' + echo "Did you try to: kubectl patch -p '{\"metadata\":{\"finalizers\":null}}'" + echo "^C now before delete..." + sleep 5 + JSON=$(kubectl get ns "$NS" -o json | jq -r '.spec.finalizers=[]') + curl -X PUT --data-binary "$JSON" -H "Content-Type: application/json" "http://localhost:$PORT/api/v1/namespaces/$NS/finalize" +done diff --git a/scripts/toggle_tmux_popup.sh b/scripts/toggle_tmux_popup.sh new file mode 100755 index 0000000..505ca66 --- /dev/null +++ b/scripts/toggle_tmux_popup.sh @@ -0,0 +1,12 @@ +#!/bin/zsh + +#! /usr/bin/env bash + +tmux_popup_session_name="_popup" + +if [ "$(tmux display-message -p -F "#{session_name}")" = "${tmux_popup_session_name}" ];then + tmux detach-client +else + tmux popup -d '#{pane_current_path}' -xC -yC -w80% -h80% -E\ + "tmux new-session -A -s ${tmux_popup_session_name}" +fi