From c19b3683db730e634e80eafbf44addf9b0ccc7a6 Mon Sep 17 00:00:00 2001 From: sophia Date: Thu, 19 Aug 2021 16:30:05 -0500 Subject: [PATCH] Add alwaystrue guest plugin --- builtin/otherplugin/guest/alwaystrue.go | 70 ++++++++++++++++++++ builtin/otherplugin/guest/cap/write_hello.go | 17 +++++ builtin/otherplugin/main.go | 2 + 3 files changed, 89 insertions(+) create mode 100644 builtin/otherplugin/guest/alwaystrue.go create mode 100644 builtin/otherplugin/guest/cap/write_hello.go diff --git a/builtin/otherplugin/guest/alwaystrue.go b/builtin/otherplugin/guest/alwaystrue.go new file mode 100644 index 000000000..2232b1c86 --- /dev/null +++ b/builtin/otherplugin/guest/alwaystrue.go @@ -0,0 +1,70 @@ +package guest + +import ( + "errors" + + "github.com/hashicorp/vagrant-plugin-sdk/component" + plugincore "github.com/hashicorp/vagrant-plugin-sdk/core" + "github.com/hashicorp/vagrant/builtin/otherplugin/guest/cap" +) + +type GuestConfig struct { +} + +// AlwaysTrueGuest is a Guest implementation for myplugin. +type AlwaysTrueGuest struct { + config GuestConfig +} + +// DetectFunc implements component.Guest +func (h *AlwaysTrueGuest) DetectFunc() interface{} { + return h.Detect +} + +func (h *AlwaysTrueGuest) Detect(t plugincore.Target) bool { + m, err := t.Specialize((*plugincore.Machine)(nil)) + if err != nil { + return false + } + machine := m.(plugincore.Machine) + machine.ConnectionInfo() + // TODO: need a communicator to connect to guest + return true +} + +// ParentsFunc implements component.Guest +func (h *AlwaysTrueGuest) ParentsFunc() interface{} { + return h.Parents +} + +func (h *AlwaysTrueGuest) Parents() []string { + return []string{"force", "guest", "platform", "match"} // We just need to have this be the most of all matches +} + +// HasCapabilityFunc implements component.Guest +func (h *AlwaysTrueGuest) HasCapabilityFunc() interface{} { + return h.CheckCapability +} + +func (h *AlwaysTrueGuest) CheckCapability(n *component.NamedCapability) bool { + if n.Capability == "hello" { + return true + } + return false +} + +// CapabilityFunc implements component.Guest +func (h *AlwaysTrueGuest) CapabilityFunc(name string) interface{} { + if name == "hello" { + return h.WriteHelloCap + } + return errors.New("invalid capability requested") +} + +func (h *AlwaysTrueGuest) WriteHelloCap(m plugincore.Machine) error { + return cap.WriteHello(m) +} + +var ( + _ component.Guest = (*AlwaysTrueGuest)(nil) +) diff --git a/builtin/otherplugin/guest/cap/write_hello.go b/builtin/otherplugin/guest/cap/write_hello.go new file mode 100644 index 000000000..eb3f68fae --- /dev/null +++ b/builtin/otherplugin/guest/cap/write_hello.go @@ -0,0 +1,17 @@ +package cap + +import ( + "io/ioutil" + + plugincore "github.com/hashicorp/vagrant-plugin-sdk/core" +) + +func WriteHello(machine plugincore.Machine) (err error) { + d1 := []byte("hello\ngo\n") + ioutil.WriteFile("/tmp/dat1", d1, 0644) + + machine.ConnectionInfo() + // TODO: write something to guest machine + // need a communicator + return +} diff --git a/builtin/otherplugin/main.go b/builtin/otherplugin/main.go index 399d54f9f..4ff54e447 100644 --- a/builtin/otherplugin/main.go +++ b/builtin/otherplugin/main.go @@ -2,11 +2,13 @@ package otherplugin import ( sdk "github.com/hashicorp/vagrant-plugin-sdk" + "github.com/hashicorp/vagrant/builtin/otherplugin/guest" ) var CommandOptions = []sdk.Option{ sdk.WithComponents( &Command{}, + &guest.AlwaysTrueGuest{}, ), sdk.WithName("otherplugin"), }