|
| 1 | +package sandbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "connectrpc.com/connect" |
| 11 | + |
| 12 | + "github.com/e2b-dev/infra/packages/shared/pkg/consts" |
| 13 | + "github.com/e2b-dev/infra/packages/shared/pkg/grpc" |
| 14 | + "github.com/e2b-dev/infra/packages/shared/pkg/grpc/envd/process" |
| 15 | + "github.com/e2b-dev/infra/packages/shared/pkg/grpc/envd/process/processconnect" |
| 16 | +) |
| 17 | + |
| 18 | +// StartEnvdProcess opens a streaming Process.Start call against this |
| 19 | +// sandbox's envd, running `script` under `/bin/bash -c` as `user`. When |
| 20 | +// timeout > 0 it sets `Connect-Timeout-Ms` so envd kills the process |
| 21 | +// hard at the deadline. Auth/user headers are wired from sandbox config. |
| 22 | +// Caller owns the returned stream (Close + Receive). |
| 23 | +func (s *Sandbox) StartEnvdProcess( |
| 24 | + ctx context.Context, |
| 25 | + script, user string, |
| 26 | + timeout time.Duration, |
| 27 | +) (*connect.ServerStreamForClient[process.StartResponse], error) { |
| 28 | + addr := fmt.Sprintf("http://%s:%d", s.Slot.HostIPString(), consts.DefaultEnvdServerPort) |
| 29 | + pc := processconnect.NewProcessClient(&http.Client{Transport: sandboxHttpClient.Transport}, addr) |
| 30 | + |
| 31 | + req := connect.NewRequest(&process.StartRequest{ |
| 32 | + Process: &process.ProcessConfig{Cmd: "/bin/bash", Args: []string{"-c", script}}, |
| 33 | + }) |
| 34 | + if timeout > 0 { |
| 35 | + req.Header().Set("Connect-Timeout-Ms", strconv.FormatInt(int64(timeout/time.Millisecond), 10)) |
| 36 | + } |
| 37 | + if s.Config.Envd.AccessToken != nil { |
| 38 | + req.Header().Set("X-Access-Token", *s.Config.Envd.AccessToken) |
| 39 | + } |
| 40 | + grpc.SetUserHeader(req.Header(), user) |
| 41 | + |
| 42 | + return pc.Start(ctx, req) |
| 43 | +} |
0 commit comments