Files
archie/internal/daemon/runtime.go
T

60 lines
1.9 KiB
Go

package daemon
import (
"context"
"github.com/samcharles93/archie-core/internal/container"
"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)
}