github.com/smithx10/nomad@v0.9.1-rc1/client/fingerprint/storage.go (about)

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