2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-08-17 22:19:44 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2020-05-26 13:44:26 +00:00
|
|
|
"context"
|
2018-08-17 22:19:44 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/nomad/plugins/device"
|
2018-11-27 19:04:43 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-08-17 22:19:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PluginFactory returns a new plugin instance
|
|
|
|
type PluginFactory func(log log.Logger) interface{}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// PluginCtxFactory returns a new plugin instance, that takes in a context
|
2020-05-26 13:44:26 +00:00
|
|
|
type PluginCtxFactory func(ctx context.Context, log log.Logger) interface{}
|
|
|
|
|
2018-08-17 22:19:44 +00:00
|
|
|
// Serve is used to serve a new Nomad plugin
|
|
|
|
func Serve(f PluginFactory) {
|
|
|
|
logger := log.New(&log.LoggerOptions{
|
|
|
|
Level: log.Trace,
|
|
|
|
JSONFormat: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
plugin := f(logger)
|
2020-05-26 13:44:26 +00:00
|
|
|
serve(plugin, logger)
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// ServeCtx is used to serve a new Nomad plugin
|
2020-05-26 13:44:26 +00:00
|
|
|
func ServeCtx(f PluginCtxFactory) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
logger := log.New(&log.LoggerOptions{
|
|
|
|
Level: log.Trace,
|
|
|
|
JSONFormat: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
plugin := f(ctx, logger)
|
|
|
|
serve(plugin, logger)
|
|
|
|
}
|
|
|
|
func serve(plugin interface{}, logger log.Logger) {
|
2018-08-17 22:19:44 +00:00
|
|
|
switch p := plugin.(type) {
|
|
|
|
case device.DevicePlugin:
|
|
|
|
device.Serve(p, logger)
|
2018-11-27 19:04:43 +00:00
|
|
|
case drivers.DriverPlugin:
|
|
|
|
drivers.Serve(p, logger)
|
2018-08-17 22:19:44 +00:00
|
|
|
default:
|
|
|
|
fmt.Println("Unsupported plugin type")
|
|
|
|
}
|
|
|
|
}
|