Remove context logic from server implementations

This commit is contained in:
Chris Roberts 2019-04-05 09:49:46 -07:00
parent dcac2a167f
commit 5605a6e843
5 changed files with 244 additions and 379 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
go_plugin "github.com/hashicorp/go-plugin"
@ -50,46 +49,36 @@ type GRPCGuestCapabilitiesServer struct {
func (s *GRPCGuestCapabilitiesServer) GuestCapabilities(ctx context.Context, req *vagrant_proto.Empty) (resp *vagrant_proto.SystemCapabilityList, err error) {
resp = &vagrant_proto.SystemCapabilityList{}
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
r, err := s.Impl.GuestCapabilities()
if err != nil {
return
}
for _, cap := range r {
rcap := &vagrant_proto.SystemCapability{Name: cap.Name, Platform: cap.Platform}
resp.Capabilities = append(resp.Capabilities, rcap)
}
r, err := s.Impl.GuestCapabilities()
if err != nil {
return
})
err = g.Wait()
}
for _, cap := range r {
rcap := &vagrant_proto.SystemCapability{Name: cap.Name, Platform: cap.Platform}
resp.Capabilities = append(resp.Capabilities, rcap)
}
return
}
func (s *GRPCGuestCapabilitiesServer) GuestCapability(ctx context.Context, req *vagrant_proto.GuestCapabilityRequest) (resp *vagrant_proto.GenericResponse, err error) {
resp = &vagrant_proto.GenericResponse{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
var args interface{}
if err = json.Unmarshal([]byte(req.Arguments), &args); err != nil {
return
}
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
cap := &vagrant.SystemCapability{
Name: req.Capability.Name,
Platform: req.Capability.Platform}
r, err := s.Impl.GuestCapability(gctx, cap, args, machine)
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
var args interface{}
if err = json.Unmarshal([]byte(req.Arguments), &args); err != nil {
return
})
err = g.Wait()
}
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
cap := &vagrant.SystemCapability{
Name: req.Capability.Name,
Platform: req.Capability.Platform}
r, err := s.Impl.GuestCapability(ctx, cap, args, machine)
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
return
}
@ -174,47 +163,37 @@ type GRPCHostCapabilitiesServer struct {
func (s *GRPCHostCapabilitiesServer) HostCapabilities(ctx context.Context, req *vagrant_proto.Empty) (resp *vagrant_proto.SystemCapabilityList, err error) {
resp = &vagrant_proto.SystemCapabilityList{}
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
r, err := s.Impl.HostCapabilities()
if err != nil {
return
}
for _, cap := range r {
rcap := &vagrant_proto.SystemCapability{Name: cap.Name, Platform: cap.Platform}
resp.Capabilities = append(resp.Capabilities, rcap)
}
r, err := s.Impl.HostCapabilities()
if err != nil {
return
})
err = g.Wait()
}
for _, cap := range r {
rcap := &vagrant_proto.SystemCapability{Name: cap.Name, Platform: cap.Platform}
resp.Capabilities = append(resp.Capabilities, rcap)
}
return
}
func (s *GRPCHostCapabilitiesServer) HostCapability(ctx context.Context, req *vagrant_proto.HostCapabilityRequest) (resp *vagrant_proto.GenericResponse, err error) {
resp = &vagrant_proto.GenericResponse{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
var args interface{}
if err = json.Unmarshal([]byte(req.Arguments), &args); err != nil {
return
}
env, err := vagrant.LoadEnvironment(req.Environment, s.Impl)
if err != nil {
return
}
cap := &vagrant.SystemCapability{
Name: req.Capability.Name,
Platform: req.Capability.Platform}
r, err := s.Impl.HostCapability(gctx, cap, args, env)
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
var args interface{}
if err = json.Unmarshal([]byte(req.Arguments), &args); err != nil {
return
})
err = g.Wait()
}
env, err := vagrant.LoadEnvironment(req.Environment, s.Impl)
if err != nil {
return
}
cap := &vagrant.SystemCapability{
Name: req.Capability.Name,
Platform: req.Capability.Platform}
r, err := s.Impl.HostCapability(ctx, cap, args, env)
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
return
}
@ -301,50 +280,40 @@ type GRPCProviderCapabilitiesServer struct {
func (s *GRPCProviderCapabilitiesServer) ProviderCapabilities(ctx context.Context, req *vagrant_proto.Empty) (resp *vagrant_proto.ProviderCapabilityList, err error) {
resp = &vagrant_proto.ProviderCapabilityList{}
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
r, err := s.Impl.ProviderCapabilities()
if err != nil {
return
}
for _, cap := range r {
rcap := &vagrant_proto.ProviderCapability{Name: cap.Name, Provider: cap.Provider}
resp.Capabilities = append(resp.Capabilities, rcap)
}
r, err := s.Impl.ProviderCapabilities()
if err != nil {
return
})
err = g.Wait()
}
for _, cap := range r {
rcap := &vagrant_proto.ProviderCapability{Name: cap.Name, Provider: cap.Provider}
resp.Capabilities = append(resp.Capabilities, rcap)
}
return
}
func (s *GRPCProviderCapabilitiesServer) ProviderCapability(ctx context.Context, req *vagrant_proto.ProviderCapabilityRequest) (resp *vagrant_proto.GenericResponse, err error) {
resp = &vagrant_proto.GenericResponse{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
var args interface{}
if err = json.Unmarshal([]byte(req.Arguments), &args); err != nil {
return
}
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
cap := &vagrant.ProviderCapability{
Name: req.Capability.Name,
Provider: req.Capability.Provider}
r, err := s.Impl.ProviderCapability(gctx, cap, args, m)
if err != nil {
return
}
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
var args interface{}
if err = json.Unmarshal([]byte(req.Arguments), &args); err != nil {
return
})
err = g.Wait()
}
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
cap := &vagrant.ProviderCapability{
Name: req.Capability.Name,
Provider: req.Capability.Provider}
r, err := s.Impl.ProviderCapability(ctx, cap, args, m)
if err != nil {
return
}
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
return
}

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
go_plugin "github.com/hashicorp/go-plugin"
@ -50,80 +49,60 @@ type GRPCConfigServer struct {
func (s *GRPCConfigServer) ConfigAttributes(ctx context.Context, req *vagrant_proto.Empty) (resp *vagrant_proto.ListResponse, err error) {
resp = &vagrant_proto.ListResponse{}
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
resp.Items, err = s.Impl.ConfigAttributes()
return
})
err = g.Wait()
resp.Items, err = s.Impl.ConfigAttributes()
return
}
func (s *GRPCConfigServer) ConfigLoad(ctx context.Context, req *vagrant_proto.Configuration) (resp *vagrant_proto.Configuration, err error) {
resp = &vagrant_proto.Configuration{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
var data map[string]interface{}
err = json.Unmarshal([]byte(req.Data), &data)
if err != nil {
return
}
r, err := s.Impl.ConfigLoad(gctx, data)
if err != nil {
return
}
mdata, err := json.Marshal(r)
if err != nil {
return
}
resp.Data = string(mdata)
var data map[string]interface{}
err = json.Unmarshal([]byte(req.Data), &data)
if err != nil {
return
})
err = g.Wait()
}
r, err := s.Impl.ConfigLoad(ctx, data)
if err != nil {
return
}
mdata, err := json.Marshal(r)
if err != nil {
return
}
resp.Data = string(mdata)
return
}
func (s *GRPCConfigServer) ConfigValidate(ctx context.Context, req *vagrant_proto.Configuration) (resp *vagrant_proto.ListResponse, err error) {
resp = &vagrant_proto.ListResponse{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
var data map[string]interface{}
err = json.Unmarshal([]byte(req.Data), &data)
if err != nil {
return
}
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
resp.Items, err = s.Impl.ConfigValidate(gctx, data, m)
var data map[string]interface{}
err = json.Unmarshal([]byte(req.Data), &data)
if err != nil {
return
})
err = g.Wait()
}
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
resp.Items, err = s.Impl.ConfigValidate(ctx, data, m)
return
}
func (s *GRPCConfigServer) ConfigFinalize(ctx context.Context, req *vagrant_proto.Configuration) (resp *vagrant_proto.Configuration, err error) {
resp = &vagrant_proto.Configuration{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
var data map[string]interface{}
err = json.Unmarshal([]byte(req.Data), &data)
if err != nil {
return
}
r, err := s.Impl.ConfigFinalize(gctx, data)
if err != nil {
return
}
mdata, err := json.Marshal(r)
if err != nil {
return
}
resp.Data = string(mdata)
var data map[string]interface{}
err = json.Unmarshal([]byte(req.Data), &data)
if err != nil {
return
})
err = g.Wait()
}
r, err := s.Impl.ConfigFinalize(ctx, data)
if err != nil {
return
}
mdata, err := json.Marshal(r)
if err != nil {
return
}
resp.Data = string(mdata)
return
}

View File

@ -3,7 +3,6 @@ package plugin
import (
"context"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
go_plugin "github.com/hashicorp/go-plugin"
@ -28,28 +27,18 @@ type GRPCIOServer struct {
func (s *GRPCIOServer) Read(ctx context.Context, req *vagrant_proto.Identifier) (r *vagrant_proto.Content, err error) {
r = &vagrant_proto.Content{}
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
r.Value, err = s.Impl.Read(req.Name)
return
})
err = g.Wait()
r.Value, err = s.Impl.Read(req.Name)
return
}
func (s *GRPCIOServer) Write(ctx context.Context, req *vagrant_proto.Content) (r *vagrant_proto.WriteResponse, err error) {
r = &vagrant_proto.WriteResponse{}
g, _ := errgroup.WithContext(ctx)
bytes := 0
g.Go(func() (err error) {
bytes, err = s.Impl.Write(req.Value, req.Target)
if err != nil {
return
}
r.Length = int32(bytes)
bytes, err = s.Impl.Write(req.Value, req.Target)
if err != nil {
return
})
err = g.Wait()
}
r.Length = int32(bytes)
return
}

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
go_plugin "github.com/hashicorp/go-plugin"
@ -230,155 +229,115 @@ type GRPCProviderServer struct {
func (s *GRPCProviderServer) Action(ctx context.Context, req *vagrant_proto.GenericAction) (resp *vagrant_proto.ListResponse, err error) {
resp = &vagrant_proto.ListResponse{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
r, err := s.Impl.Action(gctx, req.Name, m)
if err != nil {
return
}
resp.Items = r
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
r, err := s.Impl.Action(ctx, req.Name, m)
if err != nil {
return
}
resp.Items = r
return
}
func (s *GRPCProviderServer) RunAction(ctx context.Context, req *vagrant_proto.ExecuteAction) (resp *vagrant_proto.GenericResponse, err error) {
resp = &vagrant_proto.GenericResponse{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
var args interface{}
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
if err = json.Unmarshal([]byte(req.Data), &args); err != nil {
return
}
r, err := s.Impl.RunAction(gctx, req.Name, args, m)
if err != nil {
return
}
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
var args interface{}
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
if err = json.Unmarshal([]byte(req.Data), &args); err != nil {
return
}
r, err := s.Impl.RunAction(ctx, req.Name, args, m)
if err != nil {
return
}
result, err := json.Marshal(r)
if err != nil {
return
}
resp.Result = string(result)
return
}
func (s *GRPCProviderServer) Info(ctx context.Context, req *vagrant_proto.Empty) (resp *vagrant_proto.PluginInfo, err error) {
resp = &vagrant_proto.PluginInfo{}
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
r := s.Impl.Info()
resp.Description = r.Description
resp.Priority = r.Priority
return
})
err = g.Wait()
r := s.Impl.Info()
resp.Description = r.Description
resp.Priority = r.Priority
return
}
func (s *GRPCProviderServer) IsInstalled(ctx context.Context, req *vagrant_proto.Machine) (resp *vagrant_proto.Valid, err error) {
resp = &vagrant_proto.Valid{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
resp.Result, err = s.Impl.IsInstalled(gctx, m)
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
resp.Result, err = s.Impl.IsInstalled(ctx, m)
return
}
func (s *GRPCProviderServer) IsUsable(ctx context.Context, req *vagrant_proto.Machine) (resp *vagrant_proto.Valid, err error) {
resp = &vagrant_proto.Valid{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
resp.Result, err = s.Impl.IsUsable(gctx, m)
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
resp.Result, err = s.Impl.IsUsable(ctx, m)
return
}
func (s *GRPCProviderServer) SshInfo(ctx context.Context, req *vagrant_proto.Machine) (resp *vagrant_proto.MachineSshInfo, err error) {
resp = &vagrant_proto.MachineSshInfo{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
r, err := s.Impl.SshInfo(gctx, m)
if err != nil {
return
}
resp.Host = r.Host
resp.Port = r.Port
resp.Username = r.Username
resp.PrivateKeyPath = r.PrivateKeyPath
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
r, err := s.Impl.SshInfo(ctx, m)
if err != nil {
return
}
resp.Host = r.Host
resp.Port = r.Port
resp.Username = r.Username
resp.PrivateKeyPath = r.PrivateKeyPath
return
}
func (s *GRPCProviderServer) State(ctx context.Context, req *vagrant_proto.Machine) (resp *vagrant_proto.MachineState, err error) {
resp = &vagrant_proto.MachineState{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
r, err := s.Impl.State(gctx, m)
if err != nil {
return
}
resp.Id = r.Id
resp.ShortDescription = r.ShortDesc
resp.LongDescription = r.LongDesc
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
r, err := s.Impl.State(ctx, m)
if err != nil {
return
}
resp.Id = r.Id
resp.ShortDescription = r.ShortDesc
resp.LongDescription = r.LongDesc
return
}
func (s *GRPCProviderServer) MachineIdChanged(ctx context.Context, req *vagrant_proto.Machine) (resp *vagrant_proto.Machine, err error) {
resp = &vagrant_proto.Machine{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
if err = s.Impl.MachineIdChanged(gctx, m); err != nil {
return
}
mdata, err := vagrant.DumpMachine(m)
if err != nil {
return
}
resp = &vagrant_proto.Machine{Machine: mdata}
m, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
if err = s.Impl.MachineIdChanged(ctx, m); err != nil {
return
}
mdata, err := vagrant.DumpMachine(m)
if err != nil {
return
}
resp = &vagrant_proto.Machine{Machine: mdata}
return
}

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
go_plugin "github.com/hashicorp/go-plugin"
@ -154,103 +153,78 @@ type GRPCSyncedFolderServer struct {
func (s *GRPCSyncedFolderServer) Cleanup(ctx context.Context, req *vagrant_proto.SyncedFolders) (resp *vagrant_proto.Empty, err error) {
resp = &vagrant_proto.Empty{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Cleanup(gctx, machine, options)
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Cleanup(ctx, machine, options)
return
}
func (s *GRPCSyncedFolderServer) Disable(ctx context.Context, req *vagrant_proto.SyncedFolders) (resp *vagrant_proto.Empty, err error) {
resp = &vagrant_proto.Empty{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
var folders vagrant.FolderList
err = json.Unmarshal([]byte(req.Folders), &folders)
if err != nil {
return
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Disable(gctx, machine, folders, options)
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
var folders vagrant.FolderList
err = json.Unmarshal([]byte(req.Folders), &folders)
if err != nil {
return
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Disable(ctx, machine, folders, options)
return
}
func (s *GRPCSyncedFolderServer) Enable(ctx context.Context, req *vagrant_proto.SyncedFolders) (resp *vagrant_proto.Empty, err error) {
resp = &vagrant_proto.Empty{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
var folders vagrant.FolderList
err = json.Unmarshal([]byte(req.Folders), &folders)
if err != nil {
return
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Enable(gctx, machine, folders, options)
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
var folders vagrant.FolderList
err = json.Unmarshal([]byte(req.Folders), &folders)
if err != nil {
return
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Enable(ctx, machine, folders, options)
return
}
func (s *GRPCSyncedFolderServer) Info(ctx context.Context, req *vagrant_proto.Empty) (resp *vagrant_proto.PluginInfo, err error) {
resp = &vagrant_proto.PluginInfo{}
g, _ := errgroup.WithContext(ctx)
g.Go(func() (err error) {
r := s.Impl.Info()
resp.Description = r.Description
resp.Priority = r.Priority
return
})
err = g.Wait()
r := s.Impl.Info()
resp.Description = r.Description
resp.Priority = r.Priority
return
}
func (s *GRPCSyncedFolderServer) IsUsable(ctx context.Context, req *vagrant_proto.Machine) (resp *vagrant_proto.Valid, err error) {
resp = &vagrant_proto.Valid{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
r, err := s.Impl.IsUsable(gctx, machine)
if err != nil {
return
}
resp.Result = r
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
r, err := s.Impl.IsUsable(ctx, machine)
if err != nil {
return
}
resp.Result = r
return
}
@ -260,26 +234,21 @@ func (s *GRPCSyncedFolderServer) Name(_ context.Context, req *vagrant_proto.Empt
func (s *GRPCSyncedFolderServer) Prepare(ctx context.Context, req *vagrant_proto.SyncedFolders) (resp *vagrant_proto.Empty, err error) {
resp = &vagrant_proto.Empty{}
g, gctx := errgroup.WithContext(ctx)
g.Go(func() (err error) {
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
}
var folders vagrant.FolderList
err = json.Unmarshal([]byte(req.Folders), &folders)
if err != nil {
return
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Prepare(gctx, machine, folders, options)
machine, err := vagrant.LoadMachine(req.Machine, s.Impl)
if err != nil {
return
})
err = g.Wait()
}
var folders vagrant.FolderList
err = json.Unmarshal([]byte(req.Folders), &folders)
if err != nil {
return
}
var options vagrant.FolderOptions
err = json.Unmarshal([]byte(req.Options), &options)
if err != nil {
return
}
err = s.Impl.Prepare(ctx, machine, folders, options)
return
}