diff --git a/.github/workflows/revert-update-paev.yaml b/.github/workflows/revert-update-paev.yaml new file mode 100644 index 000000000..368757893 --- /dev/null +++ b/.github/workflows/revert-update-paev.yaml @@ -0,0 +1,41 @@ +name: Revert Pluggable Action Evaluator + +on: + workflow_dispatch: + inputs: + network_type: + required: true + default: odin + type: choice + options: + - odin + - heimdall + - thor + description: 'Which mainnet network to revert?' + +jobs: + revert-paev: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v2 + + - uses: actions/setup-python@v2.2.2 + with: + python-version: 3.10.13 + + - name: install dependencies + run: | + python -m pip install -r requirements.txt + flit install + working-directory: ./scripts + + - name: Revert Pluggable Action Evaluator + run: | + python cli.py revert-paev ${{ github.event.inputs.network_type }} + working-directory: ./scripts + env: + GITHUB_TOKEN: ${{ secrets.P_GITHUB_TOKEN }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/scripts/app/revert_update_paev.py b/scripts/app/revert_update_paev.py new file mode 100644 index 000000000..ccf04629d --- /dev/null +++ b/scripts/app/revert_update_paev.py @@ -0,0 +1,68 @@ +import boto3 +import requests +import json +from urllib.parse import urlparse + +class PAEVReverter: + def revert_last_pair( + self, + paev_url: str, + bucket_name: str = "9c-cluster-config" + ): + """ + 1) Download the appsettings.json from paev_url. + 2) Remove the LAST pair from the pairs array. + 3) Update the (new) LAST pair's 'range.end' to 9223372036854775807. + 4) Upload updated JSON back to S3. + """ + + data = self.download_json(paev_url) + + pairs = data.get("Headless", {}).get("ActionEvaluator", {}).get("pairs", []) + if not pairs: + raise Exception("No pairs found in ActionEvaluator configuration.") + + # 1. Remove the last pair + last_pair = pairs.pop() + print(f"Removed pair: {last_pair}") + + # 2. If there is still at least one pair left, set its 'end' to a very large number + if not pairs: + # Edge case: If there was only one pair, we removed it, so now zero remain. + # You can decide how to handle that scenario, or raise an exception. + raise Exception("No pairs left after removing the last one. Cannot revert further.") + + # 3. Edit the new last pair's end value + pairs[-1]["range"]["end"] = 9223372036854775807 + + # 4. Upload updated JSON back to S3 + self.upload_json(paev_url, data, bucket_name) + print(f"Successfully reverted the last pair in {paev_url}.") + + def download_json(self, url: str) -> dict: + def url_exists(u: str) -> bool: + try: + resp = requests.head(u, allow_redirects=True) + return resp.status_code == 200 + except requests.exceptions.RequestException: + return False + + if not url_exists(url): + raise Exception(f"Cannot access {url}.") + + resp = requests.get(url) + if resp.status_code != 200: + raise Exception(f"Failed to download JSON from {url}, status={resp.status_code}") + + return resp.json() + + def upload_json(self, url: str, data: dict, bucket_name: str): + file_content = json.dumps(data, indent=4) + file_name = urlparse(url).path.lstrip('/') + + s3 = boto3.resource('s3') + s3.Object(bucket_name, file_name).put( + Body=file_content, + ContentType='application/json' + ) + print(f"Reverted file uploaded back to {bucket_name}/{file_name}") diff --git a/scripts/cli.py b/scripts/cli.py index b0f341e52..30513ed42 100644 --- a/scripts/cli.py +++ b/scripts/cli.py @@ -97,5 +97,23 @@ def update_paev( """ PluggableActionEvaluatorUpdater().prep_update(network_type, new_start_value, new_lib9c_commit) +@k8s_app.command() +def revert_paev( + network_type: str = typer.Argument(..., help="Which mainnet network to revert? e.g. odin, heimdall, thor."), +): + """ + Revert the last pair from both appsettings.json and appsettings-nodeinfra.json, + setting the new last pair's end to 9223372036854775807. + """ + reverter = PAEVReverter() + + 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", + ] + + for url in urls: + reverter.revert_last_pair(url) + if __name__ == "__main__": k8s_app()