2018-05-24 06:20:22 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2018-05-24 06:20:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepCreateVolume struct {
|
|
|
|
UseBlockStorageVolume bool
|
|
|
|
VolumeName string
|
|
|
|
VolumeType string
|
|
|
|
VolumeAvailabilityZone string
|
|
|
|
volumeID string
|
|
|
|
doCleanup bool
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-05-24 06:20:22 -04:00
|
|
|
// Proceed only if block storage volume is required.
|
|
|
|
if !s.UseBlockStorageVolume {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-09-21 04:35:15 -04:00
|
|
|
sourceImage := state.Get("source_image").(string)
|
2018-05-24 06:20:22 -04:00
|
|
|
|
|
|
|
// We will need Block Storage and Image services clients.
|
|
|
|
blockStorageClient, err := config.blockStorageV3Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing block storage client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-12-18 17:59:54 -05:00
|
|
|
|
|
|
|
volumeSize := config.VolumeSize
|
2018-05-24 06:20:22 -04:00
|
|
|
|
|
|
|
// Get needed volume size from the source image.
|
2018-12-18 17:59:54 -05:00
|
|
|
if volumeSize == 0 {
|
|
|
|
imageClient, err := config.imageV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing image client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeSize, err = GetVolumeSize(imageClient, sourceImage)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating volume: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-05-24 06:20:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating volume...")
|
|
|
|
volumeOpts := volumes.CreateOpts{
|
|
|
|
Size: volumeSize,
|
|
|
|
VolumeType: s.VolumeType,
|
|
|
|
AvailabilityZone: s.VolumeAvailabilityZone,
|
|
|
|
Name: s.VolumeName,
|
2018-09-21 04:35:15 -04:00
|
|
|
ImageID: sourceImage,
|
2019-10-02 18:59:13 -04:00
|
|
|
Metadata: config.ImageMetadata,
|
2018-05-24 06:20:22 -04:00
|
|
|
}
|
|
|
|
volume, err := volumes.Create(blockStorageClient, volumeOpts).Extract()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating volume: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-05-24 07:15:03 -04:00
|
|
|
// Wait for volume to become available.
|
|
|
|
ui.Say(fmt.Sprintf("Waiting for volume %s (volume id: %s) to become available...", config.VolumeName, volume.ID))
|
2018-05-24 06:20:22 -04:00
|
|
|
if err := WaitForVolume(blockStorageClient, volume.ID); err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for volume: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Volume was created, so remember to clean it up.
|
|
|
|
s.doCleanup = true
|
|
|
|
|
|
|
|
// Set the Volume ID in the state.
|
|
|
|
ui.Message(fmt.Sprintf("Volume ID: %s", volume.ID))
|
|
|
|
state.Put("volume_id", volume.ID)
|
|
|
|
s.volumeID = volume.ID
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
|
|
|
|
if !s.doCleanup {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-05-24 06:20:22 -04:00
|
|
|
|
|
|
|
blockStorageClient, err := config.blockStorageV3Client()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error cleaning up volume. Please delete the volume manually: %s", s.volumeID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-13 05:29:24 -04:00
|
|
|
// Wait for volume to become available.
|
|
|
|
status, err := GetVolumeStatus(blockStorageClient, s.volumeID)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error getting the volume information. Please delete the volume manually: %s", s.volumeID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != "available" {
|
|
|
|
ui.Say(fmt.Sprintf(
|
|
|
|
"Waiting for volume %s (volume id: %s) to become available...", s.VolumeName, s.volumeID))
|
|
|
|
if err := WaitForVolume(blockStorageClient, s.volumeID); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error getting the volume information. Please delete the volume manually: %s", s.volumeID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 06:20:22 -04:00
|
|
|
ui.Say(fmt.Sprintf("Deleting volume: %s ...", s.volumeID))
|
2019-06-28 02:58:08 -04:00
|
|
|
err = volumes.Delete(blockStorageClient, s.volumeID, volumes.DeleteOpts{}).ExtractErr()
|
2018-05-24 06:20:22 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error cleaning up volume. Please delete the volume manually: %s", s.volumeID))
|
|
|
|
}
|
|
|
|
}
|