Update peers #100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update peers | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "0 0 * * MON" | |
concurrency: | |
group: update-peers-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
update-peers: | |
runs-on: ubuntu-22.04 | |
strategy: | |
matrix: | |
target: | |
- node: "https://rpc-t.okp4.nodestake.org" | |
chain: "drunemeton-1" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.OPS_TOKEN }} | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
- name: Import GPG key | |
uses: crazy-max/ghaction-import-gpg@v6 | |
with: | |
gpg_private_key: ${{ secrets.BOT_GPG_PRIVATE_KEY }} | |
passphrase: ${{ secrets.BOT_GPG_PASSPHRASE }} | |
git_config_global: true | |
git_user_signingkey: true | |
git_commit_gpgsign: true | |
- name: Install requests | |
run: | | |
pip install requests | |
- name: Retrieve peers | |
shell: python | |
run: | | |
import requests | |
import json | |
import re | |
import os | |
from ipaddress import ip_address, IPv4Address | |
from enum import Enum | |
class IPClass(Enum): | |
IPv4 = 1 | |
IPv6 = 2 | |
Unknown = 3 | |
def ip_class(IP: str) -> IPClass: | |
try: | |
return IPClass.IPv4 if type(ip_address(IP)) is IPv4Address else IPClass.IPv6 | |
except ValueError: | |
return IPClass.Unknown | |
def get_peer_address(peer): | |
peerId = peer['node_info']['id'] | |
remoteIp = peer['remote_ip'] | |
listen_address = peer['node_info']['listen_addr'] | |
match = re.match(r"(?:.+:\/\/)?(?:.+):(\d+)", listen_address) | |
if match: | |
listen_port = match.group(1) | |
else: | |
raise ValueError("Invalid listen address: {}".format(listen_address)) | |
match ip_class(remoteIp): | |
case IPClass.IPv4: | |
return f"{peerId}@{remoteIp}:{listen_port}" | |
case IPClass.IPv6: | |
return f"{peerId}[@{remoteIp}]:{listen_port}" | |
case _: | |
raise ValueError("Invalid IP address: {}".format(remoteIp)) | |
response = requests.get('${{ matrix.target.node }}/net_info?') | |
response.raise_for_status() | |
json_response = json.loads(response.text) | |
peers = map(get_peer_address, json_response['result']['peers']) | |
os.makedirs(os.path.dirname("./chains/${{ matrix.target.chain }}/"), exist_ok=True) | |
with open("./chains/${{ matrix.target.chain }}/peers.txt", 'w') as f: | |
for value in peers: | |
f.write(f"{value}\n") | |
- name: Add & Commit changes | |
uses: EndBug/add-and-commit@v9 | |
with: | |
message: "feat(networks): update peers list" | |
author_name: ${{ vars.BOT_GIT_AUTHOR_NAME }} | |
author_email: ${{ vars.BOT_GIT_AUTHOR_EMAIL }} | |
committer_name: ${{ vars.BOT_GIT_COMMITTER_NAME }} | |
committer_email: ${{ vars.BOT_GIT_COMMITTER_EMAIL }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.OPS_TOKEN }} |