Debian Codegraph @

src/hpct-node-setup

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