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

Add remove method to plugin

parent ad8ad4a4
No related branches found
No related tags found
1 merge request!544Resolve "Plugin is created and persisted in the database even if the creation of a network element failed"
...@@ -392,7 +392,7 @@ func shutdown() error { ...@@ -392,7 +392,7 @@ func shutdown() error {
} }
for _, plugin := range plugins { for _, plugin := range plugins {
log.Info("Defer: ", plugin.Manifest().Name) log.Info("Defer: ", plugin.Manifest().Name)
plugin.GetClient().Kill() plugin.Close()
log.Info("Defer - exited: ", plugin.GetClient().Exited()) log.Info("Defer - exited: ", plugin.GetClient().Exited())
} }
coreLock.Unlock() coreLock.Unlock()
......
...@@ -46,6 +46,7 @@ type Plugin interface { ...@@ -46,6 +46,7 @@ type Plugin interface {
Ping() error Ping() error
Restart() error Restart() error
Close() Close()
Remove() error
shared.DeviceModel shared.DeviceModel
} }
......
...@@ -723,32 +723,43 @@ func (n *NetworkElementServer) addMne(ctx context.Context, name string, opt *tpb ...@@ -723,32 +723,43 @@ func (n *NetworkElementServer) addMne(ctx context.Context, name string, opt *tpb
mne, err := nucleus.NewNetworkElement(name, networkElementID, opt, pndID, plugin, conflict.Metadata{ResourceVersion: 0}) mne, err := nucleus.NewNetworkElement(name, networkElementID, opt, pndID, plugin, conflict.Metadata{ResourceVersion: 0})
if err != nil { if err != nil {
plugin.Remove()
return uuid.Nil, err return uuid.Nil, err
} }
if mne.IsTransportValid() { if mne.IsTransportValid() {
resp, err := n.getPath(ctx, mne, "/") resp, err := n.getPath(ctx, mne, "/")
if err != nil { if err != nil {
plugin.Remove()
return uuid.Nil, err return uuid.Nil, err
} }
err = mne.ProcessResponse(resp) err = mne.ProcessResponse(resp)
if err != nil { if err != nil {
plugin.Remove()
return uuid.Nil, err return uuid.Nil, err
} }
err = n.mneService.Add(mne) err = n.mneService.Add(mne)
if err != nil { if err != nil {
plugin.Remove()
return uuid.Nil, err return uuid.Nil, err
} }
err = n.pluginService.Add(plugin) err = n.pluginService.Add(plugin)
if err != nil { if err != nil {
if err := plugin.Remove(); err != nil {
return uuid.Nil, status.Errorf(codes.Internal, "failed to clean up plugin: ", err)
}
if err := n.mneService.Delete(mne); err != nil {
return uuid.Nil, status.Errorf(codes.Internal, "failed to clean up device: ", err)
}
return uuid.Nil, err return uuid.Nil, err
} }
n.networkElementWatchter.SubscribeToNetworkElement(mne, config.GetGnmiSubscriptionPaths(), nil) n.networkElementWatchter.SubscribeToNetworkElement(mne, config.GetGnmiSubscriptionPaths(), nil)
} else { } else {
plugin.Remove()
return uuid.Nil, status.Errorf(codes.InvalidArgument, "invalid transport data provided") return uuid.Nil, status.Errorf(codes.InvalidArgument, "invalid transport data provided")
} }
......
...@@ -3,6 +3,7 @@ package nucleus ...@@ -3,6 +3,7 @@ package nucleus
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
...@@ -129,6 +130,15 @@ func (p *Plugin) ReattachConfig() *hcplugin.ReattachConfig { ...@@ -129,6 +130,15 @@ func (p *Plugin) ReattachConfig() *hcplugin.ReattachConfig {
return p.client.ReattachConfig() return p.client.ReattachConfig()
} }
// Remove ensures that the Plugin is killed and the corresponding files are
// removed.
func (p *Plugin) Remove() error {
// stop the running plugins process
p.Close()
// remove the plugins folder
return os.RemoveAll(p.ExecPath())
}
// State returns the current state of the plugin. // State returns the current state of the plugin.
// Different states of the plugin can be: // Different states of the plugin can be:
// - created // - created
...@@ -165,6 +175,7 @@ func (p *Plugin) Restart() error { ...@@ -165,6 +175,7 @@ func (p *Plugin) Restart() error {
// Close ends the execution of the plugin. // Close ends the execution of the plugin.
func (p *Plugin) Close() { func (p *Plugin) Close() {
// end the plugin process
p.client.Kill() p.client.Kill()
} }
......
...@@ -106,7 +106,7 @@ func (s *PluginService) Delete(pluginToDelete plugin.Plugin) error { ...@@ -106,7 +106,7 @@ func (s *PluginService) Delete(pluginToDelete plugin.Plugin) error {
} }
// stop the plugin // stop the plugin
pluginToDelete.GetClient().Kill() pluginToDelete.Close()
if err := s.eventService.PublishEvent(PluginEventTopic, event.NewDeleteEvent(pluginToDelete.ID())); err != nil { if err := s.eventService.PublishEvent(PluginEventTopic, event.NewDeleteEvent(pluginToDelete.ID())); err != nil {
log.Error(err) log.Error(err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment