CoACT initialize (#292)
This commit is contained in:
@@ -136,6 +136,82 @@ class PythonController:
|
||||
|
||||
logger.error("Failed to execute command.")
|
||||
return None
|
||||
|
||||
def run_python_script(self, script: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Executes a python script on the server.
|
||||
"""
|
||||
payload = json.dumps({"code": script})
|
||||
|
||||
for _ in range(self.retry_times):
|
||||
try:
|
||||
response = requests.post(self.http_server + "/run_python", headers={'Content-Type': 'application/json'},
|
||||
data=payload, timeout=90)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
return {"status": "error", "message": "Failed to execute command.", "output": None, "error": response.json()["error"]}
|
||||
except requests.exceptions.ReadTimeout:
|
||||
break
|
||||
except Exception:
|
||||
logger.error("An error occurred while trying to execute the command: %s", traceback.format_exc())
|
||||
logger.info("Retrying to execute command.")
|
||||
time.sleep(self.retry_interval)
|
||||
|
||||
logger.error("Failed to execute command.")
|
||||
return {"status": "error", "message": "Failed to execute command.", "output": "", "error": "Retry limit reached."}
|
||||
|
||||
def run_bash_script(self, script: str, timeout: int = 30, working_dir: Optional[str] = None) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Executes a bash script on the server.
|
||||
|
||||
:param script: The bash script content (can be multi-line)
|
||||
:param timeout: Execution timeout in seconds (default: 30)
|
||||
:param working_dir: Working directory for script execution (optional)
|
||||
:return: Dictionary with status, output, error, and returncode, or None if failed
|
||||
"""
|
||||
payload = json.dumps({
|
||||
"script": script,
|
||||
"timeout": timeout,
|
||||
"working_dir": working_dir
|
||||
})
|
||||
|
||||
for _ in range(self.retry_times):
|
||||
try:
|
||||
response = requests.post(
|
||||
self.http_server + "/run_bash_script",
|
||||
headers={'Content-Type': 'application/json'},
|
||||
data=payload,
|
||||
timeout=timeout + 100 # Add buffer to HTTP timeout
|
||||
)
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
logger.info("Bash script executed successfully with return code: %d", result.get("returncode", -1))
|
||||
return result
|
||||
else:
|
||||
logger.error("Failed to execute bash script. Status code: %d, response: %s",
|
||||
response.status_code, response.text)
|
||||
logger.info("Retrying to execute bash script.")
|
||||
except requests.exceptions.ReadTimeout:
|
||||
logger.error("Bash script execution timed out")
|
||||
return {
|
||||
"status": "error",
|
||||
"output": "",
|
||||
"error": f"Script execution timed out after {timeout} seconds",
|
||||
"returncode": -1
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error("An error occurred while trying to execute the bash script: %s", e)
|
||||
logger.info("Retrying to execute bash script.")
|
||||
time.sleep(self.retry_interval)
|
||||
|
||||
logger.error("Failed to execute bash script after %d retries.", self.retry_times)
|
||||
return {
|
||||
"status": "error",
|
||||
"output": "",
|
||||
"error": f"Failed to execute bash script after {self.retry_times} retries",
|
||||
"returncode": -1
|
||||
}
|
||||
|
||||
def execute_action(self, action: Dict[str, Any]):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user