-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03_basic_commands.py
More file actions
58 lines (46 loc) · 1.37 KB
/
03_basic_commands.py
File metadata and controls
58 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
"""Basic command execution"""
import os
import sys
import random
import string
from koyeb import Sandbox
def main():
api_token = os.getenv("KOYEB_API_TOKEN")
if not api_token:
print("Error: KOYEB_API_TOKEN not set")
return 1
sandbox = None
suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
try:
sandbox = Sandbox.create(
image="koyeb/sandbox",
name=f"basic-commands-{suffix}",
wait_ready=True,
api_token=api_token,
)
# Simple command
result = sandbox.exec("echo 'Hello World'")
print(result.stdout.strip())
# Python command
result = sandbox.exec("python3 -c 'print(2 + 2)'")
print(result.stdout.strip())
# Multi-line Python script
result = sandbox.exec(
'''python3 -c "
import sys
print(f'Python version: {sys.version.split()[0]}')
print(f'Platform: {sys.platform}')
"'''
)
print(result.stdout.strip())
# Failing command returns non-zero exit code
result = sandbox.exec("ls /nonexistent")
print(f"Exit code: {result.exit_code}")
assert result.exit_code != 0, "Expected non-zero exit code"
return 0
finally:
if sandbox:
sandbox.delete()
if __name__ == "__main__":
sys.exit(main())