Skip to content

Commit 2bf52af

Browse files
authored
Merge pull request #4 from EspressoSystems/li/setup
Add `docker-compose.yml` and fix shutdow script
2 parents 9c6e43f + 904e1b7 commit 2bf52af

File tree

6 files changed

+77
-51
lines changed

6 files changed

+77
-51
lines changed

docker/aws-nitro-entrypoint.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ set -e
55
echo "Using config hash: $EXPECTED_CONFIG_SHA256"
66

77
ENCLAVE_CONFIG_SOURCE_DIR=/mnt/config # temporary mounted directory in enclave to read config from parent instance
8-
PARENT_SOURCE_CONFIG_DIR=/home/ec2-user/config # config path on parent directory
8+
PARENT_SOURCE_CONFIG_DIR=/opt/nitro/config # config path on parent directory
99
ENCLAVE_CONFIG_TARGET_DIR=/config # directory to copy config contents to inside enclave
10-
PARENT_SOURCE_DB_DIR=/home/ec2-user/.arbitrum # database path on parent directory
10+
PARENT_SOURCE_DB_DIR=/opt/nitro/arbitrum # database path on parent directory
1111

1212
echo "Start vsock proxy"
13-
socat TCP-LISTEN:2049,bind=127.0.0.1,fork,reuseaddr,keepalive VSOCK-CONNECT:3:8004,keepalive &
13+
socat TCP-LISTEN:2049,bind=127.0.0.1,fork,reuseaddr,keepalive VSOCK-CONNECT:3:8004,keepalive >/dev/null 2>&1 &
1414
sleep 2
1515

1616
echo "Mount config from ${PARENT_SOURCE_CONFIG_DIR} to ${ENCLAVE_CONFIG_SOURCE_DIR}"
@@ -70,7 +70,6 @@ start_vsock_termination_server() {
7070
if [ "$message" = "TERMINATE" ]; then
7171
echo "Received TERMINATE signal"
7272
pkill -INT -f "/usr/local/bin/nitro"
73-
break
7473
else
7574
echo "Ignoring message: $message"
7675
fi
@@ -80,6 +79,8 @@ start_vsock_termination_server() {
8079

8180
start_vsock_termination_server &
8281

82+
sleep 5
83+
8384
exec /usr/local/bin/nitro \
8485
--validation.wasm.enable-wasmroots-check=false \
8586
--conf.file "${ENCLAVE_CONFIG_TARGET_DIR}/poster_config.json"

docker/docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
enclave-batch-poster:
3+
image: # ghcr.io/espressosystems/aws-nitro-poster:<docker-tag>
4+
devices:
5+
- "/dev/nitro_enclaves:/dev/nitro_enclaves:rwm"
6+
ports:
7+
- "8547:8547"
8+
- "8548:8548"
9+
privileged: true

enclaver/enclaver.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ sources:
55
app: "nitro-image:latest"
66
defaults:
77
memory_mb: 8192
8-
cpu_count: 4
8+
cpu_count: 2
99
egress:
1010
allow:
1111
- "*"
1212
- "**"
13-
- 0.0.0.0/0
14-
ingress:
15-
- listen_port: 8547
13+
- 0.0.0.0/0

scripts/installation-tools.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ sudo systemctl enable docker || { echo "ERROR: Failed to enable docker"; exit 1;
1414
sudo systemctl start docker || { echo "ERROR: Failed to start docker"; exit 1; }
1515
sudo usermod -aG docker ec2-user || echo "WARNING: Failed to add user to docker group"
1616

17+
# Download docker compose to bin
18+
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
19+
20+
# Give execute permission
21+
sudo chmod +x /usr/local/bin/docker-compose
22+
1723
# Install enclaver
1824
echo "Downloading and installing Enclaver..."
1925
ARCH=$(uname -m)

scripts/setup-ec2-instance.sh

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,38 @@ set -e
55

66
# Setup Arbitrum directory
77
echo "Setting up Arbitrum db directory..."
8-
mkdir -p /home/ec2-user/.arbitrum || { echo "Failed to create .arbitrum directory"; exit 1; }
9-
sudo chown -R ec2-user:ec2-user /home/ec2-user/.arbitrum || { echo "Failed to set permissions for .arbitrum"; exit 1; }
8+
mkdir -p /opt/nitro/arbitrum || { echo "Failed to create .arbitrum directory"; exit 1; }
9+
sudo chown -R ec2-user:ec2-user /opt/nitro/arbitrum || { echo "Failed to set permissions for .arbitrum"; exit 1; }
1010

1111
# Setup config directory
1212
echo "Setting up config directory..."
13-
mkdir -p /home/ec2-user/config || { echo "Failed to create config directory"; exit 1; }
14-
sudo chown -R ec2-user:ec2-user /home/ec2-user/config || { echo "Failed to set permissions for config"; exit 1; }
15-
16-
# Start socat proxy in background with logging
13+
mkdir -p /opt/nitro/config || { echo "Failed to create config directory"; exit 1; }
14+
sudo chown -R ec2-user:ec2-user /opt/nitro/config || { echo "Failed to set permissions for config"; exit 1; }
15+
16+
# Create systemd service for socat
17+
echo "Creating systemd service for socat..."
18+
sudo bash -c 'cat << EOF > /etc/systemd/system/socat-vsock.service
19+
[Unit]
20+
Description=socat VSOCK to TCP proxy
21+
After=network.target nfs-server.service
22+
23+
[Service]
24+
ExecStart=/usr/bin/socat -d -d VSOCK-LISTEN:8004,fork,keepalive TCP:127.0.0.1:2049,keepalive,retry=5,interval=10
25+
Restart=always
26+
RestartSec=10
27+
28+
[Install]
29+
WantedBy=multi-user.target
30+
EOF' || { echo "Failed to create socat systemd service file"; exit 1; }
31+
32+
# Enable and start socat service
1733
echo "Starting socat proxy..."
18-
sudo socat VSOCK-LISTEN:8004,fork,keepalive TCP:127.0.0.1:2049,keepalive &
34+
sudo systemctl enable socat-vsock.service || { echo "Failed to enable socat service"; exit 1; }
35+
sudo systemctl start socat-vsock.service || { echo "Failed to start socat service"; exit 1; }
1936

2037
# Configure NFS exports
21-
echo "/home/ec2-user/.arbitrum 127.0.0.1/32(rw,insecure,crossmnt,no_subtree_check,sync,all_squash,anonuid=1000,anongid=1000)" | sudo tee -a /etc/exports || { echo "Failed to configure NFS exports"; exit 1; }
22-
echo "/home/ec2-user/config 127.0.0.1/32(ro,insecure,crossmnt,no_subtree_check,sync,all_squash,anonuid=1000,anongid=1000)" | sudo tee -a /etc/exports || { echo "Failed to configure NFS exports"; exit 1; }
38+
echo "/opt/nitro/arbitrum 127.0.0.1/32(rw,insecure,crossmnt,no_subtree_check,sync,all_squash,anonuid=1000,anongid=1000)" | sudo tee -a /etc/exports || { echo "Failed to configure NFS exports"; exit 1; }
39+
echo "/opt/nitro/config 127.0.0.1/32(ro,insecure,crossmnt,no_subtree_check,sync,all_squash,anonuid=1000,anongid=1000)" | sudo tee -a /etc/exports || { echo "Failed to configure NFS exports"; exit 1; }
2340
sudo exportfs -ra || { echo "Failed to reload NFS exports"; exit 1; }
2441

2542
# Enable and start NFS server

scripts/shutdown-batch-poster.sh

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
1+
#!/bin/bash
2+
13
MESSAGE="TERMINATE"
24
PORT=8005
3-
echo "Starting VSOCK connection attempts at CID $CID, port $PORT..."
4-
CID=16
5-
MAX_CID=100
6-
# When running inside the enclaver it is not possible to get enclave context id
7-
# Which is why we have to have this script incrementing the $CID
8-
# See issue: https://github.com/enclaver-io/enclaver/issues/215
9-
while [ $CID -le $MAX_CID ]; do
10-
echo "Trying CID $CID..."
11-
12-
# Run socat and capture output and exit status
13-
OUTPUT=$(echo "$MESSAGE" | socat - VSOCK-CONNECT:$CID:$PORT 2>&1)
14-
EXIT_STATUS=$?
15-
16-
# Check if the output contains "Connection timed out"
17-
if echo "$OUTPUT" | grep -q "Connection timed out"; then
18-
echo "Connection timed out for CID $CID: $OUTPUT"
19-
# Increment CID and continue
20-
CID=$((CID + 1))
21-
else
22-
# Success or different error
23-
echo "Connection attempt for CID $CID completed with exit status $EXIT_STATUS"
24-
echo "Output: $OUTPUT"
25-
if [ $EXIT_STATUS -eq 0 ]; then
26-
echo "Success: Connected to CID $CID, port $PORT"
27-
break
28-
else
29-
echo "Non-timeout error occurred for CID $CID. Stopping."
30-
break
31-
fi
32-
fi
33-
done
345

35-
if [ $CID -gt $MAX_CID ]; then
36-
echo "Reached maximum CID ($MAX_CID) without success."
6+
# Get the latest CID from journal logs
7+
CID=$(sudo journalctl -u socat.service -n 50 --no-pager | \
8+
grep -oP 'accepting connection from AF=40 cid:\K\d+' | \
9+
tail -n 1 | \
10+
tr -d '[:space:]')
11+
12+
# Validate CID
13+
if [[ ! "$CID" =~ ^[0-9]+$ ]]; then
14+
echo "Error: No valid CID found in socat.service logs"
3715
exit 1
3816
fi
3917

40-
exit 0
18+
echo "Attempting VSOCK connection to CID $CID, port $PORT..."
19+
20+
# Run socat and capture output and exit status
21+
OUTPUT=$(echo "$MESSAGE" | socat - VSOCK-CONNECT:$CID:$PORT 2>&1)
22+
EXIT_STATUS=$?
23+
24+
# Handle connection results
25+
if echo "$OUTPUT" | grep -q "Connection timed out"; then
26+
echo "Connection timed out for CID $CID: $OUTPUT"
27+
exit 1
28+
elif [ $EXIT_STATUS -eq 0 ]; then
29+
echo "Success: Connected to CID $CID, port $PORT"
30+
exit 0
31+
else
32+
echo "Error: Connection failed for CID $CID (Exit Status: $EXIT_STATUS)"
33+
echo "Output: $OUTPUT"
34+
exit 1
35+
fi

0 commit comments

Comments
 (0)