superchatai commited on
Commit
99a91e2
·
verified ·
1 Parent(s): c4d661c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -34
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
- with tempfile.NamedTemporaryFile(mode='w', suffix='.sh', delete=False, dir=temp_dir) as script_file:
 
 
 
 
 
 
 
 
 
 
253
  script_content = f"""#!/bin/bash
254
- cd /workspace
255
  {command}
256
  """
257
- script_file.write(script_content)
258
- script_path = script_file.name
259
-
260
- os.chmod(script_path, 0o755)
261
-
262
- docker_cmd = [
263
- 'docker', 'run',
264
- '--rm',
265
- '--name', f'forge-exec-{container_id}',
266
- '--cpus', '0.25',
267
- '--memory', '1g',
268
- '--memory-swap', '1g',
269
- '--network', 'none',
270
- '--read-only',
271
- '--tmpfs', '/tmp:rw,noexec,nosuid,size=100m',
272
- '--env', 'PYTHONUNBUFFERED=1',
273
- '--volume', f'{workspace_dir}:/workspace:rw',
274
- '--volume', f'{script_path}:/script.sh:ro',
275
- '--workdir', '/workspace',
276
- 'python:3.11-slim',
277
- 'bash', '/script.sh'
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
- subprocess.run(['docker', 'kill', f'forge-exec-{container_id}'], timeout=5)
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
- subprocess.run(['docker', 'rm', '-f', f'forge-exec-{container_id}'], timeout=5, capture_output=True)
 
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