2014-12-29 06:46:54 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
2015-06-19 12:33:11 -05:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2015-03-13 08:23:20 -05:00
|
|
|
xsclient "github.com/xenserver/go-xenserver-client"
|
2014-12-29 06:46:54 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type CommonConfig struct {
|
2014-12-31 11:09:14 -06:00
|
|
|
Username string `mapstructure:"remote_username"`
|
|
|
|
Password string `mapstructure:"remote_password"`
|
|
|
|
HostIp string `mapstructure:"remote_host"`
|
2014-12-29 06:46:54 -06:00
|
|
|
|
2016-09-05 04:05:44 -05:00
|
|
|
VMName string `mapstructure:"vm_name"`
|
|
|
|
VMDescription string `mapstructure:"vm_description"`
|
|
|
|
SrName string `mapstructure:"sr_name"`
|
|
|
|
FloppyFiles []string `mapstructure:"floppy_files"`
|
|
|
|
NetworkNames []string `mapstructure:"network_names"`
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
HostPortMin uint `mapstructure:"host_port_min"`
|
|
|
|
HostPortMax uint `mapstructure:"host_port_max"`
|
|
|
|
|
|
|
|
BootCommand []string `mapstructure:"boot_command"`
|
|
|
|
ShutdownCommand string `mapstructure:"shutdown_command"`
|
|
|
|
|
|
|
|
RawBootWait string `mapstructure:"boot_wait"`
|
|
|
|
BootWait time.Duration
|
|
|
|
|
|
|
|
ToolsIsoName string `mapstructure:"tools_iso_name"`
|
|
|
|
|
|
|
|
HTTPDir string `mapstructure:"http_directory"`
|
|
|
|
HTTPPortMin uint `mapstructure:"http_port_min"`
|
|
|
|
HTTPPortMax uint `mapstructure:"http_port_max"`
|
|
|
|
|
|
|
|
// SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
|
|
|
|
// SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
|
2015-06-19 12:33:11 -05:00
|
|
|
SSHKeyPath string `mapstructure:"ssh_key_path"`
|
|
|
|
SSHPassword string `mapstructure:"ssh_password"`
|
|
|
|
SSHPort uint `mapstructure:"ssh_port"`
|
|
|
|
SSHUser string `mapstructure:"ssh_username"`
|
|
|
|
SSHConfig `mapstructure:",squash"`
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
|
|
|
|
SSHWaitTimeout time.Duration
|
|
|
|
|
|
|
|
OutputDir string `mapstructure:"output_directory"`
|
|
|
|
Format string `mapstructure:"format"`
|
|
|
|
KeepVM string `mapstructure:"keep_vm"`
|
2015-05-18 10:13:02 -05:00
|
|
|
IPGetter string `mapstructure:"ip_getter"`
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
2015-06-19 12:33:11 -05:00
|
|
|
func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
|
2014-12-29 06:46:54 -06:00
|
|
|
var err error
|
2015-06-19 12:33:11 -05:00
|
|
|
var errs []error
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
// Set default values
|
|
|
|
|
|
|
|
if c.HostPortMin == 0 {
|
|
|
|
c.HostPortMin = 5900
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostPortMax == 0 {
|
|
|
|
c.HostPortMax = 6000
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RawBootWait == "" {
|
|
|
|
c.RawBootWait = "5s"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ToolsIsoName == "" {
|
|
|
|
c.ToolsIsoName = "xs-tools.iso"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HTTPPortMin == 0 {
|
|
|
|
c.HTTPPortMin = 8000
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HTTPPortMax == 0 {
|
|
|
|
c.HTTPPortMax = 9000
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RawSSHWaitTimeout == "" {
|
|
|
|
c.RawSSHWaitTimeout = "200m"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.FloppyFiles == nil {
|
|
|
|
c.FloppyFiles = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if c.SSHHostPortMin == 0 {
|
|
|
|
c.SSHHostPortMin = 2222
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHHostPortMax == 0 {
|
|
|
|
c.SSHHostPortMax = 4444
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-01-02 12:19:00 -06:00
|
|
|
if c.SSHPort == 0 {
|
|
|
|
c.SSHPort = 22
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if c.RawSSHWaitTimeout == "" {
|
|
|
|
c.RawSSHWaitTimeout = "20m"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.OutputDir == "" {
|
|
|
|
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
2014-12-31 11:26:27 -06:00
|
|
|
if c.VMName == "" {
|
|
|
|
c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", pc.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if c.Format == "" {
|
|
|
|
c.Format = "xva"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.KeepVM == "" {
|
|
|
|
c.KeepVM = "never"
|
|
|
|
}
|
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
if c.IPGetter == "" {
|
|
|
|
c.IPGetter = "auto"
|
|
|
|
}
|
2015-04-27 11:44:38 -05:00
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
// Validation
|
|
|
|
|
|
|
|
if c.Username == "" {
|
2014-12-31 11:09:14 -06:00
|
|
|
errs = append(errs, errors.New("remote_username must be specified."))
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Password == "" {
|
2014-12-31 11:09:14 -06:00
|
|
|
errs = append(errs, errors.New("remote_password must be specified."))
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostIp == "" {
|
2014-12-31 11:09:14 -06:00
|
|
|
errs = append(errs, errors.New("remote_host must be specified."))
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostPortMin > c.HostPortMax {
|
|
|
|
errs = append(errs, errors.New("the host min port must be less than the max"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HTTPPortMin > c.HTTPPortMax {
|
|
|
|
errs = append(errs, errors.New("the HTTP min port must be less than the max"))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.BootWait, err = time.ParseDuration(c.RawBootWait)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Failed to parse boot_wait: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHKeyPath != "" {
|
|
|
|
if _, err := os.Stat(c.SSHKeyPath); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
|
|
|
} else if _, err := commonssh.FileSigner(c.SSHKeyPath); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if c.SSHHostPortMin > c.SSHHostPortMax {
|
|
|
|
errs = append(errs,
|
|
|
|
errors.New("ssh_host_port_min must be less than ssh_host_port_max"))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if c.SSHUser == "" {
|
|
|
|
errs = append(errs, errors.New("An ssh_username must be specified."))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SSHWaitTimeout, err = time.ParseDuration(c.RawSSHWaitTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Failed to parse ssh_wait_timeout: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Format {
|
2015-02-11 11:33:36 -06:00
|
|
|
case "xva", "vdi_raw", "vdi_vhd", "none":
|
2014-12-29 06:46:54 -06:00
|
|
|
default:
|
2015-02-11 11:33:36 -06:00
|
|
|
errs = append(errs, errors.New("format must be one of 'xva', 'vdi_raw', 'vdi_vhd', 'none'"))
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
switch c.KeepVM {
|
|
|
|
case "always", "never", "on_success":
|
|
|
|
default:
|
|
|
|
errs = append(errs, errors.New("keep_vm must be one of 'always', 'never', 'on_success'"))
|
|
|
|
}
|
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
switch c.IPGetter {
|
|
|
|
case "auto", "tools", "http":
|
|
|
|
default:
|
|
|
|
errs = append(errs, errors.New("ip_getter must be one of 'auto', 'tools', 'http'"))
|
|
|
|
}
|
2015-04-27 11:44:38 -05:00
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// steps should check config.ShouldKeepVM first before cleaning up the VM
|
|
|
|
func (c CommonConfig) ShouldKeepVM(state multistep.StateBag) bool {
|
|
|
|
switch c.KeepVM {
|
|
|
|
case "always":
|
|
|
|
return true
|
|
|
|
case "never":
|
|
|
|
return false
|
|
|
|
case "on_success":
|
|
|
|
// only keep instance if build was successful
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
return !(cancelled || halted)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown keep_vm value '%s'", c.KeepVM))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 08:23:20 -05:00
|
|
|
func (config CommonConfig) GetSR(client xsclient.XenAPIClient) (*xsclient.SR, error) {
|
2014-12-29 06:46:54 -06:00
|
|
|
if config.SrName == "" {
|
|
|
|
// Find the default SR
|
|
|
|
return client.GetDefaultSR()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Use the provided name label to find the SR to use
|
|
|
|
srs, err := client.GetSRByNameLabel(config.SrName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(srs) == 0:
|
|
|
|
return nil, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName)
|
|
|
|
case len(srs) > 1:
|
|
|
|
return nil, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return srs[0], nil
|
|
|
|
}
|
|
|
|
}
|