Add alwaystrue guest plugin

This commit is contained in:
sophia 2021-08-19 16:30:05 -05:00 committed by Paul Hinze
parent 448be2fc84
commit c19b3683db
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 89 additions and 0 deletions

View File

@ -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)
)

View File

@ -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
}

View File

@ -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"),
}