Skip to content

Commit

Permalink
add SourceImageChecksum
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxtof committed Jan 6, 2025
1 parent db4f267 commit 96eefad
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 26 deletions.
1 change: 1 addition & 0 deletions .web-docs/components/builder/nutanix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Sample:
- `source_image_name` (string) - Name of the image used as disk source.
- `source_image_uuid` (string) - UUID of the image used as disk source.
- `source_image_uri` (string) - URI of the image used as disk source (if image is not already on the cluster, it will download and store it before launching output image creation process).
- `source_image_checksum` (string) - Checksum SHA-256 of the image used as disk source (work only with `source_image_uri`).
- `source_image_delete` (bool) - Delete source image once build process is completed (default is false).
- `source_image_force` (bool) - Always download and replace source image even if already exist (default is false).
- `disk_size_gb` (number) - size of the disk (in gigabytes).
Expand Down
40 changes: 33 additions & 7 deletions builder/nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
//"errors"
"fmt"
"log"
"regexp"
"time"

"github.com/hashicorp/packer-plugin-sdk/common"
Expand Down Expand Up @@ -68,13 +69,14 @@ type ClusterConfig struct {
}

type VmDisk struct {
ImageType string `mapstructure:"image_type" json:"image_type" required:"false"`
SourceImageName string `mapstructure:"source_image_name" json:"source_image_name" required:"false"`
SourceImageUUID string `mapstructure:"source_image_uuid" json:"source_image_uuid" required:"false"`
SourceImageURI string `mapstructure:"source_image_uri" json:"source_image_uri" required:"false"`
SourceImageDelete bool `mapstructure:"source_image_delete" json:"source_image_delete" required:"false"`
SourceImageForce bool `mapstructure:"source_image_force" json:"source_image_force" required:"false"`
DiskSizeGB int64 `mapstructure:"disk_size_gb" json:"disk_size_gb" required:"false"`
ImageType string `mapstructure:"image_type" json:"image_type" required:"false"`
SourceImageName string `mapstructure:"source_image_name" json:"source_image_name" required:"false"`
SourceImageUUID string `mapstructure:"source_image_uuid" json:"source_image_uuid" required:"false"`
SourceImageURI string `mapstructure:"source_image_uri" json:"source_image_uri" required:"false"`
SourceImageChecksum string `mapstructure:"source_image_checksum" json:"source_image_checksum" required:"false"`
SourceImageDelete bool `mapstructure:"source_image_delete" json:"source_image_delete" required:"false"`
SourceImageForce bool `mapstructure:"source_image_force" json:"source_image_force" required:"false"`
DiskSizeGB int64 `mapstructure:"disk_size_gb" json:"disk_size_gb" required:"false"`
}

type VmNIC struct {
Expand Down Expand Up @@ -237,6 +239,22 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}
}

// Validate each disk
for index, disk := range c.VmConfig.VmDisks {

// Validate checksum only with uri
if disk.SourceImageChecksum != "" && disk.SourceImageURI == "" {
log.Printf("disk %d: Checksum work only with Source Image URI\n", index)
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("disk %d: source_image_checksum work only with source_image_uri", index))
}

// Validate checksum format
if disk.SourceImageChecksum != "" && !IsSHA256Checksum(disk.SourceImageChecksum) {
log.Printf("disk %d: Invalid checksum format", index)
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("disk %d: invalid checksum format", index))
}
}

if c.CommConfig.SSHPort == 0 {
log.Println("SSHPort not set, defaulting to 22")
c.CommConfig.SSHPort = 22
Expand All @@ -262,3 +280,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {

return warnings, nil
}

// IsSHA256Checksum validates if the input string is a valid SHA-256 checksum
func IsSHA256Checksum(input string) bool {
// Regular expression for a 64-character hexadecimal string
const sha256Regex = `^[a-fA-F0-9]{64}$`
match, _ := regexp.MatchString(sha256Regex, input)
return match
}
30 changes: 16 additions & 14 deletions builder/nutanix/config.hcl2spec.go

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

8 changes: 8 additions & 0 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,14 @@ func (d *NutanixDriver) CreateImageURL(ctx context.Context, disk VmDisk, vm VmCo
}
req.Spec.Resources.SourceURI = &disk.SourceImageURI

if disk.SourceImageChecksum != "" {

req.Spec.Resources.Checksum = &v3.Checksum{
ChecksumValue: &disk.SourceImageChecksum,
ChecksumAlgorithm: StringPtr("SHA_256"),
}
}

image, err := conn.V3.CreateImage(ctx, req)
if err != nil {
return nil, fmt.Errorf("error while creating image: %s", err.Error())
Expand Down
1 change: 1 addition & 0 deletions docs/builders/nutanix.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Sample:
- `source_image_name` (string) - Name of the image used as disk source.
- `source_image_uuid` (string) - UUID of the image used as disk source.
- `source_image_uri` (string) - URI of the image used as disk source (if image is not already on the cluster, it will download and store it before launching output image creation process).
- `source_image_checksum` (string) - Checksum SHA-256 of the image used as disk source (work only with `source_image_uri`).
- `source_image_delete` (bool) - Delete source image once build process is completed (default is false).
- `source_image_force` (bool) - Always download and replace source image even if already exist (default is false).
- `disk_size_gb` (number) - size of the disk (in gigabytes).
Expand Down
10 changes: 5 additions & 5 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
## Examples
Validate Manifests:
packer validate .
`packer validate .`

Creating CentOS from local Image and running Provisioner:
packer build -only nutanix.centos .
`packer build -only nutanix.centos .`

Creating Ubuntu from Upstream Image and running Provisioner:
packer build -only nutanix.ubuntu .
`packer build -only nutanix.ubuntu .`

Creating from ISO with Kickstart-File:
packer build -only nutanix.centos-kickstart .
`packer build -only nutanix.centos-kickstart .`

Windows Image (ISO Boot, VirtIO Drivers, cd_files)
packer build -only nutanix.windows .
`packer build -only nutanix.windows .`

0 comments on commit 96eefad

Please sign in to comment.