Compare commits
61 Commits
main
...
834ddb4438
Author | SHA1 | Date | |
---|---|---|---|
834ddb4438 | |||
7e8587660d | |||
6b431823f5 | |||
82e6047e86 | |||
15f277057e | |||
04d44aeadf | |||
b006ea31b0 | |||
04efc25a48 | |||
cccbc07db1 | |||
7d5889553d | |||
34776214d6 | |||
0cc12c5446 | |||
57c8b81c13 | |||
382a910856 | |||
a0d2e74115 | |||
c1f23386b5 | |||
e5a0eef020 | |||
c95fce1b69 | |||
cd119be631 | |||
a5a27829de | |||
246e073092 | |||
43dbcd0a17 | |||
c062ad8e66 | |||
da56ef57f0 | |||
b89e6c20ce | |||
b93e5f890a | |||
625a8f71ae | |||
8acb4768e3 | |||
b9d966ff90 | |||
1e38b5aca6 | |||
3abc4e0a81 | |||
49c13d9e42 | |||
4e50ee554b | |||
f5706ac887 | |||
da787935df | |||
77a5f1a071 | |||
8425114abf | |||
92f6cdcd40 | |||
f2cce2f592 | |||
5d8a2cfdd6 | |||
4c0268823b | |||
bbd1de0020 | |||
3a2847295a | |||
671c7a4507 | |||
e0913ece08 | |||
1a775c6b87 | |||
26323e9c41 | |||
a79bdc94d0 | |||
272ee7522b | |||
e85f0b987e | |||
7eab098a99 | |||
c0502193b2 | |||
9be7df6bcf | |||
8183934d09 | |||
1558f60810 | |||
138fcfc321 | |||
153ccfd4db | |||
eaceb3ecfa | |||
c0fd0330af | |||
fc49f06d75 | |||
17702b1396 |
213
checker.py
Normal file
213
checker.py
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
# flake8: noqa
|
||||||
|
# pylint: disable=broad-exception-raised, raise-missing-from, too-many-arguments, redefined-outer-name
|
||||||
|
# pylance: disable=reportMissingImports, reportMissingModuleSource, reportGeneralTypeIssues
|
||||||
|
# type: ignore
|
||||||
|
|
||||||
|
import warnings
|
||||||
|
warnings.filterwarnings("ignore", category=Warning)
|
||||||
|
|
||||||
|
import re
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
import pkg_resources
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
required_packages = {
|
||||||
|
'grist-api': 'latest',
|
||||||
|
'colorama': 'latest',
|
||||||
|
'requests': '2.31.0',
|
||||||
|
'urllib3': '2.0.7',
|
||||||
|
'charset-normalizer': '3.3.2'
|
||||||
|
}
|
||||||
|
|
||||||
|
installed_packages = {pkg.key: pkg.version for pkg in pkg_resources.working_set}
|
||||||
|
|
||||||
|
for package, version in required_packages.items():
|
||||||
|
if package not in installed_packages or (version != 'latest' and installed_packages[package] != version):
|
||||||
|
if version == 'latest':
|
||||||
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package, '--break-system-packages'])
|
||||||
|
else:
|
||||||
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', f"{package}=={version}", '--break-system-packages'])
|
||||||
|
|
||||||
|
from grist_api import GristDocAPI
|
||||||
|
import colorama
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import socket
|
||||||
|
|
||||||
|
def self_update(logger):
|
||||||
|
logger.info("Checking for updates..")
|
||||||
|
script_path = os.path.abspath(__file__)
|
||||||
|
update_url = "https://gitea.vvzvlad.xyz/vvzvlad/ritual/raw/branch/main-22aug/checker.py"
|
||||||
|
try:
|
||||||
|
response = requests.get(update_url, timeout=10)
|
||||||
|
if response.status_code == 200:
|
||||||
|
current_content = ""
|
||||||
|
with open(script_path, 'r', encoding='utf-8') as f:
|
||||||
|
current_content = f.read()
|
||||||
|
|
||||||
|
if current_content != response.text:
|
||||||
|
with open(script_path, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(response.text)
|
||||||
|
logger.info("Script updated successfully, restarting")
|
||||||
|
os.execv(sys.executable, ['python3'] + sys.argv)
|
||||||
|
else:
|
||||||
|
logger.info("Script is up to date")
|
||||||
|
else:
|
||||||
|
logger.error(f"Failed to download update, status code: {response.status_code}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Update error: {str(e)}")
|
||||||
|
|
||||||
|
class GRIST:
|
||||||
|
def __init__(self, server, doc_id, api_key, logger):
|
||||||
|
self.server = server
|
||||||
|
self.doc_id = doc_id
|
||||||
|
self.api_key = api_key
|
||||||
|
self.logger = logger
|
||||||
|
self.grist = GristDocAPI(doc_id, server=server, api_key=api_key)
|
||||||
|
|
||||||
|
def table_name_convert(self, table_name):
|
||||||
|
return table_name.replace(" ", "_")
|
||||||
|
|
||||||
|
def to_timestamp(self, dtime: datetime) -> int:
|
||||||
|
if dtime.tzinfo is None:
|
||||||
|
dtime = dtime.replace(tzinfo=timezone(timedelta(hours=3)))
|
||||||
|
return int(dtime.timestamp())
|
||||||
|
|
||||||
|
def insert_row(self, data, table):
|
||||||
|
data = {key.replace(" ", "_"): value for key, value in data.items()}
|
||||||
|
row_id = self.grist.add_records(self.table_name_convert(table), [data])
|
||||||
|
return row_id
|
||||||
|
|
||||||
|
def update_column(self, row_id, column_name, value, table):
|
||||||
|
if isinstance(value, datetime):
|
||||||
|
value = self.to_timestamp(value)
|
||||||
|
column_name = column_name.replace(" ", "_")
|
||||||
|
self.grist.update_records(self.table_name_convert(table), [{ "id": row_id, column_name: value }])
|
||||||
|
|
||||||
|
def delete_row(self, row_id, table):
|
||||||
|
self.grist.delete_records(self.table_name_convert(table), [row_id])
|
||||||
|
|
||||||
|
def update(self, row_id, updates, table):
|
||||||
|
for column_name, value in updates.items():
|
||||||
|
if isinstance(value, datetime):
|
||||||
|
updates[column_name] = self.to_timestamp(value)
|
||||||
|
updates = {column_name.replace(" ", "_"): value for column_name, value in updates.items()}
|
||||||
|
self.grist.update_records(self.table_name_convert(table), [{"id": row_id, **updates}])
|
||||||
|
|
||||||
|
def fetch_table(self, table):
|
||||||
|
return self.grist.fetch_table(self.table_name_convert(table))
|
||||||
|
|
||||||
|
def find_record(self, id=None, state=None, name=None, table=None):
|
||||||
|
if table is None:
|
||||||
|
raise ValueError("Table is not specified")
|
||||||
|
table_data = self.grist.fetch_table(self.table_name_convert(table))
|
||||||
|
if id is not None:
|
||||||
|
record = [row for row in table_data if row.id == id]
|
||||||
|
return record
|
||||||
|
if state is not None and name is not None:
|
||||||
|
record = [row for row in table_data if row.State == state and row.name == name]
|
||||||
|
return record
|
||||||
|
if state is not None:
|
||||||
|
record = [row for row in table_data if row.State == state]
|
||||||
|
return record
|
||||||
|
if name is not None:
|
||||||
|
record = [row for row in table_data if row.Name == name]
|
||||||
|
return record
|
||||||
|
|
||||||
|
def find_settings(self, key, table):
|
||||||
|
table = self.fetch_table(self.table_name_convert(table))
|
||||||
|
for record in table:
|
||||||
|
if record.Setting == key:
|
||||||
|
if record.Value is None or record.Value == "":
|
||||||
|
raise ValueError(f"Setting {key} blank")
|
||||||
|
return record.Value
|
||||||
|
raise ValueError(f"Setting {key} not found")
|
||||||
|
|
||||||
|
def clean_ansi(text):
|
||||||
|
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
||||||
|
return ansi_escape.sub('', text)
|
||||||
|
|
||||||
|
def format_number(number_str):
|
||||||
|
number = int(number_str)
|
||||||
|
if number >= 1000:
|
||||||
|
return f"{number//1000}k"
|
||||||
|
return str(number)
|
||||||
|
|
||||||
|
def check_logs(logger):
|
||||||
|
try:
|
||||||
|
logs = subprocess.run(['docker', 'logs', '--since', '10m', 'infernet-node'], capture_output=True, text=True, check=True)
|
||||||
|
log_content = clean_ansi(logs.stdout)
|
||||||
|
|
||||||
|
last_subscription_id = None
|
||||||
|
head_sub_id = None
|
||||||
|
|
||||||
|
for line in log_content.splitlines():
|
||||||
|
if "Ignored subscription creation" in line and "id=" in line:
|
||||||
|
id_match = re.search(r'id=(\d+)', line)
|
||||||
|
if id_match:
|
||||||
|
last_subscription_id = id_match.group(1)
|
||||||
|
|
||||||
|
if "head sub id is:" in line:
|
||||||
|
id_match = re.search(r'head sub id is:\s*(\d+)', line)
|
||||||
|
if id_match:
|
||||||
|
head_sub_id = id_match.group(1)
|
||||||
|
|
||||||
|
if head_sub_id:
|
||||||
|
logger.info(f"Head sub id: {head_sub_id}")
|
||||||
|
return {"status": f"OK: {head_sub_id}"}
|
||||||
|
|
||||||
|
if last_subscription_id:
|
||||||
|
logger.info(f"Subscription: {last_subscription_id}")
|
||||||
|
return {"status": f"Sync: {format_number(last_subscription_id)}"}
|
||||||
|
|
||||||
|
logger.info("Not found subscription")
|
||||||
|
return {"status": "Idle"}
|
||||||
|
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
raise RuntimeError(f"Error running docker logs: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
colorama.init(autoreset=True)
|
||||||
|
logger = logging.getLogger("Checker")
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||||
|
ch = logging.StreamHandler()
|
||||||
|
ch.setFormatter(formatter)
|
||||||
|
logger.addHandler(ch)
|
||||||
|
|
||||||
|
logger.info("Checker started")
|
||||||
|
self_update(logger)
|
||||||
|
#random_sleep = random.randint(1, 60)
|
||||||
|
#logger.info(f"Sleeping for {random_sleep} seconds")
|
||||||
|
#time.sleep(random_sleep)
|
||||||
|
|
||||||
|
grist_data = {}
|
||||||
|
with open('/root/node/grist.json', 'r', encoding='utf-8') as f:
|
||||||
|
grist_data = json.loads(f.read())
|
||||||
|
|
||||||
|
GRIST_ROW_NAME = socket.gethostname()
|
||||||
|
NODES_TABLE = "Nodes"
|
||||||
|
grist = GRIST(grist_data.get('grist_server'), grist_data.get('grist_doc_id'), grist_data.get('grist_api_key'), logger)
|
||||||
|
current_vm = grist.find_record(name=GRIST_ROW_NAME, table=NODES_TABLE)[0]
|
||||||
|
def grist_callback(msg): grist.update(current_vm.id, msg, NODES_TABLE)
|
||||||
|
|
||||||
|
for attempt in range(3):
|
||||||
|
try:
|
||||||
|
result = check_logs(logger)
|
||||||
|
grist_callback({ "Health": result["status"] })
|
||||||
|
logger.info(f"Status: {result['status']}")
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error on attempt {attempt+1}/3: {e}")
|
||||||
|
if attempt == 2:
|
||||||
|
grist_callback({ "Health": f"Error: {e}" })
|
||||||
|
if attempt < 2:
|
||||||
|
time.sleep(5)
|
@ -1,8 +1,7 @@
|
|||||||
version: '3'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
node:
|
node:
|
||||||
image: ritualnetwork/infernet-node:1.2.0
|
image: ritualnetwork/infernet-node:1.4.0
|
||||||
ports:
|
ports:
|
||||||
- "0.0.0.0:4000:4000"
|
- "0.0.0.0:4000:4000"
|
||||||
volumes:
|
volumes:
|
||||||
@ -21,6 +20,11 @@ services:
|
|||||||
- "host.docker.internal:host-gateway"
|
- "host.docker.internal:host-gateway"
|
||||||
stop_grace_period: 1m
|
stop_grace_period: 1m
|
||||||
container_name: infernet-node
|
container_name: infernet-node
|
||||||
|
logging:
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-file: 5
|
||||||
|
max-size: 10m
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:latest
|
image: redis:latest
|
||||||
@ -33,6 +37,11 @@ services:
|
|||||||
- redis-data:/data
|
- redis-data:/data
|
||||||
restart:
|
restart:
|
||||||
unless-stopped
|
unless-stopped
|
||||||
|
logging:
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-file: 5
|
||||||
|
max-size: 10m
|
||||||
|
|
||||||
fluentbit:
|
fluentbit:
|
||||||
image: fluent/fluent-bit:latest
|
image: fluent/fluent-bit:latest
|
||||||
@ -47,6 +56,11 @@ services:
|
|||||||
- network
|
- network
|
||||||
restart:
|
restart:
|
||||||
unless-stopped
|
unless-stopped
|
||||||
|
logging:
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-file: 5
|
||||||
|
max-size: 10m
|
||||||
|
|
||||||
infernet-anvil:
|
infernet-anvil:
|
||||||
image: ritualnetwork/infernet-anvil:1.0.0
|
image: ritualnetwork/infernet-anvil:1.0.0
|
||||||
@ -58,6 +72,11 @@ services:
|
|||||||
container_name: infernet-anvil
|
container_name: infernet-anvil
|
||||||
restart:
|
restart:
|
||||||
unless-stopped
|
unless-stopped
|
||||||
|
logging:
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-file: 5
|
||||||
|
max-size: 10m
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
network:
|
network:
|
||||||
|
5
grist.json
Normal file
5
grist.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"grist_server": "###GRIST_SERVER###",
|
||||||
|
"grist_doc_id": "###GRIST_DOC_ID###",
|
||||||
|
"grist_api_key": "###GRIST_API_KEY###"
|
||||||
|
}
|
323
grpcbalancer/grpc-balancer.py
Normal file
323
grpcbalancer/grpc-balancer.py
Normal file
@ -0,0 +1,323 @@
|
|||||||
|
import threading
|
||||||
|
from collections import deque
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
import random
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
import socket
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from grist_api import GristDocAPI
|
||||||
|
from flask import Flask, request, Response
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
BACKEND_SERVERS = []
|
||||||
|
SERVER_STATS = {}
|
||||||
|
STATS_LOCK = threading.Lock()
|
||||||
|
ADDRESS_STATS = {}
|
||||||
|
ADDRESS_STATS_LOCK = threading.Lock()
|
||||||
|
STATISTICS_THRESHOLD = 10
|
||||||
|
STATISTICS_WINDOW = timedelta(minutes=10)
|
||||||
|
MAX_WORKERS = 500
|
||||||
|
MAX_ERROR_RATE = 0.7
|
||||||
|
PORT = 5000
|
||||||
|
|
||||||
|
HOP_BY_HOP_HEADERS = {
|
||||||
|
'connection',
|
||||||
|
'keep-alive',
|
||||||
|
'proxy-authenticate',
|
||||||
|
'proxy-authorization',
|
||||||
|
'te',
|
||||||
|
'trailers',
|
||||||
|
'transfer-encoding',
|
||||||
|
'upgrade',
|
||||||
|
}
|
||||||
|
|
||||||
|
class GRIST:
|
||||||
|
def __init__(self, server, doc_id, api_key, logger):
|
||||||
|
self.server = server
|
||||||
|
self.doc_id = doc_id
|
||||||
|
self.api_key = api_key
|
||||||
|
self.logger = logger
|
||||||
|
self.grist = GristDocAPI(doc_id, server=server, api_key=api_key)
|
||||||
|
|
||||||
|
def table_name_convert(self, table_name):
|
||||||
|
return table_name.replace(" ", "_")
|
||||||
|
|
||||||
|
def to_timestamp(self, dtime: datetime) -> int:
|
||||||
|
if dtime.tzinfo is None:
|
||||||
|
dtime = dtime.replace(tzinfo=timezone(timedelta(hours=3)))
|
||||||
|
return int(dtime.timestamp())
|
||||||
|
|
||||||
|
def insert_row(self, data, table):
|
||||||
|
data = {key.replace(" ", "_"): value for key, value in data.items()}
|
||||||
|
row_id = self.grist.add_records(self.table_name_convert(table), [data])
|
||||||
|
return row_id
|
||||||
|
|
||||||
|
def update_column(self, row_id, column_name, value, table):
|
||||||
|
if isinstance(value, datetime):
|
||||||
|
value = self.to_timestamp(value)
|
||||||
|
column_name = column_name.replace(" ", "_")
|
||||||
|
self.grist.update_records(self.table_name_convert(table), [{ "id": row_id, column_name: value }])
|
||||||
|
|
||||||
|
def delete_row(self, row_id, table):
|
||||||
|
self.grist.delete_records(self.table_name_convert(table), [row_id])
|
||||||
|
|
||||||
|
def update(self, row_id, updates, table):
|
||||||
|
for column_name, value in updates.items():
|
||||||
|
if isinstance(value, datetime):
|
||||||
|
updates[column_name] = self.to_timestamp(value)
|
||||||
|
updates = {column_name.replace(" ", "_"): value for column_name, value in updates.items()}
|
||||||
|
self.grist.update_records(self.table_name_convert(table), [{"id": row_id, **updates}])
|
||||||
|
|
||||||
|
def fetch_table(self, table):
|
||||||
|
return self.grist.fetch_table(self.table_name_convert(table))
|
||||||
|
|
||||||
|
def find_record(self, id=None, state=None, name=None, table=None):
|
||||||
|
if table is None:
|
||||||
|
raise ValueError("Table is not specified")
|
||||||
|
table_data = self.grist.fetch_table(self.table_name_convert(table))
|
||||||
|
if id is not None:
|
||||||
|
record = [row for row in table_data if row.id == id]
|
||||||
|
return record
|
||||||
|
if state is not None and name is not None:
|
||||||
|
record = [row for row in table_data if row.State == state and row.name == name]
|
||||||
|
return record
|
||||||
|
if state is not None:
|
||||||
|
record = [row for row in table_data if row.State == state]
|
||||||
|
return record
|
||||||
|
if name is not None:
|
||||||
|
record = [row for row in table_data if row.Name == name]
|
||||||
|
return record
|
||||||
|
|
||||||
|
def find_settings(self, key, table):
|
||||||
|
table = self.fetch_table(self.table_name_convert(table))
|
||||||
|
for record in table:
|
||||||
|
if record.Setting == key:
|
||||||
|
if record.Value is None or record.Value == "":
|
||||||
|
raise ValueError(f"Setting {key} blank")
|
||||||
|
return record.Value
|
||||||
|
raise ValueError(f"Setting {key} not found")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=['POST'])
|
||||||
|
def proxy():
|
||||||
|
data = request.get_data()
|
||||||
|
headers = dict(request.headers)
|
||||||
|
headers.pop('Accept-Encoding', None)
|
||||||
|
|
||||||
|
try:
|
||||||
|
data_json = json.loads(data.decode('utf-8'))
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logging.warning(f'Invalid JSON from {request.remote_addr}: {data}')
|
||||||
|
return Response('Invalid JSON', status=400)
|
||||||
|
|
||||||
|
# Функция для обновления статистики запросов по адресу
|
||||||
|
def update_address_stats(from_address):
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
with ADDRESS_STATS_LOCK:
|
||||||
|
if from_address not in ADDRESS_STATS:
|
||||||
|
ADDRESS_STATS[from_address] = deque()
|
||||||
|
ADDRESS_STATS[from_address].append(now)
|
||||||
|
# Удаление запросов, вышедших за пределы окна
|
||||||
|
while ADDRESS_STATS[from_address] and ADDRESS_STATS[from_address][0] < now - STATISTICS_WINDOW:
|
||||||
|
ADDRESS_STATS[from_address].popleft()
|
||||||
|
|
||||||
|
# Функция для извлечения 'from' адреса из запроса
|
||||||
|
def extract_from_address(req):
|
||||||
|
params = req.get("params", [])
|
||||||
|
if isinstance(params, list) and len(params) > 0 and isinstance(params[0], dict):
|
||||||
|
return params[0].get("from")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Проверка, является ли запрос массивом (батч-запрос)
|
||||||
|
if isinstance(data_json, list):
|
||||||
|
for req in data_json:
|
||||||
|
from_address = extract_from_address(req)
|
||||||
|
if from_address:
|
||||||
|
update_address_stats(from_address)
|
||||||
|
elif isinstance(data_json, dict):
|
||||||
|
from_address = extract_from_address(data_json)
|
||||||
|
if from_address:
|
||||||
|
update_address_stats(from_address)
|
||||||
|
|
||||||
|
if data_json.get("method") == "eth_chainId":
|
||||||
|
response_json = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": data_json.get("id"),
|
||||||
|
"result": "0x2105" #base
|
||||||
|
}
|
||||||
|
response_str = json.dumps(response_json)
|
||||||
|
return Response(response_str, status=200, mimetype='application/json')
|
||||||
|
|
||||||
|
|
||||||
|
selected_servers = select_servers()
|
||||||
|
for server in selected_servers:
|
||||||
|
server_url = server['url']
|
||||||
|
server_id = server['id']
|
||||||
|
try:
|
||||||
|
headers['Host'] = server_url.split('//')[-1].split('/')[0]
|
||||||
|
#logging.info(f'Proxying request to {server_url}: {data}')
|
||||||
|
response = requests.post(server_url, data=data, headers=headers, timeout=5)
|
||||||
|
if response.status_code == 200:
|
||||||
|
print(".", end="", flush=True)
|
||||||
|
#MAX_DATA_LENGTH = 20
|
||||||
|
#data_str = data.decode('utf-8')
|
||||||
|
#data_json = json.loads(data_str)
|
||||||
|
#if "jsonrpc" in data_json: data_json.pop("jsonrpc")
|
||||||
|
#if 'params' in data_json and isinstance(data_json['params'], list):
|
||||||
|
# for idx, param in enumerate(data_json['params']):
|
||||||
|
# if isinstance(param, dict) and 'data' in param:
|
||||||
|
# original_data = param['data']
|
||||||
|
# if isinstance(original_data, str) and len(original_data) > MAX_DATA_LENGTH:
|
||||||
|
# truncated_data = original_data[:MAX_DATA_LENGTH - len("....SKIPPED")] + "....SKIPPED"
|
||||||
|
# data_json['params'][idx]['data'] = truncated_data
|
||||||
|
#truncated_data_str = json.dumps(data_json)
|
||||||
|
|
||||||
|
|
||||||
|
#response_str = response.content.decode('utf-8')
|
||||||
|
#response_json = json.loads(response_str)
|
||||||
|
#if "jsonrpc" in response_json: response_json.pop("jsonrpc")
|
||||||
|
#if 'result' in response_json:
|
||||||
|
# original_result = response_json['result']
|
||||||
|
# if isinstance(original_result, str) and len(original_result) > MAX_DATA_LENGTH:
|
||||||
|
# truncated_result = original_result[:MAX_DATA_LENGTH - len("....SKIPPED")] + "....SKIPPED"
|
||||||
|
# response_json['result'] = truncated_result
|
||||||
|
#truncated_response_str = json.dumps(response_json)
|
||||||
|
|
||||||
|
#logging.info(f'OK: {request.remote_addr}: {truncated_data_str} -> {server_url}: {response.status_code}/{truncated_response_str}')
|
||||||
|
with STATS_LOCK:
|
||||||
|
SERVER_STATS[server_id].append((datetime.now(timezone.utc), True))
|
||||||
|
filtered_headers = {
|
||||||
|
k: v for k, v in response.headers.items()
|
||||||
|
if k.lower() not in HOP_BY_HOP_HEADERS
|
||||||
|
}
|
||||||
|
filtered_headers.pop('Content-Encoding', None)
|
||||||
|
connection_header = response.headers.get('Connection', '')
|
||||||
|
connection_tokens = [token.strip().lower() for token in connection_header.split(',')]
|
||||||
|
for token in connection_tokens:
|
||||||
|
filtered_headers.pop(token, None)
|
||||||
|
return Response(response.content, status=response.status_code, headers=filtered_headers)
|
||||||
|
else:
|
||||||
|
logging.warning(f'Failed to proxy request to {server_url}: {response.status_code}/{response.content}')
|
||||||
|
with STATS_LOCK:
|
||||||
|
SERVER_STATS[server_id].append((datetime.now(timezone.utc), False))
|
||||||
|
continue
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logging.error(f'Exception while proxying to {server_url}: {e}')
|
||||||
|
with STATS_LOCK:
|
||||||
|
SERVER_STATS[server_id].append((datetime.now(timezone.utc), False))
|
||||||
|
continue
|
||||||
|
return Response('All backend servers are unavailable', status=503)
|
||||||
|
|
||||||
|
|
||||||
|
def select_servers():
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
with STATS_LOCK:
|
||||||
|
for server in BACKEND_SERVERS:
|
||||||
|
server_id = server['id']
|
||||||
|
stats = SERVER_STATS[server_id]
|
||||||
|
while stats and stats[0][0] < now - STATISTICS_WINDOW:
|
||||||
|
stats.popleft()
|
||||||
|
total_requests = sum(len(SERVER_STATS[server['id']]) for server in BACKEND_SERVERS)
|
||||||
|
|
||||||
|
if total_requests < STATISTICS_THRESHOLD:
|
||||||
|
servers = BACKEND_SERVERS.copy()
|
||||||
|
random.shuffle(servers)
|
||||||
|
#logging.info("Total requests below threshold. Shuffled servers: %s", servers)
|
||||||
|
return servers
|
||||||
|
|
||||||
|
server_scores = []
|
||||||
|
with STATS_LOCK:
|
||||||
|
for server in BACKEND_SERVERS:
|
||||||
|
server_id = server['id']
|
||||||
|
stats = SERVER_STATS[server_id]
|
||||||
|
failures = sum(1 for t, success in stats if not success)
|
||||||
|
successes = len(stats) - failures
|
||||||
|
total = successes + failures
|
||||||
|
error_rate = failures / total if total > 0 else 0
|
||||||
|
server_scores.append({
|
||||||
|
'server': server,
|
||||||
|
'failures': failures,
|
||||||
|
'successes': successes,
|
||||||
|
'error_rate': error_rate
|
||||||
|
})
|
||||||
|
#logging.info(f"Server {server_id}: Failures={failures}, Successes={successes}, Error Rate={error_rate:.2f}")
|
||||||
|
|
||||||
|
healthy_servers = [s for s in server_scores if s['error_rate'] <= MAX_ERROR_RATE]
|
||||||
|
|
||||||
|
if not healthy_servers:
|
||||||
|
logging.warning("No healthy servers available.")
|
||||||
|
return BACKEND_SERVERS.copy()
|
||||||
|
|
||||||
|
healthy_servers.sort(key=lambda x: x['error_rate'])
|
||||||
|
|
||||||
|
total_weight = sum(1 - s['error_rate'] for s in healthy_servers)
|
||||||
|
if total_weight == 0:
|
||||||
|
weights = [1 for _ in healthy_servers]
|
||||||
|
else:
|
||||||
|
weights = [(1 - s['error_rate']) / total_weight for s in healthy_servers]
|
||||||
|
|
||||||
|
selected_server = random.choices( [s['server'] for s in healthy_servers], weights=weights, k=1 )[0]
|
||||||
|
selected_servers = [selected_server] + [s['server'] for s in healthy_servers if s['server'] != selected_server]
|
||||||
|
return selected_servers
|
||||||
|
|
||||||
|
def upload_stats_to_grist(update_row):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
total_stats = {
|
||||||
|
'failures': 0,
|
||||||
|
'successes': 0,
|
||||||
|
'rps': 0
|
||||||
|
}
|
||||||
|
|
||||||
|
with STATS_LOCK:
|
||||||
|
for server in BACKEND_SERVERS:
|
||||||
|
server_id = server['id']
|
||||||
|
server_stats = SERVER_STATS[server_id]
|
||||||
|
failures = sum(1 for t, success in server_stats if not success)
|
||||||
|
successes = len(server_stats) - failures
|
||||||
|
total_stats['failures'] += failures
|
||||||
|
total_stats['successes'] += successes
|
||||||
|
total_stats['rps'] += len(server_stats)/STATISTICS_WINDOW.total_seconds()
|
||||||
|
|
||||||
|
health = f"{total_stats['successes']}/{total_stats['failures']}/{total_stats['rps']:.2f}"
|
||||||
|
update_row({"Health": health})
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Failed to upload stats to Grist: {str(e)}")
|
||||||
|
time.sleep(30)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
GRIST_ROW_NAME = socket.gethostname()
|
||||||
|
NODES_TABLE = "Nodes"
|
||||||
|
RPC_TABLE = "RPC_list"
|
||||||
|
|
||||||
|
with open('/root/node/grist.json', 'r', encoding='utf-8') as f:
|
||||||
|
grist_data = json.loads(f.read())
|
||||||
|
|
||||||
|
host = grist_data.get('grist_server')
|
||||||
|
doc_id = grist_data.get('grist_doc_id')
|
||||||
|
api_key = grist_data.get('grist_api_key')
|
||||||
|
grist = GRIST(host, doc_id, api_key, logging)
|
||||||
|
current_vm = grist.find_record(name=GRIST_ROW_NAME, table=NODES_TABLE)[0]
|
||||||
|
def grist_callback(msg): grist.update(current_vm.id, msg, NODES_TABLE)
|
||||||
|
|
||||||
|
|
||||||
|
BACKEND_SERVERS = []
|
||||||
|
SERVER_STATS = {}
|
||||||
|
table = grist.fetch_table(RPC_TABLE)
|
||||||
|
for row in table:
|
||||||
|
if row.URL:
|
||||||
|
server_info = {'id': row.id, 'url': row.URL}
|
||||||
|
BACKEND_SERVERS.append(server_info)
|
||||||
|
SERVER_STATS[row.id] = deque()
|
||||||
|
|
||||||
|
upload_thread = threading.Thread(target=upload_stats_to_grist, daemon=True, args=(grist_callback,))
|
||||||
|
upload_thread.start()
|
||||||
|
|
||||||
|
from waitress import serve
|
||||||
|
logging.info(f"Starting server on port {PORT}")
|
||||||
|
serve(app, host='0.0.0.0', port=PORT, threads=MAX_WORKERS, connection_limit=1000)
|
15
grpcbalancer/grpc-balancer.service
Normal file
15
grpcbalancer/grpc-balancer.service
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=GRPC Balancer Service
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
Environment=PYTHONUNBUFFERED=1
|
||||||
|
ExecStart=/usr/bin/python3 /usr/local/bin/grpc-balancer.py
|
||||||
|
Restart=always
|
||||||
|
RestartSec=2
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
320
playbook.yml
320
playbook.yml
@ -1,16 +1,45 @@
|
|||||||
---
|
---
|
||||||
- name: System Setup and Configuration
|
- name: System Setup and Configuration
|
||||||
hosts: all
|
hosts: all
|
||||||
become: yes
|
become: true
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Set locale to C.UTF-8
|
- name: Set locale to C.UTF-8
|
||||||
command: localectl set-locale LANG=C.UTF-8
|
command: localectl set-locale LANG=C.UTF-8
|
||||||
|
|
||||||
|
- name: Create APT configuration file to assume yes
|
||||||
|
ansible.builtin.copy:
|
||||||
|
dest: /etc/apt/apt.conf.d/90forceyes
|
||||||
|
content: |
|
||||||
|
APT::Get::Assume-Yes "true";
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
|
- name: Append command to .bash_history
|
||||||
|
ansible.builtin.blockinfile:
|
||||||
|
path: "~/.bash_history"
|
||||||
|
create: true
|
||||||
|
block: |
|
||||||
|
cd ~/node; bash rebuild.sh
|
||||||
|
nano ~/node/projects/hello-world/container/config.json
|
||||||
|
docker logs infernet-node -f
|
||||||
|
docker logs --since 10m infernet-node -f
|
||||||
|
journalctl -u node-checker.service
|
||||||
|
marker: ""
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
|
|
||||||
|
- name: Append command to .bash_rc
|
||||||
|
ansible.builtin.blockinfile:
|
||||||
|
path: "~/.bashrc"
|
||||||
|
create: true
|
||||||
|
insertafter: EOF
|
||||||
|
block: |
|
||||||
|
cd /root/node
|
||||||
|
marker: ""
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
- name: Update /etc/bash.bashrc
|
- name: Update /etc/bash.bashrc
|
||||||
blockinfile:
|
ansible.builtin.blockinfile:
|
||||||
path: /etc/bash.bashrc
|
path: /etc/bash.bashrc
|
||||||
block: |
|
block: |
|
||||||
export HISTTIMEFORMAT='%F, %T '
|
export HISTTIMEFORMAT='%F, %T '
|
||||||
@ -23,27 +52,23 @@
|
|||||||
export LC_ALL=C.UTF-8
|
export LC_ALL=C.UTF-8
|
||||||
alias ls='ls --color=auto'
|
alias ls='ls --color=auto'
|
||||||
shopt -s cmdhist
|
shopt -s cmdhist
|
||||||
|
create: true
|
||||||
|
marker: ""
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
- name: Ensure ~/.inputrc exists
|
- name: Update .inputrc for the current user
|
||||||
file:
|
ansible.builtin.blockinfile:
|
||||||
path: /root/.inputrc
|
path: "{{ ansible_env.HOME }}/.inputrc"
|
||||||
state: touch
|
|
||||||
|
|
||||||
- name: Update ~/.inputrc
|
|
||||||
blockinfile:
|
|
||||||
path: ~/.inputrc
|
|
||||||
block: |
|
block: |
|
||||||
"\e[A": history-search-backward
|
"\e[A": history-search-backward
|
||||||
"\e[B": history-search-forward
|
"\e[B": history-search-forward
|
||||||
|
create: true
|
||||||
- name: Ensure ~/.nanorc exists
|
marker: ""
|
||||||
file:
|
mode: '0644'
|
||||||
path: /root/.nanorc
|
|
||||||
state: touch
|
|
||||||
|
|
||||||
- name: Update ~/.nanorc
|
- name: Update ~/.nanorc
|
||||||
blockinfile:
|
ansible.builtin.blockinfile:
|
||||||
path: ~/.nanorc
|
path: "{{ ansible_env.HOME }}/.nanorc"
|
||||||
block: |
|
block: |
|
||||||
set nohelp
|
set nohelp
|
||||||
set tabsize 4
|
set tabsize 4
|
||||||
@ -54,24 +79,31 @@
|
|||||||
set backupdir /tmp/
|
set backupdir /tmp/
|
||||||
set locking
|
set locking
|
||||||
include /usr/share/nano/*.nanorc
|
include /usr/share/nano/*.nanorc
|
||||||
|
create: true
|
||||||
|
marker: ""
|
||||||
|
mode: '0644'
|
||||||
|
|
||||||
- name: Set hostname
|
- name: Set hostname
|
||||||
shell: |
|
ansible.builtin.hostname:
|
||||||
hostnamectl set-hostname {{ serverid }}
|
name: "{{ serverid }}"
|
||||||
echo "127.0.1.1 {{ serverid }}" >> /etc/hosts
|
|
||||||
|
|
||||||
- name: Update and upgrade apt
|
- name: Ensure hostname is in /etc/hosts
|
||||||
apt:
|
ansible.builtin.lineinfile:
|
||||||
update_cache: yes
|
path: /etc/hosts
|
||||||
upgrade: dist
|
regexp: '^127\.0\.1\.1\s+'
|
||||||
force_apt_get: yes
|
line: "127.0.1.1 {{ serverid }}"
|
||||||
register: apt_update_result
|
state: present
|
||||||
retries: 50
|
|
||||||
delay: 50
|
#- name: Update and upgrade apt
|
||||||
until: apt_update_result is succeeded
|
# ansible.builtin.apt:
|
||||||
|
# update_cache: true
|
||||||
|
# upgrade: dist
|
||||||
|
# force_apt_get: true
|
||||||
|
# register: apt_update_result
|
||||||
|
# until: apt_update_result is success
|
||||||
|
|
||||||
- name: Install necessary packages
|
- name: Install necessary packages
|
||||||
apt:
|
ansible.builtin.apt:
|
||||||
name:
|
name:
|
||||||
- apt-transport-https
|
- apt-transport-https
|
||||||
- ca-certificates
|
- ca-certificates
|
||||||
@ -83,132 +115,196 @@
|
|||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: Install pip package web3
|
- name: Install pip package web3
|
||||||
pip:
|
ansible.builtin.pip:
|
||||||
name: web3
|
name: web3
|
||||||
extra_args: --break-system-packages
|
extra_args: --break-system-packages
|
||||||
|
|
||||||
- name: Install Docker
|
|
||||||
shell: curl -sL https://get.docker.com | sudo sh -
|
|
||||||
|
|
||||||
- name: Ensure /etc/docker/daemon.json exists
|
|
||||||
file:
|
|
||||||
path: /etc/docker/daemon.json
|
|
||||||
state: touch
|
|
||||||
|
|
||||||
|
# - name: Install Docker
|
||||||
|
# ansible.builtin.shell: curl -sL https://get.docker.com | sudo sh -
|
||||||
|
#
|
||||||
- name: Update Docker daemon configuration for journald logging
|
- name: Update Docker daemon configuration for journald logging
|
||||||
copy:
|
ansible.builtin.copy:
|
||||||
dest: /etc/docker/daemon.json
|
dest: /etc/docker/daemon.json
|
||||||
content: |
|
content: |
|
||||||
{
|
{
|
||||||
"log-driver": "journald"
|
"log-driver": "journald",
|
||||||
|
"registry-mirrors": ["https://dockerregistry.vvzvlad.xyz"]
|
||||||
}
|
}
|
||||||
|
|
||||||
- name: Restart Docker
|
- name: Restart Docker
|
||||||
service:
|
ansible.builtin.service:
|
||||||
name: docker
|
name: docker
|
||||||
state: restarted
|
state: restarted
|
||||||
|
|
||||||
- name: Docker login
|
#- name: Docker login
|
||||||
shell: docker login -u {{ docker_username }} -p {{ docker_password }}
|
# ansible.builtin.shell: docker login -u {{ docker_username }} -p {{ docker_password }}
|
||||||
|
|
||||||
- name: Update journald log SystemMaxUse=2G configuration
|
- name: Docker pull hello-world
|
||||||
lineinfile:
|
ansible.builtin.shell: docker pull ritualnetwork/hello-world-infernet:latest
|
||||||
path: /etc/systemd/journald.conf
|
|
||||||
line: 'SystemMaxUse=2G'
|
|
||||||
insertafter: EOF
|
|
||||||
create: yes
|
|
||||||
|
|
||||||
- name: Restart journald
|
# - name: Update journald log SystemMaxUse=2G configuration
|
||||||
service:
|
# ansible.builtin.lineinfile:
|
||||||
name: systemd-journald
|
# path: /etc/systemd/journald.conf
|
||||||
state: restarted
|
# regexp: '^SystemMaxUse='
|
||||||
|
# line: 'SystemMaxUse=2G'
|
||||||
|
# state: present
|
||||||
|
# backup: yes
|
||||||
|
# validate: 'journaldctl check-config %s'
|
||||||
|
#
|
||||||
|
# - name: Restart journald
|
||||||
|
# ansible.builtin.service:
|
||||||
|
# name: systemd-journald
|
||||||
|
# state: restarted
|
||||||
|
|
||||||
- name: Setup Foundry
|
- name: Setup Foundry
|
||||||
shell: |
|
ansible.builtin.shell: |
|
||||||
mkdir -p ~/foundry && cd ~/foundry
|
mkdir -p ~/foundry && cd ~/foundry
|
||||||
curl -L https://foundry.paradigm.xyz | bash
|
curl -L https://foundry.paradigm.xyz | bash
|
||||||
args:
|
args:
|
||||||
executable: /bin/bash
|
executable: /bin/bash
|
||||||
|
|
||||||
- name: Run foundryup
|
- name: Run foundryup
|
||||||
shell: |
|
ansible.builtin.shell: |
|
||||||
source ~/.bashrc && foundryup
|
source ~/.bashrc && foundryup
|
||||||
args:
|
args:
|
||||||
executable: /bin/bash
|
executable: /bin/bash
|
||||||
|
|
||||||
- name: Clone ritual-says-gm repository
|
- name: Clone repository
|
||||||
git:
|
ansible.builtin.git:
|
||||||
repo: https://gitea.vvzvlad.xyz/vvzvlad/ritual-says-gm.git
|
repo: https://gitea.vvzvlad.xyz/vvzvlad/ritual.git
|
||||||
dest: ~/ritual-says-gm
|
dest: "{{ ansible_env.HOME }}/node"
|
||||||
force: yes
|
version: "{{ git_version }}"
|
||||||
|
force: true
|
||||||
|
async: "{{ 60 * 15 }}"
|
||||||
|
poll: 30
|
||||||
|
|
||||||
- name: Update wallet, private key and RPC URL in project
|
- name: Update environment variables
|
||||||
shell: |
|
ansible.builtin.shell: |
|
||||||
cd ~/ritual-says-gm
|
chmod +x ./update.sh
|
||||||
bash update.sh {{ wallet }} {{ private_key }} {{ rpc_url }}
|
./update.sh ID "{{ serverid }}"
|
||||||
|
./update.sh GRIST_SERVER "{{ grist_server }}"
|
||||||
|
./update.sh GRIST_DOC_ID "{{ grist_doc_id }}"
|
||||||
|
./update.sh GRIST_API_KEY "{{ grist_api_key }}"
|
||||||
|
./update.sh WALLET_ADDRESS "{{ wallet }}"
|
||||||
|
./update.sh PRIVATE_KEY "{{ private_key }}"
|
||||||
|
./update.sh RPC_URL "{{ rpc_url }}"
|
||||||
|
args:
|
||||||
|
chdir: "{{ ansible_env.HOME }}/node"
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
- name: Remove old Forge and Infernet SDK
|
|
||||||
shell: |
|
- name: Install grpcbalancer dependencies
|
||||||
cd ~/ritual-says-gm
|
ansible.builtin.pip:
|
||||||
rm -rf projects/hello-world/contracts/lib/forge-std
|
name:
|
||||||
rm -rf projects/hello-world/contracts/lib/infernet-sdk
|
- grist-api
|
||||||
|
- flask
|
||||||
|
- requests
|
||||||
|
- waitress
|
||||||
|
extra_args: --break-system-packages
|
||||||
|
|
||||||
|
- name: Copy grpcbalancer files
|
||||||
|
ansible.builtin.shell: |
|
||||||
|
cp {{ ansible_env.HOME }}/node/grpcbalancer/grpc-balancer.py /usr/local/bin/
|
||||||
|
chmod 755 /usr/local/bin/grpc-balancer.py
|
||||||
|
args:
|
||||||
|
executable: /bin/bash
|
||||||
|
|
||||||
|
#- name: Install grpcbalancer service
|
||||||
|
# ansible.builtin.shell: |
|
||||||
|
# cp {{ ansible_env.HOME }}/node/grpcbalancer/grpc-balancer.service /etc/systemd/system/
|
||||||
|
# chmod 644 /etc/systemd/system/grpc-balancer.service
|
||||||
|
# args:
|
||||||
|
# executable: /bin/bash
|
||||||
|
|
||||||
|
#- name: Start and enable grpcbalancer service
|
||||||
|
# ansible.builtin.systemd:
|
||||||
|
# name: grpc-balancer
|
||||||
|
# state: started
|
||||||
|
# enabled: yes
|
||||||
|
# daemon_reload: yes
|
||||||
|
|
||||||
- name: Install Forge and Infernet SDK
|
- name: Install Forge and Infernet SDK
|
||||||
shell: |
|
ansible.builtin.shell: |
|
||||||
cd ~/foundry && source ~/.bashrc && foundryup
|
rm -rf {{ ansible_env.HOME }}/node/projects/hello-world/contracts/lib/forge-std
|
||||||
cd ~/ritual-says-gm
|
rm -rf {{ ansible_env.HOME }}/node/projects/hello-world/contracts/lib/infernet-sdk
|
||||||
cd projects/hello-world/contracts
|
cd {{ ansible_env.HOME }}/foundry && source {{ ansible_env.HOME }}/.bashrc && foundryup
|
||||||
|
cd {{ ansible_env.HOME }}/node/projects/hello-world/contracts
|
||||||
forge install --no-commit foundry-rs/forge-std
|
forge install --no-commit foundry-rs/forge-std
|
||||||
forge install --no-commit ritual-net/infernet-sdk
|
forge install --no-commit ritual-net/infernet-sdk
|
||||||
args:
|
args:
|
||||||
executable: /bin/bash
|
executable: /bin/bash
|
||||||
|
|
||||||
- name: Deploy container
|
- name: Deploy container
|
||||||
shell: |
|
ansible.builtin.shell: project=hello-world make deploy-container
|
||||||
cd ~/ritual-says-gm && project=hello-world make deploy-container
|
args:
|
||||||
|
chdir: "{{ ansible_env.HOME }}/node"
|
||||||
|
|
||||||
- name: Deploy contracts
|
- name: Deploy contracts
|
||||||
shell: cd ~/ritual-says-gm && project=hello-world make deploy-contracts 2>&1
|
ansible.builtin.shell: project=hello-world make deploy-contracts 2>&1
|
||||||
register: contract_deploy_output
|
register: contract_deploy_output
|
||||||
ignore_errors: yes
|
args:
|
||||||
retries: 3
|
chdir: "{{ ansible_env.HOME }}/node"
|
||||||
delay: 53
|
executable: /bin/bash
|
||||||
|
retries: 5
|
||||||
|
delay: 120
|
||||||
|
async: 120
|
||||||
|
poll: 30
|
||||||
until: '"ONCHAIN EXECUTION COMPLETE & SUCCESSFUL" in contract_deploy_output.stdout'
|
until: '"ONCHAIN EXECUTION COMPLETE & SUCCESSFUL" in contract_deploy_output.stdout'
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
- name: Update CallContract.s.sol with contract address
|
- name: Update CallContract.s.sol with contract address
|
||||||
shell: |
|
ansible.builtin.shell: bash update_contracts.sh
|
||||||
cd ~/ritual-says-gm
|
args:
|
||||||
contract_address=$(jq -r '.transactions[0].contractAddress' projects/hello-world/contracts/broadcast/Deploy.s.sol/8453/run-latest.json)
|
chdir: "{{ ansible_env.HOME }}/node"
|
||||||
checksum_address=$(python3 toChecksumAddress.py $contract_address)
|
|
||||||
sed -i "s/SaysGM(.*/SaysGM($checksum_address);/" projects/hello-world/contracts/script/CallContract.s.sol
|
|
||||||
|
|
||||||
- name: Call contract
|
- name: Call contract
|
||||||
shell: cd ~/ritual-says-gm && project=hello-world make call-contract 2>&1
|
ansible.builtin.shell: project=hello-world make call-contract 2>&1
|
||||||
register: contract_output
|
register: contract_call_output
|
||||||
ignore_errors: yes
|
args:
|
||||||
retries: 3
|
chdir: "{{ ansible_env.HOME }}/node"
|
||||||
delay: 55
|
executable: /bin/bash
|
||||||
until: '"ONCHAIN EXECUTION COMPLETE & SUCCESSFUL" in contract_output.stdout'
|
retries: 5
|
||||||
|
delay: 120
|
||||||
|
async: 120
|
||||||
|
poll: 30
|
||||||
|
until: '"ONCHAIN EXECUTION COMPLETE & SUCCESSFUL" in contract_call_output.stdout'
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
- name: Set Docker containers to restart unless stopped
|
# - name: Set Docker containers to restart unless stopped
|
||||||
shell: |
|
# ansible.builtin.shell: |
|
||||||
docker update --restart unless-stopped hello-world
|
# docker update --restart unless-stopped hello-world
|
||||||
docker update --restart unless-stopped infernet-node
|
# docker update --restart unless-stopped infernet-node
|
||||||
docker update --restart unless-stopped deploy-redis-1
|
# docker update --restart unless-stopped deploy-redis-1
|
||||||
docker update --restart unless-stopped infernet-anvil
|
# docker update --restart unless-stopped infernet-anvil
|
||||||
docker update --restart unless-stopped deploy-fluentbit-1
|
# docker update --restart unless-stopped deploy-fluentbit-1
|
||||||
|
|
||||||
- name: Create APT configuration file to assume yes
|
- name: Copy checker service file
|
||||||
copy:
|
ansible.builtin.copy:
|
||||||
dest: /etc/apt/apt.conf.d/90forceyes
|
dest: /etc/systemd/system/node-checker.service
|
||||||
content: |
|
content: |
|
||||||
APT::Get::Assume-Yes "true";
|
[Unit]
|
||||||
|
Description=Node Checker Service
|
||||||
- name: Set permissions on APT configuration file
|
After=network.target
|
||||||
file:
|
|
||||||
path: /etc/apt/apt.conf.d/90forceyes
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=root
|
||||||
|
WorkingDirectory={{ ansible_env.HOME }}/node
|
||||||
|
ExecStart=/usr/bin/python3 {{ ansible_env.HOME }}/node/checker.py
|
||||||
|
Restart=always
|
||||||
|
RestartSec=600
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
mode: '0644'
|
mode: '0644'
|
||||||
|
|
||||||
- name: Remove docker login credentials
|
- name: Enable and start node-checker service
|
||||||
shell: rm -rf /root/.docker/config.json
|
ansible.builtin.systemd:
|
||||||
ignore_errors: yes
|
name: node-checker
|
||||||
|
enabled: yes
|
||||||
|
state: started
|
||||||
|
daemon_reload: yes
|
||||||
|
|
||||||
|
#- name: Remove docker login credentials
|
||||||
|
# ansible.builtin.shell: rm -rf /root/.docker/config.json
|
||||||
|
@ -5,29 +5,27 @@
|
|||||||
},
|
},
|
||||||
"chain": {
|
"chain": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"trail_head_blocks": 0,
|
"trail_head_blocks": 3,
|
||||||
"rpc_url": "###RPC_URL###",
|
"rpc_url": "###RPC_URL###",
|
||||||
"registry_address": "0x3B1554f346DFe5c482Bb4BA31b880c1C18412170",
|
"registry_address": "0x3B1554f346DFe5c482Bb4BA31b880c1C18412170",
|
||||||
"wallet": {
|
"wallet": {
|
||||||
"max_gas_limit": 4000000,
|
"max_gas_limit": 4000000,
|
||||||
"private_key": "###PRIVATE_KEY###"
|
"private_key": "###PRIVATE_KEY###"
|
||||||
|
},
|
||||||
|
"snapshot_sync": {
|
||||||
|
"sleep": 3,
|
||||||
|
"batch_size": 800,
|
||||||
|
"starting_sub_id": 210000,
|
||||||
|
"sync_period": 30
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"startup_wait": 1.0,
|
"startup_wait": 1.0,
|
||||||
"docker": {
|
|
||||||
"username": "your-username",
|
|
||||||
"password": ""
|
|
||||||
},
|
|
||||||
"redis": {
|
"redis": {
|
||||||
"host": "redis",
|
"host": "redis",
|
||||||
"port": 6379
|
"port": 6379
|
||||||
},
|
},
|
||||||
"forward_stats": true,
|
"forward_stats": true,
|
||||||
"snapshot_sync": {
|
|
||||||
"sleep": 2,
|
|
||||||
"batch_size": 10000,
|
|
||||||
"starting_sub_id": 100000
|
|
||||||
},
|
|
||||||
"containers": [
|
"containers": [
|
||||||
{
|
{
|
||||||
"id": "hello-world",
|
"id": "hello-world",
|
||||||
|
8
rebuild.sh
Normal file
8
rebuild.sh
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
cd ~/ritual
|
||||||
|
project=hello-world make deploy-container
|
||||||
|
project=hello-world make deploy-contracts
|
||||||
|
bash update_contracts.sh
|
||||||
|
project=hello-world make call-contract
|
20
update.sh
20
update.sh
@ -1,23 +1,21 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
if [ "$#" -ne 3 ]; then
|
if [ "$#" -ne 2 ]; then
|
||||||
echo "Usage: $0 <wallet_address> <private_key> <rpc_url>"
|
echo "Usage: $0 <PARAMETER> <NEW_VALUE>"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
WALLET_ADDRESS=$1
|
PARAMETER=$1
|
||||||
PRIVATE_KEY=$2
|
NEW_VALUE=$2
|
||||||
RPC_URL=$3
|
|
||||||
|
|
||||||
# List of files
|
# Список файлов
|
||||||
FILES=(
|
FILES=(
|
||||||
"./projects/hello-world/container/config.json"
|
"./projects/hello-world/container/config.json"
|
||||||
"./projects/hello-world/contracts/Makefile"
|
"./projects/hello-world/contracts/Makefile"
|
||||||
|
"grist.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
for FILE in "${FILES[@]}"; do
|
for FILE in "${FILES[@]}"; do
|
||||||
EXPANDED_FILE=$(eval echo "$FILE")
|
EXPANDED_FILE=$(eval echo "$FILE")
|
||||||
sed -i "s|###WALLET_ADDRESS###|$WALLET_ADDRESS|g" "$EXPANDED_FILE"
|
sed -i "s|###$PARAMETER###|$NEW_VALUE|g" "$EXPANDED_FILE"
|
||||||
sed -i "s|###PRIVATE_KEY###|$PRIVATE_KEY|g" "$EXPANDED_FILE"
|
done
|
||||||
sed -i "s|###RPC_URL###|$RPC_URL|g" "$EXPANDED_FILE"
|
|
||||||
done
|
|
6
update_contracts.sh
Normal file
6
update_contracts.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
contract_address=$(jq -r '.transactions[0].contractAddress' projects/hello-world/contracts/broadcast/Deploy.s.sol/8453/run-latest.json)
|
||||||
|
checksum_address=$(python3 toChecksumAddress.py $contract_address)
|
||||||
|
sed -i "s/SaysGM(.*/SaysGM($checksum_address);/" projects/hello-world/contracts/script/CallContract.s.sol
|
Reference in New Issue
Block a user