Skip to content
Snippets Groups Projects
Commit 1951b42c authored by Malte Bauch's avatar Malte Bauch
Browse files

Add a file storage to plugin registry

See merge request !690
parent 3870b1b8
No related branches found
No related tags found
1 merge request!690Add a file storage to plugin registry
Pipeline #178368 passed
[{"id":"e2c358b3-6482-4010-b0d8-679dff73153b","path":"plugins/arista/bundled_plugin.zip","manifest":{"name":"Arista","firmware":"standard","author":"goSDN-Team","version":"1.0.0"}},{"id":"d1c269a2-6482-4010-b0d8-679dff73153b","path":"plugins/openconfig/bundled_plugin.zip","manifest":{"name":"Openconfig","firmware":"standard","author":"goSDN-Team","version":"1.0.0"}}]
......@@ -55,7 +55,6 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-tty v0.0.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
......@@ -73,7 +72,6 @@ require (
github.com/sethvargo/go-retry v0.2.4
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
......@@ -101,7 +99,6 @@ require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/fatih/color v1.15.0 // indirect
......
This diff is collapsed.
......@@ -9,13 +9,14 @@ import (
pb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/plugin-registry"
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/util"
"github.com/google/uuid"
"google.golang.org/grpc"
)
const (
pluginFilePath = "plugins"
pluginName = "bundled_plugin.zip"
pluginFilePath = "plugins"
pluginStorePath = "plugin-store.json"
)
func main() {
......@@ -41,43 +42,58 @@ func main() {
// after reboot. Therefore it would make sense to use a database. For a simple
// prototype this is currently hardcoded.
func registerPlugins() *PluginRegistry {
files, err := os.ReadDir(pluginFilePath)
dirs, err := os.ReadDir(pluginFilePath)
if err != nil {
panic(err)
}
pr := &PluginRegistry{}
pluginStore := NewStore(pluginStorePath)
for i, file := range files {
fmt.Printf("File %+v\n", file)
plugins, err := pluginStore.GetAll()
if err != nil {
fmt.Println(err)
}
if file.IsDir() {
manifest, err := plugin.ReadManifestFromFile(filepath.Join(pluginFilePath, file.Name()))
for _, dir := range dirs {
fmt.Printf("File %+v\n", dir)
if dir.IsDir() {
dirPath := filepath.Join(pluginFilePath, dir.Name())
manifest, err := plugin.ReadManifestFromFile(dirPath)
if err != nil {
panic(err)
}
var id uuid.UUID
switch i {
case 0:
id = uuid.MustParse("e2c358b3-6482-4010-b0d8-679dff73153b")
case 1:
id = uuid.MustParse("d1c269a2-6482-4010-b0d8-679dff73153b")
case 2:
id = uuid.MustParse("f3b474c2-6482-4010-b0d8-679dff73153b")
default:
break
var plugin *Plugin
for i, p := range plugins {
if *p.Manifest == *manifest {
plugin = p
plugins[i] = plugins[len(plugins)-1]
plugins = plugins[:len(plugins)-1]
break
}
}
plugin := &Plugin{
ID: id,
Path: filepath.Join(pluginFilePath, file.Name(), pluginName),
Manifest: manifest,
if plugin == nil {
plugin = &Plugin{
ID: uuid.New(),
Path: filepath.Join(dirPath, util.BundledPluginName),
Manifest: manifest,
}
}
pr.Plugins = append(pr.Plugins, plugin)
}
}
for _, p := range pr.Plugins {
fmt.Printf("registeredPlugin: %v\n", p)
}
if err := pluginStore.Update(pr.Plugins); err != nil {
fmt.Println(err)
}
return pr
}
......@@ -20,4 +20,5 @@ EXPOSE 55057
WORKDIR /app/
COPY --from=builder /plugin-registry/artifacts/plugin-registry .
COPY --from=builder /plugin-registry/plugin-registry/plugins ./plugins
COPY --from=builder /plugin-registry/dev_env_data/plugin-registry/plugin-store.json ./plugin-store.json
ENTRYPOINT ["./plugin-registry", "-socket", "55057"]
package main
import (
"encoding/json"
"os"
"sync"
)
type Store struct {
fm sync.Mutex
path string
}
func NewStore(path string) *Store {
return &Store{
path: path,
}
}
func (s *Store) Update(plugins []*Plugin) error {
s.fm.Lock()
defer s.fm.Unlock()
serializedPlugins, err := json.Marshal(plugins)
if err != nil {
return err
}
if err := os.WriteFile(s.path, serializedPlugins, 0600); err != nil {
return err
}
return nil
}
func (s *Store) GetAll() ([]*Plugin, error) {
s.fm.Lock()
defer s.fm.Unlock()
var plugins []*Plugin
j, err := os.ReadFile(s.path)
if err != nil {
return nil, err
}
if err := json.Unmarshal(j, &plugins); err != nil {
return nil, err
}
return plugins, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment