| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # ========================================================================== |
| 3 | # _ _ _ ____ ____ _____ |
| 4 | # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ |
| 5 | # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| |
| 6 | # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | |
| 7 | # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| |
| 8 | # |
| 9 | # --- High-Performance Connectivity Tracer (HiPerConTracer) --- |
| 10 | # https://www.nntb.no/~dreibh/hipercontracer/ |
| 11 | # ========================================================================== |
| 12 | # |
| 13 | # High-Performance Connectivity Tracer (HiPerConTracer) |
| 14 | # Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 3 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | # Contact: [email protected] |
| 30 | |
| 31 | # Bash options: |
| 32 | set -euo pipefail |
| 33 | |
| 34 | |
| 35 | # ###### Usage ############################################################## |
| 36 | usagescip-shell shell shell-project 0.0.0 usage(). () { |
| 37 | local exitCode="$1" |
| 38 | echoscip-shell system . . echo. >&2 "Usage: $0 node_id [-r|--remove-data] [-h|--help] [-v|--version]" |
| 39 | exit "${exitCode}" |
| 40 | } |
| 41 | |
| 42 | |
| 43 | # ###### Version ############################################################ |
| 44 | versionscip-shell shell shell-project 0.0.0 version(). () { |
| 45 | echoscip-shell system . . echo. "HiPerConTracer-Node-Removal @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@" |
| 46 | exit 0 |
| 47 | } |
| 48 | |
| 49 | |
| 50 | # ###### Main program ####################################################### |
| 51 | |
| 52 | # ====== Handle arguments =================================================== |
| 53 | if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then |
| 54 | echoscip-shell system . . echo. "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!" |
| 55 | exit 1 |
| 56 | fi |
| 57 | UNAME="$(unamescip-shell system . . uname.)" |
| 58 | case "${UNAME}" in |
| 59 | FreeBSD) |
| 60 | GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || truescip-shell system . . true.)" |
| 61 | ;; |
| 62 | NetBSD) |
| 63 | GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || truescip-shell system . . true.)" |
| 64 | ;; |
| 65 | OpenBSD) |
| 66 | GETOPT="$(command -v gnugetopt || truescip-shell system . . true.)" |
| 67 | ;; |
| 68 | Darwin) |
| 69 | GETOPT="$(envscip-shell system . . env. PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || truescip-shell system . . true.)" |
| 70 | ;; |
| 71 | *) |
| 72 | GETOPT="$(command -v getopt || truescip-shell system . . true.)" |
| 73 | ;; |
| 74 | esac |
| 75 | if "${GETOPT}" -T >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || [[ $? -ne 4 ]]; then |
| 76 | echoscip-shell system . . echo. >&2 "ERROR: Enhanced/GNU getopt is required!" |
| 77 | exit 1 |
| 78 | fi |
| 79 | options="$(${GETOPT} -o rhv --long remove-data,help,version -a -- "$@")" |
| 80 | # shellcheck disable=SC2181 |
| 81 | if [[ $? -ne 0 ]] ; then |
| 82 | usagescip-shell shell shell-project 0.0.0 usage(). 1 |
| 83 | fi |
| 84 | |
| 85 | REMOVE_DATA=0 |
| 86 | eval set -- "${options}" |
| 87 | while [scip-shell system . . `[`. $# -gt 0 ] ; do |
| 88 | case "$1" in |
| 89 | -r | --remove-data) |
| 90 | REMOVE_DATA=1 |
| 91 | ;; |
| 92 | -h | --help) |
| 93 | usagescip-shell shell shell-project 0.0.0 usage(). 0 |
| 94 | ;; |
| 95 | -v | --version) |
| 96 | versionscip-shell shell shell-project 0.0.0 version(). |
| 97 | ;; |
| 98 | --) |
| 99 | shift |
| 100 | break |
| 101 | ;; |
| 102 | *) |
| 103 | # This should not happen: wrong getopt parameters, or missing case? |
| 104 | echoscip-shell system . . echo. >&2 "INTERNAL ERROR: Unhandled option $1!" |
| 105 | exit 1 |
| 106 | ;; |
| 107 | esac |
| 108 | shift |
| 109 | done |
| 110 | if [scip-shell system . . `[`. $# -ne 1 ] ; then |
| 111 | usagescip-shell shell shell-project 0.0.0 usage(). 1 |
| 112 | fi |
| 113 | |
| 114 | NODE_ID="$1" |
| 115 | if [[ ! "${NODE_ID}" =~ ^[0-9]+$ ]] ; then |
| 116 | echoscip-shell system . . echo. >&2 "ERROR: Invalid Node ID ${NODE_ID}!" |
| 117 | exit 1 |
| 118 | fi |
| 119 | if [scip-shell system . . `[`. "${NODE_ID}" -lt 1 ] || [scip-shell system . . `[`. "${NODE_ID}" -gt 9999 ] ; then |
| 120 | echoscip-shell system . . echo. >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!" |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | # ====== Remove user ======================================================== |
| 125 | user="node${NODE_ID}" |
| 126 | echoscip-shell system . . echo. -e "\x1b[34m$(datescip-shell system . . date. +"%Y-%m-%d %H:%M:%S") Deleting user ${user} ...\x1b[0m" |
| 127 | |
| 128 | if getentscip-shell system . . getent. passwd "${user}" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 129 | echoscip-shell system . . echo. "Deleting user ${user} ..." |
| 130 | sudoscip-shell system . . sudo. userdel -r -f "${user}" || truescip-shell system . . true. |
| 131 | fi |
| 132 | if getentscip-shell system . . getent. group "${user}" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 133 | echoscip-shell system . . echo. "Deleting group ${user} ..." |
| 134 | sudoscip-shell system . . sudo. groupdel "${user}" || truescip-shell system . . true. |
| 135 | fi |
| 136 | if [scip-shell system . . `[`. ! -v SUDO_USER ] || [scip-shell system . . `[`. "${SUDO_USER}" == "" ] ; then |
| 137 | rmscip-shell system . . rm. -f ~/keys/"${user}".pub ~/keys/"${user}".pub.new |
| 138 | else |
| 139 | rmscip-shell system . . rm. -f /home/"${SUDO_USER}"/keys/"${user}".pub \ |
| 140 | /home/"${SUDO_USER}"/keys/"${user}".pub.new |
| 141 | fi |
| 142 | if [scip-shell system . . `[`. -d "/var/hipercontracer/data/${user}" ] ; then |
| 143 | if [scip-shell system . . `[`. ${REMOVE_DATA} -eq 1 ] ; then |
| 144 | rmscip-shell system . . rm. -rf "/var/hipercontracer/data/${user}" |
| 145 | else |
| 146 | echoscip-shell system . . echo. "Keeping the data directory /var/hipercontracer/data/${user}, since data removal is not requested!" |
| 147 | # The node user is not existing anymore; set ownership to "root" instead: |
| 148 | sudoscip-shell system . . sudo. chown -R root:root "/var/hipercontracer/data/${user}" |
| 149 | fi |
| 150 | fi |
| 151 | |
| 152 | echoscip-shell system . . echo. -e "\x1b[34m$(datescip-shell system . . date. +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m" |