github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/virtualbox/common/ssh_config.go (about) 1 package common 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "time" 8 9 commonssh "github.com/mitchellh/packer/common/ssh" 10 "github.com/mitchellh/packer/packer" 11 ) 12 13 type SSHConfig struct { 14 SSHHostPortMin uint `mapstructure:"ssh_host_port_min"` 15 SSHHostPortMax uint `mapstructure:"ssh_host_port_max"` 16 SSHKeyPath string `mapstructure:"ssh_key_path"` 17 SSHPassword string `mapstructure:"ssh_password"` 18 SSHPort uint `mapstructure:"ssh_port"` 19 SSHUser string `mapstructure:"ssh_username"` 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.SSHHostPortMin == 0 { 27 c.SSHHostPortMin = 2222 28 } 29 30 if c.SSHHostPortMax == 0 { 31 c.SSHHostPortMax = 4444 32 } 33 34 if c.SSHPort == 0 { 35 c.SSHPort = 22 36 } 37 38 if c.RawSSHWaitTimeout == "" { 39 c.RawSSHWaitTimeout = "20m" 40 } 41 42 templates := map[string]*string{ 43 "ssh_key_path": &c.SSHKeyPath, 44 "ssh_password": &c.SSHPassword, 45 "ssh_username": &c.SSHUser, 46 "ssh_wait_timeout": &c.RawSSHWaitTimeout, 47 } 48 49 errs := make([]error, 0) 50 for n, ptr := range templates { 51 var err error 52 *ptr, err = t.Process(*ptr, nil) 53 if err != nil { 54 errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err)) 55 } 56 } 57 58 if c.SSHKeyPath != "" { 59 if _, err := os.Stat(c.SSHKeyPath); err != nil { 60 errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err)) 61 } else if _, err := commonssh.FileSigner(c.SSHKeyPath); err != nil { 62 errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err)) 63 } 64 } 65 66 if c.SSHHostPortMin > c.SSHHostPortMax { 67 errs = append(errs, 68 errors.New("ssh_host_port_min must be less than ssh_host_port_max")) 69 } 70 71 if c.SSHUser == "" { 72 errs = append(errs, errors.New("An ssh_username must be specified.")) 73 } 74 75 var err error 76 c.SSHWaitTimeout, err = time.ParseDuration(c.RawSSHWaitTimeout) 77 if err != nil { 78 errs = append(errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err)) 79 } 80 81 return errs 82 }