Debian Codegraph @

src/hpct-node-setup

raw symbols SCIP
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 # Bash options:
32 set -euo pipefail
33
34
35 # ###### Usage ##############################################################
36 usage () {
37 local exitCode="$1"
38 echo >&2 "Usage: $0 user@collector node_id [-q|--quiet] [-w|--verbose] [-h|--help] [-v|--version]"
39 exit "${exitCode}"
40 }
41
42
43 # ###### Version ############################################################
44 version () {
45 echo "HiPerConTracer-Node-Setup @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
46 exit 0
47 }
48
49
50
51 # ###### Main program #######################################################
52
53 # ====== Handle arguments ===================================================
54 if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then
55 echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!"
56 exit 1
57 fi
58 UNAME="$(uname)"
59 case "${UNAME}" in
60 FreeBSD)
61 GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)"
62 ;;
63 NetBSD)
64 GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)"
65 ;;
66 OpenBSD)
67 GETOPT="$(command -v gnugetopt || true)"
68 ;;
69 Darwin)
70 GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)"
71 ;;
72 *)
73 GETOPT="$(command -v getopt || true)"
74 ;;
75 esac
76 if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then
77 echo >&2 "ERROR: Enhanced/GNU getopt is required!"
78 exit 1
79 fi
80 options="$(${GETOPT} -o qwhv --long quiet,verbose,help,version -a -- "$@")"
81 # shellcheck disable=SC2181
82 if [[ $? -ne 0 ]]; then
83 usage 1
84 fi
85 eval set -- "${options}"
86
87 VERBOSE=0
88 while [ $# -gt 0 ] ; do
89 case "$1" in
90 -q | --quiet)
91 VERBOSE=0
92 ;;
93 -w | --verbose)
94 VERBOSE=1
95 ;;
96 -h | --help)
97 usage 0
98 ;;
99 -v | --version)
100 version
101 ;;
102 --)
103 shift
104 break
105 ;;
106 *)
107 # This should not happen: wrong getopt parameters, or missing case?
108 echo >&2 "INTERNAL ERROR: Unhandled option $1!"
109 exit 1
110 ;;
111 esac
112 shift
113 done
114 if [ $# -ne 2 ] ; then
115 usage 1
116 fi
117
118 COLLECTOR="$1"
119 NODE_ID="$2"
120
121 NODE_HIPERCONTRACER_USER="hipercontracer"
122 NODE_HIPERCONTRACER_DIRECTORY="/var/hipercontracer"
123 COLLECTOR_HIPERCONTRACER_USER="hipercontracer"
124 COLLECTOR_HIPERCONTRACER_GROUP="hpct-nodes"
125 COLLECTOR_HIPERCONTRACER_DIRECTORY="/var/hipercontracer"
126
127 KEY_TYPE="ed25519"
128 PRIVATE_KEY="${NODE_HIPERCONTRACER_DIRECTORY}/ssh/id_${KEY_TYPE}"
129 PUBLIC_KEY="${NODE_HIPERCONTRACER_DIRECTORY}/ssh/id_${KEY_TYPE}.pub"
130 KNOWN_HOSTS="${NODE_HIPERCONTRACER_DIRECTORY}/ssh/known_hosts"
131
132 # This is the public key of the SSH server on this machine:
133 # It will be added to known_hosts in the collector!
134 SERVER_KEY="/etc/ssh/ssh_host_ed25519_key.pub"
135
136 if [[ ! "${NODE_ID}" =~ ^[0-9]+$ ]] ; then
137 echo >&2 "ERROR: Invalid Node ID ${NODE_ID}!"
138 exit 1
139 fi
140 if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then
141 echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!"
142 exit 1
143 fi
144 if [ "${COLLECTOR}" == "" ] ; then
145 echo >&2 "ERROR: No collector specified!"
146 exit 1
147 fi
148
149
150 # ====== Create server key ==================================================
151 if [ ! -e "${SERVER_KEY}" ] ; then
152 echo >&2 "ERROR: The server public key file ${SERVER_KEY} does not exist!"
153 exit 1
154 fi
155
156 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Checking local key pair ${PRIVATE_KEY} / ${PUBLIC_KEY} of Node ${NODE_ID} ...\x1b[0m"
157 directory="$(dirname ${PRIVATE_KEY})"
158 if sudo -u "${NODE_HIPERCONTRACER_USER}" test ! -d "${directory}" ; then
159 echo >&2 "ERROR: Key directory ${directory} does not exist!"
160 exit 1
161 fi
162 if sudo -u "${NODE_HIPERCONTRACER_USER}" test ! -e ${PRIVATE_KEY} || \
163 sudo -u "${NODE_HIPERCONTRACER_USER}" test ! -e ${PUBLIC_KEY} ; then
164 echo "Generating key pair ${PRIVATE_KEY} / ${PUBLIC_KEY} ..."
165 if ! sudo -u "${NODE_HIPERCONTRACER_USER}" ssh-keygen -f ${PRIVATE_KEY} -t ${KEY_TYPE} -P "" ; then
166 echo >&2 "ERROR: Failed to create key pair!"
167 exit 1
168 fi
169 if sudo -u "${NODE_HIPERCONTRACER_USER}" test ! -e ${PRIVATE_KEY} || \
170 sudo -u "${NODE_HIPERCONTRACER_USER}" test ! -e ${PUBLIC_KEY} ; then
171 echo >&2 "ERROR: Key pair files missing after generation"
172 exit 1
173 fi
174 sudo -u "${NODE_HIPERCONTRACER_USER}" chmod 600 ${PRIVATE_KEY} ${PUBLIC_KEY}
175 if sudo -u "${NODE_HIPERCONTRACER_USER}" test -e "${KNOWN_HOSTS}" ; then
176 sudo -u "${NODE_HIPERCONTRACER_USER}" chmod 600 "${KNOWN_HOSTS}"
177 fi
178 else
179 echo "Key pair ${PRIVATE_KEY} / ${PUBLIC_KEY} is already there, using it."
180 fi
181
182
183 # ====== Perform configuration of the node on the collector =================
184 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Checking access of Node ${NODE_ID} to collector ${COLLECTOR} ...\x1b[0m"
185 echo "*** This session needs sudo permissions *on the collector* to set up the node user and directories there! ***"
186
187 user="node${NODE_ID}"
188 port=$((10000+NODE_ID))
189 nodePublicKey="$(sudo -u "${NODE_HIPERCONTRACER_USER}" cat /etc/ssh/ssh_host_ed25519_key.pub)"
190 userPublicKey="$(sudo -u "${NODE_HIPERCONTRACER_USER}" cat ${PUBLIC_KEY})"
191
192 sudo -u "${NODE_HIPERCONTRACER_USER}" ssh -t -oUserKnownHostsFile=${KNOWN_HOSTS} "${COLLECTOR}" "
193 if [ ${VERBOSE} -eq 1 ] ; then set -eux ; else set -eu ; fi && mkdir -p keys && \\
194 echo \"${userPublicKey}\" >keys/node${NODE_ID}.pub.new && \\
195 if [ -e \"keys/node${NODE_ID}.pub\" ] ; then
196 if ! diff \"keys/node${NODE_ID}.pub.new\" \"keys/node${NODE_ID}.pub\" ; then \\
197 echo >&2 \"ERROR: There is already a different key for this node!\" && \\
198 echo >&2 \" Check file keys/node${NODE_ID}.pub on ${COLLECTOR}!\" && \\
199 exit 2 ; \\
200 else \\
201 echo -e \"\\x1b[33mKey node${NODE_ID}.pub already there.\\x1b[0m\" && \\
202 rm -f \"keys/node${NODE_ID}.pub.new\" ; \\
203 fi
204 else
205 echo -e \"\\x1b[33mPublic key needs to be copied ...\\x1b[0m\" && \\
206 mv \"keys/node${NODE_ID}.pub.new\" \"keys/node${NODE_ID}.pub\" ; \\
207 fi && \\
208 echo -e \"\\x1b[33mAdding server public key to .ssh/known_hosts of \${USER} ...\\x1b[0m\"
209 if [ ! -e .ssh/known_hosts ] ; then \\
210 mkdir -p .ssh && \\
211 chmod 700 .ssh && \\
212 touch .ssh/known_hosts && \\
213 chmod 600 .ssh/known_hosts ; \\
214 fi && \\
215 ssh-keygen -q -R \"localhost:${port}\" -f .ssh/known_hosts >/dev/null 2>&1 || ssh-keygen -q -R \"localhost:${port}\" -f .ssh/known_hosts && \\
216 echo \"localhost:${port} ${nodePublicKey}\" >>.ssh/known_hosts && \\
217 ssh-keygen -q -H -f .ssh/known_hosts >/dev/null 2>&1 || ssh-keygen -q -H -f .ssh/known_hosts && \\
218 if ! getent group \"${user}\" >/dev/null 2>&1; then
219 echo -e \"\\x1b[33mCreating group ${user} ...\\x1b[0m\" && \\
220 if [ \"\$(uname)\" != \"FreeBSD\" ] ; then \\
221 sudo groupadd \"${user}\" ; \\
222 else \\
223 sudo pw group add \"${user}\" ; \\
224 fi ; \\
225 else \\
226 echo -e \"\\x1b[33mGroup ${user} is already there.\\x1b[0m\" ; \\
227 fi && \\
228 if ! getent passwd \"${user}\" >/dev/null 2>&1; then
229 echo -e \"\\x1b[33mCreating user ${user} ...\\x1b[0m\" && \\
230 sudo mkdir -p \"/home/${user}/.ssh\" && \\
231 sudo chmod 700 \"/home/${user}\" && \\
232 sudo chmod 700 \"/home/${user}/.ssh\" && \\
233 sudo cp \"keys/node${NODE_ID}.pub\" \"/home/${user}/.ssh/authorized_keys\" && \\
234 sudo chmod 600 \"/home/${user}/.ssh/authorized_keys\" && \\
235 if [ \"\$(uname)\" != \"FreeBSD\" ] ; then \\
236 sudo useradd -M -g \"${user}\" -d \"/home/${user}\" -s /bin/sh -c \"HiPerConTracer Node ${NODE_ID}\" \"${user}\" ; \\
237 else \\
238 sudo pw useradd \"${user}\" -d \"/home/${user}\" -s /bin/sh -c \"HiPerConTracer Node ${NODE_ID}\" ; \\
239 fi && \\
240 sudo chown \"${user}:${user}\" \"/home/${user}\" && \\
241 sudo chown \"${user}:${user}\" \"/home/${user}/.ssh\" && \\
242 sudo chown \"${user}:${user}\" \"/home/${user}/.ssh/authorized_keys\" ; \\
243 else
244 echo -e \"\\x1b[33mUser ${user} is already there.\\x1b[0m\" ; \\
245 fi && \\
246 echo -e \"\\x1b[33mCreating data directory ${COLLECTOR_HIPERCONTRACER_DIRECTORY}/data/${user} for user ${user} ...\\x1b[0m\" && \\
247 sudo mkdir -p \"${COLLECTOR_HIPERCONTRACER_DIRECTORY}/data/${user}\" && \\
248 sudo chown -R \"${user}:${COLLECTOR_HIPERCONTRACER_GROUP}\" \"${COLLECTOR_HIPERCONTRACER_DIRECTORY}/data/${user}\" && \\
249 sudo chmod 750 \"${COLLECTOR_HIPERCONTRACER_DIRECTORY}/data/${user}\" && \\
250 echo -e \"\\x1b[33mAllowing access to ${COLLECTOR_HIPERCONTRACER_DIRECTORY}/data/${user} for user ${COLLECTOR_HIPERCONTRACER_USER} (rw) and group ${COLLECTOR_HIPERCONTRACER_DIRECTORY} (ro) via ACLs ...\\x1b[0m\" && \\
251 if [ \"\$(uname)\" != \"FreeBSD\" ] ; then \\
252 sudo setfacl -Rm d:u:${COLLECTOR_HIPERCONTRACER_USER}:rwx,u:${COLLECTOR_HIPERCONTRACER_USER}:rwx,d:g:${COLLECTOR_HIPERCONTRACER_GROUP}:rx,g:${COLLECTOR_HIPERCONTRACER_GROUP}:rx \"${COLLECTOR_HIPERCONTRACER_DIRECTORY}/data/${user}\" ; \\
253 else \\
254 sudo setfacl -Rm u:${COLLECTOR_HIPERCONTRACER_USER}:modify_set:file_inherit/dir_inherit:allow,g:${COLLECTOR_HIPERCONTRACER_GROUP}:read_set:file_inherit/dir_inherit:allow \"${COLLECTOR_HIPERCONTRACER_DIRECTORY}/data/${user}\" ; \\
255 fi && \\
256 echo -e \"\\x1b[33mDone!\\x1b[0m\"
257 " && status=0 || status=$?
258 if [ "${status}" -ne 0 ] ; then
259 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") FAILED!\x1b[0m"
260 exit 1
261 fi
262
263
264 # ====== Test connectivity ==================================================
265 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Testing connectivity to ${COLLECTOR} ...\x1b[0m"
266 collector="${COLLECTOR//*@/}"
267 sudo -u "${NODE_HIPERCONTRACER_USER}" ssh -t -oUserKnownHostsFile=${KNOWN_HOSTS} -i "${PRIVATE_KEY}" "node${NODE_ID}@${collector}" "
268 hostname -f
269 " && status=0 || status=$?
270 if [ "${status}" -ne 0 ] ; then
271 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") FAILED!\x1b[0m"
272 exit 1
273 fi
274
275
276 # ====== Update HPCT Sync configuration =====================================
277 if [ -e /etc/hipercontracer/hpct-sync.conf ] ; then
278 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Updating /etc/hipercontracer/hpct-sync.conf ...\x1b[0m"
279 echo "This needs sudo permission to modify /etc/hipercontracer/hpct-sync.conf!"
280 collector="${COLLECTOR//*@/}"
281 sudo cat /etc/hipercontracer/hpct-sync.conf | \
282 sed \
283 -e "s/^NODE_ID=.*$/NODE_ID=${NODE_ID}/g" \
284 -e "s/^COLLECTOR=.*$/COLLECTOR=${collector}/g" | \
285 sudo tee /etc/hipercontracer/hpct-sync.conf.new >/dev/null
286 sudo diff --color=always /etc/hipercontracer/hpct-sync.conf.new /etc/hipercontracer/hpct-sync.conf || true
287 sudo mv /etc/hipercontracer/hpct-sync.conf.new /etc/hipercontracer/hpct-sync.conf
288 fi
289
290
291 # ====== Enable and start HPCT Sync and HPCT RTunnel ========================
292 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Enabling and starting HPCT RTunnel and HPCT Sync ...\x1b[0m"
293 if [ -x /usr/bin/systemctl ] ; then
294 echo "*** This session needs sudo permissions *on this machine* to to enable and start the services! ***"
295 # Not enabling HiPerConTracer service here, since it has to be enabled based on measurement ID(s)!
296 sudo systemctl stop hpct-rtunnel.service
297 sudo systemctl enable hpct-rtunnel.service
298 sudo systemctl start hpct-rtunnel.service
299 sudo systemctl stop hpct-sync.timer
300 sudo systemctl enable hpct-sync.timer
301 sudo systemctl start hpct-sync.timer
302 else
303 echo "FIXME! TBD on systems without systemctl!"
304 fi
305
306 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m"