2014-12-29 06:46:54 -06:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2021-01-03 16:08:24 -06:00
|
|
|
"context"
|
2014-12-29 06:46:54 -06:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-01-03 21:21:11 -06:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2021-02-23 16:43:20 -06:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
commonsteps "github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
hconfig "github.com/hashicorp/packer-plugin-sdk/template/config"
|
2021-02-23 17:02:50 -06:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2020-09-13 03:21:07 -05:00
|
|
|
xsclient "github.com/terra-farm/go-xen-api-client"
|
2016-07-15 11:27:11 -05:00
|
|
|
xscommon "github.com/xenserver/packer-builder-xenserver/builder/xenserver/common"
|
2014-12-29 06:46:54 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
2021-01-14 00:15:33 -06:00
|
|
|
config xscommon.Config
|
2014-12-29 06:46:54 -06:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2021-01-03 21:21:11 -06:00
|
|
|
func (self *Builder) ConfigSpec() hcldec.ObjectSpec { return self.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
|
|
|
func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []string, retErr error) {
|
2014-12-29 06:46:54 -06:00
|
|
|
|
2015-06-19 12:33:11 -05:00
|
|
|
var errs *packer.MultiError
|
|
|
|
|
|
|
|
err := hconfig.Decode(&self.config, &hconfig.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2022-09-28 00:05:02 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
errs = packer.MultiErrorAppend(
|
2021-01-14 00:15:33 -06:00
|
|
|
errs, self.config.CommonConfig.Prepare(self.config.GetInterpContext(), &self.config.PackerConfig)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, self.config.SSHConfig.Prepare(self.config.GetInterpContext())...)
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
// Set default values
|
|
|
|
|
|
|
|
if self.config.RawInstallTimeout == "" {
|
|
|
|
self.config.RawInstallTimeout = "200m"
|
2024-01-16 10:29:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.config.DiskName == "" {
|
|
|
|
self.config.DiskName = "Packer-disk"
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.config.DiskSize == 0 {
|
|
|
|
self.config.DiskSize = 40000
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:14:23 -05:00
|
|
|
if self.config.VCPUsMax == 0 {
|
|
|
|
self.config.VCPUsMax = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.config.VCPUsAtStartup == 0 {
|
|
|
|
self.config.VCPUsAtStartup = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.config.VCPUsAtStartup > self.config.VCPUsMax {
|
2017-10-12 05:02:04 -05:00
|
|
|
self.config.VCPUsAtStartup = self.config.VCPUsMax
|
2017-10-12 02:14:23 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if self.config.VMMemory == 0 {
|
|
|
|
self.config.VMMemory = 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.config.CloneTemplate == "" {
|
|
|
|
self.config.CloneTemplate = "Other install media"
|
|
|
|
}
|
|
|
|
|
2022-06-05 10:28:23 -05:00
|
|
|
if self.config.Firmware == "" {
|
|
|
|
self.config.Firmware = "bios"
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if len(self.config.PlatformArgs) == 0 {
|
|
|
|
pargs := make(map[string]string)
|
|
|
|
pargs["viridian"] = "false"
|
|
|
|
pargs["nx"] = "true"
|
|
|
|
pargs["pae"] = "true"
|
|
|
|
pargs["apic"] = "true"
|
|
|
|
pargs["timeoffset"] = "0"
|
|
|
|
pargs["acpi"] = "1"
|
|
|
|
self.config.PlatformArgs = pargs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template substitution
|
|
|
|
|
|
|
|
templates := map[string]*string{
|
2023-04-07 01:23:51 -05:00
|
|
|
"clone_template": &self.config.CloneTemplate,
|
|
|
|
"iso_checksum": &self.config.ISOChecksum,
|
|
|
|
"iso_url": &self.config.ISOUrl,
|
|
|
|
"iso_name": &self.config.ISOName,
|
|
|
|
"install_timeout": &self.config.RawInstallTimeout,
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
for i := range self.config.ISOUrls {
|
|
|
|
templates[fmt.Sprintf("iso_urls[%d]", i)] = &self.config.ISOUrls[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
|
|
|
|
self.config.InstallTimeout, err = time.ParseDuration(self.config.RawInstallTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed to parse install_timeout: %s", err))
|
|
|
|
}
|
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
if self.config.ISOName == "" {
|
|
|
|
// If ISO name is not specified, assume a URL and checksum has been provided.
|
2022-09-28 00:25:52 -05:00
|
|
|
self.config.ISOChecksum = strings.ToLower(self.config.ISOChecksum)
|
2015-05-18 10:13:02 -05:00
|
|
|
|
|
|
|
if len(self.config.ISOUrls) == 0 {
|
|
|
|
if self.config.ISOUrl == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2023-04-07 01:23:51 -05:00
|
|
|
errs, errors.New("One of iso_url or iso_urls must be specified."))
|
2015-05-18 10:13:02 -05:00
|
|
|
} else {
|
|
|
|
self.config.ISOUrls = []string{self.config.ISOUrl}
|
|
|
|
}
|
2022-09-28 00:25:52 -05:00
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
} else if self.config.ISOUrl != "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("Only one of iso_url or iso_urls may be specified."))
|
|
|
|
}
|
2022-09-28 00:25:52 -05:00
|
|
|
|
|
|
|
//The SDK can validate the ISO checksum and other sanity checks on the url.
|
2023-04-07 01:23:51 -05:00
|
|
|
iso_config := commonsteps.ISOConfig{
|
2022-09-28 00:25:52 -05:00
|
|
|
ISOChecksum: self.config.ISOChecksum,
|
2023-04-07 01:23:51 -05:00
|
|
|
ISOUrls: self.config.ISOUrls,
|
2022-09-28 00:25:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_, iso_errs := iso_config.Prepare(nil)
|
|
|
|
if iso_errs != nil {
|
2023-04-07 01:23:51 -05:00
|
|
|
for _, this_err := range iso_errs {
|
|
|
|
errs = packer.MultiErrorAppend(errs, this_err)
|
|
|
|
}
|
2022-09-28 00:25:52 -05:00
|
|
|
}
|
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// An ISO name has been provided. It should be attached from an available SR.
|
|
|
|
|
|
|
|
}
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
retErr = errors.New(errs.Error())
|
|
|
|
}
|
|
|
|
|
2021-01-03 21:21:11 -06:00
|
|
|
return nil, nil, retErr
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:08:24 -06:00
|
|
|
func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2020-09-13 03:21:07 -05:00
|
|
|
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password)
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2020-09-13 03:21:07 -05:00
|
|
|
return nil, err
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
ui.Say("XAPI client session established")
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
c.GetClient().Host.GetAll(c.GetSessionRef())
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
//Share state between the other steps using a statebag
|
|
|
|
state := new(multistep.BasicStateBag)
|
2020-09-13 03:21:07 -05:00
|
|
|
state.Put("client", c)
|
2014-12-29 06:46:54 -06:00
|
|
|
state.Put("config", self.config)
|
|
|
|
state.Put("commonconfig", self.config.CommonConfig)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
2014-12-30 09:01:53 -06:00
|
|
|
httpReqChan := make(chan string, 1)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
//Build the steps
|
2015-05-18 10:13:02 -05:00
|
|
|
download_steps := []multistep.Step{
|
2021-02-23 16:43:20 -06:00
|
|
|
&commonsteps.StepDownload{
|
2021-01-03 16:08:24 -06:00
|
|
|
Checksum: self.config.ISOChecksum,
|
|
|
|
Description: "ISO",
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
Url: self.config.ISOUrls,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
2015-05-18 10:13:02 -05:00
|
|
|
}
|
2023-08-07 05:12:33 -05:00
|
|
|
steps := []multistep.Step{
|
|
|
|
&xscommon.StepPrepareOutputDir{
|
|
|
|
Force: self.config.PackerForce,
|
|
|
|
Path: self.config.OutputDir,
|
|
|
|
},
|
2021-02-23 16:43:20 -06:00
|
|
|
&commonsteps.StepCreateFloppy{
|
2014-12-29 06:46:54 -06:00
|
|
|
Files: self.config.FloppyFiles,
|
2021-12-14 20:50:15 -06:00
|
|
|
Label: "cidata",
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
2014-12-30 09:01:53 -06:00
|
|
|
&xscommon.StepHTTPServer{
|
|
|
|
Chan: httpReqChan,
|
|
|
|
},
|
2014-12-29 06:46:54 -06:00
|
|
|
&xscommon.StepUploadVdi{
|
2015-02-23 12:30:57 -06:00
|
|
|
VdiNameFunc: func() string {
|
2015-05-18 10:13:02 -05:00
|
|
|
return "Packer-floppy-disk"
|
|
|
|
},
|
2014-12-29 06:46:54 -06:00
|
|
|
ImagePathFunc: func() string {
|
|
|
|
if floppyPath, ok := state.GetOk("floppy_path"); ok {
|
|
|
|
return floppyPath.(string)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
VdiUuidKey: "floppy_vdi_uuid",
|
|
|
|
},
|
2023-02-27 01:23:28 -06:00
|
|
|
&xscommon.StepFindOrUploadVdi{
|
|
|
|
xscommon.StepUploadVdi{
|
|
|
|
VdiNameFunc: func() string {
|
|
|
|
if len(self.config.ISOUrls) > 0 {
|
|
|
|
return path.Base(self.config.ISOUrls[0])
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
ImagePathFunc: func() string {
|
|
|
|
if isoPath, ok := state.GetOk("iso_path"); ok {
|
|
|
|
return isoPath.(string)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
VdiUuidKey: "iso_vdi_uuid",
|
2015-05-18 10:13:02 -05:00
|
|
|
},
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
|
|
|
&xscommon.StepFindVdi{
|
|
|
|
VdiName: self.config.ToolsIsoName,
|
|
|
|
VdiUuidKey: "tools_vdi_uuid",
|
|
|
|
},
|
2015-05-18 10:13:02 -05:00
|
|
|
&xscommon.StepFindVdi{
|
|
|
|
VdiName: self.config.ISOName,
|
|
|
|
VdiUuidKey: "isoname_vdi_uuid",
|
|
|
|
},
|
2023-04-15 16:58:13 -05:00
|
|
|
&xscommon.StepCreateInstance{
|
|
|
|
AssumePreInstalledOS: false,
|
|
|
|
},
|
2014-12-29 06:46:54 -06:00
|
|
|
&xscommon.StepAttachVdi{
|
|
|
|
VdiUuidKey: "floppy_vdi_uuid",
|
2020-09-13 03:21:07 -05:00
|
|
|
VdiType: xsclient.VbdTypeFloppy,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
|
|
|
&xscommon.StepAttachVdi{
|
|
|
|
VdiUuidKey: "iso_vdi_uuid",
|
2020-09-13 03:21:07 -05:00
|
|
|
VdiType: xsclient.VbdTypeCD,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
2015-05-18 10:13:02 -05:00
|
|
|
&xscommon.StepAttachVdi{
|
|
|
|
VdiUuidKey: "isoname_vdi_uuid",
|
2020-09-13 03:21:07 -05:00
|
|
|
VdiType: xsclient.VbdTypeCD,
|
2015-05-18 10:13:02 -05:00
|
|
|
},
|
2014-12-29 06:46:54 -06:00
|
|
|
&xscommon.StepAttachVdi{
|
|
|
|
VdiUuidKey: "tools_vdi_uuid",
|
2020-09-13 03:21:07 -05:00
|
|
|
VdiType: xsclient.VbdTypeCD,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
|
|
|
new(xscommon.StepStartVmPaused),
|
2017-02-02 02:13:23 -06:00
|
|
|
new(xscommon.StepSetVmHostSshAddress),
|
2020-12-16 01:17:27 -06:00
|
|
|
// &xscommon.StepForwardPortOverSSH{
|
|
|
|
// RemotePort: xscommon.InstanceVNCPort,
|
|
|
|
// RemoteDest: xscommon.InstanceVNCIP,
|
|
|
|
// HostPortMin: self.config.HostPortMin,
|
|
|
|
// HostPortMax: self.config.HostPortMax,
|
|
|
|
// ResultKey: "local_vnc_port",
|
|
|
|
// },
|
2014-12-29 06:46:54 -06:00
|
|
|
new(xscommon.StepBootWait),
|
|
|
|
&xscommon.StepTypeBootCommand{
|
2021-01-14 00:15:33 -06:00
|
|
|
Ctx: *self.config.GetInterpContext(),
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
2015-01-05 10:35:00 -06:00
|
|
|
&xscommon.StepWaitForIP{
|
2014-12-30 09:01:53 -06:00
|
|
|
Chan: httpReqChan,
|
|
|
|
Timeout: self.config.InstallTimeout, // @todo change this
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
|
|
|
&xscommon.StepForwardPortOverSSH{
|
2014-12-30 09:01:53 -06:00
|
|
|
RemotePort: xscommon.InstanceSSHPort,
|
|
|
|
RemoteDest: xscommon.InstanceSSHIP,
|
2014-12-29 06:46:54 -06:00
|
|
|
HostPortMin: self.config.HostPortMin,
|
|
|
|
HostPortMax: self.config.HostPortMax,
|
|
|
|
ResultKey: "local_ssh_port",
|
|
|
|
},
|
2015-06-19 12:33:11 -05:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &self.config.SSHConfig.Comm,
|
2021-01-14 00:15:33 -06:00
|
|
|
Host: xscommon.InstanceSSHIP,
|
|
|
|
SSHConfig: self.config.Comm.SSHConfigFunc(),
|
|
|
|
SSHPort: xscommon.InstanceSSHPort,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
2021-02-23 16:43:20 -06:00
|
|
|
new(commonsteps.StepProvision),
|
2015-01-05 10:58:26 -06:00
|
|
|
new(xscommon.StepShutdown),
|
2023-08-07 04:56:14 -05:00
|
|
|
}
|
|
|
|
|
2023-09-04 11:25:16 -05:00
|
|
|
if !self.config.SkipSetTemplate {
|
2023-08-07 04:56:14 -05:00
|
|
|
steps = append(steps,
|
|
|
|
new(xscommon.StepSetVmToTemplate))
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2015-01-05 10:58:26 -06:00
|
|
|
&xscommon.StepDetachVdi{
|
|
|
|
VdiUuidKey: "iso_vdi_uuid",
|
|
|
|
},
|
2016-10-10 04:39:52 -05:00
|
|
|
&xscommon.StepDetachVdi{
|
|
|
|
VdiUuidKey: "isoname_vdi_uuid",
|
|
|
|
},
|
2015-01-05 10:58:26 -06:00
|
|
|
&xscommon.StepDetachVdi{
|
|
|
|
VdiUuidKey: "tools_vdi_uuid",
|
|
|
|
},
|
2016-08-26 08:17:45 -05:00
|
|
|
&xscommon.StepDetachVdi{
|
|
|
|
VdiUuidKey: "floppy_vdi_uuid",
|
|
|
|
},
|
2023-08-07 04:56:14 -05:00
|
|
|
new(xscommon.StepExport))
|
2014-12-29 06:46:54 -06:00
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
if self.config.ISOName == "" {
|
|
|
|
steps = append(download_steps, steps...)
|
|
|
|
}
|
2015-02-23 12:30:57 -06:00
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
self.runner = &multistep.BasicRunner{Steps: steps}
|
2021-01-03 16:08:24 -06:00
|
|
|
self.runner.Run(ctx, state)
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, _ := xscommon.NewArtifact(self.config.OutputDir)
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|