github.com/aspring/packer@v0.8.1-0.20150629211158-9db281ac0f89/helper/communicator/step_connect.go (about) 1 package communicator 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/mitchellh/multistep" 8 gossh "golang.org/x/crypto/ssh" 9 ) 10 11 // StepConnect is a multistep Step implementation that connects to 12 // the proper communicator and stores it in the "communicator" key in the 13 // state bag. 14 type StepConnect struct { 15 // Config is the communicator config struct 16 Config *Config 17 18 // Host should return a host that can be connected to for communicator 19 // connections. 20 Host func(multistep.StateBag) (string, error) 21 22 // The fields below are callbacks to assist with connecting to SSH. 23 // 24 // SSHConfig should return the default configuration for 25 // connecting via SSH. 26 SSHConfig func(multistep.StateBag) (*gossh.ClientConfig, error) 27 SSHPort func(multistep.StateBag) (int, error) 28 29 // The fields below are callbacks to assist with connecting to WinRM. 30 // 31 // WinRMConfig should return the default configuration for 32 // connecting via WinRM. 33 WinRMConfig func(multistep.StateBag) (*WinRMConfig, error) 34 35 // CustomConnect can be set to have custom connectors for specific 36 // types. These take highest precedence so you can also override 37 // existing types. 38 CustomConnect map[string]multistep.Step 39 40 substep multistep.Step 41 } 42 43 func (s *StepConnect) Run(state multistep.StateBag) multistep.StepAction { 44 typeMap := map[string]multistep.Step{ 45 "none": nil, 46 "ssh": &StepConnectSSH{ 47 Config: s.Config, 48 Host: s.Host, 49 SSHConfig: s.SSHConfig, 50 SSHPort: s.SSHPort, 51 }, 52 "winrm": &StepConnectWinRM{ 53 Config: s.Config, 54 Host: s.Host, 55 WinRMConfig: s.WinRMConfig, 56 }, 57 } 58 for k, v := range s.CustomConnect { 59 typeMap[k] = v 60 } 61 62 step, ok := typeMap[s.Config.Type] 63 if !ok { 64 state.Put("error", fmt.Errorf("unknown communicator type: %s", s.Config.Type)) 65 return multistep.ActionHalt 66 } 67 68 if step == nil { 69 log.Printf("[INFO] communicator disabled, will not connect") 70 return multistep.ActionContinue 71 } 72 73 s.substep = step 74 return s.substep.Run(state) 75 } 76 77 func (s *StepConnect) Cleanup(state multistep.StateBag) { 78 if s.substep != nil { 79 s.substep.Cleanup(state) 80 } 81 }