2025-05-29 14:20:03 +02:00
|
|
|
import asyncio
|
|
|
|
|
|
2025-06-11 12:29:47 +00:00
|
|
|
from daytona import AsyncDaytona, SessionExecuteRequest
|
2025-05-29 14:20:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
async with AsyncDaytona() as daytona:
|
|
|
|
|
sandbox = await daytona.create()
|
|
|
|
|
|
|
|
|
|
exec_session_id = "exec-session-1"
|
|
|
|
|
await sandbox.process.create_session(exec_session_id)
|
|
|
|
|
|
|
|
|
|
# Get the session details any time
|
|
|
|
|
session = await sandbox.process.get_session(exec_session_id)
|
|
|
|
|
print(session)
|
|
|
|
|
|
|
|
|
|
# Execute a first command in the session
|
|
|
|
|
exec_command1 = await sandbox.process.execute_session_command(
|
|
|
|
|
exec_session_id, SessionExecuteRequest(command="export FOO=BAR")
|
|
|
|
|
)
|
|
|
|
|
if exec_command1.exit_code != 0:
|
2025-09-08 16:30:25 +02:00
|
|
|
print(f"Error: {exec_command1.exit_code} {exec_command1.stderr}")
|
2025-05-29 14:20:03 +02:00
|
|
|
|
|
|
|
|
# Get the session details again to see the command has been executed
|
|
|
|
|
session = await sandbox.process.get_session(exec_session_id)
|
|
|
|
|
print(session)
|
|
|
|
|
|
|
|
|
|
# Get the command details
|
|
|
|
|
session_command = await sandbox.process.get_session_command(exec_session_id, exec_command1.cmd_id)
|
|
|
|
|
print(session_command)
|
|
|
|
|
|
|
|
|
|
# Execute a second command in the session and see that the environment variable is set
|
2026-01-16 16:17:58 +00:00
|
|
|
exec_command2 = await sandbox.process.execute_session_command(
|
|
|
|
|
exec_session_id, SessionExecuteRequest(command="echo $FOO")
|
|
|
|
|
)
|
2025-05-29 14:20:03 +02:00
|
|
|
if exec_command2.exit_code != 0:
|
2025-09-08 16:30:25 +02:00
|
|
|
print(f"Error: {exec_command2.exit_code} {exec_command2.stderr}")
|
2025-05-29 14:20:03 +02:00
|
|
|
else:
|
2025-09-08 16:30:25 +02:00
|
|
|
print(exec_command2.stdout)
|
2025-05-29 14:20:03 +02:00
|
|
|
|
|
|
|
|
print("Now getting logs for the second command")
|
|
|
|
|
logs = await sandbox.process.get_session_command_logs(exec_session_id, exec_command2.cmd_id)
|
2025-09-08 16:30:25 +02:00
|
|
|
print(f"[STDOUT]: {logs.stdout}")
|
|
|
|
|
print(f"[STDERR]: {logs.stderr}")
|
2025-05-29 14:20:03 +02:00
|
|
|
|
|
|
|
|
# You can also list all active sessions
|
|
|
|
|
sessions = await sandbox.process.list_sessions()
|
|
|
|
|
print(sessions)
|
|
|
|
|
|
|
|
|
|
# And of course you can delete the session at any time
|
|
|
|
|
await sandbox.process.delete_session(exec_session_id)
|
|
|
|
|
|
|
|
|
|
await daytona.delete(sandbox)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
asyncio.run(main())
|