2015-06-13 17:42:38 -04:00
|
|
|
package communicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-06-13 18:05:10 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-06-13 17:42:38 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the common configuration that communicators allow within
|
|
|
|
// a builder.
|
|
|
|
type Config struct {
|
2015-06-13 17:50:45 -04:00
|
|
|
Type string `mapstructure:"communicator"`
|
2015-06-13 17:42:38 -04:00
|
|
|
SSHHost string `mapstructure:"ssh_host"`
|
|
|
|
SSHPort int `mapstructure:"ssh_port"`
|
|
|
|
SSHUsername string `mapstructure:"ssh_username"`
|
|
|
|
SSHPassword string `mapstructure:"ssh_password"`
|
|
|
|
SSHPrivateKey string `mapstructure:"ssh_private_key_file"`
|
|
|
|
SSHPty bool `mapstructure:"ssh_pty"`
|
|
|
|
SSHTimeout time.Duration `mapstructure:"ssh_timeout"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Prepare(ctx *interpolate.Context) []error {
|
2015-06-13 17:50:45 -04:00
|
|
|
if c.Type == "" {
|
|
|
|
c.Type = "ssh"
|
|
|
|
}
|
|
|
|
|
2015-06-13 17:42:38 -04:00
|
|
|
if c.SSHPort == 0 {
|
|
|
|
c.SSHPort = 22
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHTimeout == 0 {
|
|
|
|
c.SSHTimeout = 5 * time.Minute
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
var errs []error
|
2015-06-13 17:51:27 -04:00
|
|
|
if c.Type == "ssh" {
|
|
|
|
if c.SSHUsername == "" {
|
|
|
|
errs = append(errs, errors.New("An ssh_username must be specified"))
|
|
|
|
}
|
2015-06-13 18:05:10 -04:00
|
|
|
|
|
|
|
if c.SSHPrivateKey != "" {
|
|
|
|
if _, err := os.Stat(c.SSHPrivateKey); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"ssh_private_key_file is invalid: %s", err))
|
|
|
|
} else if _, err := SSHFileSigner(c.SSHPrivateKey); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"ssh_private_key_file is invalid: %s", err))
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 17:42:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|