Update getenr (#127)

* Update bash script

* Update build script version

* Update README.md

* Addressed review comments
This commit is contained in:
Alberto Soutullo
2025-08-13 17:36:40 +02:00
committed by GitHub
parent 2be0c1f05a
commit 4ded69bdfc
3 changed files with 63 additions and 26 deletions

View File

@@ -16,7 +16,7 @@ and then we use this container to retrieve their ENRs.
### Usage:
```
./getenr.sh [NUM_ENRS] [SERVICE_NAME]
./getenr.sh [NUM_ENRS] [SERVICE_NAME] [OUTPUT_FILE]
```
### Arguments:
@@ -24,12 +24,14 @@ and then we use this container to retrieve their ENRs.
Defaults to `3` if not specified.
- `SERVICE_NAME` (optional) The Kubernetes service name
to query for pod IPs. Defaults to `zerotesting-bootstrap.zerotesting`.
- `OUTPUT_FILE` (optional) Output file. Enables to retrieve multiple ENRS from different services
(ie bootstrap, store,...). Defaults to `/etc/enr/ENR`.
### Example in Kubernetes yaml
```
initContainers:
- name: grabenr
image: <your-registry>/getenr:v1.0.0
image: <your-registry>/getenr:v1.1.0
imagePullPolicy: IfNotPresent
volumeMounts:
- name: enr-data
@@ -38,6 +40,8 @@ initContainers:
- /app/getenr.sh
args:
- "3"
- "status-service-bootstrap.status-go-test"
- "/etc/enr/ENR"
...
@@ -45,11 +49,39 @@ command:
- sh
- -c
- |
. /etc/enr/enr.env
. /etc/enr/ENR
echo ENRs are $ENR1 $ENR2 $ENR3
/usr/bin/wakunode --discv5-bootstrap-node=$ENR1
```
### Example in Kubernetes yaml using multiple services
```
initContainers:
- name: grabenr
image: <your-registry>/getenr:v1.1.0
imagePullPolicy: IfNotPresent
volumeMounts:
- name: enr-data
mountPath: /etc/enr
command: ["/bin/sh", "-c"]
args:
- |
/app/getenr.sh 3 status-service-bootstrap.status-go-test /etc/enr/BOOT_ENRS && \
/app/getenr.sh 3 status-service-node.status-go-test /etc/enr/STORE_ENRS
...
command:
- sh
- -c
- |
set -a
source /etc/enr/BOOT_ENRS
source /etc/enr/STORE_ENRS
set +a
...
```
### How It Works
1. **Fetch Pod IPs**: Uses nslookup to find the IPv4 addresses
@@ -57,15 +89,21 @@ of the specified service.
2. **Query Each Pod**: Sends an HTTP request
to each pod at port `8645`,
retrieving the enrUri from its debug API.
3. **Validate Addresses**: Ensures that the retrieved ENR
3. **Validate ENRs**: Ensures that the retrieved ENR
start with `enr:`, indicating a valid ENR format.
4. **Store Valid Addresses**: Saves valid addresses
as environment variables in `/etc/enr/enr.env`.
4. **Store Valid Addresses**: Saves valid ENRs
as environment variables in default file `/etc/enr/ENR`.
### Output
If successful, the script stores addresses in `/etc/addrs/addrs.env`
If successful, the script stores ENRs in default file `/etc/enr/ENR`
as environment variables:
```
export enr1='enr:-MS4QGcHBZAnpu6qNYe_T6TGDCV6c9_3UsXlj5XlXY6QvLCUQKqajqDfs0aKOs7BISJzGxA7TuDzYXap4sP6JYUZ2Y9GAYh2F0dG5ldHOIAAAAAAAAAACEZXRoMpEJZZp0BAAAAf__________gmlkgnY0gmlwhC5QoeSJc2VjcDI1NmsxoQOZxJYJVoTfwo7zEom6U6L5Txrs3H9X0P_XBJbbOZBczYYN1ZHCCdl8'
export ENR1='enr:-MS4QGcHBZAnpu6qNYe_T6TGDCV6c9_3UsXlj5XlXY6QvLCUQKqajqDfs0aKOs7BISJzGxA7TuDzYXap4sP6JYUZ2Y9GAYh2F0dG5ldHOIAAAAAAAAAACEZXRoMpEJZZp0BAAAAf__________gmlkgnY0gmlwhC5QoeSJc2VjcDI1NmsxoQOZxJYJVoTfwo7zEom6U6L5Txrs3H9X0P_XBJbbOZBczYYN1ZHCCdl8'
```
### Changelog:
- 1.1.0
- Added third variable to output result to given file.
- Environmental variables are renamed from `ENR1=...`, `ENR2=...` and so on to `<file_name>1=...`, `<file_name>2=...`
- Default values remain unchanged

View File

@@ -1,2 +1,2 @@
docker build -t <your-registry>/getenr:v1.0.0 .
docker push <your-registry>/getenr:v1.0.0
docker build -t <your-registry>/getenr:v1.1.0 .
docker push <your-registry>/getenr:v1.1.0

View File

@@ -6,15 +6,16 @@ num_enrs=${1:-3}
# Service name to query, default to "zerotesting-bootstrap.zerotesting" if not specified
service_name=${2:-zerotesting-bootstrap.zerotesting}
# Find the IPv4 IPs of "zerotesting-bootstrap.zerotesting" using nslookup
readarray -t pod_ips < <(nslookup "$service_name" | awk '/^Address: / { print $2 }' | head -n "$num_enrs")
# Output file for the ENR data, default to "/etc/enr/ENR" if not specified
output_file=${3:-/etc/enr/ENR}
# Prepare the directory for ENR data
mkdir -p /etc/enr
enr_file="/etc/enr/enr.env"
> "$enr_file" # Clear the file to start fresh
# Ensure the directory for the output file exists
mkdir -p "$(dirname "$output_file")"
> "$output_file" # Clear the file to start fresh
# Extract basename
base_name=$(basename "$output_file")
# Function to validate ENR
validate_enr() {
if [[ $1 =~ ^enr:- ]]; then
return 0 # Valid
@@ -23,7 +24,9 @@ validate_enr() {
fi
}
# Counter for valid ENRs
# Find the IPv4 IPs of the service using nslookup
readarray -t pod_ips < <(nslookup "$service_name" | awk '/^Address: / { print $2 }' | head -n "$num_enrs")
valid_enr_count=0
# Get and validate the ENR data from up to the specified number of IPs
@@ -31,26 +34,22 @@ for pod_ip in "${pod_ips[@]}"; do
echo "Querying IP: $pod_ip"
enr=$(curl -X GET "http://$pod_ip:8645/debug/v1/info" -H "accept: application/json" | sed -n 's/.*"enrUri":"\([^"]*\)".*/\1/p')
# Validate the ENR
validate_enr "$enr"
if [ $? -eq 0 ]; then
# Save the valid ENR to the file
((valid_enr_count++))
echo "export ENR$valid_enr_count='$enr'" >> "$enr_file"
echo "export ${base_name}${valid_enr_count}='$enr'" >> "$output_file"
if [ $valid_enr_count -eq "$num_enrs" ]; then
break # Exit loop after the specified number of valid ENRs
break
fi
else
echo "Invalid ENR data received from IP $pod_ip"
fi
done
# Check if we got at least one valid ENR
if [ $valid_enr_count -eq 0 ]; then
echo "No valid ENR data received from any IPs"
exit 1
fi
# Output for debugging
echo "ENR data saved successfully:"
cat "$enr_file"
echo "ENR data saved successfully to $output_file:"
cat "$output_file"