/
spi.go
124 lines (102 loc) · 3.41 KB
/
spi.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package managerplugin
import (
"context"
"fmt"
"reflect"
"unsafe"
"github.com/ipfs-force-community/damocles/manager-plugin/kvstore"
"github.com/ipfs-force-community/damocles/manager-plugin/objstore"
)
const (
// PluginSuffix defines damocles-manager plugin's file suffix.
PluginSuffix = ".so"
// ManifestSymbol defines damocles-manager plugin's entrance symbol.
// Plugin take manifest info from this symbol.
ManifestSymbol = "PluginManifest"
)
var (
ErrInvalidPluginManifest = fmt.Errorf("invalid plugin manifest")
)
// Kind presents the kind of plugin.
type Kind uint8
const (
// KVStore indicates it is a KVStore plugin.
KVStore Kind = 1 + iota
// ObjStore indicates it is a ObjStore plugin.
ObjStore
// RegisterJsonRpc indicates it is a RegisterJsonRpc plugin.
RegisterJsonRpc
// SyncSectorState indicates it is a SyncSectorState plugin.
SyncSectorState
)
func (k Kind) String() (str string) {
switch k {
case KVStore:
str = "KVStore"
case ObjStore:
str = "ObjStore"
case SyncSectorState:
str = "SyncSectorState"
}
return
}
type Manifest struct {
// The plugin name
Name string
// The description of plugin
Description string
BuildTime string
// OnInit defines the plugin init logic.
// it will be called after damocles-manager-daemon init.
// return error will stop load plugin process and damocles-manager startup.
// `pluginsDir` is the damocles-manager plugins directory
OnInit func(ctx context.Context, pluginsDir string, manifest *Manifest) error
// OnShutDown defines the plugin cleanup logic.
// return error will write log and continue shutdown.
OnShutdown func(ctx context.Context, manifest *Manifest) error
Kind Kind
}
type ObjStoreManifest struct {
Manifest
Constructor func(cfg objstore.Config) (objstore.Store, error)
}
type KVStoreManifest struct {
Manifest
Constructor func(meta map[string]string) (kvstore.DB, error)
}
type SyncSectorStateManifest struct {
Manifest
OnImport func(args ...interface{}) error
OnInit func(args ...interface{}) error
OnUpdate func(args ...interface{}) error
OnFinalize func(args ...interface{}) error
OnRestore func(args ...interface{}) error
}
type RegisterJsonRpcManifest struct {
Manifest
// Handler returns the jsonrpc namespace and handler
// See: https://github.com/ipfs-force-community/go-jsonrpc/blob/4e8fb6324df7a31eaa6b480ef9e2a175545ba04b/server.go#L137
Handler func() (namespace string, handler interface{})
}
// ExportManifest exports a manifest to damocles-manager as a known format.
// it just casts sub-manifest to manifest.
func ExportManifest(m interface{}) *Manifest {
v := reflect.ValueOf(m)
return (*Manifest)(unsafe.Pointer(v.Pointer()))
}
// DeclareObjStoreManifest declares manifest as ObjStoreManifest.
func DeclareObjStoreManifest(m *Manifest) *ObjStoreManifest {
return (*ObjStoreManifest)(unsafe.Pointer(m))
}
// DeclareKVStoreManifest declares manifest as KVStoreManifest.
func DeclareKVStoreManifest(m *Manifest) *KVStoreManifest {
return (*KVStoreManifest)(unsafe.Pointer(m))
}
// DeclareSyncSectorStateManifest declares manifest as SyncSectorStateManifest.
func DeclareSyncSectorStateManifest(m *Manifest) *SyncSectorStateManifest {
return (*SyncSectorStateManifest)(unsafe.Pointer(m))
}
// DeclareRegisterJsonRpcManifest declares manifest as DeclareRegisterJsonRpcManifest.
func DeclareRegisterJsonRpcManifest(m *Manifest) *RegisterJsonRpcManifest {
return (*RegisterJsonRpcManifest)(unsafe.Pointer(m))
}