Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement automation for archival snapshots #428

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
cf2eb58
Implement new archival service for lite and diff snapshots
sudo-shashank Mar 22, 2024
5ebd15c
Use Ansible
sudo-shashank Mar 26, 2024
47630a4
use env vars
sudo-shashank Mar 26, 2024
c8afdd4
lint fix
sudo-shashank Mar 27, 2024
16b1770
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 27, 2024
bac9bf2
Merge branch 'main' into shashank/archival-snapshots
sudo-shashank Mar 27, 2024
ca24207
add CI workflow
sudo-shashank Apr 1, 2024
d4af8c6
Merge branch 'main' into shashank/archival-snapshots
sudo-shashank Apr 1, 2024
d145f33
setup cron job
sudo-shashank Apr 2, 2024
246a45b
setup pre requisites
sudo-shashank Apr 8, 2024
d8b8719
fix
sudo-shashank Apr 8, 2024
f2cb21d
Merge branch 'main' into shashank/archival-snapshots
sudo-shashank Apr 8, 2024
9cd8cef
fix logs
sudo-shashank Apr 8, 2024
5374561
fix shellcheck
sudo-shashank Apr 8, 2024
98382d2
fix init.sh
sudo-shashank Apr 8, 2024
78e2794
Handle silent failure cases
sudo-shashank Apr 9, 2024
7c998f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 9, 2024
f340688
fmt
sudo-shashank Apr 9, 2024
d40d717
Merge branch 'main' into shashank/archival-snapshots
sudo-shashank Apr 9, 2024
dc58c93
testing ci connection
sudo-shashank Apr 9, 2024
d6ea9a9
testing ci
sudo-shashank Apr 9, 2024
f5151b4
try ci fix
sudo-shashank Apr 9, 2024
1be6808
fix ssh config in ci
sudo-shashank Apr 9, 2024
d94d7bd
fix ssh config
sudo-shashank Apr 9, 2024
44e5fd8
fix ssh config setup
sudo-shashank Apr 9, 2024
33269e8
use correct ssh key var
sudo-shashank Apr 9, 2024
d3bca3f
use web/factory/ssh-agent
sudo-shashank Apr 9, 2024
ff52e7e
test connection with cloudflared proxy
sudo-shashank Apr 9, 2024
52f6270
fix cloudflare installation
sudo-shashank Apr 9, 2024
68a1732
fix cloudflare pkg link
sudo-shashank Apr 9, 2024
63bd263
fix cloudflare pkg installation
sudo-shashank Apr 9, 2024
85932cf
use archie private key
sudo-shashank Apr 9, 2024
e0252f7
fix ssh connection
sudo-shashank Apr 9, 2024
ba4ddd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 9, 2024
9b55ec9
add to known host
sudo-shashank Apr 10, 2024
a7142f5
check ssh connection
sudo-shashank Apr 10, 2024
d8aa928
fix
sudo-shashank Jun 6, 2024
f34ba6f
Merge branch 'main' into shashank/archival-snapshots
sudo-shashank Jun 6, 2024
5f4cd5b
fix forest pkg uzip
sudo-shashank Jun 6, 2024
9bea38f
fix unzip path
sudo-shashank Jun 6, 2024
b732021
cleanup
sudo-shashank Jun 6, 2024
8b0127f
Merge branch 'main' into shashank/archival-snapshots
sudo-shashank Jun 19, 2024
80a9c30
Merge branch 'main' into shashank/archival-snapshots
sudo-shashank Jul 1, 2024
016a9d3
handle script failure
sudo-shashank Jul 2, 2024
3842343
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/deploy-archival-snapshots.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Run Ansible playbook for archival snapshots

on:
schedule:
- cron: '0 0 */10 * *'

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install Ansible
run: |
sudo apt-get update
sudo apt-get install -y ansible

- name: Configure SSH
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -t rsa archie.chainsafe.dev >> ~/.ssh/known_hosts

- name: Run Ansible playbook
env:
ANSIBLE_HOST_KEY_CHECKING: True
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
ARCHIVAL_SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
ENDPOINT: ${{ secrets.CLOUDFLARE_ENDPOINT }}
run: |
ansible-playbook playbook.yml -i inventory.ini
2 changes: 2 additions & 0 deletions ansible/archival-snapshots/inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[remote_server]
archie.chainsafe.dev ansible_user=archie ansible_ssh_private_key_file=~/.ssh/id_rsa
31 changes: 31 additions & 0 deletions ansible/archival-snapshots/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Automate packaging, transferring, and executing a script
hosts: remote_server
vars:
local_resources_path: "resources"
remote_resources_path: "/mnt/md0/exported/archival"
zip_file_name: "resources.zip"
tasks:
- name: Zip the resources folder
ansible.builtin.command:
cmd: "zip -r {{ zip_file_name }} ."
chdir: "{{ local_resources_path }}"
delegate_to: localhost

- name: Transfer the zip file to the remote server
ansible.builtin.copy:
src: "{{ local_resources_path }}/{{ zip_file_name }}"
dest: "{{ remote_resources_path }}/{{ zip_file_name }}"

- name: Unzip the resources folder on the remote server
ansible.builtin.command:
cmd: "unzip {{ zip_file_name }}"
chdir: "{{ remote_resources_path }}"

- name: Execute the init.sh script
ansible.builtin.shell:
cmd: "./init.sh"
chdir: "{{ remote_resources_path }}"
environment:
ARCHIVAL_SLACK_TOKEN: "{{ lookup('env', 'ARCHIVAL_SLACK_TOKEN') }}"
ENDPOINT: "{{ lookup('env', 'ENDPOINT') }}"
24 changes: 24 additions & 0 deletions ansible/archival-snapshots/resources/diff_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/env bash

set -euxo pipefail

FOREST=~/forest-v0.14.0/forest-tool

EPOCH_START="$1"
shift
DIFF_STEP=3000
DIFF_COUNT=10
GENESIS_TIMESTAMP=1598306400
SECONDS_PER_EPOCH=30

for i in $(seq 1 $DIFF_COUNT); do
EPOCH=$((EPOCH_START+DIFF_STEP*i))
EPOCH_TIMESTAMP=$((GENESIS_TIMESTAMP + EPOCH*SECONDS_PER_EPOCH))
DATE=$(date --date=@"$EPOCH_TIMESTAMP" -u -I)
FILE="diff_snapshots/forest_diff_mainnet_${DATE}_height_$((EPOCH-DIFF_STEP))+$DIFF_STEP.forest.car.zst"
if ! test -f "$FILE"; then
"$FOREST" archive export --depth "$DIFF_STEP" --epoch "$EPOCH" --diff $((EPOCH-DIFF_STEP)) --diff-depth 900 --output-path "$FILE" "$@"
else
echo "Skipping $FILE"
fi
done
36 changes: 36 additions & 0 deletions ansible/archival-snapshots/resources/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

## Enable strict error handling, command tracing, and pipefail
set -euxo pipefail

FOREST_CLI="/home/archie/forest-v0.16.3/forest-cli"
FOREST_TOOL="/home/archie/forest-v0.16.3/forest-tool"

# Setting DEBIAN_FRONTEND to ensure non-interactive operations for APT
export DEBIAN_FRONTEND=noninteractive

CURRENT_SNAPSHOT=$(aws --profile prod --endpoint "$ENDPOINT" s3 ls "s3://forest-archive/mainnet/lite/" | sort | tail -n 1 | awk '{print $NF}')
CURRENT_EPOCH=$(echo "$CURRENT_SNAPSHOT" | awk -F'_' '{gsub(/[^0-9]/, "", $6); print $6}')

echo "$CURRENT_EPOCH"

LATEST_EPOCH=$($FOREST_CLI sync status | grep "Height:" | awk '{print $2}')
echo "$LATEST_EPOCH"

while ((LATEST_EPOCH - CURRENT_EPOCH > 30000)); do
NEW_EPOCH=$((CURRENT_EPOCH + 30000))
NEW_SNAPSHOT=$($FOREST_CLI snapshot export --tipset "$NEW_EPOCH" --depth 30000 | grep forest |awk -F'[:]' '{print $1}')

# Generate and upload lite snapshot
$FOREST_TOOL archive export --epoch "$NEW_EPOCH" --output-path ./lite_snapshots/ "$NEW_SNAPSHOT"
aws --profile prod --endpoint "$ENDPOINT" s3 cp ./lite_snapshots/"$NEW_SNAPSHOT" "s3://forest-archive/mainnet/lite/"

# Generate and upload diff snapshots
./diff_script.sh "$CURRENT_EPOCH" "$CURRENT_SNAPSHOT" "$NEW_SNAPSHOT"
./upload_diff.sh "$ENDPOINT"

CURRENT_EPOCH=$NEW_EPOCH
done

# Send notification on slack channel
ruby notify.rb "$CURRENT_EPOCH"
13 changes: 13 additions & 0 deletions ansible/archival-snapshots/resources/notify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'slack-ruby-client'

CHANNEL = '#forest-notifications'
SLACK_TOKEN = ENV.fetch('ARCHIVAL_SLACK_TOKEN')
EPOCH = ARGV[0]

client = Slack::Web::Client.new(token: SLACK_TOKEN)

message = "✅ Lite and Diff snapshots updated till #{EPOCH}. 🌲🌳🌲🌳🌲"

client.chat_postMessage(channel: CHANNEL, text: message, as_user: true)
14 changes: 14 additions & 0 deletions ansible/archival-snapshots/resources/upload_diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

ENDPOINT="$1"

# list files to upload
ls diff_snapshots/ > upload_files.txt

while read -r file; do
# Upload the file to the S3 bucket
aws --profile prod --endpoint "$ENDPOINT" s3 cp diff_snapshots/"$file" "s3://forest-archive/mainnet/diff/"
done < upload_files.txt

# Remove uploaded diff snapshots
rm diff_snapshots/* -rf