Skip to content

Commit 3f73d1b

Browse files
committed
Base. Debugging snapshot download
1 parent 05bcdd6 commit 3f73d1b

File tree

4 files changed

+124
-69
lines changed

4 files changed

+124
-69
lines changed

lib/base/lib/assets/base/node.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -e
3+
echo "Script is starting..."
4+
ulimit -n 500000
5+
6+
# Start the node
7+
cd /home/bcuser/node
8+
/usr/local/bin/docker-compose -f /home/bcuser/node/docker-compose.yml up -d
9+
10+
echo "Started"

lib/base/lib/assets/download-snapshot.sh

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
set +e
3+
4+
source /etc/environment
5+
6+
echo "Downloading Snapshot."
7+
8+
cd /data
9+
10+
SNAPSHOT_FILE_NAME=snapshot.tar.gz
11+
SNAPSHOT_DIR=/data
12+
SNAPSHOT_DOWNLOAD_STATUS=-1
13+
14+
if [ "$SNAPSHOT_URL" == "none" ] || [ -z "${SNAPSHOT_URL}" ]; then
15+
LATEST_SNAPSHOT_FILE_NAME=$(curl https://$NETWORK_ID-$NODE_CONFIG-snapshots.base.org/latest)
16+
SNAPSHOT_URL=https://$NETWORK_ID-$NODE_CONFIG-snapshots.base.org/$LATEST_SNAPSHOT_FILE_NAME
17+
fi
18+
19+
# take about 1 hour to download the Snapshot
20+
while (( SNAPSHOT_DOWNLOAD_STATUS != 0 ))
21+
do
22+
PIDS=$(pgrep aria2c)
23+
if [ -z "$PIDS" ]; then
24+
aria2c $SNAPSHOT_URL -d $SNAPSHOT_DIR -o $SNAPSHOT_FILE_NAME -l /data/download.log --log-level=notice --allow-overwrite=true --allow-piece-length-change=true
25+
fi
26+
SNAPSHOT_DOWNLOAD_STATUS=$?
27+
pid=$(pidof aria2c)
28+
wait $pid
29+
echo "aria2c exit."
30+
case $SNAPSHOT_DOWNLOAD_STATUS in
31+
3)
32+
echo "File does not exist."
33+
exit 3
34+
;;
35+
9)
36+
echo "No space left on device."
37+
exit 9
38+
;;
39+
*)
40+
continue
41+
;;
42+
esac
43+
done
44+
echo "Downloading Snapshot script finished"
45+
46+
sleep 60
47+
48+
echo "Starting snapshot decompression ..."
49+
50+
tar -zxvf $SNAPSHOT_DIR/$SNAPSHOT_FILE_NAME -C /data 2>&1 | tee unzip.log && echo "decompresed successfully..." || echo "decompression failed..." >> snapshots-decompression.log
51+
52+
echo "Decompresed snapshot, cleaning up..."
53+
54+
mv /data/snapshots/$NETWORK_ID/download/* /data && \
55+
rm -rf /data/snapshots && \
56+
rm -rf $SNAPSHOT_DIR/$SNAPSHOT_FILE_NAME
57+
58+
echo "Snapshot is ready, starting the service.."
59+
60+
chown -R bcuser:bcuser /data
61+
62+
sudo systemctl daemon-reload
63+
sudo systemctl enable --now base

lib/base/lib/assets/user-data/node.sh

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ yum update -y
3333
yum -y install amazon-cloudwatch-agent collectd jq yq gcc ncurses-devel aws-cfn-bootstrap zstd
3434
wget $YQ_URI -O /usr/bin/yq && chmod +x /usr/bin/yq
3535

36+
# install aria2 a p2p downloader
37+
38+
if [ "$arch" == "x86_64" ]; then
39+
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
40+
tar jxvf aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
41+
cd aria2-1.36.0-linux-gnu-64bit-build1/
42+
make install
43+
else
44+
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
45+
tar jxvf aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
46+
cd aria2-1.36.0-linux-gnu-arm-rbpi-build1/
47+
make install
48+
fi
49+
3650
cd /opt
3751

3852
echo "Downloading assets zip file"
@@ -187,6 +201,8 @@ case $NETWORK_ID in
187201
;;
188202
esac
189203

204+
echo "OP_NODE_L1_TRUST_RPC=true" >> $OP_CONFIG_FILE_PATH
205+
190206
sed -i "s#GETH_HOST_DATA_DIR=./geth-data#GETH_HOST_DATA_DIR=/data/geth#g" /home/bcuser/node/.env
191207

192208
chown -R bcuser:bcuser /home/bcuser/node
@@ -198,6 +214,27 @@ chmod 766 /opt/syncchecker.sh
198214
echo "*/5 * * * * /opt/syncchecker.sh" | crontab
199215
crontab -l
200216

217+
echo "Configuring node as a service"
218+
mkdir /home/bcuser/bin
219+
mv /opt/base/node.sh /home/bcuser/bin/node.sh
220+
chmod 766 /home/bcuser/bin/node.sh
221+
chown -R bcuser:bcuser /home/bcuser
222+
223+
sudo bash -c 'cat > /etc/systemd/system/base.service <<EOF
224+
[Unit]
225+
Description=Base Node
226+
After=network-online.target
227+
[Service]
228+
Type=simple
229+
Restart=always
230+
RestartSec=30
231+
User=bcuser
232+
Environment="PATH=/bin:/usr/bin:/home/bcuser/bin"
233+
ExecStart=/home/bcuser/bin/node.sh
234+
[Install]
235+
WantedBy=multi-user.target
236+
EOF'
237+
201238
echo "Signaling completion to CloudFormation to continue with volume mount"
202239
/opt/aws/bin/cfn-signal --stack $STACK_NAME --resource $RESOURCE_ID --region $REGION
203240

@@ -235,21 +272,21 @@ chown -R bcuser:bcuser /data
235272
chmod -R 755 /data
236273

237274
if [ "$RESTORE_FROM_SNAPSHOT" == "false" ]; then
238-
echo "Skipping restoration from snapshot. Starting docker-compose in 3 min."
239-
cd /home/bcuser/node
240-
echo "sudo su bcuser && /usr/local/bin/docker-compose -f /home/bcuser/node/docker-compose.yml up -d" | at now +3 minutes
275+
echo "Skipping restoration from snapshot. Starting node"
276+
sudo systemctl daemon-reload
277+
sudo systemctl enable --now base
241278
else
242-
echo "Restoring data from snapshot"
243-
chmod 766 /opt/start-from-snapshot.sh
244-
/opt/download-snapshot.sh
245-
if [ "$?" == 0 ]; then
246-
echo "Snapshot download successful"
247-
else
248-
echo "Snapshot download failed, falling back to fresh sync"
249-
fi
250-
chown -R bcuser:bcuser /data
251-
sudo su bcuser
252-
/usr/local/bin/docker-compose -f /home/bcuser/node/docker-compose.yml up -d
279+
echo "Restoring node from snapshot"
280+
chmod +x /opt/restore-from-snapshot.sh
281+
echo "/opt/restore-from-snapshot.sh" | at now + 1 min
282+
fi
283+
284+
if [[ "$LIFECYCLE_HOOK_NAME" != "none" ]]; then
285+
echo "Signaling ASG lifecycle hook to complete"
286+
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
287+
INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/instance-id)
288+
aws autoscaling complete-lifecycle-action --lifecycle-action-result CONTINUE --instance-id $INSTANCE_ID --lifecycle-hook-name "$LIFECYCLE_HOOK_NAME" --auto-scaling-group-name "$AUTOSCALING_GROUP_NAME" --region $AWS_REGION
253289
fi
254290

255291
echo "All Done!!"
292+
set -e

0 commit comments

Comments
 (0)