2018-08-17 22:19:44 +00:00
|
|
|
package example
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-08-28 17:30:57 +00:00
|
|
|
"os"
|
2018-08-17 22:19:44 +00:00
|
|
|
"path/filepath"
|
2018-08-28 17:30:57 +00:00
|
|
|
"sync"
|
2018-08-17 22:19:44 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-12-18 00:40:58 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-08-17 22:19:44 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
"github.com/hashicorp/nomad/plugins/device"
|
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
2018-11-12 00:36:20 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-09-28 17:09:01 +00:00
|
|
|
"github.com/kr/pretty"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-08-17 22:19:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// pluginName is the name of the plugin
|
2018-08-28 17:30:57 +00:00
|
|
|
pluginName = "example-fs-device"
|
2018-08-17 22:19:44 +00:00
|
|
|
|
|
|
|
// vendor is the vendor providing the devices
|
|
|
|
vendor = "nomad"
|
|
|
|
|
|
|
|
// deviceType is the type of device being returned
|
|
|
|
deviceType = "file"
|
|
|
|
|
|
|
|
// deviceName is the name of the devices being exposed
|
|
|
|
deviceName = "mock"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// pluginInfo describes the plugin
|
|
|
|
pluginInfo = &base.PluginInfoResponse{
|
2018-12-18 00:40:58 +00:00
|
|
|
Type: base.PluginTypeDevice,
|
|
|
|
PluginApiVersions: []string{device.ApiVersion010},
|
|
|
|
PluginVersion: "v0.1.0",
|
|
|
|
Name: pluginName,
|
2018-08-17 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// configSpec is the specification of the plugin's configuration
|
|
|
|
configSpec = hclspec.NewObject(map[string]*hclspec.Spec{
|
2018-08-28 17:30:57 +00:00
|
|
|
"dir": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("dir", "string", false),
|
|
|
|
hclspec.NewLiteral("\".\""),
|
|
|
|
),
|
2018-08-17 22:19:44 +00:00
|
|
|
"list_period": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("list_period", "string", false),
|
|
|
|
hclspec.NewLiteral("\"5s\""),
|
|
|
|
),
|
|
|
|
"unhealthy_perm": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("unhealthy_perm", "string", false),
|
|
|
|
hclspec.NewLiteral("\"-rwxrwxrwx\""),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains configuration information for the plugin.
|
|
|
|
type Config struct {
|
|
|
|
Dir string `codec:"dir"`
|
|
|
|
ListPeriod string `codec:"list_period"`
|
|
|
|
UnhealthyPerm string `codec:"unhealthy_perm"`
|
|
|
|
}
|
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
// FsDevice is an example device plugin. The device plugin exposes files as
|
2018-08-17 22:19:44 +00:00
|
|
|
// devices and periodically polls the directory for new files. If a file has a
|
|
|
|
// given file permission, it is considered unhealthy. This device plugin is
|
|
|
|
// purely for use as an example.
|
2018-08-28 17:30:57 +00:00
|
|
|
type FsDevice struct {
|
2018-08-17 22:19:44 +00:00
|
|
|
logger log.Logger
|
|
|
|
|
|
|
|
// deviceDir is the directory we expose as devices
|
|
|
|
deviceDir string
|
|
|
|
|
|
|
|
// unhealthyPerm is the permissions on a file we consider unhealthy
|
|
|
|
unhealthyPerm string
|
|
|
|
|
|
|
|
// listPeriod is how often we should list the device directory to detect new
|
|
|
|
// devices
|
|
|
|
listPeriod time.Duration
|
|
|
|
|
|
|
|
// devices is the set of detected devices and maps whether they are healthy
|
2018-08-28 17:30:57 +00:00
|
|
|
devices map[string]bool
|
|
|
|
deviceLock sync.RWMutex
|
2018-08-17 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExampleDevice returns a new example device plugin.
|
2018-08-28 17:30:57 +00:00
|
|
|
func NewExampleDevice(log log.Logger) *FsDevice {
|
|
|
|
return &FsDevice{
|
2018-08-17 22:19:44 +00:00
|
|
|
logger: log.Named(pluginName),
|
|
|
|
devices: make(map[string]bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PluginInfo returns information describing the plugin.
|
2018-08-28 17:30:57 +00:00
|
|
|
func (d *FsDevice) PluginInfo() (*base.PluginInfoResponse, error) {
|
2018-08-17 22:19:44 +00:00
|
|
|
return pluginInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigSchema returns the plugins configuration schema.
|
2018-08-28 17:30:57 +00:00
|
|
|
func (d *FsDevice) ConfigSchema() (*hclspec.Spec, error) {
|
2018-08-17 22:19:44 +00:00
|
|
|
return configSpec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig is used to set the configuration of the plugin.
|
2018-12-18 00:40:58 +00:00
|
|
|
func (d *FsDevice) SetConfig(c *base.Config) error {
|
2018-08-17 22:19:44 +00:00
|
|
|
var config Config
|
2018-12-18 00:40:58 +00:00
|
|
|
if err := base.MsgPackDecode(c.PluginConfig, &config); err != nil {
|
2018-08-17 22:19:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the device directory and the unhealthy permissions
|
|
|
|
d.deviceDir = config.Dir
|
|
|
|
d.unhealthyPerm = config.UnhealthyPerm
|
|
|
|
|
|
|
|
// Convert the poll period
|
|
|
|
period, err := time.ParseDuration(config.ListPeriod)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse list period %q: %v", config.ListPeriod, err)
|
|
|
|
}
|
|
|
|
d.listPeriod = period
|
|
|
|
|
|
|
|
d.logger.Debug("test debug")
|
|
|
|
d.logger.Info("config set", "config", log.Fmt("% #v", pretty.Formatter(config)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fingerprint streams detected devices. If device changes are detected or the
|
|
|
|
// devices health changes, messages will be emitted.
|
2018-08-28 17:30:57 +00:00
|
|
|
func (d *FsDevice) Fingerprint(ctx context.Context) (<-chan *device.FingerprintResponse, error) {
|
2018-08-17 22:19:44 +00:00
|
|
|
if d.deviceDir == "" {
|
|
|
|
return nil, status.New(codes.Internal, "device directory not set in config").Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
outCh := make(chan *device.FingerprintResponse)
|
|
|
|
go d.fingerprint(ctx, outCh)
|
|
|
|
return outCh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fingerprint is the long running goroutine that detects hardware
|
2018-08-28 17:30:57 +00:00
|
|
|
func (d *FsDevice) fingerprint(ctx context.Context, devices chan *device.FingerprintResponse) {
|
2018-08-17 22:19:44 +00:00
|
|
|
defer close(devices)
|
|
|
|
|
|
|
|
// Create a timer that will fire immediately for the first detection
|
|
|
|
ticker := time.NewTimer(0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
ticker.Reset(d.listPeriod)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.logger.Trace("scanning for changes")
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(d.deviceDir)
|
|
|
|
if err != nil {
|
|
|
|
d.logger.Error("failed to list device directory", "error", err)
|
|
|
|
devices <- device.NewFingerprintError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
detected := d.diffFiles(files)
|
|
|
|
if len(detected) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-17 22:19:44 +00:00
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
devices <- device.NewFingerprint(getDeviceGroup(detected))
|
2018-08-17 22:19:44 +00:00
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 22:19:44 +00:00
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
func (d *FsDevice) diffFiles(files []os.FileInfo) []*device.Device {
|
|
|
|
d.deviceLock.Lock()
|
|
|
|
defer d.deviceLock.Unlock()
|
2018-08-17 22:19:44 +00:00
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
// Build an unhealthy message
|
|
|
|
unhealthyDesc := fmt.Sprintf("Device has bad permissions %q", d.unhealthyPerm)
|
|
|
|
|
|
|
|
var changes bool
|
|
|
|
fnames := make(map[string]struct{})
|
|
|
|
for _, f := range files {
|
|
|
|
name := f.Name()
|
|
|
|
fnames[name] = struct{}{}
|
|
|
|
if f.IsDir() {
|
|
|
|
d.logger.Trace("skipping directory", "directory", name)
|
|
|
|
continue
|
2018-08-17 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
// Determine the health
|
|
|
|
perms := f.Mode().Perm().String()
|
|
|
|
healthy := perms != d.unhealthyPerm
|
|
|
|
d.logger.Trace("checking health", "file perm", perms, "unhealthy perms", d.unhealthyPerm, "healthy", healthy)
|
|
|
|
|
|
|
|
// See if we alreay have the device
|
|
|
|
oldHealth, ok := d.devices[name]
|
|
|
|
if ok && oldHealth == healthy {
|
2018-08-17 22:19:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
// Health has changed or we have a new object
|
|
|
|
changes = true
|
|
|
|
d.devices[name] = healthy
|
|
|
|
}
|
2018-08-17 22:19:44 +00:00
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
for id := range d.devices {
|
|
|
|
if _, ok := fnames[id]; !ok {
|
|
|
|
delete(d.devices, id)
|
|
|
|
changes = true
|
2018-08-17 22:19:44 +00:00
|
|
|
}
|
2018-08-28 17:30:57 +00:00
|
|
|
}
|
2018-08-17 22:19:44 +00:00
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
// Nothing to do
|
|
|
|
if !changes {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the devices
|
|
|
|
detected := make([]*device.Device, 0, len(d.devices))
|
|
|
|
for name, healthy := range d.devices {
|
|
|
|
var desc string
|
|
|
|
if !healthy {
|
|
|
|
desc = unhealthyDesc
|
|
|
|
}
|
2018-08-17 22:19:44 +00:00
|
|
|
|
2018-08-28 17:30:57 +00:00
|
|
|
detected = append(detected, &device.Device{
|
|
|
|
ID: name,
|
|
|
|
Healthy: healthy,
|
|
|
|
HealthDesc: desc,
|
|
|
|
})
|
2018-08-17 22:19:44 +00:00
|
|
|
}
|
2018-08-28 17:30:57 +00:00
|
|
|
|
|
|
|
return detected
|
2018-08-17 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDeviceGroup is a helper to build the DeviceGroup given a set of devices.
|
|
|
|
func getDeviceGroup(devices []*device.Device) *device.DeviceGroup {
|
|
|
|
return &device.DeviceGroup{
|
|
|
|
Vendor: vendor,
|
|
|
|
Type: deviceType,
|
|
|
|
Name: deviceName,
|
|
|
|
Devices: devices,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserve returns information on how to mount the given devices.
|
2018-08-28 17:30:57 +00:00
|
|
|
func (d *FsDevice) Reserve(deviceIDs []string) (*device.ContainerReservation, error) {
|
2018-08-17 22:19:44 +00:00
|
|
|
if len(deviceIDs) == 0 {
|
|
|
|
return nil, status.New(codes.InvalidArgument, "no device ids given").Err()
|
|
|
|
}
|
|
|
|
|
2018-12-05 02:00:54 +00:00
|
|
|
deviceDir, err := filepath.Abs(d.deviceDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Newf(codes.Internal, "failed to load device dir abs path").Err()
|
|
|
|
}
|
|
|
|
|
2018-08-17 22:19:44 +00:00
|
|
|
resp := &device.ContainerReservation{}
|
|
|
|
|
|
|
|
for _, id := range deviceIDs {
|
|
|
|
// Check if the device is known
|
|
|
|
if _, ok := d.devices[id]; !ok {
|
|
|
|
return nil, status.Newf(codes.InvalidArgument, "unknown device %q", id).Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a mount
|
2018-12-05 02:00:54 +00:00
|
|
|
resp.Mounts = append(resp.Mounts, &device.Mount{
|
|
|
|
TaskPath: fmt.Sprintf("/tmp/task-mounts/%s", id),
|
|
|
|
HostPath: filepath.Join(deviceDir, id),
|
|
|
|
ReadOnly: false,
|
2018-08-17 22:19:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-28 17:30:57 +00:00
|
|
|
|
|
|
|
// Stats streams statistics for the detected devices.
|
2018-09-28 17:09:01 +00:00
|
|
|
func (d *FsDevice) Stats(ctx context.Context, interval time.Duration) (<-chan *device.StatsResponse, error) {
|
2018-08-28 17:30:57 +00:00
|
|
|
outCh := make(chan *device.StatsResponse)
|
2018-09-28 17:09:01 +00:00
|
|
|
go d.stats(ctx, outCh, interval)
|
2018-08-28 17:30:57 +00:00
|
|
|
return outCh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stats is the long running goroutine that streams device statistics
|
2018-09-28 17:09:01 +00:00
|
|
|
func (d *FsDevice) stats(ctx context.Context, stats chan *device.StatsResponse, interval time.Duration) {
|
2018-08-28 17:30:57 +00:00
|
|
|
defer close(stats)
|
|
|
|
|
|
|
|
// Create a timer that will fire immediately for the first detection
|
|
|
|
ticker := time.NewTimer(0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2018-09-28 17:09:01 +00:00
|
|
|
ticker.Reset(interval)
|
2018-08-28 17:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deviceStats, err := d.collectStats()
|
|
|
|
if err != nil {
|
|
|
|
stats <- &device.StatsResponse{
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if deviceStats == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stats <- &device.StatsResponse{
|
|
|
|
Groups: []*device.DeviceGroupStats{deviceStats},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FsDevice) collectStats() (*device.DeviceGroupStats, error) {
|
|
|
|
d.deviceLock.RLock()
|
|
|
|
defer d.deviceLock.RUnlock()
|
|
|
|
l := len(d.devices)
|
|
|
|
if l == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
group := &device.DeviceGroupStats{
|
|
|
|
Vendor: vendor,
|
|
|
|
Type: deviceType,
|
|
|
|
Name: deviceName,
|
|
|
|
InstanceStats: make(map[string]*device.DeviceStats, l),
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range d.devices {
|
|
|
|
p := filepath.Join(d.deviceDir, k)
|
|
|
|
f, err := os.Stat(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to stat %q: %v", p, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &device.DeviceStats{
|
2018-11-12 00:36:20 +00:00
|
|
|
Summary: &structs.StatValue{
|
2018-11-13 15:06:44 +00:00
|
|
|
IntNumeratorVal: helper.Int64ToPtr(f.Size()),
|
2018-08-28 17:30:57 +00:00
|
|
|
Unit: "bytes",
|
|
|
|
Desc: "Filesize in bytes",
|
|
|
|
},
|
2018-11-12 00:36:20 +00:00
|
|
|
Stats: &structs.StatObject{
|
|
|
|
Attributes: map[string]*structs.StatValue{
|
2018-08-28 17:30:57 +00:00
|
|
|
"size": {
|
2018-11-13 15:06:44 +00:00
|
|
|
IntNumeratorVal: helper.Int64ToPtr(f.Size()),
|
2018-08-28 17:30:57 +00:00
|
|
|
Unit: "bytes",
|
|
|
|
Desc: "Filesize in bytes",
|
|
|
|
},
|
|
|
|
"modify_time": {
|
2018-11-13 15:06:44 +00:00
|
|
|
StringVal: helper.StringToPtr(f.ModTime().String()),
|
2018-08-28 17:30:57 +00:00
|
|
|
Desc: "Last modified",
|
|
|
|
},
|
|
|
|
"mode": {
|
2018-11-13 15:06:44 +00:00
|
|
|
StringVal: helper.StringToPtr(f.Mode().String()),
|
2018-08-28 17:30:57 +00:00
|
|
|
Desc: "File mode",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Timestamp: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
group.InstanceStats[k] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return group, nil
|
|
|
|
}
|