-
Notifications
You must be signed in to change notification settings - Fork 2k
/
serve.go
54 lines (44 loc) · 1.22 KB
/
serve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugins
import (
"context"
"fmt"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/plugins/device"
"github.com/hashicorp/nomad/plugins/drivers"
)
// PluginFactory returns a new plugin instance
type PluginFactory func(log log.Logger) interface{}
// PluginCtxFactory returns a new plugin instance, that takes in a context
type PluginCtxFactory func(ctx context.Context, log log.Logger) interface{}
// 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)
serve(plugin, logger)
}
// ServeCtx is used to serve a new Nomad plugin
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) {
switch p := plugin.(type) {
case device.DevicePlugin:
device.Serve(p, logger)
case drivers.DriverPlugin:
drivers.Serve(p, logger)
default:
fmt.Println("Unsupported plugin type")
}
}