oscarwang2 commited on
Commit
889f571
1 Parent(s): b56caca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -121
app.py CHANGED
@@ -1,137 +1,64 @@
 
1
  import os
2
- import tempfile
3
- import shutil
4
- from zipfile import ZipFile
5
- import logging
6
- import psutil
7
  import subprocess
8
- from flask import Flask, request, jsonify, render_template, send_file
9
-
10
- # Configure logging
11
- logging.basicConfig(level=logging.INFO)
12
- logger = logging.getLogger(__name__)
13
 
14
- # Initialize Flask app
15
  app = Flask(__name__)
16
 
17
- connected_cpus = {"localhost": {"cpu_count": psutil.cpu_count(logical=False), "usage": 0.0}}
 
 
18
 
19
- # Define the target function for multiprocessing
20
- def target_function(script_path, folder_path):
21
- output_log = tempfile.TemporaryFile(mode='w+t')
22
- try:
23
- result = subprocess.run(['python', script_path], cwd=folder_path, stdout=output_log, stderr=subprocess.STDOUT)
24
- output_log.seek(0)
25
- log_output = output_log.read()
26
- except Exception as e:
27
- log_output = str(e)
28
- finally:
29
- output_log.close()
30
- return log_output
 
 
 
 
 
 
 
31
 
32
- # Endpoint to handle file uploads and script execution
33
- @app.route('/upload', methods=['POST'])
34
- def handle_upload():
35
  try:
36
- if 'file' not in request.files or 'script_content' not in request.form:
37
- return jsonify({"status": "error", "message": "File or script content not provided"}), 400
38
 
39
- files = request.files.getlist('file')
40
- script_content = request.form['script_content']
 
 
41
 
42
- # Create a temporary directory to store uploaded files
43
- temp_dir = tempfile.mkdtemp()
 
44
 
45
- # Save the uploaded files to the temporary directory
46
- folder_path = os.path.join(temp_dir, 'uploaded_folder')
47
- os.makedirs(folder_path, exist_ok=True)
48
- for file_obj in files:
49
- file_path = os.path.join(folder_path, file_obj.filename)
50
- file_obj.save(file_path)
51
-
52
- # Save the script content to a file
53
- script_path = os.path.join(folder_path, 'user_script.py')
54
- with open(script_path, 'w') as script_file:
55
- script_file.write(script_content)
56
-
57
- # Run the script using multiprocessing
58
- log_output = run_script(script_path, folder_path)
59
-
60
- # Create a zip file of the entire folder
61
- zip_path = os.path.join(temp_dir, 'output_folder.zip')
62
- with ZipFile(zip_path, 'w') as zipf:
63
- for root, _, files in os.walk(folder_path):
64
- for file in files:
65
- zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_path))
66
-
67
- return jsonify({"status": "success", "log_output": log_output, "download_url": f"/download/{os.path.basename(zip_path)}"})
68
-
69
  except Exception as e:
70
- logger.error(f"Error in handle_upload: {e}")
71
- return jsonify({"status": "error", "message": str(e)}), 500
72
 
73
- @app.route('/download/<filename>')
74
- def download_file(filename):
75
- try:
76
- return send_file(os.path.join(tempfile.gettempdir(), filename), as_attachment=True)
77
- except Exception as e:
78
- logger.error(f"Error in download_file: {e}")
79
- return jsonify({"status": "error", "message": str(e)}), 500
80
-
81
- # Endpoint to get connected CPUs information
82
  @app.route('/cpu_info', methods=['GET'])
83
- def get_cpu_info():
84
- try:
85
- info = []
86
- for host, data in connected_cpus.items():
87
- info.append(f"{host}: {data['cpu_count']} CPUs, {data['usage']}% usage")
88
- return jsonify({"status": "success", "cpu_info": "\n".join(info)})
89
- except Exception as e:
90
- logger.error(f"Error in get_cpu_info: {e}")
91
- return jsonify({"status": "error", "message": str(e)}), 500
92
-
93
- # Endpoint to execute commands
94
- @app.route('/execute_command', methods=['POST'])
95
- def execute_command():
96
- try:
97
- command = request.form['command']
98
- if not command:
99
- return jsonify({"status": "error", "message": "No command provided"}), 400
100
-
101
- # Ensure commands are executed in a safe environment
102
- allowed_commands = ['pip install']
103
- if not any(command.startswith(cmd) for cmd in allowed_commands):
104
- return jsonify({"status": "error", "message": "Command not allowed"}), 400
105
-
106
- output_log = tempfile.TemporaryFile(mode='w+t')
107
- try:
108
- result = subprocess.run(command.split(), stdout=output_log, stderr=subprocess.STDOUT)
109
- output_log.seek(0)
110
- log_output = output_log.read()
111
- except Exception as e:
112
- log_output = str(e)
113
- finally:
114
- output_log.close()
115
- return jsonify({"status": "success", "log_output": log_output})
116
-
117
- except Exception as e:
118
- logger.error(f"Error in execute_command: {e}")
119
- return jsonify({"status": "error", "message": str(e)}), 500
120
-
121
- # Main interface
122
- @app.route('/')
123
- def index():
124
- return render_template('index.html')
125
-
126
- def run_script(script_path, folder_path):
127
- # Collect all available CPUs including the local host CPU
128
- total_cpus = sum(cpu['cpu_count'] for cpu in connected_cpus.values()) + 1
129
-
130
- # Use multiprocessing to run the script
131
- with multiprocessing.Pool(total_cpus) as pool:
132
- log_outputs = pool.starmap(target_function, [(script_path, folder_path)] * total_cpus)
133
-
134
- return '\n'.join(log_outputs)
135
 
136
  if __name__ == "__main__":
137
- app.run(host='0.0.0.0', port=7860, threaded=True)
 
1
+ from flask import Flask, request, jsonify
2
  import os
 
 
 
 
 
3
  import subprocess
4
+ import tempfile
5
+ import json
6
+ import multiprocessing
 
 
7
 
 
8
  app = Flask(__name__)
9
 
10
+ # Dictionary to store donated CPU information
11
+ donated_cpus = {}
12
+ total_donated_cpus = 0
13
 
14
+ @app.route('/donate_cpu', methods=['POST'])
15
+ def donate_cpu():
16
+ global total_donated_cpus
17
+ data = request.get_json()
18
+ host = data['host']
19
+ cpu_count = data['cpu_count']
20
+ donated_cpus[host] = {"cpu_count": cpu_count, "usage": 0.0}
21
+ total_donated_cpus += cpu_count
22
+ return jsonify({"status": "success", "message": f"CPU donated by {host}"})
23
+
24
+ @app.route('/update_cpu_usage', methods=['POST'])
25
+ def update_cpu_usage():
26
+ data = request.get_json()
27
+ host = data['host']
28
+ usage = data['usage']
29
+ if host in donated_cpus:
30
+ donated_cpus[host]['usage'] = usage
31
+ return jsonify({"status": "success", "message": f"Updated CPU usage for {host}: {usage}%"})
32
+ return jsonify({"status": "error", "message": "Host not found"}), 404
33
 
34
+ @app.route('/execute_script', methods=['POST'])
35
+ def execute_script():
 
36
  try:
37
+ data = request.get_json()
38
+ script_content = data['script_content']
39
 
40
+ # Save the script to a temporary file
41
+ script_file = tempfile.NamedTemporaryFile(delete=False, suffix='.py')
42
+ script_file.write(script_content.encode())
43
+ script_file.close()
44
 
45
+ # Run the script using the donated CPUs
46
+ cpu_count = total_donated_cpus or 1 # Default to 1 if no CPUs are donated
47
+ result = subprocess.run(['python', script_file.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=60)
48
 
49
+ os.remove(script_file.name)
50
+ return jsonify({
51
+ "stdout": result.stdout,
52
+ "stderr": result.stderr,
53
+ "returncode": result.returncode
54
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  except Exception as e:
56
+ return jsonify({"error": str(e)}), 500
 
57
 
 
 
 
 
 
 
 
 
 
58
  @app.route('/cpu_info', methods=['GET'])
59
+ def cpu_info():
60
+ info = [{"host": host, "cpu_count": data['cpu_count'], "usage": data['usage']} for host, data in donated_cpus.items()]
61
+ return jsonify({"cpu_info": info})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
+ app.run(host='0.0.0.0', port=7860)