Skip to content

Commit

Permalink
add dumping and uploading to auto-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
stfnberat committed Sep 6, 2024
1 parent 9ec42ed commit 109df50
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/bt_auto_dumper/_v2/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
import argparse
import pathlib
import subprocess
import zipfile

import requests


def main(apiver: str | None = None):
apiver = apiver or pathlib.Path(__file__).parent.name
parser = argparse.ArgumentParser(description=f"BT Auto Dumper CLI {apiver}")
parser.parse_args()
parser.add_argument("subnet_identifier", help="Subnet Identifier", type=str)
parser.add_argument("validator_address", help="Validator Address", type=str)
parser.add_argument("api_key", help="API Key", type=str)

args = parser.parse_args()

dump_and_upload(args.subnet_identifier, args.validator_address, args.api_key)


def dump_and_upload(subnet_identifier: str, validator_address: str, api_key: str):
subnets = {
"compute_horde": {
"12": ["echo 'Mainnet Command 1'", "echo 'Mainnet Command 2'"],
"t147": ["echo 'Testnet Command 1'", "echo 'Testnet Command 2'"],
}
}

if subnet_identifier in subnets:
commands = subnets[subnet_identifier]
else:
commands = {}
for subnet_data in subnets.values():
if subnet_identifier in subnet_data:
commands[subnet_identifier] = subnet_data[subnet_identifier]
break

if not commands:
print(f"Subnet identifier {subnet_identifier} not found.")
return

output_files = []
for subnet_id, cmds in commands.items():
for i, command in enumerate(cmds, start=1):
output_file = f"{subnet_id}_{i}.txt"
with open(output_file, "w") as f:
f.write(f"Command: {command}\n")
result = subprocess.run(command, shell=True, capture_output=True, text=True)
f.write(result.stdout)
output_files.append(output_file)

zip_filename = "output.zip"
with zipfile.ZipFile(zip_filename, "w") as zipf:
for file in output_files:
zipf.write(file)
send_to_autovalidator(zip_filename, validator_address, api_key)


def send_to_autovalidator(zip_filename, validator_address, api_key):
url = f"{validator_address}/api/v1/files/"
headers = {"Authorization": f"Token {api_key}"}
files = {"file": open(zip_filename, "rb")}

response = requests.post(url, headers=headers, files=files)
if response.status_code == 201:
print("File successfully uploaded and resource created.")
elif response.status_code == 200:
print("Request succeeded.")
else:
print(f"Failed to upload file. Status code: {response.status_code}")
print(response.text)


if __name__ == "__main__":
Expand Down

0 comments on commit 109df50

Please sign in to comment.