github.com/quite/nomad@v0.8.6/client/fingerprint/storage.go (about) 1 package fingerprint 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "strconv" 8 9 cstructs "github.com/hashicorp/nomad/client/structs" 10 "github.com/hashicorp/nomad/nomad/structs" 11 ) 12 13 const bytesPerMegabyte = 1024 * 1024 14 15 // StorageFingerprint is used to measure the amount of storage free for 16 // applications that the Nomad agent will run on this machine. 17 type StorageFingerprint struct { 18 StaticFingerprinter 19 logger *log.Logger 20 } 21 22 func NewStorageFingerprint(logger *log.Logger) Fingerprint { 23 fp := &StorageFingerprint{logger: logger} 24 return fp 25 } 26 27 func (f *StorageFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error { 28 cfg := req.Config 29 30 // Guard against unset AllocDir 31 storageDir := cfg.AllocDir 32 if storageDir == "" { 33 var err error 34 storageDir, err = os.Getwd() 35 if err != nil { 36 return fmt.Errorf("unable to get CWD from filesystem: %s", err) 37 } 38 } 39 40 volume, total, free, err := f.diskFree(storageDir) 41 if err != nil { 42 return fmt.Errorf("failed to determine disk space for %s: %v", storageDir, err) 43 } 44 45 resp.AddAttribute("unique.storage.volume", volume) 46 resp.AddAttribute("unique.storage.bytestotal", strconv.FormatUint(total, 10)) 47 resp.AddAttribute("unique.storage.bytesfree", strconv.FormatUint(free, 10)) 48 49 // set the disk size for the response 50 resp.Resources = &structs.Resources{ 51 DiskMB: int(free / bytesPerMegabyte), 52 } 53 resp.Detected = true 54 55 return nil 56 }