Update plugin to use cleanup
This commit is contained in:
parent
5b04ac5c35
commit
6d45d07f3f
@ -125,14 +125,13 @@ func (b *Builtin) Factory(name string) PluginRegistration {
|
||||
}
|
||||
|
||||
p = &Plugin{
|
||||
Builtin: false,
|
||||
Client: rpcClient,
|
||||
Location: fmt.Sprintf("builtin::%s", name),
|
||||
Name: info.Name(),
|
||||
Types: info.ComponentTypes(),
|
||||
components: map[component.Type]*Instance{},
|
||||
logger: log,
|
||||
src: client,
|
||||
Builtin: false,
|
||||
Client: rpcClient,
|
||||
Location: fmt.Sprintf("builtin::%s", name),
|
||||
Name: info.Name(),
|
||||
Types: info.ComponentTypes(),
|
||||
logger: log,
|
||||
src: client,
|
||||
}
|
||||
|
||||
// Close the rpcClient when plugin is closed
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"github.com/hashicorp/go-plugin"
|
||||
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/component"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cleanup"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/pluginclient"
|
||||
)
|
||||
|
||||
@ -98,15 +99,15 @@ func Factory(
|
||||
)
|
||||
|
||||
p = &Plugin{
|
||||
Builtin: false,
|
||||
Client: rpcClient,
|
||||
Location: cmd.Path,
|
||||
Name: info.Name(),
|
||||
Types: info.ComponentTypes(),
|
||||
Mappers: mappers,
|
||||
components: map[component.Type]*Instance{},
|
||||
logger: nlog.Named(info.Name()),
|
||||
src: client,
|
||||
Builtin: false,
|
||||
Client: rpcClient,
|
||||
Location: cmd.Path,
|
||||
Name: info.Name(),
|
||||
Types: info.ComponentTypes(),
|
||||
Mappers: mappers,
|
||||
cleaner: cleanup.New(),
|
||||
logger: nlog.Named(info.Name()),
|
||||
src: client,
|
||||
}
|
||||
|
||||
// Close the rpcClient when plugin is closed
|
||||
@ -138,13 +139,15 @@ func RubyFactory(
|
||||
) PluginRegistration {
|
||||
return func(log hclog.Logger) (*Plugin, error) {
|
||||
return &Plugin{
|
||||
Builtin: false,
|
||||
Client: rubyClient,
|
||||
Location: "ruby-runtime",
|
||||
Name: name,
|
||||
Types: []component.Type{typ},
|
||||
components: map[component.Type]*Instance{},
|
||||
logger: log.ResetNamed("vagrant.legacy." + strings.ToLower(typ.String())),
|
||||
Builtin: false,
|
||||
Client: rubyClient,
|
||||
Location: "ruby-runtime",
|
||||
Name: name,
|
||||
Types: []component.Type{typ},
|
||||
cleaner: cleanup.New(),
|
||||
logger: log.ResetNamed(
|
||||
fmt.Sprintf("vagrant.legacy-plugin.%s.%s", strings.ToLower(typ.String()), name),
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
@ -172,7 +175,7 @@ type Instance struct {
|
||||
|
||||
// Closer is a function that should be called to clean up resources
|
||||
// associated with this plugin.
|
||||
Close func()
|
||||
Close func() error
|
||||
}
|
||||
|
||||
func (i *Instance) Parents() []string {
|
||||
|
||||
@ -2,18 +2,19 @@ package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/go-argmapper"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/go-plugin"
|
||||
|
||||
sdk "github.com/hashicorp/vagrant-plugin-sdk"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/component"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/core"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cacher"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cleanup"
|
||||
"github.com/hashicorp/vagrant/builtin/myplugin"
|
||||
"github.com/hashicorp/vagrant/builtin/otherplugin"
|
||||
)
|
||||
@ -44,12 +45,11 @@ type Plugin struct {
|
||||
Name string // Name of the plugin
|
||||
Types []component.Type // Component types supported by this plugin
|
||||
|
||||
closers []func() error // Functions to be called when manager is closed
|
||||
components map[component.Type]*Instance // Map of created instances
|
||||
logger hclog.Logger
|
||||
m sync.Mutex
|
||||
manager *Manager // Plugin manager this plugin belongs to
|
||||
src *plugin.Client // Client for the plugin
|
||||
cleaner cleanup.Cleanup // Cleanup tasks to perform on closing
|
||||
logger hclog.Logger
|
||||
m sync.Mutex
|
||||
manager *Manager // Plugin manager this plugin belongs to
|
||||
src *plugin.Client // Client for the plugin
|
||||
}
|
||||
|
||||
// Interface for plugins with mapper support
|
||||
@ -88,7 +88,7 @@ func (p *Plugin) HasType(
|
||||
|
||||
// Add a callback to execute when plugin is closed
|
||||
func (p *Plugin) Closer(c func() error) {
|
||||
p.closers = append(p.closers, c)
|
||||
p.cleaner.Do(c)
|
||||
}
|
||||
|
||||
// Calls all registered close callbacks
|
||||
@ -96,17 +96,13 @@ func (p *Plugin) Close() (err error) {
|
||||
p.m.Lock()
|
||||
defer p.m.Unlock()
|
||||
|
||||
for _, c := range p.closers {
|
||||
if e := c(); e != nil {
|
||||
multierror.Append(err, e)
|
||||
}
|
||||
}
|
||||
return
|
||||
return p.cleaner.Close()
|
||||
}
|
||||
|
||||
// Get specific component type from plugin
|
||||
func (p *Plugin) InstanceOf(
|
||||
c component.Type,
|
||||
cfns []PluginConfigurator,
|
||||
) (i *Instance, err error) {
|
||||
p.m.Lock()
|
||||
defer p.m.Unlock()
|
||||
@ -134,21 +130,6 @@ func (p *Plugin) InstanceOf(
|
||||
return
|
||||
}
|
||||
|
||||
// Register cleanup for the instance
|
||||
p.Closer(func() error {
|
||||
p.logger.Warn("closing plugin instance",
|
||||
"plugin", p.Name,
|
||||
"instance", hclog.Fmt("%T", raw),
|
||||
)
|
||||
if c, ok := raw.(interface {
|
||||
Close() error
|
||||
}); ok {
|
||||
return c.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
// Extract the GRPC broker if possible
|
||||
b, ok := raw.(HasGRPCBroker)
|
||||
if !ok {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user