github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/builder/vmware/common/ssh_config.go (about) 1 package common 2 3 import ( 4 "errors" 5 "fmt" 6 "net" 7 "os" 8 "time" 9 10 commonssh "github.com/mitchellh/packer/common/ssh" 11 "github.com/mitchellh/packer/packer" 12 ) 13 14 type SSHConfig struct { 15 SSHUser string `mapstructure:"ssh_username"` 16 SSHKeyPath string `mapstructure:"ssh_key_path"` 17 SSHPassword string `mapstructure:"ssh_password"` 18 SSHHost string `mapstructure:"ssh_host"` 19 SSHPort uint `mapstructure:"ssh_port"` 20 SSHSkipRequestPty bool `mapstructure:"ssh_skip_request_pty"` 21 RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"` 22 23 SSHWaitTimeout time.Duration 24 } 25 26 func (c *SSHConfig) Prepare(t *packer.ConfigTemplate) []error { 27 if c.SSHPort == 0 { 28 c.SSHPort = 22 29 } 30 31 if c.RawSSHWaitTimeout == "" { 32 c.RawSSHWaitTimeout = "20m" 33 } 34 35 templates := map[string]*string{ 36 "ssh_host": &c.SSHHost, 37 "ssh_key_path": &c.SSHKeyPath, 38 "ssh_password": &c.SSHPassword, 39 "ssh_username": &c.SSHUser, 40 "ssh_wait_timeout": &c.RawSSHWaitTimeout, 41 } 42 43 errs := make([]error, 0) 44 for n, ptr := range templates { 45 var err error 46 *ptr, err = t.Process(*ptr, nil) 47 if err != nil { 48 errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err)) 49 } 50 } 51 52 if c.SSHKeyPath != "" { 53 if _, err := os.Stat(c.SSHKeyPath); err != nil { 54 errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err)) 55 } else if _, err := commonssh.FileSigner(c.SSHKeyPath); err != nil { 56 errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err)) 57 } 58 } 59 60 if c.SSHHost != "" { 61 if ip := net.ParseIP(c.SSHHost); ip == nil { 62 if _, err := net.LookupHost(c.SSHHost); err != nil { 63 errs = append(errs, errors.New("ssh_host is an invalid IP or hostname")) 64 } 65 } 66 } 67 68 if c.SSHUser == "" { 69 errs = append(errs, errors.New("An ssh_username must be specified.")) 70 } 71 72 var err error 73 c.SSHWaitTimeout, err = time.ParseDuration(c.RawSSHWaitTimeout) 74 if err != nil { 75 errs = append(errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err)) 76 } 77 78 return errs 79 }