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