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