Container runtime policies gain a runtime (docker or openshell) and the OpenShell gateway's address, workspace and token, set on the container runtime page and applied on restart. The OpenShell backend creates a sandbox per task with the worktree and volumes bound in, reports when the agent exits or outlives its maximum uptime, deletes the sandbox on release, and sweeps this instance's leftovers at boot. Storage volumes are created attachable to OpenShell sandboxes.
91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/samcharles93/archie-core/internal/container"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/openshell"
|
|
"github.com/samcharles93/archie-core/internal/storage"
|
|
)
|
|
|
|
// TaskRuntime starts and stops the isolated environment a task's agent runs
|
|
// in. The Docker container pool is one implementation.
|
|
type TaskRuntime interface {
|
|
// Acquire starts the agent from image with mounts and env.
|
|
Acquire(ctx context.Context, image string, mounts []storage.Mount, env []string) (TaskContainer, error)
|
|
// Release stops and removes a container Acquire returned.
|
|
Release(ctx context.Context, c TaskContainer)
|
|
// Reown hands dir back to uid:gid when a killed container left files the
|
|
// daemon cannot touch.
|
|
Reown(ctx context.Context, dir string, uid, gid int) error
|
|
}
|
|
|
|
// TaskContainer is one running task environment.
|
|
type TaskContainer interface {
|
|
// Exited closes when the agent stops running for any reason.
|
|
Exited() <-chan struct{}
|
|
// ExitDetail says how it ended, once Exited has closed.
|
|
ExitDetail() string
|
|
}
|
|
|
|
// DockerRuntime runs tasks in pool's containers. A nil pool is no runtime, so
|
|
// the daemon parks tasks rather than holding a runtime that cannot start one.
|
|
func DockerRuntime(pool *container.Pool) TaskRuntime {
|
|
if pool == nil {
|
|
return nil
|
|
}
|
|
return dockerRuntime{pool: pool}
|
|
}
|
|
|
|
type dockerRuntime struct{ pool *container.Pool }
|
|
|
|
func (r dockerRuntime) Acquire(ctx context.Context, image string, mounts []storage.Mount, env []string) (TaskContainer, error) {
|
|
c, err := r.pool.Acquire(ctx, image, mounts, env)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// Release releases a container this runtime acquired; any other value has
|
|
// nothing of this pool's to release.
|
|
func (r dockerRuntime) Release(ctx context.Context, c TaskContainer) {
|
|
if ctr, ok := c.(*container.Container); ok {
|
|
r.pool.Release(ctx, ctr)
|
|
}
|
|
}
|
|
|
|
func (r dockerRuntime) Reown(ctx context.Context, dir string, uid, gid int) error {
|
|
return r.pool.Reown(ctx, dir, uid, gid)
|
|
}
|
|
|
|
// OpenShellRuntime runs tasks as rt's OpenShell sandboxes. A nil rt is no
|
|
// runtime, as with DockerRuntime.
|
|
func OpenShellRuntime(rt *openshell.Runtime) TaskRuntime {
|
|
if rt == nil {
|
|
return nil
|
|
}
|
|
return openShellRuntime{rt: rt}
|
|
}
|
|
|
|
type openShellRuntime struct{ rt *openshell.Runtime }
|
|
|
|
func (r openShellRuntime) Acquire(ctx context.Context, image string, mounts []storage.Mount, env []string) (TaskContainer, error) {
|
|
sb, err := r.rt.Acquire(ctx, image, mounts, env)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return sb, nil
|
|
}
|
|
|
|
// Release releases a sandbox this runtime acquired.
|
|
func (r openShellRuntime) Release(ctx context.Context, c TaskContainer) {
|
|
if sb, ok := c.(*openshell.Sandbox); ok {
|
|
r.rt.Release(ctx, sb)
|
|
}
|
|
}
|
|
|
|
func (r openShellRuntime) Reown(ctx context.Context, dir string, uid, gid int) error {
|
|
return r.rt.Reown(ctx, dir, uid, gid)
|
|
}
|