#!/usr/bin/env bash # AI lab operational shell helpers. This file is intended to be sourced. alias ll='ls -alF' alias la='ls -A' alias ports='ss -tulpn' alias dus='du -xhd1 2>/dev/null | sort -h' alias dufh='df -hT' alias failed='systemctl --failed --no-pager' alias jerr='journalctl -p err -b --no-pager' alias timers='systemctl list-timers --all --no-pager' alias dps='command -v docker >/dev/null 2>&1 && docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}" || printf "Docker is not installed\n"' alias ddf='command -v docker >/dev/null 2>&1 && docker system df || printf "Docker is not installed\n"' alias dcu='command -v docker >/dev/null 2>&1 && docker compose up -d || printf "Docker Compose is not available\n"' alias vms='command -v virsh >/dev/null 2>&1 && virsh list --all || printf "virsh is not installed\n"' alias gpu='command -v nvidia-smi >/dev/null 2>&1 && nvidia-smi || printf "nvidia-smi is not installed\n"' alias gpuloop='command -v nvidia-smi >/dev/null 2>&1 && watch -n 2 nvidia-smi || printf "nvidia-smi is not installed\n"' alias now='date "+%Y-%m-%d %H:%M:%S %Z"' svc_status() { if (($# != 1)); then printf 'Usage: svc_status SERVICE\n' >&2 return 2 fi systemctl status "$1" --no-pager } svc_logs() { if (($# < 1 || $# > 2)); then printf 'Usage: svc_logs SERVICE [LINES]\n' >&2 return 2 fi local lines="${2:-100}" [[ "$lines" =~ ^[0-9]+$ ]] || { printf 'LINES must be numeric\n' >&2 return 2 } journalctl -u "$1" -n "$lines" --no-pager } docker_logs() { if (($# < 1 || $# > 2)); then printf 'Usage: docker_logs CONTAINER [LINES]\n' >&2 return 2 fi command -v docker >/dev/null 2>&1 || { printf 'Docker is not installed\n' >&2 return 1 } local lines="${2:-100}" [[ "$lines" =~ ^[0-9]+$ ]] || { printf 'LINES must be numeric\n' >&2 return 2 } docker logs --tail "$lines" "$1" } docker_restart() { if (($# != 1)); then printf 'Usage: docker_restart CONTAINER\n' >&2 return 2 fi command -v docker >/dev/null 2>&1 || { printf 'Docker is not installed\n' >&2 return 1 } docker restart "$1" } vm_autostart() { if (($# != 1)); then printf 'Usage: vm_autostart VM\n' >&2 return 2 fi command -v virsh >/dev/null 2>&1 || { printf 'virsh is not installed\n' >&2 return 1 } virsh autostart "$1" } vm_no_autostart() { if (($# != 1)); then printf 'Usage: vm_no_autostart VM\n' >&2 return 2 fi command -v virsh >/dev/null 2>&1 || { printf 'virsh is not installed\n' >&2 return 1 } virsh autostart --disable "$1" } path_backup() { if (($# != 1)); then printf 'Usage: path_backup PATH\n' >&2 return 2 fi if [[ ! -e "$1" ]]; then printf 'Path does not exist: %s\n' "$1" >&2 return 1 fi local destination destination="${1%/}.$(date '+%Y%m%d-%H%M%S').bak" cp -a -- "$1" "$destination" printf 'Backup created: %s\n' "$destination" } extract() { if (($# != 1)); then printf 'Usage: extract ARCHIVE\n' >&2 return 2 fi if [[ ! -f "$1" ]]; then printf 'Archive does not exist: %s\n' "$1" >&2 return 1 fi case "$1" in *.tar.bz2|*.tbz2) tar xjf "$1" ;; *.tar.gz|*.tgz) tar xzf "$1" ;; *.tar.xz|*.txz) tar xJf "$1" ;; *.tar) tar xf "$1" ;; *.bz2) bunzip2 "$1" ;; *.gz) gunzip "$1" ;; *.zip) unzip "$1" ;; *.7z) 7z x "$1" ;; *.rar) unrar x "$1" ;; *) printf 'Unsupported archive type: %s\n' "$1" >&2 return 2 ;; esac }