helper/communicator: can be disabled

This commit is contained in:
Mitchell Hashimoto 2015-06-13 17:50:45 -04:00
parent d545431f9b
commit 4b4fe2280d
4 changed files with 94 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import (
// Config is the common configuration that communicators allow within
// a builder.
type Config struct {
Type string `mapstructure:"communicator"`
SSHHost string `mapstructure:"ssh_host"`
SSHPort int `mapstructure:"ssh_port"`
SSHUsername string `mapstructure:"ssh_username"`
@ -20,6 +21,10 @@ type Config struct {
}
func (c *Config) Prepare(ctx *interpolate.Context) []error {
if c.Type == "" {
c.Type = "ssh"
}
if c.SSHPort == 0 {
c.SSHPort = 22
}

View File

@ -0,0 +1,28 @@
package communicator
import (
"testing"
"github.com/mitchellh/packer/template/interpolate"
)
func testConfig() *Config {
return &Config{
SSHUsername: "root",
}
}
func TestConfigType(t *testing.T) {
c := testConfig()
if err := c.Prepare(testContext(t)); len(err) > 0 {
t.Fatalf("bad: %#v", err)
}
if c.Type != "ssh" {
t.Fatal("bad: %#v", c)
}
}
func testContext(t *testing.T) *interpolate.Context {
return nil
}

View File

@ -1,6 +1,9 @@
package communicator
import (
"fmt"
"log"
"github.com/mitchellh/multistep"
gossh "golang.org/x/crypto/ssh"
)
@ -26,14 +29,27 @@ type StepConnect struct {
}
func (s *StepConnect) Run(state multistep.StateBag) multistep.StepAction {
// Eventually we might switch between multiple of these depending
// on the communicator type.
s.substep = &StepConnectSSH{
Config: s.Config,
SSHAddress: s.SSHAddress,
SSHConfig: s.SSHConfig,
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)
}

View File

@ -0,0 +1,39 @@
package communicator
import (
"bytes"
"testing"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
func TestStepConnect_impl(t *testing.T) {
var _ multistep.Step = new(StepConnect)
}
func TestStepConnect_none(t *testing.T) {
state := testState(t)
step := &StepConnect{
Config: &Config{
Type: "none",
},
}
defer step.Cleanup(state)
// run the step
if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
}
func testState(t *testing.T) multistep.StateBag {
state := new(multistep.BasicStateBag)
state.Put("hook", &packer.MockHook{})
state.Put("ui", &packer.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
})
return state
}