open-nomad/client/fingerprint/storage.go

63 lines
1.6 KiB
Go
Raw Normal View History

package fingerprint
import (
"fmt"
"os"
"strconv"
log "github.com/hashicorp/go-hclog"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/nomad/structs"
)
2015-12-09 21:34:18 +00:00
const bytesPerMegabyte = 1024 * 1024
// StorageFingerprint is used to measure the amount of storage free for
// applications that the Nomad agent will run on this machine.
type StorageFingerprint struct {
2015-11-05 21:46:02 +00:00
StaticFingerprinter
logger log.Logger
}
func NewStorageFingerprint(logger log.Logger) Fingerprint {
fp := &StorageFingerprint{logger: logger.Named("storage")}
return fp
}
func (f *StorageFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
cfg := req.Config
// Guard against unset AllocDir
storageDir := cfg.AllocDir
if storageDir == "" {
var err error
storageDir, err = os.Getwd()
if err != nil {
return fmt.Errorf("unable to get CWD from filesystem: %s", err)
}
}
volume, total, free, err := f.diskFree(storageDir)
if err != nil {
return fmt.Errorf("failed to determine disk space for %s: %v", storageDir, err)
}
resp.AddAttribute("unique.storage.volume", volume)
resp.AddAttribute("unique.storage.bytestotal", strconv.FormatUint(total, 10))
resp.AddAttribute("unique.storage.bytesfree", strconv.FormatUint(free, 10))
// set the disk size for the response
2018-09-30 00:23:41 +00:00
// COMPAT(0.10): Remove in 0.10
resp.Resources = &structs.Resources{
DiskMB: int(free / bytesPerMegabyte),
}
2018-09-30 00:23:41 +00:00
resp.NodeResources = &structs.NodeResources{
Disk: structs.NodeDiskResources{
2018-10-16 22:34:32 +00:00
DiskMB: int64(free / bytesPerMegabyte),
2018-09-30 00:23:41 +00:00
},
}
2018-01-31 22:03:55 +00:00
resp.Detected = true
return nil
}