Skip to content

Commit 88a4bc7

Browse files
authored
fix gateway_wait script for hermes (#1931)
Signed-off-by: Peter Jausovec <peter.jausovec@solo.io>
1 parent 3524e75 commit 88a4bc7

4 files changed

Lines changed: 53 additions & 64 deletions

File tree

go/core/pkg/sandboxbackend/openshell/agentharness_openshell_client.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,23 @@ func (c *AgentHarnessOpenShellClient) ExecSandboxID(ctx context.Context, sandbox
177177
return name, nil
178178
}
179179

180+
type ExecSandboxResult struct {
181+
ExitCode int32
182+
Stdout string
183+
Stderr string
184+
}
185+
180186
// ExecSandbox runs a command inside the sandbox via OpenShell ExecSandbox streaming RPC.
181187
func (c *AgentHarnessOpenShellClient) ExecSandbox(ctx context.Context, sandboxID string, command []string, stdin []byte, env map[string]string, timeoutSec uint32) (int32, string, error) {
188+
res, err := c.ExecSandboxOutput(ctx, sandboxID, command, stdin, env, timeoutSec)
189+
return res.ExitCode, res.Stderr, err
190+
}
191+
192+
// ExecSandboxOutput runs a command inside the sandbox and captures stdout, stderr, and the exit code.
193+
func (c *AgentHarnessOpenShellClient) ExecSandboxOutput(ctx context.Context, sandboxID string, command []string, stdin []byte, env map[string]string, timeoutSec uint32) (ExecSandboxResult, error) {
182194
osCli := c.openShell()
183195
if osCli == nil {
184-
return -1, "", fmt.Errorf("openshell client is nil")
196+
return ExecSandboxResult{ExitCode: -1}, fmt.Errorf("openshell client is nil")
185197
}
186198
req := &openshellv1.ExecSandboxRequest{
187199
SandboxId: sandboxID,
@@ -194,8 +206,9 @@ func (c *AgentHarnessOpenShellClient) ExecSandbox(ctx context.Context, sandboxID
194206
}
195207
stream, err := osCli.ExecSandbox(ctx, req)
196208
if err != nil {
197-
return -1, "", err
209+
return ExecSandboxResult{ExitCode: -1}, err
198210
}
211+
var stdout strings.Builder
199212
var stderr strings.Builder
200213
var exitCode int32 = -1
201214
for {
@@ -204,10 +217,13 @@ func (c *AgentHarnessOpenShellClient) ExecSandbox(ctx context.Context, sandboxID
204217
if errors.Is(err, io.EOF) {
205218
break
206219
}
207-
return exitCode, stderr.String(), err
220+
return ExecSandboxResult{ExitCode: exitCode, Stdout: stdout.String(), Stderr: stderr.String()}, err
208221
}
209222
switch p := ev.GetPayload().(type) {
210223
case *openshellv1.ExecSandboxEvent_Stdout:
224+
if p.Stdout != nil {
225+
stdout.Write(p.Stdout.GetData())
226+
}
211227
case *openshellv1.ExecSandboxEvent_Stderr:
212228
if p.Stderr != nil {
213229
stderr.Write(p.Stderr.GetData())
@@ -219,9 +235,9 @@ func (c *AgentHarnessOpenShellClient) ExecSandbox(ctx context.Context, sandboxID
219235
}
220236
}
221237
if exitCode == -1 {
222-
return exitCode, stderr.String(), fmt.Errorf("ExecSandbox finished without exit status")
238+
return ExecSandboxResult{ExitCode: exitCode, Stdout: stdout.String(), Stderr: stderr.String()}, fmt.Errorf("ExecSandbox finished without exit status")
223239
}
224-
return exitCode, stderr.String(), nil
240+
return ExecSandboxResult{ExitCode: exitCode, Stdout: stdout.String(), Stderr: stderr.String()}, nil
225241
}
226242

227243
// ErrEmptyResponse is returned when OpenShell returns success with an empty Sandbox payload.

go/core/pkg/sandboxbackend/openshell/hermes.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,9 @@ func (b *HermesBackend) OnAgentHarnessReady(ctx context.Context, ah *v1alpha2.Ag
144144
return fmt.Errorf("start hermes gateway: exit %d: %s", code, strings.TrimSpace(stderr))
145145
}
146146

147-
waitGateway := hermes.GatewayListenWaitScript(hermes.HermesInternalGatewayPort)
148-
code, stderr, err = b.ExecSandbox(withAuth(gwCtx, token), execID, []string{"sh", "-c", waitGateway}, nil, execEnv, 45)
149-
if err != nil {
147+
if err := b.waitHermesGatewayListen(withAuth(gwCtx, token), execID, hermes.HermesInternalGatewayPort, execEnv); err != nil {
150148
return fmt.Errorf("wait for hermes gateway listen: %w", err)
151149
}
152-
if code != 0 {
153-
return fmt.Errorf("wait for hermes gateway listen: exit %d: %s", code, strings.TrimSpace(stderr))
154-
}
155150

156151
socatStart := fmt.Sprintf(
157152
`command -v socat >/dev/null 2>&1 && nohup socat TCP-LISTEN:%d,bind=0.0.0.0,fork,reuseaddr TCP:127.0.0.1:%d >>/tmp/socat.log 2>&1 &`,
@@ -170,6 +165,37 @@ func (b *HermesBackend) OnAgentHarnessReady(ctx context.Context, ah *v1alpha2.Ag
170165
return nil
171166
}
172167

168+
func (b *HermesBackend) waitHermesGatewayListen(ctx context.Context, execID string, port int, execEnv map[string]string) error {
169+
listenAddr := fmt.Sprintf("127.0.0.1:%d", port)
170+
var lastResult ExecSandboxResult
171+
for range 30 {
172+
result, err := b.ExecSandboxOutput(ctx, execID, []string{"ss", "-tln"}, nil, execEnv, 5)
173+
if err != nil {
174+
return err
175+
}
176+
lastResult = result
177+
if result.ExitCode != 0 {
178+
return fmt.Errorf("ss -tln exit %d: %s", result.ExitCode, strings.TrimSpace(result.Stderr))
179+
}
180+
if strings.Contains(result.Stdout, listenAddr) {
181+
return nil
182+
}
183+
timer := time.NewTimer(time.Second)
184+
select {
185+
case <-ctx.Done():
186+
timer.Stop()
187+
return ctx.Err()
188+
case <-timer.C:
189+
}
190+
}
191+
return fmt.Errorf(
192+
"timed out after 30s waiting for %s; last ss output: %s; stderr: %s",
193+
listenAddr,
194+
strings.TrimSpace(lastResult.Stdout),
195+
strings.TrimSpace(lastResult.Stderr),
196+
)
197+
}
198+
173199
func buildHermesCreateRequest(ah *v1alpha2.AgentHarness, messagingProviders []string) (*openshellv1.CreateSandboxRequest, []string) {
174200
req, unsupported := buildAgentHarnessOpenshellCreateRequest(ah)
175201
if req.GetSpec().GetTemplate() == nil {

go/core/pkg/sandboxbackend/openshell/hermes/gateway_wait.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

go/core/pkg/sandboxbackend/openshell/hermes/gateway_wait_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)