go/pkg/devops/devops.go

219 lines
5.1 KiB
Go
Raw Normal View History

// Package devops provides a portable development environment using LinuxKit images.
package devops
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"github.com/host-uk/core/pkg/container"
docs(audit): add dependency security audit report (#248) * feat(devops): migrate filesystem operations to io.Local abstraction Migrate config.go: - os.ReadFile → io.Local.Read Migrate devops.go: - os.Stat → io.Local.IsFile Migrate images.go: - os.MkdirAll → io.Local.EnsureDir - os.Stat → io.Local.IsFile - os.ReadFile → io.Local.Read - os.WriteFile → io.Local.Write Migrate test.go: - os.ReadFile → io.Local.Read - os.Stat → io.Local.IsFile Migrate claude.go: - os.Stat → io.Local.IsDir Updated tests to reflect improved behavior: - Manifest.Save() now creates parent directories - hasFile() correctly returns false for directories Part of #101 (io.Medium migration tracking issue). Closes #107 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore(io): migrate remaining packages to io.Local abstraction Migrate filesystem operations to use the io.Local abstraction for improved security, testability, and consistency: - pkg/cache: Replace os.ReadFile, WriteFile, Remove, RemoveAll with io.Local equivalents. io.Local.Write creates parent dirs automatically. - pkg/agentic: Migrate config.go and context.go to use io.Local for reading config files and gathering file context. - pkg/repos: Use io.Local.Read, Exists, IsDir, List for registry operations and git repo detection. - pkg/release: Use io.Local for config loading, existence checks, and artifact discovery. - pkg/devops/sources: Use io.Local.EnsureDir for CDN download. All paths are converted to absolute using filepath.Abs() before calling io.Local methods to handle relative paths correctly. Closes #104, closes #106, closes #108, closes #111 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore(io): migrate pkg/cli and pkg/container to io.Local abstraction Continue io.Medium migration for the remaining packages: - pkg/cli/daemon.go: PIDFile Acquire/Release now use io.Local.Read, Delete, and Write for managing daemon PID files. - pkg/container/state.go: LoadState and SaveState use io.Local for JSON state persistence. EnsureLogsDir uses io.Local.EnsureDir. - pkg/container/templates.go: Template loading and directory scanning now use io.Local.IsFile, IsDir, Read, and List. - pkg/container/linuxkit.go: Image validation uses io.Local.IsFile, log file check uses io.Local.IsFile. Streaming log file creation (os.Create) remains unchanged as io.Local doesn't support streaming. Closes #105, closes #107 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs(audit): add dependency security audit report Complete security audit of all project dependencies: - Run govulncheck: No vulnerabilities found - Run go mod verify: All modules verified - Document 15 direct dependencies and 161 indirect - Assess supply chain risks: Low risk overall - Verify lock files are committed with integrity hashes - Provide CI integration recommendations Closes #185 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(ci): build core CLI from source instead of downloading release The workflows were trying to download from a non-existent release URL. Now builds the CLI directly using `go build` with version injection. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore: trigger CI with updated workflow * chore(ci): add workflow_dispatch trigger for manual runs --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 08:04:26 +00:00
"github.com/host-uk/core/pkg/io"
)
// DevOps manages the portable development environment.
type DevOps struct {
Migrate pkg/devops to Medium abstraction (#293) * chore(io): migrate pkg/devops to Medium abstraction This commit migrates the pkg/devops package to use the io.Medium abstraction instead of direct calls to io.Local or the os package. Changes: - Updated DevOps, ImageManager, and Manifest structs to hold an io.Medium. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. This migration improves the testability and flexibility of the devops package by allowing for different storage backends. * chore(io): migrate pkg/devops to Medium abstraction This commit completes the migration of the pkg/devops package to the io.Medium abstraction. Changes: - Refactored DevOps, ImageManager, and Manifest structs to use io.Medium for storage operations. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. - Fixed formatting issues in test files. This migration enables easier testing and supports alternative storage backends.
2026-02-04 14:58:03 +00:00
medium io.Medium
config *Config
images *ImageManager
container *container.LinuxKitManager
}
Migrate pkg/devops to Medium abstraction (#293) * chore(io): migrate pkg/devops to Medium abstraction This commit migrates the pkg/devops package to use the io.Medium abstraction instead of direct calls to io.Local or the os package. Changes: - Updated DevOps, ImageManager, and Manifest structs to hold an io.Medium. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. This migration improves the testability and flexibility of the devops package by allowing for different storage backends. * chore(io): migrate pkg/devops to Medium abstraction This commit completes the migration of the pkg/devops package to the io.Medium abstraction. Changes: - Refactored DevOps, ImageManager, and Manifest structs to use io.Medium for storage operations. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. - Fixed formatting issues in test files. This migration enables easier testing and supports alternative storage backends.
2026-02-04 14:58:03 +00:00
// New creates a new DevOps instance using the provided medium.
func New(m io.Medium) (*DevOps, error) {
cfg, err := LoadConfig(m)
if err != nil {
return nil, fmt.Errorf("devops.New: failed to load config: %w", err)
}
Migrate pkg/devops to Medium abstraction (#293) * chore(io): migrate pkg/devops to Medium abstraction This commit migrates the pkg/devops package to use the io.Medium abstraction instead of direct calls to io.Local or the os package. Changes: - Updated DevOps, ImageManager, and Manifest structs to hold an io.Medium. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. This migration improves the testability and flexibility of the devops package by allowing for different storage backends. * chore(io): migrate pkg/devops to Medium abstraction This commit completes the migration of the pkg/devops package to the io.Medium abstraction. Changes: - Refactored DevOps, ImageManager, and Manifest structs to use io.Medium for storage operations. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. - Fixed formatting issues in test files. This migration enables easier testing and supports alternative storage backends.
2026-02-04 14:58:03 +00:00
images, err := NewImageManager(m, cfg)
if err != nil {
return nil, fmt.Errorf("devops.New: failed to create image manager: %w", err)
}
Migrate pkg/container to io.Medium abstraction (#292) * chore(io): migrate pkg/container to Medium abstraction Migrated State, Templates, and LinuxKitManager in pkg/container to use the io.Medium abstraction for storage operations. - Introduced TemplateManager struct to handle template logic with injected medium. - Updated State struct to use injected medium for persistence. - Updated LinuxKitManager to hold and use an io.Medium instance. - Updated all internal callers in internal/cmd/vm and pkg/devops to use new APIs. - Adapted and maintained comprehensive test coverage in linuxkit_test.go. - Fixed naming collision with standard io package by aliasing it as goio. * chore(io): migrate pkg/container to Medium abstraction (v2) - Migrated State, Templates, and LinuxKitManager in pkg/container to use io.Medium. - Introduced TemplateManager struct for dependency injection. - Updated all call sites in internal/cmd/vm and pkg/devops. - Restored and adapted comprehensive test suite in linuxkit_test.go. - Fixed naming collisions and followed project test naming conventions. * chore(io): address PR feedback for container Medium migration - Added Open method to io.Medium interface to support log streaming. - Implemented Open in local.Medium and MockMedium. - Fixed extension inconsistency in GetTemplate (.yml vs .yaml). - Refactored TemplateManager to use configurable WorkingDir and HomeDir. - Reused TemplateManager instance in cmd_templates.go. - Updated LinuxKitManager to use medium.Open for log access. - Maintained and updated all tests to verify these improvements.
2026-02-04 15:33:22 +00:00
mgr, err := container.NewLinuxKitManager(io.Local)
if err != nil {
return nil, fmt.Errorf("devops.New: failed to create container manager: %w", err)
}
return &DevOps{
Migrate pkg/devops to Medium abstraction (#293) * chore(io): migrate pkg/devops to Medium abstraction This commit migrates the pkg/devops package to use the io.Medium abstraction instead of direct calls to io.Local or the os package. Changes: - Updated DevOps, ImageManager, and Manifest structs to hold an io.Medium. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. This migration improves the testability and flexibility of the devops package by allowing for different storage backends. * chore(io): migrate pkg/devops to Medium abstraction This commit completes the migration of the pkg/devops package to the io.Medium abstraction. Changes: - Refactored DevOps, ImageManager, and Manifest structs to use io.Medium for storage operations. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. - Fixed formatting issues in test files. This migration enables easier testing and supports alternative storage backends.
2026-02-04 14:58:03 +00:00
medium: m,
config: cfg,
images: images,
container: mgr,
}, nil
}
// ImageName returns the platform-specific image name.
func ImageName() string {
return fmt.Sprintf("core-devops-%s-%s.qcow2", runtime.GOOS, runtime.GOARCH)
}
// ImagesDir returns the path to the images directory.
func ImagesDir() (string, error) {
if dir := os.Getenv("CORE_IMAGES_DIR"); dir != "" {
return dir, nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".core", "images"), nil
}
// ImagePath returns the full path to the platform-specific image.
func ImagePath() (string, error) {
dir, err := ImagesDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ImageName()), nil
}
// IsInstalled checks if the dev image is installed.
func (d *DevOps) IsInstalled() bool {
path, err := ImagePath()
if err != nil {
return false
}
Migrate pkg/devops to Medium abstraction (#293) * chore(io): migrate pkg/devops to Medium abstraction This commit migrates the pkg/devops package to use the io.Medium abstraction instead of direct calls to io.Local or the os package. Changes: - Updated DevOps, ImageManager, and Manifest structs to hold an io.Medium. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. This migration improves the testability and flexibility of the devops package by allowing for different storage backends. * chore(io): migrate pkg/devops to Medium abstraction This commit completes the migration of the pkg/devops package to the io.Medium abstraction. Changes: - Refactored DevOps, ImageManager, and Manifest structs to use io.Medium for storage operations. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. - Fixed formatting issues in test files. This migration enables easier testing and supports alternative storage backends.
2026-02-04 14:58:03 +00:00
return d.medium.IsFile(path)
}
// Install downloads and installs the dev image.
func (d *DevOps) Install(ctx context.Context, progress func(downloaded, total int64)) error {
return d.images.Install(ctx, progress)
}
// CheckUpdate checks if an update is available.
func (d *DevOps) CheckUpdate(ctx context.Context) (current, latest string, hasUpdate bool, err error) {
return d.images.CheckUpdate(ctx)
}
// BootOptions configures how to boot the dev environment.
type BootOptions struct {
Memory int // MB, default 4096
CPUs int // default 2
Name string // container name
Fresh bool // destroy existing and start fresh
}
// DefaultBootOptions returns sensible defaults.
func DefaultBootOptions() BootOptions {
return BootOptions{
Memory: 4096,
CPUs: 2,
Name: "core-dev",
}
}
// Boot starts the dev environment.
func (d *DevOps) Boot(ctx context.Context, opts BootOptions) error {
if !d.images.IsInstalled() {
return fmt.Errorf("dev image not installed (run 'core dev install' first)")
}
// Check if already running
if !opts.Fresh {
running, err := d.IsRunning(ctx)
if err == nil && running {
return fmt.Errorf("dev environment already running (use 'core dev stop' first or --fresh)")
}
}
// Stop existing if fresh
if opts.Fresh {
_ = d.Stop(ctx)
}
imagePath, err := ImagePath()
if err != nil {
return err
}
// Build run options for LinuxKitManager
runOpts := container.RunOptions{
Name: opts.Name,
Memory: opts.Memory,
CPUs: opts.CPUs,
SSHPort: 2222,
Detach: true,
}
_, err = d.container.Run(ctx, imagePath, runOpts)
return err
}
// Stop stops the dev environment.
func (d *DevOps) Stop(ctx context.Context) error {
c, err := d.findContainer(ctx, "core-dev")
if err != nil {
return err
}
if c == nil {
return fmt.Errorf("dev environment not found")
}
return d.container.Stop(ctx, c.ID)
}
// IsRunning checks if the dev environment is running.
func (d *DevOps) IsRunning(ctx context.Context) (bool, error) {
c, err := d.findContainer(ctx, "core-dev")
if err != nil {
return false, err
}
return c != nil && c.Status == container.StatusRunning, nil
}
// findContainer finds a container by name.
func (d *DevOps) findContainer(ctx context.Context, name string) (*container.Container, error) {
containers, err := d.container.List(ctx)
if err != nil {
return nil, err
}
for _, c := range containers {
if c.Name == name {
return c, nil
}
}
return nil, nil
}
// DevStatus returns information about the dev environment.
type DevStatus struct {
Installed bool
Running bool
ImageVersion string
ContainerID string
Memory int
CPUs int
SSHPort int
Uptime time.Duration
}
// Status returns the current dev environment status.
func (d *DevOps) Status(ctx context.Context) (*DevStatus, error) {
status := &DevStatus{
Installed: d.images.IsInstalled(),
SSHPort: 2222,
}
if info, ok := d.images.manifest.Images[ImageName()]; ok {
status.ImageVersion = info.Version
}
c, _ := d.findContainer(ctx, "core-dev")
if c != nil {
status.Running = c.Status == container.StatusRunning
status.ContainerID = c.ID
status.Memory = c.Memory
status.CPUs = c.CPUs
if status.Running {
status.Uptime = time.Since(c.StartedAt)
}
}
return status, nil
}