2014-12-29 06:46:54 -06:00
|
|
|
package xva
|
|
|
|
|
|
|
|
import (
|
2021-01-03 16:08:24 -06:00
|
|
|
"context"
|
2014-12-31 09:21:54 -06:00
|
|
|
"fmt"
|
2022-05-26 10:21:49 -05:00
|
|
|
"log"
|
2014-12-31 09:21:54 -06:00
|
|
|
"os"
|
2014-12-29 06:46:54 -06:00
|
|
|
|
2021-02-23 16:43:20 -06:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/packer"
|
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
|
|
|
)
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
type stepImportInstance struct {
|
2020-09-13 03:21:07 -05:00
|
|
|
instance xsclient.VMRef
|
|
|
|
vdi xsclient.VDIRef
|
2014-12-29 06:46:54 -06:00
|
|
|
}
|
|
|
|
|
2021-01-03 16:08:24 -06:00
|
|
|
func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-12-31 09:21:54 -06:00
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
c := state.Get("client").(*xscommon.Connection)
|
2021-01-14 00:15:33 -06:00
|
|
|
config := state.Get("config").(xscommon.Config)
|
2014-12-31 09:21:54 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Step: Import Instance")
|
|
|
|
|
2022-05-26 10:21:49 -05:00
|
|
|
if config.SourcePath == "" {
|
|
|
|
log.Println("Skipping imporing instance - no `source_path` configured.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
// find the SR
|
2020-09-13 03:21:07 -05:00
|
|
|
srs, err := c.GetClient().SR.GetAll(c.GetSessionRef())
|
|
|
|
sr := srs[0]
|
2014-12-31 09:21:54 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file for reading (NB: httpUpload closes the file for us)
|
|
|
|
fh, err := os.Open(config.SourcePath)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to open XVA '%s': %s", config.SourcePath, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := xscommon.HTTPUpload(fmt.Sprintf("https://%s/import?session_id=%s&sr_id=%s",
|
2020-09-13 03:21:07 -05:00
|
|
|
c.Host,
|
|
|
|
c.GetSession(),
|
|
|
|
sr,
|
2014-12-31 09:21:54 -06:00
|
|
|
), fh, state)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to upload VDI: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2020-09-13 03:21:07 -05:00
|
|
|
if result == "" {
|
2014-12-31 09:21:54 -06:00
|
|
|
ui.Error("XAPI did not reply with an instance reference")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
instance := xsclient.VMRef(result)
|
2014-12-29 06:46:54 -06:00
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
instanceId, err := c.GetClient().VM.GetUUID(c.GetSessionRef(), instance)
|
2014-12-31 09:21:54 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put("instance_uuid", instanceId)
|
2015-05-01 19:45:43 -05:00
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
err = c.GetClient().VM.SetVCPUsMax(c.GetSessionRef(), instance, int(config.VCPUsMax))
|
2020-03-24 10:21:35 -05:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error setting VM VCPUs Max=%d: %s", config.VCPUsMax, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
err = c.GetClient().VM.SetVCPUsAtStartup(c.GetSessionRef(), instance, int(config.VCPUsAtStartup))
|
2020-03-24 10:21:35 -05:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error setting VM VCPUs At Startup=%d: %s", config.VCPUsAtStartup, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
err = c.GetClient().VM.SetNameDescription(c.GetSessionRef(), instance, config.VMDescription)
|
2015-05-01 19:45:43 -05:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error setting VM description: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2022-05-26 11:28:05 -05:00
|
|
|
err = xscommon.AddVMTags(c, instance, config.VMTags)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Failed to add tags: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
ui.Say(fmt.Sprintf("Imported instance '%s'", instanceId))
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
func (self *stepImportInstance) Cleanup(state multistep.StateBag) {
|
2014-12-29 06:46:54 -06:00
|
|
|
/*
|
|
|
|
config := state.Get("config").(config)
|
|
|
|
if config.ShouldKeepVM(state) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if self.instance != nil {
|
|
|
|
ui.Say("Destroying VM")
|
|
|
|
_ = self.instance.HardShutdown() // redundant, just in case
|
|
|
|
err := self.instance.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.vdi != nil {
|
|
|
|
ui.Say("Destroying VDI")
|
|
|
|
err := self.vdi.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|