abb1da903f
Project deploys (both standard and blue-green) now stamp the new
workload labels on every container and dual-write a row into the
containers index alongside the existing instances row. The legacy
project/stage/instance-id labels stay for now so operator runbooks
don't break — they will be removed after the migration soaks.
New labels:
- tinyforge.managed (every Tinyforge container)
- tinyforge.workload.id (workload row primary key)
- tinyforge.workload.kind ('project' | 'stack' | 'site')
- tinyforge.role (stage name for projects)
ContainerConfig grows WorkloadID/WorkloadKind/Role fields. The
deployer resolves the project's workload row (guaranteed to exist
by boot-time backfill) and passes the IDs through. Container row
ID matches instance ID by construction so removeInstance can drop
both records together.
Stack and static-site managers still need the same treatment;
those land in the next commit.
127 lines
4.2 KiB
Go
127 lines
4.2 KiB
Go
package docker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/moby/moby/client"
|
|
)
|
|
|
|
// Labels applied to all containers managed by Tinyforge.
|
|
//
|
|
// Workload-shaped labels (LabelWorkloadID, LabelWorkloadKind, LabelRole,
|
|
// LabelManaged) are the canonical set going forward and what the reconciler
|
|
// queries by. The legacy project/stage/instance-id labels are still emitted
|
|
// alongside them for back-compat with anything that selects on them
|
|
// (operator runbooks, monitoring scrape rules, ad-hoc shell debugging) — they
|
|
// will be removed once the migration soaks.
|
|
const (
|
|
LabelProject = "tinyforge.project"
|
|
LabelStage = "tinyforge.stage"
|
|
LabelInstanceID = "tinyforge.instance-id"
|
|
|
|
LabelManaged = "tinyforge.managed" // present on every Tinyforge-managed container
|
|
LabelWorkloadID = "tinyforge.workload.id" // workload row primary key
|
|
LabelWorkloadKind = "tinyforge.workload.kind" // 'project' | 'stack' | 'site'
|
|
LabelRole = "tinyforge.role" // stage name (project), service name (stack), '' (site)
|
|
)
|
|
|
|
// Client wraps the Docker Engine API client.
|
|
type Client struct {
|
|
api client.APIClient
|
|
}
|
|
|
|
// New creates a new Docker client connected to the default Docker socket.
|
|
func New() (*Client, error) {
|
|
api, err := client.NewClientWithOpts(
|
|
client.FromEnv,
|
|
client.WithAPIVersionNegotiation(),
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create docker client: %w", err)
|
|
}
|
|
|
|
return &Client{api: api}, nil
|
|
}
|
|
|
|
// Close releases resources held by the Docker client.
|
|
func (c *Client) Close() error {
|
|
if err := c.api.Close(); err != nil {
|
|
return fmt.Errorf("close docker client: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Ping checks connectivity to the Docker daemon.
|
|
func (c *Client) Ping(ctx context.Context) error {
|
|
_, err := c.api.Ping(ctx, client.PingOptions{})
|
|
if err != nil {
|
|
return fmt.Errorf("ping docker daemon: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DaemonInfo captures the subset of Docker daemon info surfaced in the UI.
|
|
// Fields map directly to /info and /version; JSON tags match the wire format
|
|
// consumed by the frontend's DockerHealth type.
|
|
type DaemonInfo struct {
|
|
Version string `json:"version,omitempty"`
|
|
APIVersion string `json:"api_version,omitempty"`
|
|
OS string `json:"os,omitempty"`
|
|
Arch string `json:"arch,omitempty"`
|
|
Kernel string `json:"kernel,omitempty"`
|
|
OperatingSystem string `json:"operating_system,omitempty"`
|
|
StorageDriver string `json:"storage_driver,omitempty"`
|
|
RootDir string `json:"root_dir,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
NCPU int `json:"ncpu,omitempty"`
|
|
MemoryTotal int64 `json:"memory_total,omitempty"`
|
|
Containers int `json:"containers,omitempty"`
|
|
Running int `json:"running,omitempty"`
|
|
Paused int `json:"paused,omitempty"`
|
|
Stopped int `json:"stopped,omitempty"`
|
|
Images int `json:"images,omitempty"`
|
|
}
|
|
|
|
// Info returns a compact snapshot of daemon health data. Missing pieces
|
|
// (e.g. if ServerVersion fails but Info succeeds) are returned as zero
|
|
// values rather than bubbling up — the endpoint should degrade gracefully.
|
|
func (c *Client) Info(ctx context.Context) (DaemonInfo, error) {
|
|
info, err := c.api.Info(ctx, client.InfoOptions{})
|
|
if err != nil {
|
|
return DaemonInfo{}, fmt.Errorf("docker info: %w", err)
|
|
}
|
|
out := DaemonInfo{
|
|
OperatingSystem: info.Info.OperatingSystem,
|
|
Kernel: info.Info.KernelVersion,
|
|
Arch: info.Info.Architecture,
|
|
OS: info.Info.OSType,
|
|
StorageDriver: info.Info.Driver,
|
|
RootDir: info.Info.DockerRootDir,
|
|
Name: info.Info.Name,
|
|
NCPU: info.Info.NCPU,
|
|
MemoryTotal: info.Info.MemTotal,
|
|
Containers: info.Info.Containers,
|
|
Running: info.Info.ContainersRunning,
|
|
Paused: info.Info.ContainersPaused,
|
|
Stopped: info.Info.ContainersStopped,
|
|
Images: info.Info.Images,
|
|
Version: info.Info.ServerVersion,
|
|
}
|
|
|
|
if ver, verr := c.api.ServerVersion(ctx, client.ServerVersionOptions{}); verr == nil {
|
|
if ver.Version != "" {
|
|
out.Version = ver.Version
|
|
}
|
|
out.APIVersion = ver.APIVersion
|
|
if out.Arch == "" {
|
|
out.Arch = ver.Arch
|
|
}
|
|
if out.OS == "" {
|
|
out.OS = ver.Os
|
|
}
|
|
}
|
|
|
|
return out, nil
|
|
}
|