File: ruby_client.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (132 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package serverclient

import (
	"context"
	"errors"

	"github.com/hashicorp/go-hclog"
	"github.com/hashicorp/go-plugin"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/types/known/emptypb"

	"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/pluginclient"
	"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
	"github.com/hashicorp/vagrant/internal/server/proto/ruby_vagrant"
)

type RubyVagrant interface {
	GetPlugins() ([]*ruby_vagrant.Plugin, error)
	ParseVagrantfile(string) (*vagrant_plugin_sdk.Vagrantfile_Vagrantfile, error)
}

// This is the implementation of plugin.GRPCPlugin so we can serve/consume this.
type RubyVagrantPlugin struct {
	plugin.NetRPCUnsupportedPlugin

	Impl RubyVagrant
}

type RubyVagrantClient struct {
	broker *plugin.GRPCBroker
	client ruby_vagrant.RubyVagrantClient
	ctx    context.Context
}

func RubyVagrantPluginConfig(log hclog.Logger) *plugin.ClientConfig {
	log = log.ResetNamed("vagrant.legacy")
	config := pluginclient.ClientConfig(log)
	config.Logger = log
	config.VersionedPlugins[1]["vagrantrubyruntime"] = &RubyVagrantPlugin{}
	return config
}

// No Go server implementation. Server is provided by the Vagrant Ruby runtime
func (p *RubyVagrantPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
	return errors.New("vagrant ruby runtime server not implemented")
}

func (p *RubyVagrantPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
	return RubyVagrantClient{
		broker: broker,
		client: ruby_vagrant.NewRubyVagrantClient(c),
		ctx:    ctx,
	}, nil
}

func (r *RubyVagrantClient) GRPCBroker() *plugin.GRPCBroker {
	return r.broker
}

func (r *RubyVagrantClient) GetPlugins() ([]*ruby_vagrant.Plugin, error) {
	plugins, err := r.client.GetPlugins(context.Background(), &ruby_vagrant.GetPluginsRequest{})
	if err != nil {
		return nil, err
	}
	return plugins.Plugins, nil
}

func (r *RubyVagrantClient) ParseVagrantfile(path string) (*vagrant_plugin_sdk.Args_Hash, error) {
	vf, err := r.client.ParseVagrantfile(
		context.Background(),
		&ruby_vagrant.ParseVagrantfileRequest{Path: path},
	)
	if err != nil {
		return nil, err
	}

	return vf.Data, nil
}

func (r *RubyVagrantClient) ParseVagrantfileProc(ref *vagrant_plugin_sdk.Args_ProcRef) (*vagrant_plugin_sdk.Args_Hash, error) {
	vf, err := r.client.ParseVagrantfileProc(
		context.Background(),
		&ruby_vagrant.ParseVagrantfileProcRequest{
			Proc: ref,
		},
	)
	if err != nil {
		return nil, err
	}

	return vf.Data, nil
}

func (r *RubyVagrantClient) ParseVagrantfileSubvm(subvm *vagrant_plugin_sdk.Config_RawRubyValue) (*vagrant_plugin_sdk.Args_Hash, error) {
	resp, err := r.client.ParseVagrantfileSubvm(
		context.Background(),
		&ruby_vagrant.ParseVagrantfileSubvmRequest{
			Subvm: subvm,
		},
	)

	if err != nil {
		return nil, err
	}

	return resp.Data, nil
}

func (r *RubyVagrantClient) ParseVagrantfileProvider(provider string, subvm *vagrant_plugin_sdk.Config_RawRubyValue) (*vagrant_plugin_sdk.Args_Hash, error) {
	resp, err := r.client.ParseVagrantfileProvider(
		context.Background(),
		&ruby_vagrant.ParseVagrantfileProviderRequest{
			Provider: provider,
			Subvm:    subvm,
		},
	)

	if err != nil {
		return nil, err
	}

	return resp.Data, nil
}

func (r *RubyVagrantClient) Stop() error {
	_, err := r.client.Stop(context.Background(), &emptypb.Empty{})
	if err != nil {
		return err
	}

	return nil
}