Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload image only to dedicated PE Cluster #13

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ Temporary Items
.apdisk

### VisualStudioCode ###
.vscode/*
.vscode/*
37 changes: 29 additions & 8 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Driver interface {
//GetImage(string) (*nutanixImage, error)
GetHost(string) (*nutanixHost, error)
PowerOff(string) error
UploadImage(string) (*nutanixImage, error)
UploadImage(string, VmConfig) (*nutanixImage, error)
DeleteImage(string) error
SaveVMDisk(string, string) (*nutanixImage, error)
WaitForShutdown(string, <-chan struct{}) bool
Expand Down Expand Up @@ -321,7 +321,7 @@ func (d *NutanixDriver) CreateRequest(vm VmConfig) (*v3.VMIntentInput, error) {
} else if vm.ClusterName != "" {
cluster, err = findClusterByName(conn, vm.ClusterName)
if err != nil {
return nil, fmt.Errorf("subnet not found %s", vm.ClusterName)
return nil, fmt.Errorf("cluster not found %s", vm.ClusterName)
}
}

Expand Down Expand Up @@ -423,8 +423,8 @@ func (d *NutanixDriver) Delete(vmUUID string) error {
return nil
}

//UploadImage (string) (*nutanixImage, error)
func (d *NutanixDriver) UploadImage(imagePath string) (*nutanixImage, error) {
//UploadImage (string, VmConfig) (*nutanixImage, error)
func (d *NutanixDriver) UploadImage(imagePath string, vm VmConfig) (*nutanixImage, error) {
configCreds := client.Credentials{
URL: fmt.Sprintf("%s:%d", d.ClusterConfig.Endpoint, d.ClusterConfig.Port),
Endpoint: d.ClusterConfig.Endpoint,
Expand All @@ -441,10 +441,28 @@ func (d *NutanixDriver) UploadImage(imagePath string) (*nutanixImage, error) {

_, file := path.Split(imagePath)

cluster := &v3.ClusterIntentResponse{}
if vm.ClusterUUID != "" {
cluster, err = conn.V3.GetCluster(vm.ClusterUUID)
if err != nil {
return nil, fmt.Errorf("cluster not found %s", vm.ClusterUUID)
}
} else if vm.ClusterName != "" {
cluster, err = findClusterByName(conn, vm.ClusterName)
if err != nil {
return nil, fmt.Errorf("cluster not found %s", vm.ClusterName)
}
}

refvalue := BuildReferenceValue(*cluster.Metadata.UUID, "cluster")
InitialPlacementRef := []*v3.ReferenceValues{refvalue}
req := &v3.ImageIntentInput{
Spec: &v3.Image{
Name: &file,
Resources: &v3.ImageResources{},
Name: &file,
Resources: &v3.ImageResources{
ImageType: StringPtr("ISO_IMAGE"),
InitialPlacementRefList: InitialPlacementRef,
},
},
Metadata: &v3.Metadata{
Kind: StringPtr("image"),
Expand All @@ -460,7 +478,7 @@ func (d *NutanixDriver) UploadImage(imagePath string) (*nutanixImage, error) {
for {
running, err := conn.V3.GetImage(*image.Metadata.UUID)
if err != nil {
log.Printf("Error while image create, %s", err.Error())
log.Printf("Error while retrieve image create status, %s", err.Error())
return nil, err
}
log.Printf("Creating Image: %s", *running.Status.State)
Expand All @@ -478,7 +496,7 @@ func (d *NutanixDriver) UploadImage(imagePath string) (*nutanixImage, error) {
for {
running, err := conn.V3.GetImage(*image.Metadata.UUID)
if err != nil {
log.Printf("Error while upload, %s", err.Error())
log.Printf("Error while retrieve upload status, %s", err.Error())
return nil, err
}
if *running.Status.State == "COMPLETE" {
Expand All @@ -502,10 +520,12 @@ func (d *NutanixDriver) DeleteImage(imageUUID string) error {

conn, err := v3.NewV3Client(configCreds)
if err != nil {
log.Printf("Error while creating new client connection, %s", err.Error())
return err
}
_, err = conn.V3.DeleteImage(imageUUID)
if err != nil {
log.Printf("Error while deleting image, %s", err.Error())
return err
}
return nil
Expand Down Expand Up @@ -617,6 +637,7 @@ func (d *NutanixDriver) SaveVMDisk(diskUUID string, imageName string) (*nutanixI
if err != nil {
return nil, err
}

req := &v3.ImageIntentInput{
Spec: &v3.Image{
Name: &imageName,
Expand Down
10 changes: 6 additions & 4 deletions builder/nutanix/step_build_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ func (s *stepBuildVM) Run(ctx context.Context, state multistep.StateBag) multist
config := state.Get("config").(*Config)

// Determine if we even have a cd_files disk to attach
log.Println("Check for temporary iso-disks to attach")
if cdPathRaw, ok := state.GetOk("cd_path"); ok {
cdFilesPath := cdPathRaw.(string)
log.Println("cdrom found, "+cdFilesPath)
cdfilesImage, err := d.UploadImage(cdFilesPath)
log.Println("temporary iso found, "+cdFilesPath)
cdfilesImage, err := d.UploadImage(cdFilesPath, config.VmConfig)
if err != nil {
ui.Error("Error uploading image"+ err.Error())
ui.Error("Error uploading temporary image:")
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say("Temporary ISO uploaded")
Expand All @@ -37,7 +39,7 @@ func (s *stepBuildVM) Run(ctx context.Context, state multistep.StateBag) multist
}
config.VmConfig.VmDisks=append(config.VmConfig.VmDisks,temp_cd)
} else {
log.Println("No cdrom, not attaching.")
log.Println("No temporary iso, not attaching.")
}

//CreateRequest()
Expand Down
4 changes: 2 additions & 2 deletions builder/nutanix/step_copy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
d := state.Get("driver").(Driver)
vm, _ := d.GetVM(vmUUID)

ui.Say("Saving VM for uuid: " + vmUUID)
ui.Say("Creating image for uuid: " + vmUUID)

ui.Message("Initiatiating save VM DISK task.")
// Choose disk to replicate - looking for first "DISK"
Expand All @@ -46,7 +46,7 @@ func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multi
state.Put("error", err)
return multistep.ActionHalt
}
ui.Message("Successfully saved vm disk: " + *imageResponse.image.Metadata.UUID)
ui.Message("Successfully created image: " + *imageResponse.image.Metadata.UUID)
state.Put("vm_disk_uuid", (*imageResponse.image.Metadata.UUID))
return multistep.ActionContinue
}
Expand Down
8 changes: 8 additions & 0 deletions builder/nutanix/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ func BuildReference(uuid, kind string) *v3.Reference {
UUID: StringPtr(uuid),
}
}

// BuildReferenceValue create referencevalue from defined object
func BuildReferenceValue(uuid, kind string) *v3.ReferenceValues {
return &v3.ReferenceValues{
Kind: kind,
UUID: uuid,
}
}