Files
felhom.eu/hub/internal/store/small_disk.go
T

71 lines
2.8 KiB
Go

package store
import (
"strconv"
"strings"
"time"
)
// A box that was installed on a small system disk with the operator's flag (D83, `16-storage-physical-identity.md`
// §14.5; R-1398 part 2). The installer says so in its enrolment request (`small_disk: true`, and — a later installer
// — `system_disk_bytes`); the hub keeps it so that the operator can see which boxes were accepted below 200 GB.
//
// Kept as one of the hub's small marks that NAME the box (`os_alarm:small_disk:<host>:<bytes>`, the value is when it
// was noted): DeleteHost already removes every mark that names a deleted box, so this needs no delete of its own and
// leaves nothing behind (D73). The size is part of the key, 0 = not told. The values come from a box's installer:
// the flag must be the JSON `true`, and the size is kept only when it is a plausible disk size.
//
// Pinned by TestSmallDisk_* (small_disk_test.go) and TestD83_* (api, web).
const (
smallDiskMark = "small_disk:"
smallDiskMaxBytes = int64(200) * 1000 * 1000 * 1000 // a box marked small is BELOW the 200 GB line
)
// SmallDisk is what the hub knows of a box that was accepted on a small system disk.
type SmallDisk struct {
Bytes int64 // 0 = the installer did not say
NotedAt time.Time // when the hub was told (the hub's clock)
}
// NoteSmallDisk records that hostID was installed with the small-disk flag. bytes outside (0, 200 GB) is kept as
// „not told” — a size at or above the line contradicts the flag and is a box's word. Told again, the newest wins.
func (s *Store) NoteSmallDisk(hostID string, bytes int64, now time.Time) error {
if hostID == "" || strings.Contains(hostID, ":") {
return nil
}
if bytes <= 0 || bytes >= smallDiskMaxBytes {
bytes = 0
}
for _, k := range s.smallDiskKeys(hostID) {
_ = s.SetOSAlarmRaised(k, time.Time{})
}
return s.SetOSAlarmRaised(smallDiskMark+hostID+":"+strconv.FormatInt(bytes, 10), now)
}
// SmallDiskOf returns the mark of hostID, or nil when the hub was never told (an older installer says nothing, and a
// box on a disk of 200 GB or more has no mark).
func (s *Store) SmallDiskOf(hostID string) *SmallDisk {
for _, k := range s.smallDiskKeys(hostID) {
n, err := strconv.ParseInt(k[strings.LastIndex(k, ":")+1:], 10, 64)
if err != nil {
continue
}
return &SmallDisk{Bytes: n, NotedAt: s.OSAlarmRaised(k)}
}
return nil
}
// smallDiskKeys lists the marks of exactly this box: a box whose id only STARTS with hostID is another box.
func (s *Store) smallDiskKeys(hostID string) []string {
if hostID == "" || strings.Contains(hostID, ":") {
return nil
}
var out []string
for _, k := range s.OSAlarmKeys(smallDiskMark + hostID + ":") {
if rest := strings.TrimPrefix(k, smallDiskMark+hostID+":"); rest != "" && !strings.Contains(rest, ":") {
out = append(out, k)
}
}
return out
}