Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -249,46 +249,47 @@ async def execute_get(command: str, files=None):
|
|
| 249 |
with open(file_path, 'wb') as f:
|
| 250 |
f.write(content)
|
| 251 |
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
script_content = f"""#!/bin/bash
|
| 254 |
-
cd
|
| 255 |
{command}
|
| 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 |
-
process = subprocess.Popen(
|
| 281 |
-
docker_cmd,
|
| 282 |
-
stdout=subprocess.PIPE,
|
| 283 |
-
stderr=subprocess.STDOUT,
|
| 284 |
-
text=True,
|
| 285 |
-
bufsize=1
|
| 286 |
-
)
|
| 287 |
|
| 288 |
while True:
|
| 289 |
if time.time() - start_time > timeout_seconds:
|
| 290 |
try:
|
| 291 |
-
|
| 292 |
except:
|
| 293 |
pass
|
| 294 |
yield f"data: Command timed out after 30 seconds\n\n"
|
|
@@ -315,7 +316,8 @@ cd /workspace
|
|
| 315 |
yield f"data: Error: {str(e)}\n\n"
|
| 316 |
finally:
|
| 317 |
try:
|
| 318 |
-
|
|
|
|
| 319 |
except:
|
| 320 |
pass
|
| 321 |
|
|
|
|
| 249 |
with open(file_path, 'wb') as f:
|
| 250 |
f.write(content)
|
| 251 |
|
| 252 |
+
temp_user = f"forge_temp_{container_id}"
|
| 253 |
+
user_created = False
|
| 254 |
+
try:
|
| 255 |
+
subprocess.run(['useradd', '--no-create-home', '--shell', '/bin/bash', temp_user],
|
| 256 |
+
check=True, capture_output=True)
|
| 257 |
+
user_created = True
|
| 258 |
+
|
| 259 |
+
subprocess.run(['chown', '-R', temp_user, workspace_dir],
|
| 260 |
+
check=True, capture_output=True)
|
| 261 |
+
|
| 262 |
+
script_path = os.path.join(temp_dir, 'script.sh')
|
| 263 |
script_content = f"""#!/bin/bash
|
| 264 |
+
cd {workspace_dir}
|
| 265 |
{command}
|
| 266 |
"""
|
| 267 |
+
with open(script_path, 'w') as f:
|
| 268 |
+
f.write(script_content)
|
| 269 |
+
os.chmod(script_path, 0o755)
|
| 270 |
+
|
| 271 |
+
subprocess.run(['chown', temp_user, script_path],
|
| 272 |
+
check=True, capture_output=True)
|
| 273 |
+
|
| 274 |
+
cmd = ['sudo', '-u', temp_user, 'bash', script_path]
|
| 275 |
+
|
| 276 |
+
process = subprocess.Popen(
|
| 277 |
+
cmd,
|
| 278 |
+
stdout=subprocess.PIPE,
|
| 279 |
+
stderr=subprocess.STDOUT,
|
| 280 |
+
text=True,
|
| 281 |
+
bufsize=1,
|
| 282 |
+
cwd=workspace_dir,
|
| 283 |
+
env={'PYTHONUNBUFFERED': '1', 'PATH': '/usr/local/bin:/usr/bin:/bin'}
|
| 284 |
+
)
|
| 285 |
+
except subprocess.CalledProcessError as e:
|
| 286 |
+
yield f"data: Failed to setup execution environment: {e}\n\n"
|
| 287 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
while True:
|
| 290 |
if time.time() - start_time > timeout_seconds:
|
| 291 |
try:
|
| 292 |
+
process.kill()
|
| 293 |
except:
|
| 294 |
pass
|
| 295 |
yield f"data: Command timed out after 30 seconds\n\n"
|
|
|
|
| 316 |
yield f"data: Error: {str(e)}\n\n"
|
| 317 |
finally:
|
| 318 |
try:
|
| 319 |
+
if user_created:
|
| 320 |
+
subprocess.run(['userdel', '-r', temp_user], timeout=5, capture_output=True)
|
| 321 |
except:
|
| 322 |
pass
|
| 323 |
|