github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/builder/parallels/common/ssh_config.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/mitchellh/packer/packer"
     7  	"os"
     8  	"time"
     9  )
    10  
    11  type SSHConfig struct {
    12  	SSHKeyPath        string `mapstructure:"ssh_key_path"`
    13  	SSHPassword       string `mapstructure:"ssh_password"`
    14  	SSHPort           uint   `mapstructure:"ssh_port"`
    15  	SSHUser           string `mapstructure:"ssh_username"`
    16  	RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
    17  
    18  	SSHWaitTimeout time.Duration
    19  }
    20  
    21  func (c *SSHConfig) Prepare(t *packer.ConfigTemplate) []error {
    22  	if c.SSHPort == 0 {
    23  		c.SSHPort = 22
    24  	}
    25  
    26  	if c.RawSSHWaitTimeout == "" {
    27  		c.RawSSHWaitTimeout = "20m"
    28  	}
    29  
    30  	templates := map[string]*string{
    31  		"ssh_key_path":     &c.SSHKeyPath,
    32  		"ssh_password":     &c.SSHPassword,
    33  		"ssh_username":     &c.SSHUser,
    34  		"ssh_wait_timeout": &c.RawSSHWaitTimeout,
    35  	}
    36  
    37  	errs := make([]error, 0)
    38  	for n, ptr := range templates {
    39  		var err error
    40  		*ptr, err = t.Process(*ptr, nil)
    41  		if err != nil {
    42  			errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err))
    43  		}
    44  	}
    45  
    46  	if c.SSHKeyPath != "" {
    47  		if _, err := os.Stat(c.SSHKeyPath); err != nil {
    48  			errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
    49  		} else if _, err := sshKeyToSigner(c.SSHKeyPath); err != nil {
    50  			errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
    51  		}
    52  	}
    53  
    54  	if c.SSHUser == "" {
    55  		errs = append(errs, errors.New("An ssh_username must be specified."))
    56  	}
    57  
    58  	var err error
    59  	c.SSHWaitTimeout, err = time.ParseDuration(c.RawSSHWaitTimeout)
    60  	if err != nil {
    61  		errs = append(errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err))
    62  	}
    63  
    64  	return errs
    65  }