Procházet zdrojové kódy

continue pier broadcast status construction

reid před 2 roky
rodič
revize
032404e185
3 změnil soubory, kde provedl 17 přidání a 18 odebrání
  1. 4 4
      broadcast/broadcast.go
  2. 11 12
      docker/docker.go
  3. 2 2
      structs/structs.go

+ 4 - 4
broadcast/broadcast.go

@@ -125,9 +125,9 @@ func constructPierInfo(piers []string) (map[string]structs.Urbit, error) {
 	for pier, status := range pierStatus {
 		// pull docker info from json
 		var dockerConfig structs.UrbitDocker
-		var dockerStats structs.ContainerStats
+		var dockerStats *structs.ContainerStats
 		dockerStats, err := docker.GetContainerStats(pier)
-		if err != null {
+		if err != nil {
 			errmsg := fmt.Sprintf("Unable to load %s stats: %v", pier, err)
 			logger.Error(errmsg)
 			continue
@@ -162,13 +162,13 @@ func constructPierInfo(piers []string) (map[string]structs.Urbit, error) {
 		urbit.Info.Network = shipNetworks[pier]
 		urbit.Info.URL = "http://" + hostName + ":" + string(dockerConfig.HTTPPort)
 		urbit.Info.LoomSize = int(math.Pow(2, float64(dockerConfig.LoomSize)) / math.Pow(1024, 2))
-		urbit.Info.DiskUsage = dockerStats.DiskUsage
+		urbit.Info.DiskUsage = *dockerStats.DiskUsage
 		urbit.Info.MemUsage = dockerStats.MemoryUsage
 		urbit.Info.DevMode = dockerConfig.DevMode
 		urbit.Info.Vere = dockerConfig.UrbitVersion
 		urbit.Info.DetectBootStatus = bootStatus
 		urbit.Info.Remote = setRemote
-		urbit.Info.Vere = dockerconfig.UrbitVersion
+		urbit.Info.Vere = dockerConfig.UrbitVersion
 		updates[pier] = urbit
 	}
 	return updates, nil

+ 11 - 12
docker/docker.go

@@ -4,6 +4,8 @@ import (
 	"context"
 	"fmt"
 	"log/slog"
+	"goseg/structs"
+	"encoding/json"
 	"os"
 
 	"github.com/docker/docker/api/types"
@@ -68,31 +70,28 @@ func GetContainerNetwork(name string) (string, error) {
 	return "", fmt.Errorf("container is not attached to any network")
 }
 
-func GetContainerStats(containerName string) (*ContainerStats, error) {
+func GetContainerStats(containerName string) (*structs.ContainerStats, error) {
 	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
 	if err != nil {
 		return nil, err
 	}
 	defer cli.Close()
-	stats, err := cli.ContainerStats(context.Background(), containerName, false)
+	statsResp, err := cli.ContainerStats(context.Background(), containerName, false)
 	if err != nil {
 		return nil, err
 	}
-	defer stats.Body.Close()
-
+	defer statsResp.Body.Close()
 	var stat types.StatsJSON
-	if err := stats.Decode(&stat); err != nil {
+	if err := json.NewDecoder(statsResp.Body).Decode(&stat); err != nil {
 		return nil, err
 	}
 	memUsage := stat.MemoryStats.Usage
-	var diskUsage uint64
-	for _, mount := range stat.Mounts {
-		if mount.Name == "container_root" {
-			diskUsage = mount.SizeRw
-			break
-		}
+	inspectResp, err := cli.ContainerInspect(context.Background(), containerName)
+	if err != nil {
+		return nil, err
 	}
-	return &ContainerStats{
+	diskUsage := inspectResp.SizeRw
+	return &structs.ContainerStats{
 		MemoryUsage: memUsage,
 		DiskUsage:   diskUsage,
 	}, nil

+ 2 - 2
structs/structs.go

@@ -156,7 +156,7 @@ type Urbit struct {
 		URL              string `json:"url"`
 		UrbAlias         bool   `json:"urbAlias"`
 		MemUsage         uint64    `json:"memUsage"`
-		DiskUsage        uint64    `json:"diskUsage"`
+		DiskUsage        int64    `json:"diskUsage"`
 		LoomSize         int    `json:"loomSize"`
 		DevMode          bool   `json:"devMode"`
 		DetectBootStatus bool   `json:"detectBootStatus"`
@@ -256,5 +256,5 @@ type UrbitDocker struct {
 
 type ContainerStats struct {
 	MemoryUsage uint64
-	DiskUsage   uint64
+	DiskUsage   *int64
 }