Skip to content

Commit

Permalink
fix multi disks vm image creation (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxtof authored Mar 21, 2023
1 parent 5e46212 commit df8ce13
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
2 changes: 1 addition & 1 deletion builder/nutanix/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
if imageUUID != nil {
artifact := &Artifact{
Name: b.config.ImageName,
UUID: imageUUID.(string),
UUID: imageUUID.([]string)[0],
}
return artifact, nil
}
Expand Down
13 changes: 9 additions & 4 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Driver interface {
UploadImage(string, string, string, VmConfig) (*nutanixImage, error)
DeleteImage(string) error
GetImage(string) (*nutanixImage, error)
SaveVMDisk(string, []Category) (*nutanixImage, error)
SaveVMDisk(string, int, []Category) (*nutanixImage, error)
WaitForShutdown(string, <-chan struct{}) bool
}

Expand Down Expand Up @@ -734,7 +734,7 @@ func (d *NutanixDriver) PowerOff(vmUUID string) error {
log.Printf("PowerOff task: %s", taskUUID)
return nil
}
func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageCategories []Category) (*nutanixImage, error) {
func (d *NutanixDriver) SaveVMDisk(diskUUID string, index int, imageCategories []Category) (*nutanixImage, error) {

configCreds := client.Credentials{
URL: fmt.Sprintf("%s:%d", d.ClusterConfig.Endpoint, d.ClusterConfig.Port),
Expand All @@ -750,10 +750,15 @@ func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageCategories []Category)
return nil, fmt.Errorf("error while NewV3Client, %s", err.Error())
}

name := d.Config.VmConfig.ImageName
if index > 0 {
name = fmt.Sprintf("%s-disk%d", name, index+1)
}

// When force_deregister, check if image already exists
if d.Config.ForceDeregister {
log.Println("force_deregister is set, check if image already exists")
ImageList, err := conn.V3.ListAllImage(fmt.Sprintf("name==%s", d.Config.VmConfig.ImageName))
ImageList, err := conn.V3.ListAllImage(fmt.Sprintf("name==%s", name))
if err != nil {
return nil, fmt.Errorf("error while ListAllImage, %s", err.Error())
}
Expand Down Expand Up @@ -785,7 +790,7 @@ func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageCategories []Category)

req := &v3.ImageIntentInput{
Spec: &v3.Image{
Name: &d.Config.VmConfig.ImageName,
Name: &name,
Resources: &v3.ImageResources{
ImageType: StringPtr("DISK_IMAGE"),
DataSourceReference: BuildReference(diskUUID, "vm_disk"),
Expand Down
47 changes: 28 additions & 19 deletions builder/nutanix/step_copy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,41 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
d := state.Get("driver").(Driver)
vm, _ := d.GetVM(vmUUID)

ui.Say(fmt.Sprintf("Creating image from virtual machine %s...", s.Config.VMName))
ui.Say(fmt.Sprintf("Creating image(s) from virtual machine %s...", s.Config.VMName))

// Choose disk to replicate - looking for first "DISK"
var diskToCopy string
var disksToCopy []string

for i := range vm.nutanix.Spec.Resources.DiskList {
if *vm.nutanix.Spec.Resources.DiskList[i].DeviceProperties.DeviceType == "DISK" {
diskToCopy = *vm.nutanix.Spec.Resources.DiskList[i].UUID
disksToCopy = append(disksToCopy, *vm.nutanix.Spec.Resources.DiskList[i].UUID)
diskID := fmt.Sprintf("%s:%d", *vm.nutanix.Spec.Resources.DiskList[i].DeviceProperties.DiskAddress.AdapterType, *vm.nutanix.Spec.Resources.DiskList[i].DeviceProperties.DiskAddress.DeviceIndex)
ui.Message("Found disk to copy: " + diskID)
break
}
}

if diskToCopy == "" {
if len(disksToCopy) == 0 {
err := errors.New("no DISK was found to save, halting build")
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}

imageResponse, err := d.SaveVMDisk(diskToCopy, s.Config.ImageCategories)
if err != nil {
ui.Error("Image creation failed: " + err.Error())
state.Put("error", err)
return multistep.ActionHalt
var imageList []string

for i, diskToCopy := range disksToCopy {

imageResponse, err := d.SaveVMDisk(diskToCopy, i, s.Config.ImageCategories)
if err != nil {
ui.Error("Image creation failed: " + err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
imageList = append(imageList, *imageResponse.image.Metadata.UUID)
ui.Message(fmt.Sprintf("Image successfully created: %s (%s)", *imageResponse.image.Spec.Name, *imageResponse.image.Metadata.UUID))
}
ui.Message(fmt.Sprintf("Successfully created image: %s (%s)", *imageResponse.image.Spec.Name, *imageResponse.image.Metadata.UUID))
state.Put("image_uuid", (*imageResponse.image.Metadata.UUID))

state.Put("image_uuid", imageList)
return multistep.ActionContinue
}

Expand All @@ -60,14 +66,17 @@ func (s *stepCopyImage) Cleanup(state multistep.StateBag) {
}

if imgUUID, ok := state.GetOk("image_uuid"); ok {
ui.Say(fmt.Sprintf("Deleting image %s...", s.Config.ImageName))
ui.Say(fmt.Sprintf("Deleting image(s) %s...", s.Config.ImageName))

err := d.DeleteImage(imgUUID.(string))
if err != nil {
ui.Error("An error occurred while deleting image")
return
} else {
ui.Message("Image successfully deleted")
for _, image := range imgUUID.([]string) {

err := d.DeleteImage(image)
if err != nil {
ui.Error("An error occurred while deleting image")
return
} else {
ui.Message(fmt.Sprintf("Image successfully deleted (%s)", image))
}
}
}
}
3 changes: 2 additions & 1 deletion test/e2e/centos-iso/scripts/ks.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ selinux --enforcing
bootloader --location=mbr --boot-drive=sda

# Setting up Logical Volume Manager and autopartitioning
clearpart --all --drives=sda --initlabel
clearpart --all --drives=sda,sdb --initlabel
ignoredisk --only-use=sda,sdb
autopart --type=lvm

# Eject cdrom and reboot
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/centos-iso/source.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ source "nutanix" "centos" {
disk_size_gb = 40
}

vm_disks {
image_type = "DISK"
disk_size_gb = 20
}

vm_nics {
subnet_name = var.nutanix_subnet
}
Expand Down

0 comments on commit df8ce13

Please sign in to comment.