-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbulk_launcher.py
200 lines (160 loc) · 6.77 KB
/
bulk_launcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import asyncio
from time import sleep
import subprocess
import json
import os
import re
import shutil
from communex.compat.key import Ss58Address
from loguru import logger
# Set the environment variable
os.environ['COMX_YES_TO_ALL'] = 'true'
os.environ['COMX_OUTPUT_JSON'] = 'true'
def copy_and_rename_class(filename, original_classname, new_classname):
with open(filename, 'r') as file:
lines = file.readlines()
class_found = False
new_lines = []
class_lines = []
inside_class = False
class_indent = None
for line in lines:
new_lines.append(line)
if not class_found and re.match(rf'^\s*class {original_classname}', line):
class_found = True
inside_class = True
class_indent = re.match(r"\s*", line).group()
if inside_class:
current_indent = re.match(r"\s*", line).group()
if current_indent == "" and len(class_lines) > 0:
inside_class = False
else:
class_lines.append(line)
# Rename the copied class
if class_lines:
class_lines[0] = class_lines[0].replace(original_classname, new_classname, 1)
new_lines.append("\n")
new_lines.extend(class_lines)
new_lines.append("\n")
else:
raise ValueError(f"Class {original_classname} not found in {filename}")
# Write the new content back to the file
with open(filename, 'w') as file:
file.writelines(new_lines)
# Function to validate the module path
def module_path_check(module_path):
try:
if re.match(r'^[a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*$', module_path):
filename, classname = module_path.split('.')
return filename, classname
else:
print("Invalid characters in module path")
return None, None
except Exception as e:
print(f"An error occurred: {e}")
return None, None
def serve_modules(module_path, source_module, port, NumModules, Netuid):
for i in range(NumModules):
# Separates by filename and classname (by the dot)
filename, classname = module_path_check(module_path)
classname_instance = f"{classname}_{i}"
module_name = f"{module_path}_{i}"
next_port = port
# Create keys for the modules if they don't exist
key_path = os.path.expanduser(f"~/.commune/key/{module_name}.json")
if not os.path.isfile(key_path):
subprocess.run(["comx", "key", "create", module_name])
# Check if the destination file does not exist
source_directory = os.path.dirname(source_module)
# Define the new file path
new_file_path = os.path.join(source_directory, filename + ".py")
if not os.path.isfile(new_file_path):
# Copy the source module to the new file path
shutil.copy(source_module, new_file_path)
# Creates a new class in the new miner file for this specific miner
copy_and_rename_class(new_file_path, "Miner", classname_instance)
print("Serving Miner")
command = f'pm2 start "python -m eden_subnet.miner.{filename} --key_name {module_name} --host 0.0.0.0 --port {next_port}" --name "{module_name}"'
os.system(command)
print("Miner served.")
next_port = port + i
def get_ss58_address(name):
# Construct the path to the JSON file
file_path = os.path.expanduser(f"~/.commune/key/{name}.json")
try:
# Open and read the JSON file
with open(file_path, 'r') as file:
data = json.load(file)
# Extract the 'data' field and parse it as JSON
data_json = json.loads(data['data'])
# Return the 'ss58_address' field
return data_json['ss58_address']
except FileNotFoundError:
print(f"No file found for {name}")
return None
except KeyError as e:
print(f"Key error: {e} - Check JSON structure")
return None
except json.JSONDecodeError:
print("Error decoding JSON")
return None
except Exception as e:
logger.error(f"Error procesing thing:\n{e}")
return None
def register(module_path, wan_ip, port, NumModules, Netuid):
for i in range(NumModules):
key = "module"
module_name = f"{module_path}_{i}"
next_port = port + i
ss58 = Ss58Address(module_name)
print("Port: ", next_port)
print("Transfer Com to new miner key")
try:
value = subprocess.run(["comx", "balance", "transfer", key, "305", ss58], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(value.stdout)
except Exception as e:
logger.error(f"Error processing thing:\n{e}")
sleep(10)
print("Register new miner key")
try:
value = subprocess.run(["comx", "module", "register", "--ip", wan_ip, "--port", f"{next_port}", "--stake", "300", module_name, module_name, "--netuid", f"{Netuid}"], check=True)
except subprocess.CalledProcessError as e:
logger.error(f"Error registering miner, {e}")
except Exception as e:
logger.error(f"Error processing thing:\n{e}")
print(f"Registered {module_name} at {wan_ip}:{next_port}")
sleep(10)
print("Remove Temp Stake from new miner")
try:
value = subprocess.run(["comx", "balance", "unstake", module_name, "250", ss58, "--netuid", f"{Netuid}"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(value.stdout)
except Exception as e:
logger.error(f"Error processing thing:\n{e}")
print(f"Stake Removed")
sleep(10)
print("Send fund back from new miner")
try:
value = subprocess.run(["comx", "balance", "transfer", module_name, "250", ss58], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(value.stdout)
except Exception as e:
logger.error(f"Error processing thing:\n{e}")
print(f"Funds send")
sleep(10)
#print("Test call miner")
# c call model.openrouter::cool2/generate hey
#subprocess.run(["c", "call", module_name+"/generate", "hey"])
#sleep(5)
# Wait before repeating the registration process
print("Register loop: f{i}")
sleep(60)
if __name__ == "__main__":
source_miner="eden_subnet/miner/miner.py"
source_validator="eden_subnet/validator/validator.py"
module_path="z90.studio"
source_module=source_miner
wan_ip="199.126.197.240"
port=26400
NumModules=10
Netuid=10
serve_modules(module_path=module_path,source_module=source_miner,port=port, NumModules=NumModules, Netuid=Netuid)
register(module_path=module_path,wan_ip=wan_ip,port=port, NumModules=NumModules, Netuid=Netuid)