diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index 280c208f2..cb8cbaaca 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -19,6 +19,17 @@ import ( type workerRunner func(context.Context, agentworker.Settings, *slog.Logger) error +// runHold blocks until signalled and exits 0. It is the keepalive for a Kit +// container that runs no worker: the agent binary is archie's own and is +// mounted into every Kit, so the container needs no binary the image may not +// ship, and holds no environment of its own. +func runHold() int { + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + <-ctx.Done() + return 0 +} + func main() { os.Exit(run()) } @@ -28,6 +39,9 @@ func run() int { } func runCommand(args []string, getenv func(string) string, stderr io.Writer, runWorker workerRunner) int { + if len(args) > 0 && args[0] == "hold" { + return runHold() + } if len(args) > 0 && args[0] == "relay" { return runRelay(args[1:], stderr, agentworker.RunRelay) } diff --git a/internal/infrastructure/harnesssession/harnesssession.go b/internal/infrastructure/harnesssession/harnesssession.go index 97b73b329..40f11e1e5 100644 --- a/internal/infrastructure/harnesssession/harnesssession.go +++ b/internal/infrastructure/harnesssession/harnesssession.go @@ -25,6 +25,9 @@ import ( var ( ErrProfileUnknown = errors.New("agent profile is not configured") ErrProfileNotKit = errors.New("agent profile is not a Kit profile") + // ErrNoShell reports a Kit image that provides no login shell, so a setup + // terminal cannot be opened in it. + ErrNoShell = errors.New("the Kit image provides no shell") ) // Launcher starts and ends a setup session's container. *kitrun.Launcher @@ -63,7 +66,15 @@ func (m *Manager) Open(ctx context.Context, org, profile string) (*Session, erro if err != nil { return nil, err } - pty, err := container.ExecPTY(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, []string{"sh", "-l"}, 24, 80) + shell, err := pickShell(func(argv []string) (int, error) { + code, _, err := container.ExecIn(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, argv) + return code, err + }) + if err != nil { + _ = m.Launcher.Release(context.WithoutCancel(ctx), run) + return nil, fmt.Errorf("%w: %s", ErrNoShell, p.Kit) + } + pty, err := container.ExecPTY(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, shell, 24, 80) if err != nil { _ = m.Launcher.Release(context.WithoutCancel(ctx), run) return nil, err @@ -107,6 +118,18 @@ func (s *Session) Close() error { return err } +// pickShell returns the login shell the image provides, preferring bash over +// sh, or ErrNoShell when it provides neither. The image, not archie, decides +// what is installed, so the probe runs in the container. +func pickShell(probe func(argv []string) (int, error)) ([]string, error) { + for _, shell := range []string{"bash", "sh"} { + if code, err := probe([]string{shell, "-c", "exit 0"}); err == nil && code == 0 { + return []string{shell, "-l"}, nil + } + } + return nil, ErrNoShell +} + // sessionID is the per-session key for the container, network and volumes. It // is random so two sessions for one profile never collide on a container name. func sessionID() (string, error) { diff --git a/internal/infrastructure/harnesssession/harnesssession_test.go b/internal/infrastructure/harnesssession/harnesssession_test.go new file mode 100644 index 000000000..c61356088 --- /dev/null +++ b/internal/infrastructure/harnesssession/harnesssession_test.go @@ -0,0 +1,54 @@ +package harnesssession + +import ( + "errors" + "slices" + "testing" +) + +// A setup terminal must run a shell the image provides, not one archie assumes. +// bash is preferred when both exist; neither is the image's honest failure. +func TestPickShell(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + have map[string]bool + failing bool + want []string + wantErr bool + }{ + {name: "bash wins when both exist", have: map[string]bool{"bash": true, "sh": true}, want: []string{"bash", "-l"}}, + {name: "sh when bash is absent", have: map[string]bool{"sh": true}, want: []string{"sh", "-l"}}, + {name: "neither shell is an error", have: map[string]bool{}, wantErr: true}, + {name: "a probe that cannot run is not a shell", failing: true, wantErr: true}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + probe := func(argv []string) (int, error) { + if tc.failing { + return 0, errors.New("exec failed") + } + if tc.have[argv[0]] { + return 0, nil + } + return 127, nil + } + got, err := pickShell(probe) + if tc.wantErr { + if !errors.Is(err, ErrNoShell) { + t.Fatalf("pickShell() = %v, want ErrNoShell", err) + } + return + } + if err != nil { + t.Fatalf("pickShell() = %v", err) + } + if !slices.Equal(got, tc.want) { + t.Errorf("pickShell() = %v, want %v", got, tc.want) + } + }) + } +} diff --git a/internal/infrastructure/kitrun/launcher.go b/internal/infrastructure/kitrun/launcher.go index bdaa035c6..c67a1365d 100644 --- a/internal/infrastructure/kitrun/launcher.go +++ b/internal/infrastructure/kitrun/launcher.go @@ -183,16 +183,20 @@ func (l *Launcher) LaunchSetup(ctx context.Context, req SetupRequest) (*Run, err l.release(run) return nil, err } - // The image's own entrypoint is the harness CLI and would exit or run a - // task; a setup container holds still until the PTY's shell ends. The - // container environment stays empty (WorkerEnv nil): the harness env is - // passed explicitly on the exec, and no worker credential reaches it. + // The container runs archie's own agent as a keepalive rather than the + // image's entrypoint (the harness CLI, which would exit) and rather than an + // image binary such as sleep, which a Kit image need not provide. The + // environment stays empty (WorkerEnv nil): the harness env is passed + // explicitly on the exec, and no worker credential reaches it. run.Container, err = l.Pool.AcquireKit(ctx, container.KitSpec{ - Name: "archie-kit-" + req.Session, - Image: req.Kit, - Network: run.network, - Worker: []string{"sleep", "infinity"}, - Binds: append([]string{l.CAFile + ":" + caPath + ":ro"}, skillsBinds(k.skills, l.skillsDir())...), + Name: "archie-kit-" + req.Session, + Image: req.Kit, + Network: run.network, + Worker: []string{agentPath, "hold"}, + Binds: append([]string{ + l.AgentBinary + ":" + agentPath + ":ro", + l.CAFile + ":" + caPath + ":ro", + }, skillsBinds(k.skills, l.skillsDir())...), Launch: launch, InstallDone: session.EnterRuntime, })