-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
374 lines (293 loc) · 14.5 KB
/
app.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from flask import Flask, render_template, request, jsonify, Response, send_file, redirect
from functions import *
import json
import subprocess
import os
import paramiko
import time
import re
import hjson
from datetime import datetime
import globals
import sched
app = Flask(__name__)
# Open the log file in append mode
try:
# Open the log file in append mode
log_file = open('log.log', 'a+')
except FileNotFoundError:
# If the file doesn't exist, create it
log_file = open('log.log', 'w+')
@app.route('/')
def index():
# Clear the log file
bot_instances = load_instances()
open('log.log', 'w').close()
if bot_instances == []:
message = (f"load config: ERROR: config file empty! Did you create it?")
write_to_log(log_file, message)
return render_template('index.html', instances=bot_instances)
@app.route('/get_data', methods=['POST'])
def get_data():
bot_instances = load_instances()
instance_id = int(request.form['instance_id'])
instance = next((inst for inst in bot_instances['instances'] if inst['id'] == instance_id), None)
#update_available = False #default to false
# print (instance)
# time.sleep(60)
if instance:
try:
sftp_client, exec_client = ssh_connect(instance, log_file)
#print('1')
bot_active = check_bot_running(exec_client, instance)
#print('2')
try:
config_file_path = instance['ds_config_file']
with sftp_client.open(config_file_path, 'r') as file:
config_data = json.load(file)
except Exception as e:
message = (f"get_data: ERROR: I could not find the specified config file [ds_config_file]")
write_to_log(log_file, message, instance)
close_ssh_connect()
return jsonify({'error': 'config file not found'})
commandline_data = instance['ds_start_command']
#close_ssh_connect(sftp_client,exec_client)
update_available = verify_application_version(exec_client, instance, log_file)
#print('3')
#update_available = True #debug
strategies = find_strategies_in_multibot(instance, exec_client, log_file)
message = (f"get_data: available strategies: {strategies}")
write_to_log(log_file, message, instance)
# Serialize the data without sorting keys
response_data = json.dumps({'config_data': config_data, 'commandline_data': commandline_data, 'bot_status': bot_active, 'update_available': update_available}, indent=2, sort_keys=False)
message = (f"get_data: done")
write_to_log(log_file, message, instance)
return Response(response_data, content_type='application/json')
except Exception as e:
message = (f"get_data: ERROR3 {str(e)}")
write_to_log(log_file, message, instance)
try:
close_ssh_connect()
except:
pass
return jsonify({'error': str(e)})
else:
message = (f"get_data: ERROR1 {str(e)}")
write_to_log(log_file, message, instance)
close_ssh_connect()
return jsonify({'error': 'Instance not found'})
@app.route('/get_config', methods=['POST'])
def get_config():
instances_data = load_instances()
# Return instances_data as a JSON string
response_data = json.dumps({'config_data': instances_data}, indent=2)
message = "get_config: ok"
write_to_log(log_file, message)
return Response(response_data, content_type='application/json')
@app.route('/save_json', methods=['POST'])
def save_json():
try:
#1: check if valid json
#2: determine config type (ds config OR controller config
#3.1: ds config code
#3.2: controller code
new_json = request.form['json']
#print (new_json)
# Check if the new JSON is valid JSON
try:
json.loads(new_json)
except json.JSONDecodeError as e:
return jsonify({'success': False, 'error': f'Invalid JSON: {e}'})
new_json_dict = json.loads(new_json)
if new_json_dict.get('api') is not None:
# print ("its a remote config.json")
try:
bot_instances = load_instances()
instance_id = int(request.form['instance_id'])
instance = next((inst for inst in bot_instances['instances'] if inst['id'] == instance_id), None)
sftp_client, exec_client = ssh_connect(instance, log_file)
config_file_path = instance['ds_config_file']
with sftp_client.open(config_file_path, 'w') as file:
file.write(new_json)
message = (f"save_config: succesfully saved the config on the remote server")
write_to_log(log_file, message, instance)
return jsonify({'success': True})
except Exception as e:
message = (f"save_config: ERROR: {str(e)}")
write_to_log(log_file, message, instance)
return jsonify({'success': False, 'error': str(e)})
elif new_json_dict.get('instances') is not None:
try:
# print ("its local controller hjson")
# Get the updated instances data from the form
updated_instances_data = new_json
# Write the updated data to the instances.hjson file
with open('config.hjson', 'w') as file:
file.write(updated_instances_data)
message = (f"save_config: succesfully saved the instances config")
write_to_log(log_file, message)
load_instances() #reload the instances for the selector.
# index()
return jsonify({'success': True})
except Exception as e:
message = (f"save_config: ERROR: {str(e)}")
write_to_log(log_file, message)
return jsonify({'success': False, 'error': str(e)})
else:
# print("None of the conditions are True")
message = (f"save_config: ERROR: could not dermine config type")
write_to_log(log_file, message, instance)
return jsonify({'success': False, 'error': str(e)})
except Exception as e:
message = (f"save_config: ERROR: {str(e)}")
write_to_log(log_file, message)
return jsonify({'success': False, 'error': str(e)})
@app.route('/restart_application', methods=['POST'])
def restart_application():
"""
1. match Startcommand commandlineData against instance[ds_start_command]. if not equal, update config.hjson.
2. def stop_application
3. def verify_application_active
4. def start_application
5. def verify_application_active
6. log
"""
bot_instances = load_instances()
instance_id = int(request.form['instance_id'])
commandlineData = request.form['commandLineData']
instance = next((inst for inst in bot_instances['instances'] if inst['id'] == instance_id), None)
#print (instance)
#time.sleep(60)
if instance:
try:
write_to_log(log_file, "(re)start_application: restarting, hold on", instance)
"""
1. check if commandlineData is equal to instance['ds_start_command'].
2.1 if not. save into config.hjson.
2.2 if equal, continue
"""
if commandlineData != instance['ds_start_command']:
update_ds_start_command(instance, commandlineData, log_file)
# #save startcommand file
# commandline_file_path = instance['ds_start_commandline_file']
# with sftp_client.open(commandline_file_path, 'w') as file:
# file.write(commandlineData)
# print (commandlineData)
#open connection to vps
sftp_client, exec_client = ssh_connect(instance, log_file)
#time.sleep(60)
func_stop_application(exec_client, instance, log_file)
bot_active = verify_application_active(exec_client, instance, log_file)
if bot_active:
message = (f"stop_application: ERROR: I was unable to shutdown the bot. Check manually!")
write_to_log(log_file, message, instance)
return jsonify({'success': False, 'error': 'I was unable to shutdown the bot. Check manually'})
func_start_application(exec_client, instance, bot_active, log_file, commandlineData)
bot_active = verify_application_active(exec_client, instance, log_file)
if not bot_active:
message = (f"(re)start_application: ERROR: I was unable to start the bot. Check manually!")
write_to_log(log_file, message, instance)
return jsonify({'success': False, 'error': 'I was unable to start the bot. Check manually'})
message = (f"(re)start_application: succesfully (re)started the application")
write_to_log(log_file, message, instance)
return jsonify({'success': True})
except Exception as e:
write_to_log(log_file, f"(re)start_application: ERROR: {str(e)}", instance)
return jsonify({'success': False, 'error': str(e)})
else:
write_to_log(log_file, f"(re)start_application: ERROR: {str(e)}", instance)
return jsonify({'success': False, 'error': 'Instance not found'})
@app.route('/stop_application', methods=['POST'])
def stop_application():
bot_instances = load_instances()
instance_id = int(request.form['instance_id'])
instance = next((inst for inst in bot_instances['instances'] if inst['id'] == instance_id), None)
#print (instance)
if instance:
try:
write_to_log(log_file, "stop_application: stopping, hold on", instance)
#open connection to vps
sftp_client, exec_client = ssh_connect(instance, log_file, globals.connected_instance)
func_stop_application(exec_client, instance, log_file)
bot_active = verify_application_active(exec_client, instance, log_file)
if bot_active:
message = (f"stop_application: ERROR: I was unable to shutdown the bot. Check manually!")
write_to_log(log_file, message, instance)
return jsonify({'success': False, 'error': 'I was unable to shutdown the bot. Check manually'})
write_to_log(log_file, "stop_application: stop done", instance)
return jsonify({'success': True})
except Exception as e:
write_to_log(log_file, f"stop_application: ERROR: {str(e)}", instance)
return jsonify({'success': False, 'error': str(e)})
else:
write_to_log(log_file, f"stop_application: ERROR: {str(e)}", instance)
return jsonify({'success': False, 'error': 'Instance not found'})
@app.route('/update_application', methods=['POST'])
def update_application():
bot_instances = load_instances()
instance_id = int(request.form['instance_id'])
instance = next((inst for inst in bot_instances['instances'] if inst['id'] == instance_id), None)
#TODO: write the requirements_updated check
requirements_updated = False
if instance:
try:
sftp_client, exec_client = ssh_connect(instance, log_file)
# SSH command to perform git pull
git_pull_command = f"cd {instance['ds_location']} && git pull"
stdin, stdout, stderr = exec_client.exec_command(f"{git_pull_command}")
# Start threads to write stdout and stderr messages concurrently as they come in.
stdout_thread = threading.Thread(target=write_stdout, args=(stdout, instance))
stderr_thread = threading.Thread(target=write_stderr, args=(stderr, instance))
stdout_thread.start()
stderr_thread.start()
# Wait for the threads to complete
stdout_thread.join()
stderr_thread.join()
if write_stderr: #check if it returns True
print ("test")
write_to_log(log_file, f"ERROR3: update_application: update failed. check manually", instance)
return jsonify({'success': False, 'error3': 'update failed. check manually'})
# # for line in stdin:
# # print("in:", line.strip())
# for line in stdout:
# #logger.critical(f"{line.strip()}")
# message = (f"update_Application: {line.strip()}")
# write_to_log(log_file, message, instance)
# #print("out:", line.strip())
# #TODO:
# if "requirements.txt" in line.strip():
# #requirement updated
# requirements_updated = True
# write_to_log(log_file, f"update_application: WARNING: requirments updated", instance)
#build a function that updates the requirements on the server.
#1. exec_client.exec_command( cd to ds_location)
#2. activate venv if its enabled.
#3. pip install -r requirements.txt
# #TODO: need this for errors in git pull, but also gets hit with normal operation.
# for line in stderr:
# #logger.critical(f"Error: {line.strip()}")
# message = (f"ERROR: update_Application: {line.strip()}")
# write_to_log(log_file, message, instance)
# print("err:", line.strip())
# error_messages = True
# if error_messages:
# return jsonify({'success': False, 'error': 'failed to update, check the log'})
compare_config_files(instance)
get_data() #reload data after update
write_to_log(log_file, f"update_application: update done", instance)
if requirements_updated:
write_to_log(log_file, f"update_application: WARNING: requirements have been updated. Update them on the server)", instance)
write_to_log(log_file, f"update_application: don't forget to (re)start)", instance)
return jsonify({'success': True})
except Exception as e:
write_to_log(log_file, f"update_application: ERROR2: {str(e)}", instance)
return jsonify({'success': False, 'error2': str(e)})
else:
write_to_log(log_file, f"update_application: ERROR1: {str(e)}", instance)
return jsonify({'success': False, 'error1': 'Instance not found'})
@app.route('/get_log')
def get_log():
# Send the log file as a response
return send_file('log.log')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5431)