Pass scopes to components directly, allow them to be mapped automatically

This commit is contained in:
Chris Roberts 2021-05-20 11:25:26 -07:00 committed by Paul Hinze
parent 06f3d7fd21
commit 3cfa9e3b00
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
4 changed files with 15 additions and 85 deletions

View File

@ -17,7 +17,6 @@ import (
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/datadir"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/plugincore"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/protomappers"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
@ -237,7 +236,6 @@ func (b *Basis) LoadProject(popts ...ProjectOption) (p *Project, err error) {
err = multierror.Append(err, oerr)
}
}
if err != nil {
return
}
@ -247,6 +245,8 @@ func (b *Basis) LoadProject(popts ...ProjectOption) (p *Project, err error) {
return project, nil
}
b.projects[p.ResourceId()] = p
// Ensure project directory is set
if p.dir == nil {
if p.dir, err = b.dir.Project(p.project.Name); err != nil {
@ -357,22 +357,6 @@ func (b *Basis) Run(ctx context.Context, task *vagrant_server.Task) (err error)
return
}
// Pass along to the call
basis := plugincore.NewBasisPlugin(b, b.logger)
streamId, err := wrapInstance(basis, cmd.plugin.Broker, b)
bproto := &vagrant_plugin_sdk.Args_Basis{StreamId: streamId}
// NOTE(spox): Should this be closed after the dynamic func
// call is complete, or when we tear this down? The latter
// would allow plugins to keep a persistent connection if
// multiple things are being run
b.Closer(func() error {
if c, ok := basis.(closes); ok {
return c.Close()
}
return nil
})
result, err := b.callDynamicFunc(
ctx,
b.logger,
@ -380,8 +364,8 @@ func (b *Basis) Run(ctx context.Context, task *vagrant_server.Task) (err error)
cmd,
cmd.Value.(component.Command).ExecuteFunc(strings.Split(task.CommandName, " ")),
argmapper.Typed(task.CliArgs),
argmapper.Typed(bproto),
argmapper.Named("basis", bproto),
argmapper.Typed(b),
argmapper.Named("basis", b),
)
if err != nil || result == nil || result.(int64) != 0 {
b.logger.Error("failed to execute command", "type", component.CommandType, "name", task.Component.Name, "error", err)
@ -414,7 +398,7 @@ func (b *Basis) specializeComponent(c *Component) (cmp plugin.PluginMetadata, er
func (b *Basis) convertCommandInfo(c *component.CommandInfo, names []string) []*vagrant_server.Job_Command {
names = append(names, c.Name)
cmds := []*vagrant_server.Job_Command{
&vagrant_server.Job_Command{
{
Name: strings.Join(names, " "),
Synopsis: c.Synopsis,
Help: c.Help,

View File

@ -12,29 +12,3 @@ type closer interface {
type closes interface {
Close() error
}
func wrapInstance(p plugin.GRPCPlugin, b *plugin.GRPCBroker, c closer) (uint32, error) {
id := b.NextId()
errChan := make(chan error, 1)
go b.AcceptAndServe(id, func(opts []grpc.ServerOption) *grpc.Server {
server := plugin.DefaultGRPCServer(opts)
if err := p.GRPCServer(b, server); err != nil {
errChan <- err
return nil
}
c.Closer(func() error {
server.GracefulStop()
return nil
})
close(errChan)
return server
})
err := <-errChan
if err != nil {
return 0, err
}
return id, nil
}

View File

@ -17,7 +17,6 @@ import (
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/core"
"github.com/hashicorp/vagrant-plugin-sdk/datadir"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/plugincore"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
@ -74,17 +73,17 @@ func (p *Project) DataDir() (*datadir.Project, error) {
func (p *Project) VagrantfileName() (name string, err error) {
// TODO: implement
return
return "VagrantFile", nil
}
func (p *Project) Home() (path string, err error) {
// TODO: implement
return
return "/home", nil
}
func (p *Project) LocalData() (path string, err error) {
// TODO: implement
return
return "/local/data", nil
}
func (p *Project) Tmp() (path string, err error) {
@ -94,7 +93,7 @@ func (p *Project) Tmp() (path string, err error) {
func (p *Project) DefaultPrivateKey() (path string, err error) {
// TODO: implement
return
return "/key/path", nil
}
func (p *Project) Host() (host core.Host, err error) {
@ -104,7 +103,7 @@ func (p *Project) Host() (host core.Host, err error) {
func (p *Project) MachineNames() (names []string, err error) {
// TODO: implement
return
return []string{"test"}, nil
}
// End required core.Project interface functions
@ -219,18 +218,6 @@ func (p *Project) Run(ctx context.Context, task *vagrant_server.Task) (err error
return
}
// Pass along to the call
project := plugincore.NewProjectPlugin(p, p.logger)
streamId, err := wrapInstance(project, cmd.plugin.Broker, p)
pproto := &vagrant_plugin_sdk.Args_Project{StreamId: streamId}
p.Closer(func() error {
if c, ok := project.(closes); ok {
return c.Close()
}
return nil
})
result, err := p.callDynamicFunc(
ctx,
p.logger,
@ -238,8 +225,8 @@ func (p *Project) Run(ctx context.Context, task *vagrant_server.Task) (err error
cmd,
cmd.Value.(component.Command).ExecuteFunc(strings.Split(task.CommandName, " ")),
argmapper.Typed(task.CliArgs),
argmapper.Typed(pproto),
argmapper.Named("project", pproto),
argmapper.Typed(p),
argmapper.Named("project", p),
)
if err != nil || result == nil || result.(int64) != 0 {
p.logger.Error("failed to execute command", "type", component.CommandType, "name", task.Component.Name, "result", result, "error", err)
@ -407,7 +394,6 @@ func WithProjectName(name string) ProjectOption {
return errors.New("failed to load project")
}
p.project = result.Project
p.basis.projects[p.project.Name] = p
return
}
@ -463,8 +449,7 @@ func WithProjectRef(r *vagrant_plugin_sdk.Ref_Project) ProjectOption {
return errors.New("project basis configuration is invalid")
}
p.project = project
// Finally set the project into the basis
p.basis.projects[p.Name()] = p
return
}
}

View File

@ -19,7 +19,6 @@ import (
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/core"
"github.com/hashicorp/vagrant-plugin-sdk/datadir"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/plugincore"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
@ -137,18 +136,6 @@ func (t *Target) Run(ctx context.Context, task *vagrant_server.Task) (err error)
return
}
// Pass along to the call
target := plugincore.NewTargetPlugin(t, t.logger)
streamId, err := wrapInstance(target, cmd.plugin.Broker, t)
tproto := &vagrant_plugin_sdk.Args_Project{StreamId: streamId}
t.Closer(func() error {
if c, ok := target.(closes); ok {
return c.Close()
}
return nil
})
result, err := t.callDynamicFunc(
ctx,
t.logger,
@ -156,8 +143,8 @@ func (t *Target) Run(ctx context.Context, task *vagrant_server.Task) (err error)
cmd,
cmd.Value.(component.Command).ExecuteFunc(strings.Split(task.CommandName, " ")),
argmapper.Typed(task.CliArgs),
argmapper.Typed(tproto),
argmapper.Named("target", target),
argmapper.Typed(t),
argmapper.Named("target", t),
)
if err != nil || result == nil || result.(int64) != 0 {