packer-plugin-xenserver/builder/xenserver/step_create_instance.go

193 lines
4.8 KiB
Go
Raw Normal View History

package xenserver
import (
2014-12-08 09:34:48 -06:00
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
)
type stepCreateInstance struct {
2014-12-08 09:34:48 -06:00
InstanceId string
}
func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
2014-12-08 09:34:48 -06:00
client := state.Get("client").(XenAPIClient)
config := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)
2014-12-08 09:34:48 -06:00
ui.Say("Step: Create Instance")
2014-12-08 09:34:48 -06:00
// Get the template to clone from
2014-12-08 09:34:48 -06:00
vms, err := client.GetVMByNameLabel(config.CloneTemplate)
2014-12-08 09:34:48 -06:00
switch {
case len(vms) == 0:
log.Fatal(fmt.Sprintf("Couldn't find a template with the name-label '%s'. Aborting.", config.CloneTemplate))
return multistep.ActionHalt
case len(vms) > 1:
log.Fatal(fmt.Sprintf("Found more than one template with the name '%s'. The name must be unique. Aborting.", config.CloneTemplate))
return multistep.ActionHalt
}
2014-12-08 09:34:48 -06:00
template := vms[0]
2014-12-08 09:34:48 -06:00
// Clone that VM template
instance, _ := template.Clone(config.InstanceName)
instance.SetIsATemplate(false)
instance.SetStaticMemoryRange(config.InstanceMemory, config.InstanceMemory)
instance.SetPlatform(config.PlatformArgs)
2014-12-08 09:34:48 -06:00
// Create VDI for the instance
var sr *SR
2014-12-08 09:34:48 -06:00
if config.SrName == "" {
// Find the default SR
default_sr, err := client.GetDefaultSR()
sr = default_sr
2014-12-08 09:34:48 -06:00
if err != nil {
log.Fatal(fmt.Sprintf("Error getting default SR: %s", err.Error()))
return multistep.ActionHalt
}
2014-12-08 09:34:48 -06:00
} else {
// Use the provided name label to find the SR to use
srs, err := client.GetSRByNameLabel(config.SrName)
2014-12-08 09:34:48 -06:00
if err != nil {
log.Fatal(fmt.Sprintf("Error getting default SR: %s", err.Error()))
return multistep.ActionHalt
}
2014-12-08 09:34:48 -06:00
switch {
case len(srs) == 0:
log.Fatal(fmt.Sprintf("Couldn't find a SR with the specified name-label '%s'. Aborting.", config.SrName))
return multistep.ActionHalt
case len(srs) > 1:
log.Fatal(fmt.Sprintf("Found more than one SR with the name '%s'. The name must be unique. Aborting.", config.SrName))
return multistep.ActionHalt
}
2014-12-08 09:34:48 -06:00
sr = srs[0]
}
2014-12-08 09:34:48 -06:00
vdi, _ := sr.CreateVdi("Packer-disk", config.RootDiskSize)
2014-12-08 09:34:48 -06:00
instance.ConnectVdi(vdi, false)
2014-12-08 09:34:48 -06:00
// Connect Network
2014-12-08 09:34:48 -06:00
var network *Network
2014-12-08 09:34:48 -06:00
if config.NetworkName == "" {
// No network has be specified. Use the management interface
network = new(Network)
network.Ref = ""
network.Client = &client
2014-12-08 09:34:48 -06:00
pifs, err := client.GetPIFs()
2014-12-08 09:34:48 -06:00
if err != nil {
log.Fatal(fmt.Sprintf("Error getting PIFs %s", err.Error()))
return multistep.ActionHalt
}
2014-12-08 09:34:48 -06:00
for _, pif := range pifs {
pif_rec, err := pif.GetRecord()
2014-12-08 09:34:48 -06:00
if err != nil {
log.Fatal(fmt.Sprintf("Error getting PIF record: %s", err.Error()))
return multistep.ActionHalt
}
2014-12-08 09:34:48 -06:00
if pif_rec["management"].(bool) {
network.Ref = pif_rec["network"].(string)
}
2014-12-08 09:34:48 -06:00
}
2014-12-08 09:34:48 -06:00
if network.Ref == "" {
log.Fatal("Error: couldn't find management network. Aborting.")
return multistep.ActionHalt
}
} else {
// Look up the network by it's name label
2014-12-08 09:34:48 -06:00
networks, err := client.GetNetworkByNameLabel(config.NetworkName)
2014-12-08 09:34:48 -06:00
if err != nil {
log.Fatal(fmt.Sprintf("Error occured getting Network by name-label: %s", err.Error()))
return multistep.ActionHalt
}
switch {
case len(networks) == 0:
log.Fatal(fmt.Sprintf("Couldn't find a network with the specified name-label '%s'. Aborting.", config.NetworkName))
return multistep.ActionHalt
case len(networks) > 1:
log.Fatal(fmt.Sprintf("Found more than one SR with the name '%s'. The name must be unique. Aborting.", config.NetworkName))
return multistep.ActionHalt
}
network = networks[0]
}
if err != nil {
ui.Say(err.Error())
}
_, err = instance.ConnectNetwork(network, "0")
if err != nil {
ui.Say(err.Error())
}
// Connect the ISO
//iso_vdi_uuid := state.Get("iso_vdi_uuid").(string)
2014-12-08 09:34:48 -06:00
isos, err := client.GetVdiByNameLabel(config.IsoName)
switch {
case len(isos) == 0:
log.Fatal(fmt.Sprintf("Couldn't find an ISO named '%s'. Aborting", config.IsoName))
return multistep.ActionHalt
case len(isos) > 1:
log.Fatal(fmt.Sprintf("Found more than one VDI with name '%s'. Name must be unique. Aborting.", config.IsoName))
return multistep.ActionHalt
}
2014-12-08 09:34:48 -06:00
iso := isos[0]
2014-12-08 09:34:48 -06:00
//iso, _ := client.GetVdiByUuid(config.IsoUuid)
//ui.Say("Using VDI: " + iso_vdi_uuid)
//iso, _ := client.GetVdiByUuid(iso_vdi_uuid)
instance.ConnectVdi(iso, true)
2014-12-08 09:34:48 -06:00
// Stash the VM reference
self.InstanceId, _ = instance.GetUuid()
state.Put("instance_uuid", self.InstanceId)
state.Put("instance", instance)
ui.Say(fmt.Sprintf("Created instance '%s'", self.InstanceId))
2014-12-08 09:34:48 -06:00
return multistep.ActionContinue
}
func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
2014-12-08 09:34:48 -06:00
// client := state.Get("client").(*XenAPIClient)
// config := state.Get("config").(config)
// ui := state.Get("ui").(packer.Ui)
2014-12-08 09:34:48 -06:00
// If instance hasn't been created, we have nothing to do.
if self.InstanceId == "" {
return
}
2014-12-08 09:34:48 -06:00
// @todo: destroy the created instance.
2014-12-08 09:34:48 -06:00
return
}