Skip to content

Commit fe4140d

Browse files
committed
Add support for KOYEB_REGION env var
1 parent ff86e02 commit fe4140d

4 files changed

Lines changed: 125 additions & 40 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ Using the Koyeb Python SDK requires an API token. Complete the following steps t
4747
export KOYEB_API_TOKEN="YOUR_API_TOKEN"
4848
```
4949
50+
Optionally, set a default region for all sandbox deployments (defaults to `"na"` if not set):
51+
52+
```bash copy
53+
export KOYEB_REGION="fra"
54+
```
55+
5056
### Example sandbox code
5157

5258
See the [examples](/examples) directory for more basic operations.

docs/sandbox.md

Lines changed: 115 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,11 @@ def create(cls,
999999
enable_tcp_proxy: bool = False,
10001000
privileged: bool = False,
10011001
registry_secret: Optional[str] = None,
1002-
_experimental_enable_light_sleep: bool = False) -> Sandbox
1002+
_experimental_enable_light_sleep: bool = False,
1003+
_experimental_deep_sleep_value: int = 3900,
1004+
delete_after_delay: int = 0,
1005+
delete_after_inactivity_delay: int = 0,
1006+
app_id: Optional[str] = None) -> Sandbox
10031007
```
10041008

10051009
Create a new sandbox instance.
@@ -1014,7 +1018,7 @@ Create a new sandbox instance.
10141018
If None, defaults to "http".
10151019
If provided, must be one of "http" or "http2".
10161020
- `env` - Environment variables
1017-
- `region` - Region to deploy to (default: "na")
1021+
- `region` - Region to deploy to. Defaults to KOYEB_REGION env var, or "na" if not set.
10181022
- `api_token` - Koyeb API token (if None, will try to get from KOYEB_API_TOKEN env var)
10191023
- `timeout` - Timeout for sandbox creation in seconds
10201024
- `idle_timeout` - Sleep timeout in seconds. Behavior depends on _experimental_enable_light_sleep:
@@ -1028,6 +1032,11 @@ Create a new sandbox instance.
10281032
pulling private images. Create the secret via Koyeb dashboard or CLI first.
10291033
- `_experimental_enable_light_sleep` - If True, uses idle_timeout for light_sleep and sets
10301034
deep_sleep=3900. If False, uses idle_timeout for deep_sleep (default: False)
1035+
- `delete_after_create` - If >0, automatically delete the sandbox if there was no activity
1036+
after this many seconds since creation.
1037+
- `delete_after_sleep` - If >0, automatically delete the sandbox if service sleeps due to inactivity
1038+
after this many seconds.
1039+
- `app_id` - If provided, create the sandbox service in an existing app instead of creating a new one.
10311040

10321041

10331042
**Returns**:
@@ -1135,6 +1144,16 @@ def delete() -> None
11351144

11361145
Delete the sandbox instance.
11371146

1147+
<a id="koyeb/sandbox.sandbox.Sandbox.get_url_and_header_from_metadata"></a>
1148+
1149+
#### get\_url\_and\_header\_from\_metadata
1150+
1151+
```python
1152+
def get_url_and_header_from_metadata() -> Optional[Tuple[str, str]]
1153+
```
1154+
1155+
Get the public url of the sandbox and the routing key to use to reach it.
1156+
11381157
<a id="koyeb/sandbox.sandbox.Sandbox.get_domain"></a>
11391158

11401159
#### get\_domain
@@ -1402,6 +1421,34 @@ for cleanup operations.
14021421
>>> count = sandbox.kill_all_processes()
14031422
>>> print(f"Killed {count} processes")
14041423

1424+
<a id="koyeb/sandbox.sandbox.Sandbox.update_lifecycle"></a>
1425+
1426+
#### update\_lifecycle
1427+
1428+
```python
1429+
def update_lifecycle(delete_after_delay: Optional[int] = None,
1430+
delete_after_inactivity: Optional[int] = None) -> None
1431+
```
1432+
1433+
Update the sandbox's life cycle settings.
1434+
1435+
**Arguments**:
1436+
1437+
- `delete_after_delay` - If >0, automatically delete the sandbox if there was no activity
1438+
after this many seconds since creation.
1439+
- `delete_after_inactivity` - If >0, automatically delete the sandbox if service sleeps due to inactivity
1440+
after this many seconds.
1441+
1442+
1443+
**Raises**:
1444+
1445+
- `SandboxError` - If updating life cycle fails
1446+
1447+
1448+
**Example**:
1449+
1450+
>>> sandbox.update_life_cycle(delete_after_delay=600, delete_after_inactivity=300)
1451+
14051452
<a id="koyeb/sandbox.sandbox.Sandbox.__enter__"></a>
14061453

14071454
#### \_\_enter\_\_
@@ -1468,22 +1515,25 @@ Get a sandbox by service ID asynchronously.
14681515

14691516
```python
14701517
@classmethod
1471-
async def create(
1472-
cls,
1473-
image: str = "koyeb/sandbox",
1474-
name: str = "quick-sandbox",
1475-
wait_ready: bool = True,
1476-
instance_type: str = "micro",
1477-
exposed_port_protocol: Optional[str] = None,
1478-
env: Optional[Dict[str, str]] = None,
1479-
region: Optional[str] = None,
1480-
api_token: Optional[str] = None,
1481-
timeout: int = 300,
1482-
idle_timeout: int = 300,
1483-
enable_tcp_proxy: bool = False,
1484-
privileged: bool = False,
1485-
registry_secret: Optional[str] = None,
1486-
_experimental_enable_light_sleep: bool = False) -> AsyncSandbox
1518+
async def create(cls,
1519+
image: str = "koyeb/sandbox",
1520+
name: str = "quick-sandbox",
1521+
wait_ready: bool = True,
1522+
instance_type: str = "micro",
1523+
exposed_port_protocol: Optional[str] = None,
1524+
env: Optional[Dict[str, str]] = None,
1525+
region: Optional[str] = None,
1526+
api_token: Optional[str] = None,
1527+
timeout: int = 300,
1528+
idle_timeout: int = 0,
1529+
enable_tcp_proxy: bool = False,
1530+
privileged: bool = False,
1531+
registry_secret: Optional[str] = None,
1532+
_experimental_enable_light_sleep: bool = False,
1533+
_experimental_deep_sleep_value: int = 3900,
1534+
delete_after_delay: int = 0,
1535+
delete_after_inactivity_delay: int = 0,
1536+
app_id: Optional[str] = None) -> AsyncSandbox
14871537
```
14881538

14891539
Create a new sandbox instance with async support.
@@ -1498,20 +1548,27 @@ Create a new sandbox instance with async support.
14981548
If None, defaults to "http".
14991549
If provided, must be one of "http" or "http2".
15001550
- `env` - Environment variables
1501-
- `region` - Region to deploy to (default: "na")
1551+
- `region` - Region to deploy to. Defaults to KOYEB_REGION env var, or "na" if not set.
15021552
- `api_token` - Koyeb API token (if None, will try to get from KOYEB_API_TOKEN env var)
15031553
- `timeout` - Timeout for sandbox creation in seconds
15041554
- `idle_timeout` - Sleep timeout in seconds. Behavior depends on _experimental_enable_light_sleep:
1505-
- If _experimental_enable_light_sleep is True: sets light_sleep value (deep_sleep=3900)
1555+
- If _experimental_enable_light_sleep is True: sets light_sleep value (deep_sleep uses _experimental_deep_sleep_value)
15061556
- If _experimental_enable_light_sleep is False: sets deep_sleep value
15071557
- If 0: disables scale-to-zero (keep always-on)
15081558
- If None: uses default values
15091559
- `enable_tcp_proxy` - If True, enables TCP proxy for direct TCP access to port 3031
15101560
- `privileged` - If True, run the container in privileged mode (default: False)
15111561
- `registry_secret` - Name of a Koyeb secret containing registry credentials for
15121562
pulling private images. Create the secret via Koyeb dashboard or CLI first.
1513-
- `_experimental_enable_light_sleep` - If True, uses idle_timeout for light_sleep and sets
1514-
deep_sleep=3900. If False, uses idle_timeout for deep_sleep (default: False)
1563+
- `_experimental_enable_light_sleep` - If True, uses idle_timeout for light_sleep and configurable
1564+
deep_sleep (default: False)
1565+
- `_experimental_deep_sleep_value` - Number of seconds for deep sleep when light sleep is enabled (default: 3900).
1566+
Only used if _experimental_enable_light_sleep is True
1567+
- `delete_after_delay` - If >0, automatically delete the sandbox if there was no activity
1568+
after this many seconds since creation.
1569+
- `delete_after_inactivity_delay` - If >0, automatically delete the sandbox if service sleeps due to inactivity
1570+
after this many seconds.
1571+
- `app_id` - If provided, create the sandbox service in an existing app instead of creating a new one.
15151572

15161573

15171574
**Returns**:
@@ -1682,6 +1739,19 @@ async def kill_all_processes() -> int
16821739

16831740
Kill all running background processes asynchronously.
16841741

1742+
<a id="koyeb/sandbox.sandbox.AsyncSandbox.update_lifecycle"></a>
1743+
1744+
#### update\_lifecycle
1745+
1746+
```python
1747+
@async_wrapper("update_lifecycle")
1748+
async def update_lifecycle(
1749+
delete_after_delay: Optional[int] = None,
1750+
delete_after_inactivity: Optional[int] = None) -> None
1751+
```
1752+
1753+
Update the sandbox's life cycle settings asynchronously.
1754+
16851755
<a id="koyeb/sandbox.sandbox.AsyncSandbox.__aenter__"></a>
16861756

16871757
#### \_\_aenter\_\_
@@ -1740,7 +1810,8 @@ seconds for HTTP requests
17401810
def get_api_client(
17411811
api_token: Optional[str] = None,
17421812
host: Optional[str] = None
1743-
) -> tuple[AppsApi, ServicesApi, InstancesApi, CatalogInstancesApi]
1813+
) -> tuple[AppsApi, ServicesApi, InstancesApi, CatalogInstancesApi,
1814+
DeploymentsApi]
17441815
```
17451816

17461817
Get configured API clients for Koyeb operations.
@@ -1879,8 +1950,8 @@ def create_deployment_definition(
18791950
routes: Optional[List[DeploymentRoute]] = None,
18801951
idle_timeout: int = 300,
18811952
enable_tcp_proxy: bool = False,
1882-
_experimental_enable_light_sleep: bool = False
1883-
) -> DeploymentDefinition
1953+
_experimental_enable_light_sleep: bool = False,
1954+
_experimental_deep_sleep_value: int = 3900) -> DeploymentDefinition
18841955
```
18851956

18861957
Create deployment definition for a sandbox service.
@@ -1894,12 +1965,14 @@ Create deployment definition for a sandbox service.
18941965
- `exposed_port_protocol` - Protocol to expose ports with ("http" or "http2").
18951966
If None, defaults to "http".
18961967
If provided, must be one of "http" or "http2".
1897-
- `region` - Region to deploy to (defaults to "na")
1968+
- `region` - Region to deploy to. Defaults to KOYEB_REGION env var, or "na" if not set.
18981969
- `routes` - List of routes for public access
18991970
- `idle_timeout` - Number of seconds to wait before sleeping the instance if it receives no traffic
19001971
- `enable_tcp_proxy` - If True, enables TCP proxy for direct TCP access to port 3031
19011972
- `_experimental_enable_light_sleep` - If True, uses light sleep when reaching idle_timeout.
1902-
Light Sleep reduces cold starts to ~200ms. After scaling to zero, the service stays in Light Sleep for 3600s before going into Deep Sleep.
1973+
Light Sleep reduces cold starts to ~200ms. After scaling to zero, the service stays in Light Sleep for idle_timeout seconds before going into Deep Sleep.
1974+
- `_experimental_deep_sleep_value` - Number of seconds for deep sleep when light sleep is enabled (default: 3900).
1975+
Only used if _experimental_enable_light_sleep is True. Ignored otherwise.
19031976

19041977

19051978
**Returns**:
@@ -2018,8 +2091,7 @@ The sync method is called via super() and executed in an executor.
20182091
#### create\_sandbox\_client
20192092

20202093
```python
2021-
def create_sandbox_client(sandbox_url: Optional[str],
2022-
sandbox_secret: Optional[str],
2094+
def create_sandbox_client(conn_info: Optional['ConnectionInfo'],
20232095
existing_client: Optional[Any] = None) -> Any
20242096
```
20252097

@@ -2030,8 +2102,7 @@ Used by Sandbox, SandboxExecutor, and SandboxFilesystem to avoid duplication.
20302102

20312103
**Arguments**:
20322104

2033-
- `sandbox_url` - The sandbox URL (from _get_sandbox_url() or sandbox._get_sandbox_url())
2034-
- `sandbox_secret` - The sandbox secret
2105+
- `conn_info` - The information needed to connect to the sandbox executor API
20352106
- `existing_client` - Existing client instance to return if not None
20362107

20372108

@@ -2072,6 +2143,17 @@ Sandbox Executor API Client
20722143

20732144
A simple Python client for interacting with the Sandbox Executor API.
20742145

2146+
<a id="koyeb/sandbox.executor_client.ConnectionInfo"></a>
2147+
2148+
## ConnectionInfo Objects
2149+
2150+
```python
2151+
@dataclass
2152+
class ConnectionInfo()
2153+
```
2154+
2155+
Information needed to connect to a sandbox
2156+
20752157
<a id="koyeb/sandbox.executor_client.SandboxClient"></a>
20762158

20772159
## SandboxClient Objects
@@ -2087,17 +2169,14 @@ Client for the Sandbox Executor API.
20872169
#### \_\_init\_\_
20882170

20892171
```python
2090-
def __init__(base_url: str,
2091-
secret: str,
2092-
timeout: float = DEFAULT_HTTP_TIMEOUT)
2172+
def __init__(conn_info: ConnectionInfo, timeout: float = DEFAULT_HTTP_TIMEOUT)
20932173
```
20942174

20952175
Initialize the Sandbox Client.
20962176

20972177
**Arguments**:
20982178

2099-
- `base_url` - The base URL of the sandbox server (e.g., 'http://localhost:8080')
2100-
- `secret` - The authentication secret/token
2179+
- `conn_info` - The parameters needed to connect to the sandbox
21012180
- `timeout` - Request timeout in seconds (default: 30)
21022181

21032182
<a id="koyeb/sandbox.executor_client.SandboxClient.close"></a>

koyeb/sandbox/sandbox.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def create(
134134
If None, defaults to "http".
135135
If provided, must be one of "http" or "http2".
136136
env: Environment variables
137-
region: Region to deploy to (default: "na")
137+
region: Region to deploy to. Defaults to KOYEB_REGION env var, or "na" if not set.
138138
api_token: Koyeb API token (if None, will try to get from KOYEB_API_TOKEN env var)
139139
timeout: Timeout for sandbox creation in seconds
140140
idle_timeout: Sleep timeout in seconds. Behavior depends on _experimental_enable_light_sleep:
@@ -1039,7 +1039,7 @@ async def create(
10391039
If None, defaults to "http".
10401040
If provided, must be one of "http" or "http2".
10411041
env: Environment variables
1042-
region: Region to deploy to (default: "na")
1042+
region: Region to deploy to. Defaults to KOYEB_REGION env var, or "na" if not set.
10431043
api_token: Koyeb API token (if None, will try to get from KOYEB_API_TOKEN env var)
10441044
timeout: Timeout for sandbox creation in seconds
10451045
idle_timeout: Sleep timeout in seconds. Behavior depends on _experimental_enable_light_sleep:

koyeb/sandbox/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def create_deployment_definition(
254254
exposed_port_protocol: Protocol to expose ports with ("http" or "http2").
255255
If None, defaults to "http".
256256
If provided, must be one of "http" or "http2".
257-
region: Region to deploy to (defaults to "na")
257+
region: Region to deploy to. Defaults to KOYEB_REGION env var, or "na" if not set.
258258
routes: List of routes for public access
259259
idle_timeout: Number of seconds to wait before sleeping the instance if it receives no traffic
260260
enable_tcp_proxy: If True, enables TCP proxy for direct TCP access to port 3031
@@ -267,7 +267,7 @@ def create_deployment_definition(
267267
DeploymentDefinition object
268268
"""
269269
if region is None:
270-
region = "na"
270+
region = os.getenv("KOYEB_REGION", "na")
271271

272272
# Convert single region string to list for API
273273
regions_list = [region]

0 commit comments

Comments
 (0)