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"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *StorageFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
|
|
|
|
|
|
|
// Initialize these to empty defaults
|
|
|
|
node.Attributes["storage.volume"] = ""
|
|
|
|
node.Attributes["storage.bytestotal"] = ""
|
2015-08-27 19:37:05 +00:00
|
|
|
node.Attributes["storage.bytesfree"] = ""
|
|
|
|
if node.Resources == nil {
|
|
|
|
node.Resources = &structs.Resources{}
|
|
|
|
}
|
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 {
|
2015-12-09 21:34:18 +00:00
|
|
|
return false, 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 {
|
2015-12-09 21:34:18 +00:00
|
|
|
return false, 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
|
|
|
|
2015-11-30 21:34:18 +00:00
|
|
|
node.Attributes["storage.volume"] = volume
|
|
|
|
node.Attributes["storage.bytestotal"] = strconv.FormatUint(total, 10)
|
|
|
|
node.Attributes["storage.bytesfree"] = strconv.FormatUint(free, 10)
|
2015-08-27 00:07:31 +00:00
|
|
|
|
2015-12-09 21:34:18 +00:00
|
|
|
node.Resources.DiskMB = int(free / bytesPerMegabyte)
|
2015-08-27 00:07:31 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|