forked from 0rtis/mochimo-farm-manager-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mining_farm.py
306 lines (248 loc) · 10.6 KB
/
mining_farm.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
"""
MIT License
Copyright (c) 2018 Ortis (cao.ortis.org@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import sys
import argparse
import getpass
import logging
import json
import threading
import miner_statistics
from multithread_http_server import MultiThreadHttpServer
from mining_farm_http_handler import MiningFarmHTTPHandler
from urllib.parse import urlparse, parse_qs
from pathlib import Path
from miner import MinerConfig
from encryption import AESCipher
LOG_LEVEL = logging.INFO
# must set the log level as function so child process can be configured as well
def set_log_level(log_level=None):
if log_level is None:
log_level = LOG_LEVEL
logging.basicConfig(level=log_level)
logging.getLogger("paramiko").setLevel(logging.WARNING)
class MiningFarm:
def __init__(self, html_repository, farm_config_path, password=None, bind="127.0.0.1:80", http_parallelism=5, stat_parallelism=2, stat_heartbeat=30, log=None):
self.stop_requested = False
if log is None:
self.log = logging.getLogger("farm")
else:
self.log = log
self.html_repository = html_repository
config_file = open(farm_config_path, 'r')
config = json.load(config_file)
config_file.close()
self.miners = []
self.stat_lock = threading.Lock()
self.cipher = None
miner_ids = []
for miner_config in config["miners"]:
config = MinerConfig(miner_id=miner_config["id"], host=self.__parse_sensitive_field(miner_config, "host", password), user=self.__parse_sensitive_field(miner_config, "user", password))
if config.miner_id in miner_ids:
raise Exception("Miner id "+config.miner_id+" already exists")
else:
miner_ids.append(config.miner_id)
config.password = self.__parse_sensitive_field(miner_config, "password", password)
config.private_key_path = self.__parse_sensitive_field(miner_config, "privateKeyPath", password)
config.start_command = self.__parse_sensitive_field(miner_config, "startCommand", password)
config.stop_command = self.__parse_sensitive_field(miner_config, "stopCommand", password)
config.version_command = self.__parse_sensitive_field(miner_config, "versionCommand", password)
config.reboot_command = self.__parse_sensitive_field(miner_config, "rebootCommand", password)
config.log_command = self.__parse_sensitive_field(miner_config, "logCommand", password)
miner = config.build()
self.miners.append(miner)
buffer = bind.split(':')
self.host = buffer[0].strip()
self.port = int(buffer[1].strip())
self.http_parallelism = http_parallelism
self.statistic_pool = miner_statistics.StatisticsProcessingPool(self, stat_parallelism, stat_heartbeat)
def start(self):
self.statistic_pool.start()
self.start_server()
def stop(self):
self.stop_requested = True
self.statistic_pool.stop()
def start_server(self):
self.log.info("Binding server to "+self.host+":"+str(self.port))
server = MultiThreadHttpServer((self.host, self.port), self.http_parallelism, MiningFarmHTTPHandler, request_callback=self.http_handler)
server.start()
def get_miner(self, miner_id):
for miner in self.miners:
if miner.miner_id == miner_id:
return miner
return None
def get_miners(self):
copy = []
copy.extend(self.miners)
return copy
def get_statistics(self, *args):
if len(args) >= 1:
return self.statistic_pool.get_statistics(args[0])
farm_statistics = {}
farm_statistics["totalHPS"] = 0
farm_statistics["totalSolved"] = 0
solving_count = 0
statuses = []
farm_statistics["farm"] = statuses
for miner in self.miners:
status = self.get_statistics(miner.miner_id)
if status is None:
continue
if "hps" in status:
farm_statistics["totalHPS"] += float(status["hps"])
if "solved" in status:
farm_statistics["totalSolved"] += int(status["solved"])
if "solving" in status and status["solving"]:
solving_count += 1
statuses.append(status)
if len(statuses) > 0:
farm_statistics["solvingRate"] = 100 * float(solving_count / len(statuses))
else:
farm_statistics["solvingRate"] = 0
return farm_statistics
def clear_statistics(self, miner_id):
self.statistic_pool.init_statistics(miner_id)
def http_handler(self, http_request):
url = urlparse(http_request.path)
if len(url.path) <= 1:
http_request.send_response(301)
http_request.send_header('Location', "/dashboard.html?refresh=10000")
http_request.end_headers()
elif url.path.upper().endswith(".HTML"):
html_page = Path(MINING_FARM.html_repository + url.path)
if html_page.is_file():
self.log.info("Serving HTML page " + str(html_page.absolute()))
http_request.send_response(200)
http_request.send_header('Content-type', 'text/html')
http_request.end_headers()
http_request.wfile.write(html_page.absolute().read_bytes())
else:
self.log.info("HTML file " + url.path + " not found")
http_request.send_response(404)
elif url.path.upper() == "/STATUS":
http_request.send_response(200)
http_request.send_header('Content-type', 'application/json')
http_request.end_headers()
farm_statistics = self.get_statistics()
http_request.wfile.write(bytes(json.dumps(farm_statistics, indent=4), "utf-8"))
elif url.path.upper() == "/MINER":
miner_id = MiningFarm.__get_parameter(url.query, 'id')
miner = self.get_miner(miner_id)
if miner is None:
http_request.send_response(400)
http_request.send_header('Content-type', 'application/json')
http_request.end_headers()
http_request.wfile.write(bytes("{\"error\": \"Miner not found\"}", "utf-8"))
else:
http_request.send_response(200)
http_request.send_header('Content-type', 'application/json')
http_request.end_headers()
statistics = self.get_statistics(miner.miner_id)
http_request.wfile.write(bytes(json.dumps(statistics, indent=4), "utf-8"))
elif url.path.upper() == "/COMMAND":
miner_id = MiningFarm.__get_parameter(url.query, 'id')
miner = self.get_miner(miner_id)
if miner is None:
http_request.send_response(400)
http_request.send_header('Content-type', 'application/json')
http_request.end_headers()
http_request.wfile.write(bytes("{\"error\": \"Miner not found\"}", "utf-8"))
else:
command = MiningFarm.__get_parameter(url.query, 'cmd')
if command is None:
http_request.send_response(400)
http_request.send_header('Content-type', 'application/json')
http_request.end_headers()
http_request.wfile.write(bytes("{\"error\": \"Command not found\"}", "utf-8"))
elif command.upper() == "START":
if miner.start():
http_request.send_response(200)
self.clear_statistics(miner.miner_id)
else:
http_request.send_response(500)
elif command.upper() == "STOP":
if miner.stop():
http_request.send_response(200)
self.clear_statistics(miner.miner_id)
else:
http_request.send_response(500)
elif command.upper() == "REBOOT":
if miner.reboot():
http_request.send_response(200)
self.clear_statistics(miner.miner_id)
else:
http_request.send_response(400)
http_request.send_header('Content-type', 'application/json')
http_request.end_headers()
http_request.wfile.write(bytes("{\"error\": \"Command not found\"}", "utf-8"))
else:
http_request.send_response(404)
@staticmethod
def __get_parameter(query, parameter_id):
params = parse_qs(query)[parameter_id]
if len(params) != 1: # params is a list
return None
else:
return params[0]
def __parse_sensitive_field(self, miner_config, field_name, password):
if field_name in miner_config and miner_config[field_name] is not None:
return miner_config[field_name]
else:
encrypted_field_name = "encrypted"+field_name[0].upper()+field_name[1:]
if encrypted_field_name in miner_config:
if self.cipher is None: # check cipher
if password is None:
password = getpass.getpass("Farm configuration file password:")
self.cipher = AESCipher(password)
return self.cipher.decrypt_as_string(miner_config[encrypted_field_name]) # decrypt field
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Nyzo manager')
parser.add_argument('html_repository', default="html", help="The HTML directory")
parser.add_argument('farm_file', default="example_mining_farm.json", help="The farm configuration file")
parser.add_argument('-pwd', dest="password", default=None, help="Farm configuration file password")
parser.add_argument('-b', dest="bind", default="127.0.0.1:80", help="Host to bind")
parser.add_argument('-hp', dest="http_parallelism", type=int, default=5, help="Number of http handlers")
parser.add_argument('-sp', dest="stat_parallelism", type=int, default=3, help="Number of process for statistics computing")
parser.add_argument('-sh', dest="stat_heartbeat", type=int, default=30, help="Delay between statistic computation in seconds")
parser.add_argument('-ll', dest="log_level", type=str, default="INFO", help="Log level (DEBUG, INFO, WARNING, WARN, ERROR)")
args = parser.parse_args(sys.argv[1:])
LOG_LEVEL = args.log_level.upper()
if args.log_level.upper() == "DEBUG":
LOG_LEVEL = logging.DEBUG
elif args.log_level.upper() == "WARNING":
LOG_LEVEL = logging.WARNING
elif args.log_level.upper() == "WARN":
LOG_LEVEL = logging.WARN
elif args.log_level.upper() == "ERROR":
LOG_LEVEL = logging.ERROR
else:
LOG_LEVEL = logging.INFO
set_log_level()
log = logging.getLogger("farm")
try:
MINING_FARM = MiningFarm(args.html_repository, args.farm_file, args.password, args.bind, http_parallelism=args.http_parallelism, stat_parallelism=args.stat_parallelism, stat_heartbeat=args.stat_heartbeat, log=log)
MINING_FARM.start()
except KeyboardInterrupt:
MINING_FARM.stop()
log.info("HTTP server stopped")
except Exception as e:
log.error(str(e))
sys.exit(-1)