Skip to content

Commit

Permalink
edit paev update code
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Jan 7, 2025
1 parent 24e09d6 commit 36d5626
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 47 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/update-paev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ on:
new_start_value:
required: true
description: 'The new start index (e.g. 20000000)'
new_plugin_url:
new_lib9c_commit:
required: true
description: 'The plugin URL for the newly added evaluator (e.g. https://lib9c-plugin.s3.us-east-2.amazonaws.com/678ac90398f376698ee75da63ed089fe678d11e6/linux-arm64.zip)'
description: 'The lib9c commit for the newly added evaluator (e.g. 678ac90398f376698ee75da63ed089fe678d11e6)'

jobs:
update-paev:
Expand All @@ -42,7 +42,7 @@ jobs:
python cli.py update-paev \
${{ github.event.inputs.network_type }} \
${{ github.event.inputs.new_start_value }} \
${{ github.event.inputs.new_plugin_url }}
${{ github.event.inputs.new_lib9c_commit }}
working-directory: ./scripts
env:
GITHUB_TOKEN: ${{ secrets.P_GITHUB_TOKEN }}
Expand Down
82 changes: 40 additions & 42 deletions scripts/app/update_paev.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,97 @@
import boto3 # type: ignore
import boto3
import requests
import json

from app.client import GithubClient
from app.config import config
from urllib.parse import urlparse

class PluggableActionEvaluatorUpdater:
def __init__(self) -> None:
self.github_client = GithubClient(
config.github_token, org="planetarium", repo="TEMP"
)
pass

def prep_update(
self,
network_type: str,
new_start_value: int,
new_plugin_url: str
new_lib9c_commit: str
):
"""
Prepares and updates the PAEV configuration for both appsettings.json
and appsettings-nodeinfra.json by computing cutoff_value automatically.
This method downloads:
1) appsettings.json (for arm64),
2) appsettings-nodeinfra.json (for x64),
modifies the final evaluator's end range to (new_start_value - 1),
and appends a new evaluator from new_start_value to max long, using
the plugin path from new_lib9c_commit + (arm64 or x64 suffix).
"""
cutoff_value = new_start_value - 1 # always set cutoff_value to new_start_value - 1

# Define base URL and suffixes
base_url = "https://lib9c-plugin.s3.us-east-2.amazonaws.com/"
arm64_suffix = "/linux-arm64.zip"
x64_suffix = "/linux-x64.zip"

# Define URLs based on network type
paev_urls = [
f"https://9c-cluster-config.s3.us-east-2.amazonaws.com/9c-main/{network_type}/appsettings.json",
f"https://9c-cluster-config.s3.us-east-2.amazonaws.com/9c-main/{network_type}/appsettings-nodeinfra.json",
]

# Iterate over each URL in the list
for url in paev_urls:
self.update(url, cutoff_value, new_start_value, new_plugin_url)
# Choose the suffix based on the presence of "nodeinfra"
suffix = x64_suffix if "nodeinfra" in url else arm64_suffix

# Construct the plugin URL from the new commit
plugin_url = f"{base_url}{new_lib9c_commit}{suffix}"

# Update the file, passing new_start_value
self.update(url, new_start_value, plugin_url)

def update(
self,
paev_url: str,
cutoff_value: int,
new_start_value: int,
new_plugin_url: str,
bucket_name: str = "9c-cluster-config"
):
"""
1. Download the appsettings.json from paev_url.
2. Modify the last evaluator so that its end = cutoff_value.
2. Modify the last evaluator so that its end = new_start_value - 1.
3. Append a new evaluator that starts at new_start_value and ends at a very large number.
4. Upload updated JSON back to S3.
"""

# Example logic adapted to your code:
import boto3
import requests
import json
from urllib.parse import urlparse

def url_exists(url):
def url_exists(url: str) -> bool:
try:
resp = requests.head(url, allow_redirects=True)
return resp.status_code == 200
except requests.exceptions.RequestException as e:
print(f"Error checking URL {url}: {e}")
return False

def extract_path_from_url(url):
def extract_path_from_url(url: str) -> str:
parsed = urlparse(url)
return parsed.path.lstrip('/')

# 1) Validate URLs
if not url_exists(paev_url):
raise Exception(f"URL not accessible: {paev_url}")
if not url_exists(new_plugin_url):
raise Exception(f"Plugin URL not accessible: {new_plugin_url}")

# 2) Download the JSON
resp = requests.get(paev_url)
if resp.status_code != 200:
raise Exception(f"Failed to download JSON from {paev_url}, status: {resp.status_code}")

data = resp.json()

# 3) Modify the last pair
pairs = data.get('Headless', {}).get('ActionEvaluator', {}).get('pairs', [])
if not pairs:
raise Exception("No pairs found in ActionEvaluator configuration.")

# Modify the last pair
last_pair = pairs[-1]
last_pair['range']['end'] = cutoff_value
# The last evaluator ends just before new_start_value
last_pair['range']['end'] = new_start_value - 1

# Append the new pair
# 4) Append the new evaluator
new_pair = {
"range": {
"start": new_start_value,
Expand All @@ -97,7 +104,7 @@ def extract_path_from_url(url):
}
pairs.append(new_pair)

# Upload updated JSON back to S3
# 5) Upload the modified JSON back to S3
s3_resource = boto3.resource('s3')
file_content = json.dumps(data, indent=4)
file_name = extract_path_from_url(paev_url)
Expand All @@ -107,18 +114,9 @@ def extract_path_from_url(url):
ContentType='application/json'
)

print(f"Successfully updated {paev_url} with new evaluator from {cutoff_value} to {new_start_value}.")

def url_exists(url):
try:
response = requests.head(url, allow_redirects=True)
return response.status_code == 200
except requests.exceptions.RequestException as e:
print(f"Error checking URL {url}: {e}")
return False

def extract_path_from_url(url):
# Parse the URL to get the path part
parsed_url = urlparse(url)
# The `path` attribute contains the path component of the URL
return parsed_url.path.lstrip('/')
print(
f"Successfully updated {paev_url}:\n"
f" - Last evaluator end = {new_start_value - 1}\n"
f" - New evaluator start = {new_start_value}, end = 9223372036854775807\n"
f" - Plugin URL = {new_plugin_url}"
)
4 changes: 2 additions & 2 deletions scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ def update_apv(
def update_paev(
network_type: str = typer.Argument(..., help="e.g. mainnet, internal, etc."),
new_start_value: int = typer.Argument(..., help="The start index for the newly added evaluator (e.g. 20000000)"),
new_plugin_url: str = typer.Argument(..., help="The plugin URL for the newly added evaluator"),
new_lib9c_commit: str = typer.Argument(..., help="The new lib9c commit for the newly added evaluator"),
):
"""
Update the PluggableActionEvaluator (PAEV) configuration.
This modifies the last evaluator so that it ends at new_start_value - 1,
then appends a new evaluator starting at new_start_value.
"""
PluggableActionEvaluatorUpdater().prep_update(network_type, new_start_value, new_plugin_url)
PluggableActionEvaluatorUpdater().prep_update(network_type, new_start_value, new_lib9c_commit)

if __name__ == "__main__":
k8s_app()

0 comments on commit 36d5626

Please sign in to comment.