2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-08-27 00:57:23 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
2018-05-24 07:46:30 -04:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
|
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2018-11-25 14:30:31 -05:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
2018-05-24 07:46:30 -04:00
|
|
|
type stepCreateImage struct {
|
|
|
|
UseBlockStorageVolume bool
|
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2019-02-25 09:03:11 -05:00
|
|
|
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2015-06-12 00:16:43 -04:00
|
|
|
server := state.Get("server").(*servers.Server)
|
2013-08-31 15:37:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
// We need the v2 compute client
|
2018-11-25 14:30:31 -05:00
|
|
|
computeClient, err := config.computeV2Client()
|
2015-06-12 00:16:43 -04:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing compute client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-11-25 14:30:31 -05:00
|
|
|
// We need the v2 image client
|
|
|
|
imageClient, err := config.imageV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing image service client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-05-24 07:46:30 -04:00
|
|
|
// Create the image.
|
|
|
|
// Image source depends on the type of the Compute instance. It can be
|
|
|
|
// Block Storage service volume or regular Compute service local volume.
|
2013-08-27 00:57:23 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating the image: %s", config.ImageName))
|
2018-05-24 07:46:30 -04:00
|
|
|
var imageId string
|
2019-08-21 06:15:46 -04:00
|
|
|
var blockStorageClient *gophercloud.ServiceClient
|
2018-05-24 07:46:30 -04:00
|
|
|
if s.UseBlockStorageVolume {
|
|
|
|
// We need the v3 block storage client.
|
2019-08-22 11:20:46 -04:00
|
|
|
blockStorageClient, err = config.blockStorageV3Client()
|
2018-05-24 07:46:30 -04:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing block storage client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
volume := state.Get("volume_id").(string)
|
|
|
|
image, err := volumeactions.UploadImage(blockStorageClient, volume, volumeactions.UploadImageOpts{
|
2018-09-13 05:53:54 -04:00
|
|
|
DiskFormat: config.ImageDiskFormat,
|
2018-09-12 00:20:04 -04:00
|
|
|
ImageName: config.ImageName,
|
2018-05-24 07:46:30 -04:00
|
|
|
}).Extract()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
imageId = image.ImageID
|
|
|
|
} else {
|
2018-11-25 14:30:31 -05:00
|
|
|
imageId, err = servers.CreateImage(computeClient, server.ID, servers.CreateImageOpts{
|
2018-05-24 07:46:30 -04:00
|
|
|
Name: config.ImageName,
|
|
|
|
Metadata: config.ImageMetadata,
|
|
|
|
}).ExtractImageID()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the Image ID in the state
|
2015-06-12 00:16:43 -04:00
|
|
|
ui.Message(fmt.Sprintf("Image: %s", imageId))
|
2013-08-31 15:37:07 -04:00
|
|
|
state.Put("image", imageId)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
// Wait for the image to become ready
|
2015-12-17 14:12:52 -05:00
|
|
|
ui.Say(fmt.Sprintf("Waiting for image %s (image id: %s) to become ready...", config.ImageName, imageId))
|
2019-02-25 09:03:11 -05:00
|
|
|
if err := WaitForImage(ctx, imageClient, imageId); err != nil {
|
2013-08-27 00:57:23 -04:00
|
|
|
err := fmt.Errorf("Error waiting for image: %s", err)
|
2013-08-31 15:37:07 -04:00
|
|
|
state.Put("error", err)
|
2013-08-27 00:57:23 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-10-02 16:16:18 -04:00
|
|
|
if s.UseBlockStorageVolume {
|
|
|
|
volume := state.Get("volume_id").(string)
|
|
|
|
if len(config.ImageMetadata) > 0 {
|
|
|
|
err = volumeactions.SetImageMetadata(blockStorageClient, volume, volumeactions.ImageMetadataOpts{
|
|
|
|
Metadata: config.ImageMetadata,
|
|
|
|
}).ExtractErr()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error setting image metadata: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-08-21 06:15:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:37:07 -04:00
|
|
|
func (s *stepCreateImage) Cleanup(multistep.StateBag) {
|
2013-08-27 00:57:23 -04:00
|
|
|
// No cleanup...
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForImage waits for the given Image ID to become ready.
|
2019-02-25 09:03:11 -05:00
|
|
|
func WaitForImage(ctx context.Context, client *gophercloud.ServiceClient, imageId string) error {
|
2015-12-18 13:25:55 -05:00
|
|
|
maxNumErrors := 10
|
|
|
|
numErrors := 0
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
for {
|
2019-02-25 09:03:11 -05:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
image, err := images.Get(client, imageId).Extract()
|
2013-08-27 00:57:23 -04:00
|
|
|
if err != nil {
|
2016-11-27 19:59:26 -05:00
|
|
|
errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
|
2015-12-18 13:37:31 -05:00
|
|
|
if ok && (errCode.Actual == 500 || errCode.Actual == 404) {
|
2015-12-18 13:25:55 -05:00
|
|
|
numErrors++
|
|
|
|
if numErrors >= maxNumErrors {
|
|
|
|
log.Printf("[ERROR] Maximum number of errors (%d) reached; failing with: %s", numErrors, err)
|
|
|
|
return err
|
|
|
|
}
|
2015-12-18 13:37:31 -05:00
|
|
|
log.Printf("[ERROR] %d error received, will ignore and retry: %s", errCode.Actual, err)
|
2015-06-12 00:16:43 -04:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-25 14:30:31 -05:00
|
|
|
if image.Status == "active" {
|
2013-08-27 00:57:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-25 14:30:31 -05:00
|
|
|
log.Printf("Waiting for image creation status: %s", image.Status)
|
2013-08-27 00:57:23 -04:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|