2014-12-29 06:46:54 -06:00
|
|
|
package xva
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-12-30 09:01:53 -06:00
|
|
|
"time"
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/common"
|
2015-06-19 12:33:11 -05:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
|
|
|
hconfig "github.com/mitchellh/packer/helper/config"
|
2014-12-29 06:46:54 -06:00
|
|
|
"github.com/mitchellh/packer/packer"
|
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"
|
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 config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
xscommon.CommonConfig `mapstructure:",squash"`
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
SourcePath string `mapstructure:"source_path"`
|
2015-03-13 08:23:20 -05:00
|
|
|
VMMemory uint `mapstructure:"vm_memory"`
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
PlatformArgs map[string]string `mapstructure:"platform_args"`
|
|
|
|
|
2015-06-19 12:33:11 -05:00
|
|
|
ctx interpolate.Context
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error) {
|
|
|
|
|
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 {
|
2015-06-19 12:33:11 -05:00
|
|
|
packer.MultiErrorAppend(errs, err)
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
errs = packer.MultiErrorAppend(
|
2015-06-19 12:33:11 -05:00
|
|
|
errs, self.config.CommonConfig.Prepare(&self.config.ctx, &self.config.PackerConfig)...)
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
// Set default values
|
|
|
|
|
|
|
|
if self.config.VMMemory == 0 {
|
|
|
|
self.config.VMMemory = 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
if self.config.SourcePath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("A source_path must be specified"))
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
retErr = errors.New(errs.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, retErr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
|
|
|
//Setup XAPI client
|
2015-03-13 08:23:20 -05:00
|
|
|
client := xsclient.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password)
|
2014-12-29 06:46:54 -06:00
|
|
|
|
|
|
|
err := client.Login()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err.(error)
|
|
|
|
}
|
|
|
|
ui.Say("XAPI client session established")
|
|
|
|
|
|
|
|
client.GetHosts()
|
|
|
|
|
|
|
|
//Share state between the other steps using a statebag
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("cache", cache)
|
|
|
|
state.Put("client", client)
|
|
|
|
state.Put("config", self.config)
|
2014-12-31 09:21:54 -06:00
|
|
|
state.Put("commonconfig", self.config.CommonConfig)
|
2014-12-29 06:46:54 -06:00
|
|
|
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
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&xscommon.StepPrepareOutputDir{
|
|
|
|
Force: self.config.PackerForce,
|
|
|
|
Path: self.config.OutputDir,
|
|
|
|
},
|
|
|
|
&common.StepCreateFloppy{
|
|
|
|
Files: self.config.FloppyFiles,
|
|
|
|
},
|
|
|
|
new(xscommon.StepHTTPServer),
|
|
|
|
&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",
|
|
|
|
},
|
|
|
|
&xscommon.StepFindVdi{
|
|
|
|
VdiName: self.config.ToolsIsoName,
|
|
|
|
VdiUuidKey: "tools_vdi_uuid",
|
|
|
|
},
|
2014-12-31 09:21:54 -06:00
|
|
|
new(stepImportInstance),
|
2014-12-29 06:46:54 -06:00
|
|
|
&xscommon.StepAttachVdi{
|
|
|
|
VdiUuidKey: "floppy_vdi_uuid",
|
2015-03-13 08:23:20 -05:00
|
|
|
VdiType: xsclient.Floppy,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
|
|
|
&xscommon.StepAttachVdi{
|
|
|
|
VdiUuidKey: "tools_vdi_uuid",
|
2015-03-13 08:23:20 -05:00
|
|
|
VdiType: xsclient.CD,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
2014-12-30 09:01:53 -06:00
|
|
|
new(xscommon.StepStartVmPaused),
|
|
|
|
new(xscommon.StepGetVNCPort),
|
|
|
|
&xscommon.StepForwardPortOverSSH{
|
|
|
|
RemotePort: xscommon.InstanceVNCPort,
|
|
|
|
RemoteDest: xscommon.InstanceVNCIP,
|
|
|
|
HostPortMin: self.config.HostPortMin,
|
|
|
|
HostPortMax: self.config.HostPortMax,
|
|
|
|
ResultKey: "local_vnc_port",
|
|
|
|
},
|
|
|
|
new(xscommon.StepBootWait),
|
|
|
|
&xscommon.StepTypeBootCommand{
|
2015-06-19 12:33:11 -05:00
|
|
|
Ctx: self.config.ctx,
|
2014-12-30 09:01:53 -06:00
|
|
|
},
|
|
|
|
&xscommon.StepWaitForIP{
|
|
|
|
Chan: httpReqChan,
|
2015-03-13 08:23:20 -05:00
|
|
|
Timeout: 300 * time.Minute, /*self.config.InstallTimeout*/ // @todo change this
|
2014-12-30 09:01:53 -06:00
|
|
|
},
|
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,
|
|
|
|
Host: xscommon.CommHost,
|
|
|
|
SSHConfig: xscommon.SSHConfigFunc(self.config.CommonConfig.SSHConfig),
|
|
|
|
SSHPort: xscommon.SSHPort,
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
|
|
|
new(common.StepProvision),
|
2015-01-05 10:58:26 -06:00
|
|
|
new(xscommon.StepShutdown),
|
2014-12-29 06:46:54 -06:00
|
|
|
&xscommon.StepDetachVdi{
|
|
|
|
VdiUuidKey: "floppy_vdi_uuid",
|
|
|
|
},
|
|
|
|
&xscommon.StepDetachVdi{
|
2015-01-05 10:58:26 -06:00
|
|
|
VdiUuidKey: "tools_vdi_uuid",
|
2014-12-29 06:46:54 -06:00
|
|
|
},
|
2015-01-05 10:58:26 -06:00
|
|
|
new(xscommon.StepExport),
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
self.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
self.runner.Run(state)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Builder) Cancel() {
|
|
|
|
if self.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
self.runner.Cancel()
|
|
|
|
}
|
|
|
|
fmt.Println("Cancelling the builder")
|
|
|
|
}
|