Debian Codegraph @

src/hpct-rtunnel

raw
1 #!/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 set -euo pipefail
32
33
34 # ###### Usage ##############################################################
35 usage () {
36 local exitCode="$1"
37 echo >&2 "Usage: $0 [-R|--remote remote_location] [-C|--collector collector] [-N|--nodeid node_id] [-K|--key ssh_private_key_file] [-q|--quiet] [-w|--verbose] [-h|--help] [-v|--version]"
38 exit "${exitCode}"
39 }
40
41
42 # ###### Version ############################################################
43 version () {
44 echo "HiPerConTracer-RTunnel @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
45 exit 0
46 }
47
48
49 # ###### Main program #######################################################
50
51 # ====== Handle arguments ===================================================
52 if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then
53 echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!"
54 exit 1
55 fi
56 UNAME="$(uname)"
57 case "${UNAME}" in
58 FreeBSD)
59 GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)"
60 ;;
61 NetBSD)
62 GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)"
63 ;;
64 OpenBSD)
65 GETOPT="$(command -v gnugetopt || true)"
66 ;;
67 Darwin)
68 GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)"
69 ;;
70 *)
71 GETOPT="$(command -v getopt || true)"
72 ;;
73 esac
74 if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then
75 echo >&2 "ERROR: Enhanced/GNU getopt is required!"
76 exit 1
77 fi
78 options="$(${GETOPT} -o N:R:C:K:H:qwhv --long nodeid:,remote:,collector:,key:,known-hosts:,quiet,verbose,help,version -a -- "$@")"
79 # shellcheck disable=SC2181
80 if [[ $? -ne 0 ]]; then
81 usage 1
82 fi
83
84 NODE_ID=
85 REMOTE="/var/hipercontracer/data"
86 COLLECTOR=
87 KEY="/var/hipercontracer/ssh/id_ed25519"
88 KNOWN_HOSTS="/var/hipercontracer/ssh/known_hosts"
89 VERBOSE=1
90
91 eval set -- "${options}"
92 while [ $# -gt 0 ] ; do
93 case "$1" in
94 -N | --nodeid)
95 NODE_ID="$2"
96 shift
97 ;;
98 -R | --remote)
99 REMOTE="$2"
100 shift
101 ;;
102 -C | --collector)
103 COLLECTOR="$2"
104 shift
105 ;;
106 -K | --key)
107 KEY="$2"
108 shift
109 ;;
110 -H | --known-hosts)
111 KNOWN_HOSTS="$2"
112 shift
113 ;;
114 -q | --quiet)
115 VERBOSE=0
116 ;;
117 -w | --verbose)
118 VERBOSE=1
119 ;;
120 -h | --help)
121 usage 0
122 ;;
123 -v | --version)
124 version
125 ;;
126 --)
127 shift
128 break
129 ;;
130 *)
131 # This should not happen: wrong getopt parameters, or missing case?
132 echo >&2 "INTERNAL ERROR: Unhandled option $1!"
133 exit 1
134 ;;
135 esac
136 shift
137 done
138 if [ $# -ne 0 ] ; then
139 usage 1
140 fi
141
142 if [ "${COLLECTOR}" == "" ] ; then
143 usage 1
144 fi
145 if [ ! -e "${KEY}" ] ; then
146 echo >&2 "ERROR: SSH private key file ${KEY} does not exist!"
147 exit 1
148 fi
149
150 if [[ ! "${NODE_ID}" =~ [0-9]+ ]] ; then
151 echo >&2 "ERROR: No or invalid Node ID specified!"
152 exit 1
153 fi
154 if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then
155 echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!"
156 exit 1
157 fi
158
159
160 # ====== Obtain node status =================================================
161 NAME="Unknown"
162 VERSION="?.?"
163 . /etc/os-release
164 # Getting the version of hipercontracer-common here, since it is a dependency of
165 # hpct-rtunnel. So, it should have been upgraded earlier.
166 if [ -x /usr/bin/dpkg ] ; then
167 nodePackageVersion="$(dpkg -s hipercontracer-common 2>/dev/null | grep '^Version: ' | sed -e "s/^Version: //" || echo "N/A")"
168 elif [ -x /usr/bin/rpm ] ; then
169 nodePackageVersion="$(rpm -q hipercontracer-common | sed -e "s/^hipercontracer-common-//" || echo "N/A")"
170 else
171 nodePackageVersion=""
172 fi
173 nodeStatus="\
174 lsb_release: ${NAME} ${VERSION}
175 node_package_version: ${nodePackageVersion}
176 node_uptime: $(uptime -s)
177 tunnel_uptime: $(date -u +"%Y-%m-%d %H:%M:%S")"
178
179
180 # ====== Establish reverse tunnel ===========================================
181 sshOptions="-i ${KEY} -oUserKnownHostsFile=${KNOWN_HOSTS}"
182 user="node${NODE_ID}"
183 port=$((10000+NODE_ID))
184 lastSeen="${REMOTE}/${user}/last-seen.txt"
185
186 if [ ${VERBOSE} -ne 0 ] ; then
187 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running SSH (ssh -tt -R ${port}:localhost:22 ${sshOptions} ${user}@${COLLECTOR}) ...\x1b[0m"
188 echo "Private key file: ${KEY}"
189 echo "Known hosts file: ${KNOWN_HOSTS}"
190 fi
191
192 # ping -c1 ${COLLECTOR} || true
193
194 # shellcheck disable=SC2086
195 ssh -tt -R "${port}:localhost:22" ${sshOptions} "${user}"@"${COLLECTOR}" "while true ; do ( date -u +\"%Y-%m-%d %H:%M:%S\" ; echo \"${nodeStatus}\" ) >${lastSeen}.tmp && mv ${lastSeen}.tmp ${lastSeen} ; sleep 255 ; done" \
196 </dev/null >/dev/null 2>&1 && status=0 || status=$?
197 if [ "${status}" -ne 0 ] ; then
198 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") SSH failed!\x1b[0m"
199 exit 1
200 else
201 if [ ${VERBOSE} -ne 0 ] ; then
202 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m"
203 fi
204 fi