diff --git a/go/sandboxaid/handler/handler.go b/go/sandboxaid/handler/handler.go index 482b2be..1d792ff 100644 --- a/go/sandboxaid/handler/handler.go +++ b/go/sandboxaid/handler/handler.go @@ -31,15 +31,17 @@ var log Logger = stdlog.New(os.Stderr, "", stdlog.LstdFlags) type Handler struct { http.Handler - client client.Client + client client.Client + defaultImage string } -func NewHandler(client client.Client) *Handler { +func NewHandler(client client.Client, defaultImage string) *Handler { r := chi.NewRouter() h := &Handler{ - Handler: r, - client: client, + Handler: r, + client: client, + defaultImage: defaultImage, } // Log to stderr. @@ -76,6 +78,9 @@ func (h *Handler) v1PostSandbox(w http.ResponseWriter, r *http.Request) { sendError(w, r, err, http.StatusBadRequest) return } + if s.Spec.Image == "" { + s.Spec.Image = h.defaultImage + } created, err := h.client.CreateSandbox(r.Context(), space, &s) if err != nil { diff --git a/go/sandboxaid/main.go b/go/sandboxaid/main.go index 9c35ebc..c618efe 100644 --- a/go/sandboxaid/main.go +++ b/go/sandboxaid/main.go @@ -18,6 +18,10 @@ import ( "github.com/substratusai/sandboxai/go/sandboxaid/handler" ) +const ( + defaultDefaultImage = "substratusai/sandboxai-box:v0.2.0" +) + func main() { host, ok := os.LookupEnv("SANDBOXAID_HOST") if !ok { @@ -41,6 +45,11 @@ func main() { deleteOnShutdown = strings.ToLower(strings.TrimSpace(val)) == "true" } + defaultImage, ok := os.LookupEnv("SANDBOXAID_DEFAULT_IMAGE") + if !ok { + defaultImage = defaultDefaultImage + } + log := log.New(os.Stderr, "", log.LstdFlags) handler.SetLogger(log) docker.SetLogger(log) @@ -86,7 +95,7 @@ func main() { server := &http.Server{ Addr: fmt.Sprintf("%s:%s", host, port), - Handler: handler.NewHandler(client), + Handler: handler.NewHandler(client, defaultImage), } go func() { diff --git a/python/sandboxai/sandbox.py b/python/sandboxai/sandbox.py index 87e8d53..1381e29 100644 --- a/python/sandboxai/sandbox.py +++ b/python/sandboxai/sandbox.py @@ -10,8 +10,6 @@ log = getLogger(__name__) -DEFAULT_IMAGE = "substratusai/sandboxai-box:v0.2.0" - # Prevent multiple Sandbox() instances from attempting to start the # embedded server at the same time. embedded_mutex = Lock() @@ -35,7 +33,7 @@ def __init__( lazy_create: bool = False, space: str = "default", name: str = None, - image: str = DEFAULT_IMAGE, + image: str = None, env: dict = None, ): """ diff --git a/python/sandboxai/sandbox_test.py b/python/sandboxai/sandbox_test.py index 92db40d..76491df 100644 --- a/python/sandboxai/sandbox_test.py +++ b/python/sandboxai/sandbox_test.py @@ -1,12 +1,15 @@ import os import pytest from sandboxai import Sandbox -from sandboxai.sandbox import DEFAULT_IMAGE + +import logging + +logging.basicConfig(level=logging.DEBUG) @pytest.fixture def box_image(): - return os.environ.get("BOX_IMAGE", DEFAULT_IMAGE) + return os.environ.get("BOX_IMAGE") @pytest.fixture @@ -53,14 +56,20 @@ def test_sandbox_lazy_create(box_image): def test_with_specified_name(box_image): try: - sb = Sandbox(embedded=True, lazy_create=True, image=box_image, name="test-name") - assert sb.name == "test-name" - sb.create() + sb = Sandbox(embedded=True, image=box_image, name="test-name") assert sb.name == "test-name" finally: sb.delete() +def test_non_embedded(base_url): + try: + sb = Sandbox(base_url=base_url) + assert sb.image + finally: + sb.delete() + + # These tests pass locally but fail on github actions. # def test_sandbox_embedded_server(): # with Sandbox(embedded=True) as sb: diff --git a/test/e2e/run.sh b/test/e2e/run.sh index 0a41aac..40c5d22 100755 --- a/test/e2e/run.sh +++ b/test/e2e/run.sh @@ -50,5 +50,5 @@ go test -v . cd $repo_root cd $repo_root/python -uv run pytest +uv run pytest -o log_cli=true cd $repo_root