Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions centml/sdk/utils/config_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
from typing import Optional

from platform_api_python_client import ConfigFileMount


# Load a file off disk into a ConfigFileMount. `mount_path` is the parent
# directory inside the container; the file lands at `mount_path/filename`.
# Field-level validation (size cap, filename charset, mount_path rules) is
# left to the API so SDK doesn't drift when server limits change.
def load_config_file_mount(path: str, mount_path: str, filename: Optional[str] = None) -> ConfigFileMount:
with open(path, "r", encoding="utf-8") as f:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 Badge Preserve original line endings in config file content

Reading with open(path, "r", encoding="utf-8") uses universal-newline translation, so files with \r\n or \r line endings are normalized to \n before upload. In environments where config line endings are significant (for example Windows-authored configs consumed byte-sensitively), this silently alters the mounted content.

Useful? React with 👍 / 👎.

content = f.read()
return ConfigFileMount(filename=filename or os.path.basename(path), mount_path=mount_path, content=content)
7 changes: 7 additions & 0 deletions examples/sdk/create_inference.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import centml
from centml.sdk.api import get_centml_client
from centml.sdk import DeploymentType, CreateInferenceV3DeploymentRequest, UserVaultType
from centml.sdk.utils.config_file import load_config_file_mount


def main():
Expand All @@ -22,6 +23,12 @@ def main():
max_unavailable=0, # Keep all pods available during updates
healthcheck="/",
concurrency=10,
# Mounts ./default.conf at /etc/nginx/conf.d/default.conf. mount_path
# is the parent directory; filename defaults to os.path.basename(path)
# so the resulting file lands at mount_path/filename. Pass an inline
# ConfigFileMount(filename=..., mount_path=..., content=...) if the
# content is already in memory.
config_file=load_config_file_mount(path="./default.conf", mount_path="/etc/nginx/conf.d"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Ship referenced config file or make mount optional

create_inference.py now unconditionally calls load_config_file_mount("./default.conf", ...), but this repository does not include examples/sdk/default.conf, so the example raises FileNotFoundError before any deployment call when run as-is. This makes the existing example non-runnable unless users discover and create an extra file manually.

Useful? React with 👍 / 👎.

)
response = cclient.create_inference(request)
print("Create deployment response: ", response)
Expand Down
50 changes: 50 additions & 0 deletions examples/sdk/create_inference_vllm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import centml
from centml.sdk.api import get_centml_client
from centml.sdk import CreateInferenceV3DeploymentRequest
from centml.sdk.utils.config_file import load_config_file_mount


def main():
with get_centml_client() as cclient:
# Mounts ./chat_template.jinja at /etc/vllm/chat_template.jinja and
# tells vLLM to use it via --chat-template. mount_path is the parent
# directory; filename defaults to os.path.basename(path).
request = CreateInferenceV3DeploymentRequest(
name="vllm-llama",
cluster_id=1000,
hardware_instance_id=1001, # GPU instance
image_url="vllm/vllm-openai:latest",
port=8000,
min_replicas=1,
max_replicas=1,
initial_replicas=1,
max_surge=1,
max_unavailable=0,
healthcheck="/health",
concurrency=10,
env_vars={"HF_TOKEN": "<your-hf-token>"},
command=(
"python -m vllm.entrypoints.openai.api_server "
"--model meta-llama/Llama-3.2-3B-Instruct "
"--port 8000 "
"--chat-template /etc/vllm/chat_template.jinja"
),
config_file=load_config_file_mount(path="./chat_template.jinja", mount_path="/etc/vllm"),
Comment thread
michaelshin marked this conversation as resolved.
Outdated
)
response = cclient.create_inference(request)
print("Create deployment response: ", response)

deployment = cclient.get_inference(response.id)
print("Deployment details: ", deployment)

'''
### Pause the deployment
cclient.pause(deployment.id)

### Delete the deployment
cclient.delete(deployment.id)
'''


if __name__ == "__main__":
main()
41 changes: 41 additions & 0 deletions tests/test_sdk_config_file_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Tests for centml.sdk.utils.config_file.load_config_file_mount."""

import pytest

from centml.sdk.utils.config_file import load_config_file_mount


def test_default_filename_from_basename(tmp_path):
src = tmp_path / "nginx.conf"
src.write_text("server { listen 80; }\n")

mount = load_config_file_mount(str(src), "/etc/nginx/conf.d")

assert mount.filename == "nginx.conf"
assert mount.mount_path == "/etc/nginx/conf.d"
assert mount.content == "server { listen 80; }\n"


def test_explicit_filename_overrides_basename(tmp_path):
src = tmp_path / "local.txt"
src.write_text("payload")

mount = load_config_file_mount(str(src), "/app/etc", filename="remote.conf")

assert mount.filename == "remote.conf"
assert mount.mount_path == "/app/etc"
assert mount.content == "payload"


def test_utf8_multibyte_content_roundtrips(tmp_path):
src = tmp_path / "i18n.conf"
src.write_text("配置内容 = 测试\n", encoding="utf-8")

mount = load_config_file_mount(str(src), "/etc/app")

assert mount.content == "配置内容 = 测试\n"


def test_missing_file_raises_filenotfound(tmp_path):
with pytest.raises(FileNotFoundError):
load_config_file_mount(str(tmp_path / "does-not-exist.conf"), "/etc/x")
Loading