Newer
Older
package nucleus
import (
"encoding/json"
"fmt"
"os/exec"
Fabian Seidl
committed
"code.fbi.h-da.de/danet/gosdn/controller/customerrs"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin"
"code.fbi.h-da.de/danet/gosdn/controller/plugin/shared"
"github.com/google/uuid"
hcplugin "github.com/hashicorp/go-plugin"
"go.mongodb.org/mongo-driver/bson"
)
type Plugin struct {
UUID uuid.UUID
state plugin.State
path string
manifest *plugin.Manifest
client *hcplugin.Client
shared.DeviceModel
}
func NewPlugin(id uuid.UUID) (*Plugin, error) {
if id == uuid.Nil {
id = uuid.New()
}
client := hcplugin.NewClient(&hcplugin.ClientConfig{
HandshakeConfig: shared.Handshake,
Plugins: shared.PluginMap,
Cmd: exec.Command("sh", "-c", fmt.Sprintf("plugins/%s/plugin", id.String())),
AllowedProtocols: []hcplugin.Protocol{hcplugin.ProtocolGRPC},
})
manifest, err := plugin.ReadManifestFromFile(fmt.Sprintf("plugins/%s", id.String()))
if err != nil {
return nil, err
}
// connect through grpc
gRPCClient, err := client.Client()
if err != nil {
return nil, err
}
// Request the plugin
raw, err := gRPCClient.Dispense("deviceModel")
if err != nil {
return nil, err
}
model, ok := raw.(shared.DeviceModel)
if !ok {
return nil, customerrs.InvalidTypeAssertionError{
Value: raw,
Type: (*shared.DeviceModel)(nil),
}
}
return &Plugin{
UUID: id,
client: client,
DeviceModel: model,
manifest: manifest,
state: plugin.CREATED,
}, nil
}
func NewPluginThroughReattachConfig(loadedPlugin plugin.LoadedPlugin) (plugin.Plugin, error) {
client := hcplugin.NewClient(&hcplugin.ClientConfig{
HandshakeConfig: shared.Handshake,
Plugins: shared.PluginMap,
Reattach: &loadedPlugin.ReattachConfig,
AllowedProtocols: []hcplugin.Protocol{hcplugin.ProtocolGRPC},
})
// connect through grpc
gRPCClient, err := client.Client()
if err != nil {
return nil, err
}
// Request the plugin
raw, err := gRPCClient.Dispense("deviceModel")
if err != nil {
return nil, err
}
model, ok := raw.(shared.DeviceModel)
if !ok {
return nil, customerrs.InvalidTypeAssertionError{
Value: model,
Type: (*shared.DeviceModel)(nil),
}
}
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
return &Plugin{
UUID: uuid.MustParse(loadedPlugin.ID),
client: client,
DeviceModel: model,
manifest: &loadedPlugin.Manifest,
state: plugin.CREATED,
}, nil
}
// ID returns the ID of the plugin.
func (p *Plugin) ID() uuid.UUID {
return p.UUID
}
// ID returns the ID of the plugin.
func (p *Plugin) ReattachConfig() *hcplugin.ReattachConfig {
return p.client.ReattachConfig()
}
// State returns the current state of the plugin.
// Different states of the plugin can be:
// - built
// - loaded
// - faulty
func (p *Plugin) State() plugin.State {
return p.state
}
func (p *Plugin) Path() string {
return p.path
}
// GetClient returns the client of the plugin.
func (p *Plugin) GetClient() *hcplugin.Client {
return p.client
}
// Manifest returns the manifest of the plugin.
func (p *Plugin) Manifest() *plugin.Manifest {
return p.manifest
}
func (p *Plugin) Update() error {
return fmt.Errorf("not implemented yet")
}
func (p *Plugin) Restart() error {
return fmt.Errorf("not implemented yet")
}
// Close ends the execution of the plugin.
func (p *Plugin) Close() {
p.client.Kill()
}
// Ping checks if the client connection is healthy.
func (p *Plugin) Ping() error {
protocolClient, err := p.client.Client()
if err != nil {
return err
}
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
return protocolClient.Ping()
}
// TODO: update for the new way of handling plugins
// UpdatePlugin updates a given Plugin. Therefore the version of the
// `plugin.yml` manifest file is compared to the version in use. If a new
// version is within the plugin folder, the new version of the plugin is built.
func UpdatePlugin(p plugin.Plugin) (updated bool, err error) {
return false, fmt.Errorf("not implemented yet")
}
func (p *Plugin) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
ID uuid.UUID `json:"id,omitempty"`
Manifest *plugin.Manifest `json:"manifest" bson:"manifest"`
State plugin.State `json:"state,omitempty" bson:"state"`
ReattachConfig *hcplugin.ReattachConfig `json:"reattatch_config,omitempty" bson:"reattatch_config"`
}{
ID: p.ID(),
Manifest: p.Manifest(),
State: p.State(),
ReattachConfig: p.ReattachConfig(),
})
}
func (p *Plugin) MarshalBSON() ([]byte, error) {
return bson.Marshal(&struct {
ID string `bson:"_id,omitempty"`
Manifest *plugin.Manifest `json:"manifest" bson:"manifest"`
State plugin.State `json:"state,omitempty" bson:"state"`
ReattachConfig *hcplugin.ReattachConfig `json:"reattatch_config,omitempty" bson:"reattatch_config"`
}{
ID: p.ID().String(),
Manifest: p.Manifest(),
State: p.State(),
ReattachConfig: p.ReattachConfig(),
})