Skip to content

Commit

Permalink
artefact identification improvement (#71)
Browse files Browse the repository at this point in the history
- add vm description
- add downloaded image description
- add customizable image description
- add Image Name in sucesfull message
  • Loading branch information
tuxtof authored Nov 21, 2022
1 parent 002f1c1 commit 390ad16
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions builder/nutanix/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
// builder.go
func (b *Builder) newDriver(cConfig ClusterConfig) (Driver, error) {
driver := &NutanixDriver{
Config: b.config,
ClusterConfig: cConfig,
}

Expand Down
3 changes: 2 additions & 1 deletion builder/nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type Config struct {
shutdowncommand.ShutdownConfig `mapstructure:",squash"`
ClusterConfig `mapstructure:",squash"`
VmConfig `mapstructure:",squash"`
ForceDeregister bool `mapstructure:"force_deregister" json:"force_deregister" required:"false"`
ForceDeregister bool `mapstructure:"force_deregister" json:"force_deregister" required:"false"`
ImageDescription string `mapstructure:"image_description" json:"image_description" required:"false"`

ctx interpolate.Context
}
Expand Down
2 changes: 2 additions & 0 deletions builder/nutanix/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
v3 "github.com/nutanix-cloud-native/prism-go-client/pkg/nutanix/v3"
)

const (
defaultImageBuiltDescription = "built by Packer"
defaultImageDLDescription = "added by Packer"
vmDescription = "Packer vm building image %s"
)

// A driver is able to talk to Nutanix PrismCentral and perform certain
// operations with it.
type Driver interface {
Expand All @@ -25,11 +31,12 @@ type Driver interface {
UploadImage(string, string, string, VmConfig) (*nutanixImage, error)
DeleteImage(string) error
GetImage(string) (*nutanixImage, error)
SaveVMDisk(string, string, bool) (*nutanixImage, error)
SaveVMDisk(string) (*nutanixImage, error)
WaitForShutdown(string, <-chan struct{}) bool
}

type NutanixDriver struct {
Config Config
ClusterConfig ClusterConfig
vmEndCh <-chan int
}
Expand Down Expand Up @@ -378,6 +385,7 @@ func (d *NutanixDriver) CreateRequest(vm VmConfig) (*v3.VMIntentInput, error) {
NicList: NICList,
},
ClusterReference: BuildReference(*cluster.Metadata.UUID, "cluster"),
Description: StringPtr(fmt.Sprintf(vmDescription, d.Config.VmConfig.ImageName)),
},
Metadata: &v3.Metadata{
Kind: StringPtr("vm"),
Expand Down Expand Up @@ -527,6 +535,7 @@ func (d *NutanixDriver) UploadImage(imagePath string, sourceType string, imageTy
ImageType: &imageType,
InitialPlacementRefList: InitialPlacementRef,
},
Description: StringPtr(defaultImageDLDescription),
},
Metadata: &v3.Metadata{
Kind: StringPtr("image"),
Expand Down Expand Up @@ -713,7 +722,7 @@ func (d *NutanixDriver) PowerOff(vmUUID string) error {
log.Printf("PowerOff task: %s", taskUUID)
return nil
}
func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageName string, ForceDeregister bool) (*nutanixImage, error) {
func (d *NutanixDriver) SaveVMDisk(diskUUID string) (*nutanixImage, error) {

configCreds := client.Credentials{
URL: fmt.Sprintf("%s:%d", d.ClusterConfig.Endpoint, d.ClusterConfig.Port),
Expand All @@ -730,9 +739,9 @@ func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageName string, ForceDereg
}

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

imgDescription := defaultImageBuiltDescription
if d.Config.ImageDescription != "" {
imgDescription = d.Config.ImageDescription
}

req := &v3.ImageIntentInput{
Spec: &v3.Image{
Name: &imageName,
Name: &d.Config.VmConfig.ImageName,
Resources: &v3.ImageResources{
ImageType: StringPtr("DISK_IMAGE"),
DataSourceReference: BuildReference(diskUUID, "vm_disk"),
},
Description: StringPtr(imgDescription),
},
Metadata: &v3.Metadata{
Kind: StringPtr("image"),
Expand Down
5 changes: 3 additions & 2 deletions builder/nutanix/step_copy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nutanix
import (
"context"
"errors"
"fmt"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -40,13 +41,13 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
return multistep.ActionHalt
}

imageResponse, err := d.SaveVMDisk(diskToCopy, s.Config.VmConfig.ImageName, s.Config.ForceDeregister)
imageResponse, err := d.SaveVMDisk(diskToCopy)
if err != nil {
ui.Error("Unexpected Nutanix Task status: " + err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
ui.Message("Successfully created image: " + *imageResponse.image.Metadata.UUID)
ui.Message(fmt.Sprintf("Successfully created image: %s (%s)", *imageResponse.image.Spec.Name, *imageResponse.image.Metadata.UUID))
state.Put("vm_disk_uuid", (*imageResponse.image.Metadata.UUID))
return multistep.ActionContinue
}
Expand Down

0 comments on commit 390ad16

Please sign in to comment.