Introduce a base client

Provide a base client type that can then be used for loading
scoped items (like basis, project, etc). Allow per-run setup
of server and runner be handled by the client. Add project
loading and project detection support to basis implementation
and load vagrantfiles during client setup.
This commit is contained in:
Chris Roberts 2021-08-05 11:20:27 -07:00 committed by Paul Hinze
parent d16eee410f
commit 1c87679bc0
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 388 additions and 285 deletions

View File

@ -8,228 +8,175 @@ import (
"github.com/hashicorp/go-hclog" "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin" "github.com/hashicorp/go-plugin"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/hashicorp/vagrant-plugin-sdk/datadir" "github.com/hashicorp/vagrant-plugin-sdk/helper/path"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk" "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant-plugin-sdk/terminal" "github.com/hashicorp/vagrant-plugin-sdk/terminal"
configpkg "github.com/hashicorp/vagrant/internal/config" configpkg "github.com/hashicorp/vagrant/internal/config"
"github.com/hashicorp/vagrant/internal/runner"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/hashicorp/vagrant/internal/serverclient" "github.com/hashicorp/vagrant/internal/serverclient"
) )
type Basis struct { type Basis struct {
ui terminal.UI basis *vagrant_server.Basis
cleanupFuncs []func() error
basis *vagrant_server.Basis client *Client
Project *Project ctx context.Context
logger hclog.Logger
client *serverclient.VagrantClient path path.Path
vagrantRubyRuntime plugin.ClientProtocol ui terminal.UI
logger hclog.Logger vagrant *serverclient.VagrantClient
runner *vagrant_server.Ref_Runner
cleanupFuncs []func()
config *configpkg.Config
labels map[string]string
dataSourceOverrides map[string]string
datadir *datadir.Basis
local bool
localServer bool // True when a local server is created
localRunner *runner.Runner
} }
func New(ctx context.Context, opts ...Option) (basis *Basis, err error) { func (b *Basis) DetectProject() (p *Project, err error) {
basis = &Basis{ // look for a vagrantfile!
logger: hclog.L(), v, err := configpkg.FindPath(nil, "")
runner: &vagrant_server.Ref_Runner{ // if an error was encountered, or no path was found, we return
Target: &vagrant_server.Ref_Runner_Any{ if err != nil || v == nil {
Any: &vagrant_server.Ref_RunnerAny{}, return
}
// we did find a path, so use the directory name as project name
// TODO(spox): we'll need to do better than just dir name
_, n := v.Dir().Base().Split()
p, err = b.LoadProject(n)
if err != nil && status.Code(err) != codes.NotFound {
return
}
if err == nil {
p.vagrantfile = v
return
}
result, err := b.vagrant.UpsertProject(
b.ctx,
&vagrant_server.UpsertProjectRequest{
Project: &vagrant_server.Project{
Name: n,
Basis: b.Ref(),
Path: v.Dir().String(),
}, },
}, },
}
// Apply any options
var cfg config
for _, opt := range opts {
err := opt(basis, &cfg)
if err != nil {
return nil, err
}
}
basis.logger = basis.logger.Named("basis")
// If no internal basis was provided, set it up now
if basis.basis == nil {
dir, err := datadir.NewBasis("default")
if err != nil {
basis.logger.Error("failed to determine vagrant home",
"error", err)
return nil, err
}
basis.basis = &vagrant_server.Basis{
Name: "default",
Path: dir.ConfigDir().String(),
}
}
// If no UI was provided, create a default
if basis.ui == nil {
basis.ui = terminal.ConsoleUI(ctx)
}
// If a client was not provided, establish a new connection through
// the serverclient package, or by spinning up an in-process server
if basis.client == nil {
basis.logger.Trace("no API client provided, initializing connection if possible")
conn, err := basis.initServerClient(context.Background(), &cfg)
if err != nil {
return nil, err
}
basis.client = serverclient.WrapVagrantClient(conn)
}
// If the ruby runtime isn't provided, set it up
if basis.vagrantRubyRuntime == nil {
if basis.vagrantRubyRuntime, err = basis.initVagrantRubyRuntime(); err != nil {
return nil, err
}
}
// Negotiate the version
if err := basis.negotiateApiVersion(ctx); err != nil {
return nil, err
}
// Setup our basis within the database
result, err := basis.client.FindBasis(
context.Background(),
&vagrant_server.FindBasisRequest{
Basis: basis.basis,
},
) )
if err == nil && result.Found {
basis.basis = result.Basis
if err = basis.LoadVagrantfiles(); err != nil {
return nil, err
}
return basis, nil
}
basis.logger.Trace("failed to locate existing basis", "basis", basis.basis,
"result", result, "error", err)
uresult, err := basis.client.UpsertBasis(
context.Background(),
&vagrant_server.UpsertBasisRequest{
Basis: basis.basis,
},
)
if err != nil { if err != nil {
return nil, err return
} }
basis.basis = uresult.Basis return &Project{
basis: b,
// Find and load Vagrantfiles for the basis client: b.client,
if err = basis.LoadVagrantfiles(); err != nil { ctx: b.ctx,
return nil, err logger: b.logger.Named("project"),
} project: result.Project,
ui: b.ui,
basis.datadir, err = datadir.NewBasis(basis.basis.Name) vagrant: b.vagrant,
vagrantfile: v,
return basis, err }, nil
} }
func (b *Basis) LoadProject(p *vagrant_server.Project) (*Project, error) { func (b *Basis) LoadProject(n string) (*Project, error) {
result, err := b.client.FindProject( result, err := b.vagrant.FindProject(
context.Background(), b.ctx,
&vagrant_server.FindProjectRequest{ &vagrant_server.FindProjectRequest{
Project: p, Project: &vagrant_server.Project{
}, Name: n,
) Basis: b.Ref(),
// d, err := b.datadir.Project(result.Project.Name) },
// if err != nil {
// return nil, err
// }
if err == nil && result.Found {
b.Project = &Project{
ui: b.ui,
basis: b,
project: result.Project,
logger: b.logger.Named("project"),
// datadir: d,
}
if err = b.Project.LoadVagrantfiles(); err != nil {
return nil, err
}
return b.Project, nil
}
b.logger.Trace("failed to locate existing project", "project", p,
"result", result, "error", err)
uresult, err := b.client.UpsertProject(
context.Background(),
&vagrant_server.UpsertProjectRequest{
Project: p,
}, },
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
b.Project = &Project{ if !result.Found {
ui: b.ui, return nil, NotFoundErr
project: uresult.Project,
basis: b,
logger: b.logger.Named("project"),
//datadir: d,
} }
if err = b.Project.LoadVagrantfiles(); err != nil { return &Project{
return nil, err basis: b,
} client: b.client,
return b.Project, nil ctx: b.ctx,
logger: b.logger.Named("project"),
project: result.Project,
ui: b.ui,
vagrant: b.vagrant,
}, nil
} }
// Finds the Vagrantfile associated with the basis // Finds the Vagrantfile associated with the basis
func (b *Basis) LoadVagrantfiles() error { func (b *Basis) LoadVagrantfile() error {
vagrantfilePath, err := configpkg.FindPath(b.basis.Path, configpkg.GetVagrantfileName()) vpath := b.path.Join(configpkg.GetVagrantfileName())
if err != nil { l := b.logger.With(
"basis", b.basis.Name,
"path", vpath,
)
l.Trace("attempting to load basis vagrantfile")
// If the path does not exist, no Vagrantfile was found
if _, err := os.Stat(vpath.String()); os.IsNotExist(err) {
l.Warn("basis vagrantfile does not exist")
return nil
} else if err != nil {
l.Error("failed to load basis vagrantfile",
"error", err,
)
return err return err
} }
// If the path does not exist, no Vagrantfile was found
if _, err := os.Stat(vagrantfilePath); os.IsNotExist(err) {
return nil
}
raw, err := b.vagrantRubyRuntime.Dispense("vagrantrubyruntime") raw, err := b.client.rubyRuntime.Dispense("vagrantrubyruntime")
if err != nil { if err != nil {
l.Warn("failed to load ruby runtime for vagrantfile parsing",
"error", err,
)
return err return err
} }
rvc, ok := raw.(serverclient.RubyVagrantClient) rvc, ok := raw.(serverclient.RubyVagrantClient)
if !ok { if !ok {
l.Warn("failed to attach to ruby runtime for vagrantfile parsing",
"error", err,
)
return fmt.Errorf("Couldn't attach to Ruby runtime") return fmt.Errorf("Couldn't attach to Ruby runtime")
} }
vagrantfile, err := rvc.ParseVagrantfile(vagrantfilePath)
vagrantfile, err := rvc.ParseVagrantfile(vpath.String())
if err != nil { if err != nil {
l.Error("failed to parse basis vagrantfile",
"error", err,
)
return err return err
} }
l.Trace("storing updated basis configuration",
"configuration", vagrantfile,
)
b.basis.Configuration = vagrantfile b.basis.Configuration = vagrantfile
// Push Vagrantfile updates to basis // Push Vagrantfile updates to basis
b.client.UpsertBasis( result, err := b.vagrant.UpsertBasis(
context.Background(), b.ctx,
&vagrant_server.UpsertBasisRequest{ &vagrant_server.UpsertBasisRequest{
Basis: b.basis, Basis: b.basis,
}, },
) )
if err != nil {
l.Error("failed to store basis updates",
"error", err,
)
return err
}
b.basis = result.Basis
return nil return nil
} }
@ -253,123 +200,13 @@ func (b *Basis) Close() error {
// Client returns the raw Vagrant server API client. // Client returns the raw Vagrant server API client.
func (b *Basis) Client() *serverclient.VagrantClient { func (b *Basis) Client() *serverclient.VagrantClient {
return b.client return b.vagrant
} }
func (b *Basis) VagrantRubyRuntime() plugin.ClientProtocol { func (b *Basis) VagrantRubyRuntime() plugin.ClientProtocol {
return b.vagrantRubyRuntime return b.client.rubyRuntime
}
// Local is true if the server is an in-process just-in-time server.
func (b *Basis) Local() bool {
return b.localServer
} }
func (b *Basis) UI() terminal.UI { func (b *Basis) UI() terminal.UI {
return b.ui return b.ui
} }
func (b *Basis) cleanup(f func()) {
b.cleanupFuncs = append(b.cleanupFuncs, f)
}
type config struct {
connectOpts []serverclient.ConnectOption
}
type Option func(*Basis, *config) error
func WithBasis(pbb *vagrant_server.Basis) Option {
return func(b *Basis, cfg *config) error {
b.basis = pbb
return nil
}
}
func WithProject(p *Project) Option {
return func(b *Basis, cfg *config) error {
p.basis = b
b.Project = p
return nil
}
}
// WithClient sets the client directly. In this case, the runner won't
// attempt any connection at all regardless of other configuration (env
// vars or vagrant config file). This will be used.
func WithClient(client *serverclient.VagrantClient) Option {
return func(b *Basis, cfg *config) error {
if client != nil {
b.client = client
}
return nil
}
}
// WithClientConnect specifies the options for connecting to a client.
// If WithClient is specified, that client is always used.
//
// If WithLocal is set and no client is specified and no server creds
// can be found, then an in-process server will be created.
func WithClientConnect(opts ...serverclient.ConnectOption) Option {
return func(b *Basis, cfg *config) error {
cfg.connectOpts = opts
return nil
}
}
// WithLocal puts the client in local exec mode. In this mode, the client
// will spin up a per-operation runner locally and reference the local on-disk
// data for all operations.
func WithLocal() Option {
return func(b *Basis, cfg *config) error {
b.local = true
return nil
}
}
// WithLogger sets the logger for the client.
func WithLogger(log hclog.Logger) Option {
return func(b *Basis, cfg *config) error {
b.logger = log
return nil
}
}
// WithUI sets the UI to use for the client.
func WithUI(ui terminal.UI) Option {
return func(b *Basis, cfg *config) error {
b.ui = ui
return nil
}
}
func WithCleanup(f func()) Option {
return func(b *Basis, cfg *config) error {
b.cleanup(f)
return nil
}
}
// WithSourceOverrides sets the data source overrides for queued jobs.
func WithSourceOverrides(m map[string]string) Option {
return func(b *Basis, cfg *config) error {
b.dataSourceOverrides = m
return nil
}
}
// WithLabels sets the labels or any operations.
func WithLabels(m map[string]string) Option {
return func(b *Basis, cfg *config) error {
b.labels = m
return nil
}
}
func WithConfig(c *configpkg.Config) Option {
return func(b *Basis, cfg *config) error {
b.config = c
return nil
}
}

266
internal/client/client.go Normal file
View File

@ -0,0 +1,266 @@
package client
import (
"context"
"errors"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/vagrant-plugin-sdk/helper/paths"
"github.com/hashicorp/vagrant-plugin-sdk/terminal"
"github.com/hashicorp/vagrant/internal/config"
"github.com/hashicorp/vagrant/internal/runner"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/hashicorp/vagrant/internal/serverclient"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
NotFoundErr = errors.New("failed to locate requested resource")
)
type Client struct {
config *config.Config
cleanupFns []func() error
client *serverclient.VagrantClient
ctx context.Context
localRunner bool
localServer bool
logger hclog.Logger
rubyRuntime plugin.ClientProtocol
runner *runner.Runner
runnerRef *vagrant_server.Ref_Runner
ui terminal.UI
}
func New(ctx context.Context, opts ...Option) (c *Client, err error) {
c = &Client{
ctx: ctx,
logger: hclog.L().Named("vagrant.client"),
runnerRef: &vagrant_server.Ref_Runner{
Target: &vagrant_server.Ref_Runner_Any{
Any: &vagrant_server.Ref_RunnerAny{},
},
},
}
// If an error was encountered, ensure that
// we return back a nil value for the client
defer func() {
if err != nil {
c = nil
}
}()
// Apply any provided options
var cfg clientConfig
for _, opt := range opts {
if e := opt(c, &cfg); e != nil {
err = multierror.Append(err, e)
}
}
if err != nil {
return
}
// If no UI is configured, create a default
if c.ui == nil {
c.ui = terminal.ConsoleUI(ctx)
}
// If no client is configured, establish a new connection
// or spin up an in-process server
if c.client == nil {
conn, err := c.initServerClient(context.Background(), &cfg)
if err != nil {
c.logger.Error("failed to establish server connection",
"error", err)
return nil, err
}
c.client = serverclient.WrapVagrantClient(conn)
c.logger.Info("established connection to vagrant server")
} else {
c.logger.Warn("using provided client for vagrant server connection")
}
// Negotiate the version
if err = c.negotiateApiVersion(ctx); err != nil {
return
}
// If no Ruby runtime is configured, start one
if c.rubyRuntime == nil {
if c.rubyRuntime, err = c.initVagrantRubyRuntime(); err != nil {
c.logger.Error("failed to start vagrant ruby runtime",
"error", err)
return
}
}
// If we are using a local runner, spin it up
if c.localRunner {
c.runner, err = c.startRunner()
if err != nil {
return
}
c.logger.Info("started local runner",
"runner-id", c.runner.Id())
// Set our local runner as the target
c.runnerRef.Target = &vagrant_server.Ref_Runner_Id{
Id: &vagrant_server.Ref_RunnerId{
Id: c.runner.Id(),
},
}
c.Cleanup(func() error {
c.logger.Info("stopping local runner",
"runner-id", c.runner.Id())
return c.runner.Close()
})
}
return
}
func (c *Client) LoadBasis(n string) (*Basis, error) {
var basis *vagrant_server.Basis
result, err := c.client.FindBasis(
c.ctx,
&vagrant_server.FindBasisRequest{
Basis: &vagrant_server.Basis{
Name: n,
},
},
)
if err != nil {
if status.Code(err) != codes.NotFound {
return nil, err
}
uresult, err := c.client.UpsertBasis(
c.ctx,
&vagrant_server.UpsertBasisRequest{
Basis: &vagrant_server.Basis{
Name: n,
},
},
)
if err != nil {
return nil, err
}
basis = uresult.Basis
} else {
basis = result.Basis
}
p, err := paths.NamedVagrantConfig(n)
if err != nil {
return nil, err
}
return &Basis{
basis: basis,
client: c,
ctx: c.ctx,
logger: c.logger.Named("basis"),
path: p,
ui: c.ui,
vagrant: c.client,
}, nil
}
// Close the client and call any cleanup functions
// that have been defined
func (c *Client) Close() (err error) {
for _, f := range c.cleanupFns {
if e := f(); e != nil {
err = multierror.Append(err, e)
}
}
return
}
func (c *Client) Cleanup(f ...func() error) {
c.cleanupFns = append(c.cleanupFns, f...)
}
func (c *Client) UI() terminal.UI {
return c.ui
}
type clientConfig struct {
connectOpts []serverclient.ConnectOption
}
type Option func(*Client, *clientConfig) error
// WithClient sets the client directly. In this case, the runner won't
// attempt any connection at all regardless of other configuration (env
// vars or vagrant config file). This will be used.
func WithClient(client *serverclient.VagrantClient) Option {
return func(c *Client, cfg *clientConfig) error {
if client != nil {
c.client = client
}
return nil
}
}
// WithClientConnect specifies the options for connecting to a client.
// If WithClient is specified, that client is always used.
//
// If WithLocal is set and no client is specified and no server creds
// can be found, then an in-process server will be created.
func WithClientConnect(opts ...serverclient.ConnectOption) Option {
return func(_ *Client, cfg *clientConfig) error {
cfg.connectOpts = opts
return nil
}
}
// WithLocal puts the client in local exec mode. In this mode, the client
// will spin up a per-operation runner locally and reference the local on-disk
// data for all operations.
func WithLocal() Option {
return func(c *Client, cfg *clientConfig) error {
c.localRunner = true
return nil
}
}
// WithLogger sets the logger for the client.
func WithLogger(log hclog.Logger) Option {
return func(c *Client, cfg *clientConfig) error {
c.logger = log
return nil
}
}
// WithUI sets the UI to use for the client.
func WithUI(ui terminal.UI) Option {
return func(c *Client, cfg *clientConfig) error {
c.ui = ui
return nil
}
}
// Register cleanup callback
func WithCleanup(f func() error) Option {
return func(c *Client, cfg *clientConfig) error {
c.Cleanup(f)
return nil
}
}
func WithConfig(cfg *config.Config) Option {
return func(c *Client, _ *clientConfig) error {
c.config = cfg
return nil
}
}

View File

@ -18,7 +18,7 @@ import (
// so that any side effect file creation doesn't impact the real working // so that any side effect file creation doesn't impact the real working
// directory. If you need to use your working directory, query it before // directory. If you need to use your working directory, query it before
// calling this. // calling this.
func TestBasis(t testing.T, opts ...Option) *Basis { func TestBasis(t testing.T, opts ...Option) *Client {
require := require.New(t) require := require.New(t)
client := singleprocess.TestServer(t) client := singleprocess.TestServer(t)