61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package communicator
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
gossh "golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// StepConnect is a multistep Step implementation that connects to
|
|
// the proper communicator and stores it in the "communicator" key in the
|
|
// state bag.
|
|
type StepConnect struct {
|
|
// Config is the communicator config struct
|
|
Config *Config
|
|
|
|
// The fields below are callbacks to assist with connecting to SSH.
|
|
//
|
|
// SSHAddress should return the default host to connect to for SSH.
|
|
// This is only called if ssh_host isn't specified in the config.
|
|
//
|
|
// SSHConfig should return the default configuration for
|
|
// connecting via SSH.
|
|
SSHAddress func(multistep.StateBag) (string, error)
|
|
SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error)
|
|
|
|
substep multistep.Step
|
|
}
|
|
|
|
func (s *StepConnect) Run(state multistep.StateBag) multistep.StepAction {
|
|
typeMap := map[string]multistep.Step{
|
|
"none": nil,
|
|
"ssh": &StepConnectSSH{
|
|
Config: s.Config,
|
|
SSHAddress: s.SSHAddress,
|
|
SSHConfig: s.SSHConfig,
|
|
},
|
|
}
|
|
|
|
step, ok := typeMap[s.Config.Type]
|
|
if !ok {
|
|
state.Put("error", fmt.Errorf("unknown communicator type: %s", s.Config.Type))
|
|
return multistep.ActionHalt
|
|
}
|
|
|
|
if step == nil {
|
|
log.Printf("[INFO] communicator disabled, will not connect")
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
s.substep = step
|
|
return s.substep.Run(state)
|
|
}
|
|
|
|
func (s *StepConnect) Cleanup(state multistep.StateBag) {
|
|
if s.substep != nil {
|
|
s.substep.Cleanup(state)
|
|
}
|
|
}
|