Merge branch 'dev' into chore/io-migrate-repos-medium-11165034141497363118

This commit is contained in:
Snider 2026-02-04 14:38:12 +00:00 committed by GitHub
commit a99774f08e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 80 additions and 14 deletions

View file

@ -74,13 +74,14 @@ func IsStderrTTY() bool {
// PIDFile manages a process ID file for single-instance enforcement. // PIDFile manages a process ID file for single-instance enforcement.
type PIDFile struct { type PIDFile struct {
path string medium io.Medium
mu sync.Mutex path string
mu sync.Mutex
} }
// NewPIDFile creates a PID file manager. // NewPIDFile creates a PID file manager.
func NewPIDFile(path string) *PIDFile { func NewPIDFile(m io.Medium, path string) *PIDFile {
return &PIDFile{path: path} return &PIDFile{medium: m, path: path}
} }
// Acquire writes the current PID to the file. // Acquire writes the current PID to the file.
@ -90,7 +91,7 @@ func (p *PIDFile) Acquire() error {
defer p.mu.Unlock() defer p.mu.Unlock()
// Check if PID file exists // Check if PID file exists
if data, err := io.Local.Read(p.path); err == nil { if data, err := p.medium.Read(p.path); err == nil {
pid, err := strconv.Atoi(data) pid, err := strconv.Atoi(data)
if err == nil && pid > 0 { if err == nil && pid > 0 {
// Check if process is still running // Check if process is still running
@ -101,19 +102,19 @@ func (p *PIDFile) Acquire() error {
} }
} }
// Stale PID file, remove it // Stale PID file, remove it
_ = io.Local.Delete(p.path) _ = p.medium.Delete(p.path)
} }
// Ensure directory exists // Ensure directory exists
if dir := filepath.Dir(p.path); dir != "." { if dir := filepath.Dir(p.path); dir != "." {
if err := io.Local.EnsureDir(dir); err != nil { if err := p.medium.EnsureDir(dir); err != nil {
return fmt.Errorf("failed to create PID directory: %w", err) return fmt.Errorf("failed to create PID directory: %w", err)
} }
} }
// Write current PID // Write current PID
pid := os.Getpid() pid := os.Getpid()
if err := io.Local.Write(p.path, strconv.Itoa(pid)); err != nil { if err := p.medium.Write(p.path, strconv.Itoa(pid)); err != nil {
return fmt.Errorf("failed to write PID file: %w", err) return fmt.Errorf("failed to write PID file: %w", err)
} }
@ -124,7 +125,7 @@ func (p *PIDFile) Acquire() error {
func (p *PIDFile) Release() error { func (p *PIDFile) Release() error {
p.mu.Lock() p.mu.Lock()
defer p.mu.Unlock() defer p.mu.Unlock()
return io.Local.Delete(p.path) return p.medium.Delete(p.path)
} }
// Path returns the PID file path. // Path returns the PID file path.
@ -246,6 +247,9 @@ func (h *HealthServer) Addr() string {
// DaemonOptions configures daemon mode execution. // DaemonOptions configures daemon mode execution.
type DaemonOptions struct { type DaemonOptions struct {
// Medium is the filesystem abstraction.
Medium io.Medium
// PIDFile path for single-instance enforcement. // PIDFile path for single-instance enforcement.
// Leave empty to skip PID file management. // Leave empty to skip PID file management.
PIDFile string PIDFile string
@ -283,13 +287,17 @@ func NewDaemon(opts DaemonOptions) *Daemon {
opts.ShutdownTimeout = 30 * time.Second opts.ShutdownTimeout = 30 * time.Second
} }
if opts.Medium == nil {
opts.Medium = io.Local
}
d := &Daemon{ d := &Daemon{
opts: opts, opts: opts,
reload: make(chan struct{}, 1), reload: make(chan struct{}, 1),
} }
if opts.PIDFile != "" { if opts.PIDFile != "" {
d.pid = NewPIDFile(opts.PIDFile) d.pid = NewPIDFile(opts.Medium, opts.PIDFile)
} }
if opts.HealthAddr != "" { if opts.HealthAddr != "" {

View file

@ -8,6 +8,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/host-uk/core/pkg/io"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -31,7 +32,7 @@ func TestPIDFile(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
pidPath := filepath.Join(tmpDir, "test.pid") pidPath := filepath.Join(tmpDir, "test.pid")
pid := NewPIDFile(pidPath) pid := NewPIDFile(io.Local, pidPath)
// Acquire should succeed // Acquire should succeed
err := pid.Acquire() err := pid.Acquire()
@ -58,7 +59,7 @@ func TestPIDFile(t *testing.T) {
err := os.WriteFile(pidPath, []byte("999999999"), 0644) err := os.WriteFile(pidPath, []byte("999999999"), 0644)
require.NoError(t, err) require.NoError(t, err)
pid := NewPIDFile(pidPath) pid := NewPIDFile(io.Local, pidPath)
// Should acquire successfully (stale PID removed) // Should acquire successfully (stale PID removed)
err = pid.Acquire() err = pid.Acquire()
@ -72,7 +73,7 @@ func TestPIDFile(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
pidPath := filepath.Join(tmpDir, "subdir", "nested", "test.pid") pidPath := filepath.Join(tmpDir, "subdir", "nested", "test.pid")
pid := NewPIDFile(pidPath) pid := NewPIDFile(io.Local, pidPath)
err := pid.Acquire() err := pid.Acquire()
require.NoError(t, err) require.NoError(t, err)
@ -85,9 +86,26 @@ func TestPIDFile(t *testing.T) {
}) })
t.Run("path getter", func(t *testing.T) { t.Run("path getter", func(t *testing.T) {
pid := NewPIDFile("/tmp/test.pid") pid := NewPIDFile(io.Local, "/tmp/test.pid")
assert.Equal(t, "/tmp/test.pid", pid.Path()) assert.Equal(t, "/tmp/test.pid", pid.Path())
}) })
t.Run("with mock medium", func(t *testing.T) {
mock := io.NewMockMedium()
pidPath := "/tmp/mock.pid"
pid := NewPIDFile(mock, pidPath)
err := pid.Acquire()
require.NoError(t, err)
assert.True(t, mock.Exists(pidPath))
data, _ := mock.Read(pidPath)
assert.NotEmpty(t, data)
err = pid.Release()
require.NoError(t, err)
assert.False(t, mock.Exists(pidPath))
})
} }
func TestHealthServer(t *testing.T) { func TestHealthServer(t *testing.T) {
@ -244,6 +262,26 @@ func TestDaemon(t *testing.T) {
d := NewDaemon(DaemonOptions{}) d := NewDaemon(DaemonOptions{})
assert.Equal(t, 30*time.Second, d.opts.ShutdownTimeout) assert.Equal(t, 30*time.Second, d.opts.ShutdownTimeout)
}) })
t.Run("with mock medium", func(t *testing.T) {
mock := io.NewMockMedium()
pidPath := "/tmp/daemon.pid"
d := NewDaemon(DaemonOptions{
Medium: mock,
PIDFile: pidPath,
HealthAddr: "127.0.0.1:0",
})
err := d.Start()
require.NoError(t, err)
assert.True(t, mock.Exists(pidPath))
err = d.Stop()
require.NoError(t, err)
assert.False(t, mock.Exists(pidPath))
})
} }
func TestRunWithTimeout(t *testing.T) { func TestRunWithTimeout(t *testing.T) {

View file

@ -52,6 +52,10 @@ func NewLinuxKitManagerWithHypervisor(state *State, hypervisor Hypervisor) *Linu
// Run starts a new LinuxKit VM from the given image. // Run starts a new LinuxKit VM from the given image.
func (m *LinuxKitManager) Run(ctx context.Context, image string, opts RunOptions) (*Container, error) { func (m *LinuxKitManager) Run(ctx context.Context, image string, opts RunOptions) (*Container, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
// Validate image exists // Validate image exists
if !io.Local.IsFile(image) { if !io.Local.IsFile(image) {
return nil, fmt.Errorf("image not found: %s", image) return nil, fmt.Errorf("image not found: %s", image)
@ -232,6 +236,10 @@ func (m *LinuxKitManager) waitForExit(id string, cmd *exec.Cmd) {
// Stop stops a running container by sending SIGTERM. // Stop stops a running container by sending SIGTERM.
func (m *LinuxKitManager) Stop(ctx context.Context, id string) error { func (m *LinuxKitManager) Stop(ctx context.Context, id string) error {
if err := ctx.Err(); err != nil {
return err
}
container, ok := m.state.Get(id) container, ok := m.state.Get(id)
if !ok { if !ok {
return fmt.Errorf("container not found: %s", id) return fmt.Errorf("container not found: %s", id)
@ -290,6 +298,10 @@ func (m *LinuxKitManager) Stop(ctx context.Context, id string) error {
// List returns all known containers, verifying process state. // List returns all known containers, verifying process state.
func (m *LinuxKitManager) List(ctx context.Context) ([]*Container, error) { func (m *LinuxKitManager) List(ctx context.Context) ([]*Container, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
containers := m.state.All() containers := m.state.All()
// Verify each running container's process is still alive // Verify each running container's process is still alive
@ -319,6 +331,10 @@ func isProcessRunning(pid int) bool {
// Logs returns a reader for the container's log output. // Logs returns a reader for the container's log output.
func (m *LinuxKitManager) Logs(ctx context.Context, id string, follow bool) (goio.ReadCloser, error) { func (m *LinuxKitManager) Logs(ctx context.Context, id string, follow bool) (goio.ReadCloser, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
_, ok := m.state.Get(id) _, ok := m.state.Get(id)
if !ok { if !ok {
return nil, fmt.Errorf("container not found: %s", id) return nil, fmt.Errorf("container not found: %s", id)
@ -403,6 +419,10 @@ func (f *followReader) Close() error {
// Exec executes a command inside the container via SSH. // Exec executes a command inside the container via SSH.
func (m *LinuxKitManager) Exec(ctx context.Context, id string, cmd []string) error { func (m *LinuxKitManager) Exec(ctx context.Context, id string, cmd []string) error {
if err := ctx.Err(); err != nil {
return err
}
container, ok := m.state.Get(id) container, ok := m.state.Get(id)
if !ok { if !ok {
return fmt.Errorf("container not found: %s", id) return fmt.Errorf("container not found: %s", id)