2023-09-15 14:27:38 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2023-10-30 14:51:40 +00:00
|
|
|
# LIBP2P_NODE_MASK is set via compose.yml file.
|
2023-09-15 14:27:38 +00:00
|
|
|
node_key_from_id() {
|
2023-10-30 14:51:40 +00:00
|
|
|
echo "${LIBP2P_NODE_MASK}" | sed "s/.\{${#NODE_ID}\}$/${NODE_ID}/"
|
2023-09-15 14:27:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 11:06:40 +00:00
|
|
|
END=$LIBP2P_REPLICAS
|
2023-09-15 14:27:38 +00:00
|
|
|
NODE_ID=1
|
|
|
|
NODE_IP=$(hostname -i)
|
|
|
|
NODE_KEY=$(node_key_from_id)
|
|
|
|
|
|
|
|
register_node() {
|
|
|
|
## Conditional transaction to set node config key if it doesn't exist.
|
|
|
|
## Newlines in EOF block are important, more info here:
|
|
|
|
## https://github.com/etcd-io/etcd/tree/main/etcdctl#examples-3
|
|
|
|
etcdctl txn <<EOF
|
|
|
|
mod("/node/${NODE_ID}") = "0"
|
|
|
|
|
|
|
|
put /node/${NODE_ID} "${NODE_ID}"
|
|
|
|
put /config/node/${NODE_ID}/key "${NODE_KEY}"
|
|
|
|
put /config/node/${NODE_ID}/ip "${NODE_IP}"
|
|
|
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
while [ "${NODE_ID}" -le "${END}" ]; do
|
|
|
|
result=$(register_node)
|
|
|
|
|
|
|
|
# Check if the key was registered or already exists
|
|
|
|
if [ "${result}" != "FAILURE" ]; then
|
|
|
|
break
|
|
|
|
else
|
|
|
|
NODE_ID=$((NODE_ID + 1))
|
|
|
|
NODE_KEY=$(node_key_from_id)
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "${NODE_ID}" -gt "${END}" ]; then
|
|
|
|
echo "Reached the limit without registering a ${NODE_ID}."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "${NODE_KEY}"
|