github.com/sneal/packer@v0.5.2/builder/vmware/common/ssh_config.go (about)

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