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
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user