feat!: release v2 (#6545)

Co-authored-by: Artur <145776561+arturict@users.noreply.github.com>
Co-authored-by: arturict <arturict@users.noreply.github.com>
Co-authored-by: Thomas <thomas@ajnart.fr>
Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>
Co-authored-by: Danura30082 <danura30082+github@gmail.com>
Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com>
Co-authored-by: ajnart <thomas@ajnart.fr>
Co-authored-by: ajnart <thomas@ajnart.dev>
Co-authored-by: chenjun <chenjun@foxmail.com>
Co-authored-by: Wikibear <wikibear@users.noreply.github.com>
Co-authored-by: Bergasha <bergasha@gmail.com>
Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com>
Co-authored-by: Lucas Araujo <lucas.lra@gmail.com>
Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com>
Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social>
Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com>
Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com>
Co-authored-by: thrawnx <tight.bulb9384@fastmail.com>
Co-authored-by: Chris <58918656+nordada@users.noreply.github.com>
Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com>
Co-authored-by: Yanis <naamaneyanis@gmail.com>
Co-authored-by: Wikibear <me@sej.de>
Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
This commit is contained in:
3286 files changed
+284386 -139176

No files matched your search

+187
View File
@@ -0,0 +1,187 @@
package docker
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"sync"
"time"
)
// engineAPIVersion is the oldest Engine API version that supports every
// endpoint this CLI relies on. The daemon negotiates downwards on its own.
const engineAPIVersion = "v1.41"
// PullMessage is one record of the JSON progress stream returned by the Docker
// Engine when an image is pulled. Unlike the plain-text output of `docker
// pull`, this stream carries per-layer byte counters even when stdout is not a
// terminal, which is what makes a real progress animation possible.
type PullMessage struct {
ID string `json:"id"`
Status string `json:"status"`
Detail struct {
Current int64 `json:"current"`
Total int64 `json:"total"`
} `json:"progressDetail"`
Error string `json:"error"`
}
type engineEndpoint struct {
client *http.Client
base string
err error
}
var engineOnce struct {
sync.Once
endpoint engineEndpoint
}
// EngineHost reports the Docker endpoint this machine talks to, honouring
// DOCKER_HOST and the active `docker context` (OrbStack, Colima, and Docker
// Desktop all install their own socket).
func EngineHost() string {
if host := strings.TrimSpace(os.Getenv("DOCKER_HOST")); host != "" {
return host
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "docker", "context", "inspect", "--format", "{{.Endpoints.docker.Host}}").Output()
if err == nil {
if host := strings.TrimSpace(string(out)); host != "" {
return host
}
}
return "unix:///var/run/docker.sock"
}
func engine() (*http.Client, string, error) {
engineOnce.Do(func() { engineOnce.endpoint = dialEngine(EngineHost()) })
e := engineOnce.endpoint
return e.client, e.base, e.err
}
func dialEngine(host string) engineEndpoint {
switch {
case strings.HasPrefix(host, "unix://"):
path := strings.TrimPrefix(host, "unix://")
transport := &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", path)
},
}
return engineEndpoint{client: &http.Client{Transport: transport}, base: "http://docker/" + engineAPIVersion}
case strings.HasPrefix(host, "tcp://"), strings.HasPrefix(host, "http://"):
authority := host
for _, scheme := range []string{"tcp://", "http://"} {
authority = strings.TrimPrefix(authority, scheme)
}
return engineEndpoint{client: &http.Client{}, base: "http://" + authority + "/" + engineAPIVersion}
default:
return engineEndpoint{err: fmt.Errorf("unsupported Docker endpoint %q", host)}
}
}
// SplitImageReference separates a reference into its repository and tag,
// tolerating registries that carry an explicit port.
func SplitImageReference(reference string) (repository, tag string) {
slash := strings.LastIndex(reference, "/")
colon := strings.LastIndex(reference, ":")
if colon > slash {
return reference[:colon], reference[colon+1:]
}
return reference, "latest"
}
// PullStream starts an image pull against the Engine API and returns its
// progress records. The channel closes when the pull finishes; a message
// carrying Error describes a failure. Cancelling ctx aborts the pull.
func PullStream(ctx context.Context, image, platform string) (<-chan PullMessage, error) {
client, base, err := engine()
if err != nil {
return nil, err
}
repository, tag := SplitImageReference(image)
query := url.Values{"fromImage": {repository}, "tag": {tag}}
if platform != "" {
query.Set("platform", platform)
}
request, err := http.NewRequestWithContext(ctx, http.MethodPost, base+"/images/create?"+query.Encode(), nil)
if err != nil {
return nil, err
}
response, err := client.Do(request)
if err != nil {
return nil, fmt.Errorf("contact Docker engine: %w", err)
}
if response.StatusCode < 200 || response.StatusCode > 299 {
body, _ := io.ReadAll(io.LimitReader(response.Body, 4096))
_ = response.Body.Close()
return nil, fmt.Errorf("pull %s: %s", image, engineErrorMessage(response.Status, body))
}
messages := make(chan PullMessage, 64)
go func() {
defer close(messages)
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
for {
var message PullMessage
if err := decoder.Decode(&message); err != nil {
if err != io.EOF && ctx.Err() == nil {
select {
case messages <- PullMessage{Error: err.Error()}:
case <-ctx.Done():
}
}
return
}
select {
case messages <- message:
case <-ctx.Done():
return
}
}
}()
return messages, nil
}
func engineErrorMessage(status string, body []byte) string {
var payload struct {
Message string `json:"message"`
}
if err := json.Unmarshal(body, &payload); err == nil && payload.Message != "" {
return payload.Message
}
if trimmed := strings.TrimSpace(string(body)); trimmed != "" {
return trimmed
}
return status
}
// EngineAvailable reports whether the Engine API answers, so callers can fall
// back to the `docker` binary instead of failing outright.
func EngineAvailable(ctx context.Context) bool {
client, base, err := engine()
if err != nil {
return false
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, base+"/_ping", nil)
if err != nil {
return false
}
response, err := client.Do(request)
if err != nil {
return false
}
defer response.Body.Close()
_, _ = io.Copy(io.Discard, response.Body)
return response.StatusCode >= 200 && response.StatusCode < 300
}
+37 -16
View File
@@ -378,27 +378,36 @@ func IsPortConflict(err error) bool {
strings.Contains(message, "failed programming external connectivity")
}
func Stop(name string) error {
cmd := exec.Command("docker", "stop", name)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
func IsContainerNotFound(err error) bool {
if err == nil {
return false
}
return nil
return strings.Contains(strings.ToLower(err.Error()), "no such container")
}
func Restart(name string) error {
cmd := exec.Command("docker", "restart", name)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
}
return nil
func Stop(name string) error { return StopContext(context.Background(), name) }
func Restart(name string) error { return RestartContext(context.Background(), name) }
func Remove(name string) error { return RemoveContext(context.Background(), name) }
// The context-aware variants let a background job be cancelled while the daemon
// is still thinking, which matters when Docker is wedged and the user wants the
// interface back.
func StopContext(ctx context.Context, name string) error {
return runContainerCommand(ctx, "stop", name)
}
func Remove(name string) error {
cmd := exec.Command("docker", "rm", "-f", name)
out, err := cmd.CombinedOutput()
func RestartContext(ctx context.Context, name string) error {
return runContainerCommand(ctx, "restart", name)
}
func RemoveContext(ctx context.Context, name string) error {
return runContainerCommand(ctx, "rm", "-f", name)
}
func runContainerCommand(ctx context.Context, args ...string) error {
out, err := exec.CommandContext(ctx, "docker", args...).CombinedOutput()
if err != nil {
return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
}
@@ -425,3 +434,15 @@ func FollowLogs(name string) error {
cmd.Stderr = os.Stderr
return cmd.Run()
}
func ExecInteractive(ctx context.Context, name string, command ...string) error {
if len(command) == 0 {
command = []string{"sh"}
}
args := append([]string{"exec", "-it", name}, command...)
cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
+35 -14
View File
@@ -27,6 +27,8 @@ type Image struct {
Source string
Revision string
PRNumber int
Size string
Created string
}
func (image Image) Reference() string {
@@ -34,9 +36,11 @@ func (image Image) Reference() string {
}
type imageListRow struct {
ID string
Repository string
Tag string
ID string
Repository string
Tag string
Size string
CreatedSince string
}
func parseImageList(output []byte) ([]Image, error) {
@@ -50,7 +54,7 @@ func parseImageList(output []byte) ([]Image, error) {
return nil, fmt.Errorf("parse local Docker image: %w", err)
}
if row.Repository == "homarr" && row.Tag != "<none>" {
images = append(images, Image{ID: row.ID, Tag: row.Tag})
images = append(images, Image{ID: row.ID, Tag: row.Tag, Size: row.Size, Created: row.CreatedSince})
}
}
return images, nil
@@ -86,22 +90,39 @@ func inspectImageLabels(ctx context.Context, images []Image) ([]map[string]strin
args = append(args, image.Reference())
}
out, err := exec.CommandContext(ctx, "docker", args...).Output()
if err != nil {
return nil, fmt.Errorf("inspect local Homarr images: %w", err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) != len(images) {
return nil, fmt.Errorf("inspect returned %d images, expected %d", len(lines), len(images))
if err == nil {
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == len(images) {
labelsByImage := make([]map[string]string, len(images))
valid := true
for index, line := range lines {
labelsByImage[index] = make(map[string]string)
if line == "null" || line == "" {
continue
}
if err := json.Unmarshal([]byte(line), &labelsByImage[index]); err != nil {
valid = false
break
}
}
if valid {
return labelsByImage, nil
}
}
}
labelsByImage := make([]map[string]string, len(images))
for index, line := range lines {
for index, image := range images {
labelsByImage[index] = make(map[string]string)
if line == "null" {
singleOut, singleErr := exec.CommandContext(ctx, "docker", "image", "inspect", "--format", "{{json .Config.Labels}}", image.Reference()).Output()
if singleErr != nil {
continue
}
if err := json.Unmarshal([]byte(line), &labelsByImage[index]); err != nil {
return nil, fmt.Errorf("parse labels for %s: %w", images[index].Reference(), err)
trimmed := strings.TrimSpace(string(singleOut))
if trimmed == "" || trimmed == "null" {
continue
}
_ = json.Unmarshal([]byte(trimmed), &labelsByImage[index])
}
return labelsByImage, nil
}
@@ -0,0 +1,410 @@
package docker
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/charmbracelet/x/ansi"
)
// Layer is the download and extraction state of a single image layer.
type Layer struct {
ID string
Status string
Current int64
Total int64
}
// Fraction reports how far this layer is towards being usable, treating
// download and extraction as two halves of the work. Docker restarts the byte
// counters between the two phases, so they are weighted rather than summed.
func (l Layer) Fraction() float64 {
switch normalizeLayerStatus(l.Status) {
case "already exists", "pull complete":
return 1
case "downloading":
return 0.5 * ratio(l.Current, l.Total)
case "verifying checksum", "download complete":
return 0.5
case "extracting":
return 0.5 + 0.5*ratio(l.Current, l.Total)
default:
return 0
}
}
func (l Layer) Done() bool {
status := normalizeLayerStatus(l.Status)
return status == "pull complete" || status == "already exists"
}
func ratio(current, total int64) float64 {
if total <= 0 {
return 0
}
if current >= total {
return 1
}
return float64(current) / float64(total)
}
func normalizeLayerStatus(status string) string {
return strings.ToLower(strings.TrimSpace(status))
}
// PullTracker folds a pull progress stream into stable, ordered per-layer
// state so the UI can redraw it every frame without flicker.
type PullTracker struct {
order []string
layers map[string]*Layer
status string
started time.Time
sampleAt time.Time
sampled int64
rate float64
}
func NewPullTracker() *PullTracker {
now := time.Now()
return &PullTracker{layers: make(map[string]*Layer), started: now, sampleAt: now}
}
// Apply folds one Engine API record into the tracker.
func (t *PullTracker) Apply(message PullMessage) {
if message.ID == "" {
if message.Status != "" {
t.status = message.Status
}
return
}
// Digest and manifest records reuse the ID field but are not layers.
if strings.HasPrefix(message.ID, "sha256:") || len(message.ID) > 24 {
t.status = message.Status
return
}
layer, exists := t.layers[message.ID]
if !exists {
layer = &Layer{ID: message.ID}
t.layers[message.ID] = layer
t.order = append(t.order, message.ID)
}
if message.Status != "" {
layer.Status = message.Status
}
if message.Detail.Total > 0 {
layer.Total = message.Detail.Total
}
if message.Detail.Current > 0 || message.Detail.Total > 0 {
layer.Current = message.Detail.Current
}
if layer.Done() {
layer.Current = layer.Total
}
t.sample()
}
var plainPullPattern = regexp.MustCompile(`^([0-9a-zA-Z_-]{4,64}):\s+(.+)$`)
// ApplyLine folds a line of plain `docker pull` output into the tracker. It is
// the fallback used when the Engine API is unreachable; the resulting progress
// is coarser because the CLI omits byte counters when stdout is not a TTY.
func (t *PullTracker) ApplyLine(line string) {
line = strings.TrimSpace(ansi.Strip(line))
if line == "" {
return
}
match := plainPullPattern.FindStringSubmatch(line)
if match == nil {
t.status = line
return
}
status, current, total := parsePlainProgress(match[2])
t.Apply(PullMessage{
ID: match[1],
Status: status,
Detail: struct {
Current int64 `json:"current"`
Total int64 `json:"total"`
}{Current: current, Total: total},
})
}
var plainSizePattern = regexp.MustCompile(`([0-9.]+\s*[A-Za-z]*B)\s*/\s*([0-9.]+\s*[A-Za-z]*B)`)
func parsePlainProgress(rest string) (status string, current, total int64) {
status = rest
if bracket := strings.Index(rest, "["); bracket >= 0 {
status = strings.TrimSpace(rest[:bracket])
}
if match := plainSizePattern.FindStringSubmatch(rest); match != nil {
current = ParseSize(match[1])
total = ParseSize(match[2])
}
return status, current, total
}
var sizeUnits = map[string]int64{
"b": 1,
"kb": 1000, "mb": 1000 * 1000, "gb": 1000 * 1000 * 1000, "tb": 1000 * 1000 * 1000 * 1000,
"kib": 1024, "mib": 1024 * 1024, "gib": 1024 * 1024 * 1024, "tib": 1024 * 1024 * 1024 * 1024,
}
// ParseSize converts a human readable Docker size such as "12.3MB" into bytes.
func ParseSize(value string) int64 {
value = strings.ToLower(strings.TrimSpace(value))
index := 0
for index < len(value) && (value[index] >= '0' && value[index] <= '9' || value[index] == '.') {
index++
}
number, err := strconv.ParseFloat(strings.TrimSpace(value[:index]), 64)
if err != nil {
return 0
}
unit := strings.TrimSpace(value[index:])
if unit == "" {
return int64(number)
}
multiplier, known := sizeUnits[unit]
if !known {
return int64(number)
}
return int64(number * float64(multiplier))
}
// FormatSize renders a byte count the way the Docker CLI does.
func FormatSize(bytes int64) string {
const unit = 1000
if bytes < unit {
return fmt.Sprintf("%dB", bytes)
}
value := float64(bytes)
for _, suffix := range []string{"kB", "MB", "GB", "TB"} {
value /= unit
if value < unit {
return fmt.Sprintf("%.1f%s", value, suffix)
}
}
return fmt.Sprintf("%.1fPB", value/unit)
}
func (t *PullTracker) sample() {
now := time.Now()
elapsed := now.Sub(t.sampleAt)
if elapsed < 500*time.Millisecond {
return
}
current, _ := t.Bytes()
if delta := current - t.sampled; delta > 0 {
t.rate = float64(delta) / elapsed.Seconds()
}
t.sampled = current
t.sampleAt = now
}
// Layers returns the layers in the order Docker announced them.
func (t *PullTracker) Layers() []Layer {
layers := make([]Layer, 0, len(t.order))
for _, id := range t.order {
layers = append(layers, *t.layers[id])
}
return layers
}
// Bytes reports downloaded and total bytes across every layer that announced a
// size.
func (t *PullTracker) Bytes() (current, total int64) {
for _, layer := range t.layers {
if layer.Total <= 0 {
continue
}
total += layer.Total
if layer.Done() {
current += layer.Total
continue
}
current += min(layer.Current, layer.Total)
}
return current, total
}
// Complete reports how many layers are fully pulled out of how many are known.
func (t *PullTracker) Complete() (complete, total int) {
for _, layer := range t.layers {
total++
if layer.Done() {
complete++
}
}
return complete, total
}
// Percent is the overall progress of the pull in the range [0,1].
func (t *PullTracker) Percent() float64 {
if len(t.order) == 0 {
return 0
}
sum := 0.0
for _, id := range t.order {
sum += t.layers[id].Fraction()
}
return sum / float64(len(t.order))
}
// Rate is the recent download throughput in bytes per second.
func (t *PullTracker) Rate() float64 { return t.rate }
// Status is the most recent message that was not tied to a single layer.
func (t *PullTracker) Status() string { return t.status }
// Elapsed reports how long the pull has been running.
func (t *PullTracker) Elapsed() time.Duration { return time.Since(t.started) }
// BuildStep is one BuildKit vertex, such as a Dockerfile instruction.
type BuildStep struct {
ID string
Label string
Status string
Output []string
Done bool
Cached bool
Failed bool
Canceled bool
Elapsed string
}
// BuildTracker folds `docker build --progress=plain` output into ordered steps.
type BuildTracker struct {
order []string
steps map[string]*BuildStep
total int
started time.Time
last string
}
func NewBuildTracker() *BuildTracker {
return &BuildTracker{steps: make(map[string]*BuildStep), started: time.Now()}
}
var (
buildLinePattern = regexp.MustCompile(`^#(\d+)\s*(.*)$`)
buildStagePattern = regexp.MustCompile(`^\[[^\]]*?(\d+)/(\d+)\]`)
buildTimePattern = regexp.MustCompile(`^\d+\.\d+\s`)
)
// ApplyLine folds one line of BuildKit plain output into the tracker.
func (t *BuildTracker) ApplyLine(line string) {
line = strings.TrimRight(line, "\r\n")
trimmed := strings.TrimSpace(ansi.Strip(line))
if trimmed == "" {
return
}
match := buildLinePattern.FindStringSubmatch(trimmed)
if match == nil {
t.last = trimmed
return
}
id, rest := match[1], strings.TrimSpace(match[2])
// BuildKit reserves #0 for its own preamble ("building with ... driver").
// It never completes, so counting it would leave every finished build one
// step short of done.
if id == "0" {
t.last = rest
return
}
step, exists := t.steps[id]
if !exists {
step = &BuildStep{ID: id}
t.steps[id] = step
t.order = append(t.order, id)
}
switch {
case strings.HasPrefix(rest, "DONE"):
step.Done = true
step.Status = "done"
step.Elapsed = strings.TrimSpace(strings.TrimPrefix(rest, "DONE"))
case rest == "CACHED":
step.Done, step.Cached, step.Status = true, true, "cached"
case strings.HasPrefix(rest, "ERROR"):
step.Failed, step.Done, step.Status = true, true, "failed"
step.Output = appendStepOutput(step.Output, rest)
t.last = rest
case strings.HasPrefix(rest, "CANCELED"):
step.Done, step.Status, step.Canceled = true, "canceled", true
case buildTimePattern.MatchString(rest):
// `#12 1.234 <output>` — timestamped output from inside the step.
_, output, _ := strings.Cut(rest, " ")
if output = strings.TrimSpace(output); output != "" {
step.Output = appendStepOutput(step.Output, output)
t.last = output
}
case step.Label == "":
step.Label = rest
if stage := buildStagePattern.FindStringSubmatch(rest); stage != nil {
if total, err := strconv.Atoi(stage[2]); err == nil && total > t.total {
t.total = total
}
}
default:
step.Output = appendStepOutput(step.Output, rest)
}
}
func appendStepOutput(output []string, line string) []string {
const retained = 200
output = append(output, line)
if len(output) > retained {
output = output[len(output)-retained:]
}
return output
}
// Steps returns every announced step in declaration order.
func (t *BuildTracker) Steps() []BuildStep {
steps := make([]BuildStep, 0, len(t.order))
for _, id := range t.order {
steps = append(steps, *t.steps[id])
}
return steps
}
// Active returns the step BuildKit is currently working on, if any.
func (t *BuildTracker) Active() (BuildStep, bool) {
for index := len(t.order) - 1; index >= 0; index-- {
if step := t.steps[t.order[index]]; !step.Done && step.Label != "" {
return *step, true
}
}
return BuildStep{}, false
}
// Complete reports finished steps out of the best known total. The total grows
// as BuildKit announces stages, so it is never allowed to shrink below what has
// already been seen. A step BuildKit reported as canceled or failed is finished
// but not complete — counting it would report a killed build as fully built.
func (t *BuildTracker) Complete() (complete, total int) {
for _, step := range t.steps {
if step.Done && !step.Canceled && !step.Failed {
complete++
}
}
return complete, max(t.total, len(t.order))
}
// Percent is the overall build progress in the range [0,1].
func (t *BuildTracker) Percent() float64 {
complete, total := t.Complete()
if total == 0 {
return 0
}
return ratio(int64(complete), int64(total))
}
// LastMessage is the most recent human readable line, used for error context.
func (t *BuildTracker) LastMessage() string { return t.last }
// Elapsed reports how long the build has been running.
func (t *BuildTracker) Elapsed() time.Duration { return time.Since(t.started) }
@@ -0,0 +1,163 @@
package docker
import (
"testing"
)
func TestLayerFraction(t *testing.T) {
cases := []struct {
status string
current int64
total int64
want float64
}{
{"pull complete", 0, 0, 1.0},
{"already exists", 0, 0, 1.0},
{"download complete", 100, 100, 0.5},
{"verifying checksum", 100, 100, 0.5},
{"downloading", 50, 100, 0.25},
{"extracting", 50, 100, 0.75},
{"waiting", 0, 100, 0.0},
}
for _, c := range cases {
layer := Layer{Status: c.status, Current: c.current, Total: c.total}
if got := layer.Fraction(); got != c.want {
t.Errorf("Layer{%q, %d, %d}.Fraction() = %v, want %v", c.status, c.current, c.total, got, c.want)
}
}
}
func TestParseSizeAndFormatSize(t *testing.T) {
if got := ParseSize("12.3MB"); got != 12300000 {
t.Errorf("ParseSize(12.3MB) = %d, want 12300000", got)
}
if got := ParseSize("10MiB"); got != 10*1024*1024 {
t.Errorf("ParseSize(10MiB) = %d, want %d", got, 10*1024*1024)
}
if got := ParseSize("500B"); got != 500 {
t.Errorf("ParseSize(500B) = %d, want 500", got)
}
if got := ParseSize("invalid"); got != 0 {
t.Errorf("ParseSize(invalid) = %d, want 0", got)
}
if got := FormatSize(500); got != "500B" {
t.Errorf("FormatSize(500) = %q, want 500B", got)
}
if got := FormatSize(1500000); got != "1.5MB" {
t.Errorf("FormatSize(1500000) = %q, want 1.5MB", got)
}
if got := FormatSize(2500000000); got != "2.5GB" {
t.Errorf("FormatSize(2500000000) = %q, want 2.5GB", got)
}
}
func TestPullTrackerApplyAndProgress(t *testing.T) {
tracker := NewPullTracker()
tracker.Apply(PullMessage{
ID: "layer1",
Status: "Downloading",
Detail: struct {
Current int64 `json:"current"`
Total int64 `json:"total"`
}{Current: 50, Total: 100},
})
tracker.Apply(PullMessage{
ID: "layer2",
Status: "Extracting",
Detail: struct {
Current int64 `json:"current"`
Total int64 `json:"total"`
}{Current: 50, Total: 100},
})
layers := tracker.Layers()
if len(layers) != 2 {
t.Fatalf("expected 2 layers, got %d", len(layers))
}
if layers[0].ID != "layer1" || layers[1].ID != "layer2" {
t.Errorf("unexpected layers order: %v, %v", layers[0].ID, layers[1].ID)
}
currentBytes, totalBytes := tracker.Bytes()
if currentBytes != 100 || totalBytes != 200 {
t.Errorf("Bytes() = %d, %d; want 100, 200", currentBytes, totalBytes)
}
complete, total := tracker.Complete()
if complete != 0 || total != 2 {
t.Errorf("Complete() = %d, %d; want 0, 2", complete, total)
}
if percent := tracker.Percent(); percent != 0.5 {
t.Errorf("Percent() = %v, want 0.5", percent)
}
tracker.ApplyLine("c0ffee123456: Pull complete")
if len(tracker.Layers()) != 3 {
t.Fatalf("expected 3 layers after ApplyLine, got %d", len(tracker.Layers()))
}
complete, total = tracker.Complete()
if complete != 1 || total != 3 {
t.Errorf("Complete() = %d, %d; want 1, 3", complete, total)
}
}
func TestBuildTrackerStepsAndProgress(t *testing.T) {
tracker := NewBuildTracker()
tracker.ApplyLine("#0 building with \"default\" instance using docker driver")
if len(tracker.Steps()) != 0 {
t.Fatalf("expected step #0 to be skipped, got %d steps", len(tracker.Steps()))
}
tracker.ApplyLine("#1 [internal] load build definition from Dockerfile")
tracker.ApplyLine("#1 transferring dockerfile: 3.42kB done")
tracker.ApplyLine("#1 DONE 0.0s")
tracker.ApplyLine("#2 [1/3] FROM docker.io/library/alpine:3.20")
tracker.ApplyLine("#2 CACHED")
tracker.ApplyLine("#3 [2/3] RUN echo step2")
tracker.ApplyLine("#3 0.123 hello from build")
steps := tracker.Steps()
if len(steps) != 3 {
t.Fatalf("expected 3 steps, got %d", len(steps))
}
if !steps[0].Done {
t.Errorf("step #1 should be marked Done")
}
if !steps[1].Cached || !steps[1].Done {
t.Errorf("step #2 should be marked Cached and Done")
}
if steps[2].Done {
t.Errorf("step #3 should not be Done yet")
}
active, running := tracker.Active()
if !running || active.ID != "3" {
t.Errorf("Active() = %+v, %v; want ID 3, true", active, running)
}
complete, total := tracker.Complete()
if complete != 2 || total < 3 {
t.Errorf("Complete() = %d, %d; want 2, >=3", complete, total)
}
if last := tracker.LastMessage(); last != "hello from build" {
t.Errorf("LastMessage() = %q, want 'hello from build'", last)
}
tracker.ApplyLine("#3 CANCELED")
steps = tracker.Steps()
if !steps[2].Canceled {
t.Errorf("step #3 should be marked Canceled")
}
complete, _ = tracker.Complete()
if complete != 2 {
t.Errorf("Complete() after cancel = %d, want 2", complete)
}
}
+102
View File
@@ -0,0 +1,102 @@
package docker
import (
"bufio"
"context"
"fmt"
"io"
"os/exec"
"strings"
)
// StreamLine is one line of output from a streamed command. Err is set only on
// the final line, which also has Done set.
type StreamLine struct {
Text string
Err error
Done bool
}
// splitLines is a bufio.SplitFunc that treats both newline and carriage return
// as terminators so in-place progress updates arrive as discrete lines.
func splitLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
for index, char := range data {
if char == '\n' || char == '\r' {
return index + 1, data[:index], nil
}
}
if atEOF && len(data) > 0 {
return len(data), data, nil
}
return 0, nil, nil
}
// StreamCommand runs cmd with stdout and stderr merged and delivers its output
// line by line. The channel is closed after a final Done line carrying the exit
// status. Cancelling the command's context terminates the process.
func StreamCommand(cmd *exec.Cmd) (<-chan StreamLine, error) {
reader, writer := io.Pipe()
cmd.Stdout = writer
cmd.Stderr = writer
if err := cmd.Start(); err != nil {
_ = reader.Close()
_ = writer.Close()
return nil, err
}
// Wait runs on its own goroutine and closes the write end, which is what
// gives the scanner an EOF. Waiting first and closing afterwards deadlocks:
// the scanner would block forever on a pipe this process still holds open.
// Wait only returns once every byte has been copied into the pipe, so no
// output can be lost to the close.
waited := make(chan error, 1)
go func() {
err := cmd.Wait()
_ = writer.Close()
waited <- err
}()
lines := make(chan StreamLine, 256)
go func() {
defer close(lines)
scanner := bufio.NewScanner(reader)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
scanner.Split(splitLines)
last := ""
for scanner.Scan() {
text := strings.TrimRight(scanner.Text(), "\r\n")
if strings.TrimSpace(text) == "" {
continue
}
last = text
lines <- StreamLine{Text: text}
}
_ = reader.Close()
err := <-waited
if err != nil && last != "" {
err = fmt.Errorf("%w: %s", err, strings.TrimSpace(last))
}
lines <- StreamLine{Done: true, Err: err}
}()
return lines, nil
}
// StreamContainerLogs follows a container's logs, replaying the last tail lines
// first. The stream ends when the container stops or ctx is cancelled.
func StreamContainerLogs(ctx context.Context, name string, tail int) (<-chan StreamLine, error) {
cmd := exec.CommandContext(ctx, "docker", "logs", "--follow", "--tail", fmt.Sprint(tail), name)
cmd.Cancel = func() error { return cmd.Process.Kill() }
return StreamCommand(cmd)
}
// BuildCommandStreaming returns a build command configured for BuildKit's plain
// progress output, which is the only format that can be parsed line by line.
func BuildCommandStreaming(ctx context.Context, options BuildOptions) (*exec.Cmd, error) {
cmd, err := BuildCommand(ctx, options)
if err != nil {
return nil, err
}
cmd.Args = append(cmd.Args[:len(cmd.Args)-1], "--progress=plain", cmd.Args[len(cmd.Args)-1])
cmd.Env = append(cmd.Environ(), "BUILDKIT_PROGRESS=plain", "DOCKER_BUILDKIT=1")
return cmd, nil
}
+186
View File
@@ -0,0 +1,186 @@
package docker
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
)
// Volume is a Docker volume that belongs to a Homarr instance.
type Volume struct {
Name string
Driver string
Mountpoint string
Size string
InUse bool
}
// PRNumber returns the pull request a volume stores data for, or zero.
func (v Volume) PRNumber() int {
var number int
if _, err := fmt.Sscanf(v.Name, "homarr_pr_%d_data", &number); err == nil {
return number
}
return 0
}
// Tag returns the local image tag a volume stores data for, or an empty string.
func (v Volume) Tag() string {
if v.PRNumber() > 0 {
return ""
}
name := strings.TrimPrefix(v.Name, "homarr_")
if name == v.Name {
return ""
}
return strings.TrimSuffix(name, "_data")
}
// ListVolumes returns the Homarr data volumes, optionally annotated with disk
// usage when includeSizes is true.
func ListVolumes(ctx context.Context, includeSizes bool) ([]Volume, error) {
out, err := exec.CommandContext(ctx, "docker", "volume", "ls", "--filter", "name=homarr", "--format", "{{json .}}").Output()
if err != nil {
return nil, fmt.Errorf("list Homarr volumes: %w", err)
}
volumes, err := parseVolumeList(out)
if err != nil {
return nil, err
}
var sizes map[string]string
if includeSizes {
sizes = volumeSizes(ctx)
}
inUse := volumesInUse(ctx)
for index := range volumes {
if sizes != nil {
if size, known := sizes[volumes[index].Name]; known {
volumes[index].Size = size
}
}
volumes[index].InUse = inUse[volumes[index].Name]
}
return volumes, nil
}
func parseVolumeList(output []byte) ([]Volume, error) {
volumes := make([]Volume, 0)
for _, line := range strings.Split(strings.TrimSpace(string(output)), "\n") {
if line == "" {
continue
}
var row struct {
Name string
Driver string
Mountpoint string
Size string
}
if err := json.Unmarshal([]byte(line), &row); err != nil {
return nil, fmt.Errorf("parse Docker volume: %w", err)
}
// buildx keeps its cache in volumes that match the same name filter.
if !strings.HasPrefix(row.Name, "homarr_") && row.Name != "homarr" {
continue
}
size := row.Size
if size == "N/A" {
size = ""
}
volumes = append(volumes, Volume{Name: row.Name, Driver: row.Driver, Mountpoint: row.Mountpoint, Size: size})
}
return volumes, nil
}
// volumeSizes reads reclaimable space per volume. `docker system df -v` is slow
// and optional, so failures degrade to unknown sizes rather than an error.
func volumeSizes(ctx context.Context) map[string]string {
sizes := make(map[string]string)
out, err := exec.CommandContext(ctx, "docker", "system", "df", "-v", "--format", "{{json .Volumes}}").Output()
if err != nil {
return sizes
}
var rows []struct {
Name string
Size string
}
if err := json.Unmarshal(out, &rows); err != nil {
return sizes
}
for _, row := range rows {
if row.Size != "" && row.Size != "N/A" {
sizes[row.Name] = row.Size
}
}
return sizes
}
func volumesInUse(ctx context.Context) map[string]bool {
inUse := make(map[string]bool)
out, err := exec.CommandContext(ctx, "docker", "ps", "-a", "--filter", "name=homarr", "--format", "{{.Mounts}}").Output()
if err != nil {
return inUse
}
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
for _, mount := range strings.Split(line, ",") {
if mount = strings.TrimSpace(mount); mount != "" {
inUse[mount] = true
}
}
}
return inUse
}
// InspectContainerVolumeMount verifies that a container with the expected owner
// name uses the selected data volume and returns its immutable ID for removal.
func InspectContainerVolumeMount(ctx context.Context, containerName, volumeName string) (string, bool, error) {
out, err := exec.CommandContext(
ctx,
"docker",
"container",
"inspect",
"--format",
`{"id":{{json .Id}},"mounts":{{json .Mounts}}}`,
containerName,
).CombinedOutput()
if err != nil {
return "", false, fmt.Errorf("inspect container %s: %w: %s", containerName, err, strings.TrimSpace(string(out)))
}
var inspected struct {
ID string `json:"id"`
Mounts []struct {
Name string `json:"Name"`
} `json:"mounts"`
}
if err := json.Unmarshal(out, &inspected); err != nil {
return "", false, fmt.Errorf("parse mounts for container %s: %w", containerName, err)
}
if inspected.ID == "" {
return "", false, fmt.Errorf("inspect container %s: Docker returned an empty container ID", containerName)
}
for _, mount := range inspected.Mounts {
if mount.Name == volumeName {
return inspected.ID, true, nil
}
}
return inspected.ID, false, nil
}
// RemoveVolume deletes a volume and the instance data it holds.
func RemoveVolume(ctx context.Context, name string) error {
out, err := exec.CommandContext(ctx, "docker", "volume", "rm", name).CombinedOutput()
if err != nil {
return fmt.Errorf("remove volume %s: %w: %s", name, err, strings.TrimSpace(string(out)))
}
return nil
}
// RemoveImage force-deletes a local image, including any tags pointing at it.
func RemoveImage(ctx context.Context, reference string) error {
out, err := exec.CommandContext(ctx, "docker", "image", "rm", "-f", reference).CombinedOutput()
if err != nil {
return fmt.Errorf("remove image %s: %w: %s", reference, err, strings.TrimSpace(string(out)))
}
return nil
}