2016-01-27 06:22:25 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
2016-05-09 07:54:33 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2016-01-27 06:22:25 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
)
|
|
|
|
|
2016-01-29 13:34:29 +00:00
|
|
|
// FindCgroupMountpointDir is used to find the cgroup mount point on a Linux
|
|
|
|
// system.
|
2016-01-27 06:22:25 +00:00
|
|
|
func FindCgroupMountpointDir() (string, error) {
|
|
|
|
mount, err := cgroups.FindCgroupMountpointDir()
|
|
|
|
if err != nil {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case *cgroups.NotFoundError:
|
|
|
|
// It's okay if the mount point is not discovered
|
|
|
|
return "", nil
|
|
|
|
default:
|
|
|
|
// All other errors are passed back as is
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mount, nil
|
|
|
|
}
|
2016-05-09 07:54:33 +00:00
|
|
|
|
|
|
|
// Fingerprint tries to find a valid cgroup moint point
|
2018-01-24 14:09:53 +00:00
|
|
|
func (f *CGroupFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
|
2016-05-09 07:54:33 +00:00
|
|
|
mount, err := f.mountPointDetector.MountPoint()
|
|
|
|
if err != nil {
|
2018-01-24 14:09:53 +00:00
|
|
|
resp.Attributes = f.clearCGroupAttributes(resp.Attributes)
|
|
|
|
return fmt.Errorf("Failed to discover cgroup mount point: %s", err)
|
2016-05-09 07:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a cgroup mount point was found
|
|
|
|
if mount == "" {
|
|
|
|
// Clear any attributes from the previous fingerprint.
|
2018-01-24 14:09:53 +00:00
|
|
|
resp.Attributes = f.clearCGroupAttributes(resp.Attributes)
|
2016-05-09 07:54:33 +00:00
|
|
|
|
|
|
|
if f.lastState == cgroupAvailable {
|
|
|
|
f.logger.Printf("[INFO] fingerprint.cgroups: cgroups are unavailable")
|
|
|
|
}
|
|
|
|
f.lastState = cgroupUnavailable
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2016-05-09 07:54:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
resp.Attributes["unique.cgroup.mountpoint"] = mount
|
2016-05-09 07:54:33 +00:00
|
|
|
|
|
|
|
if f.lastState == cgroupUnavailable {
|
|
|
|
f.logger.Printf("[INFO] fingerprint.cgroups: cgroups are available")
|
|
|
|
}
|
|
|
|
f.lastState = cgroupAvailable
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2016-05-09 07:54:33 +00:00
|
|
|
}
|