packer-cn/builder/openstack/step_create_image.go

94 lines
2.6 KiB
Go
Raw Normal View History

package openstack
import (
"fmt"
"log"
"time"
2016-11-27 19:59:26 -05:00
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/images"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
type stepCreateImage struct{}
2013-08-31 15:37:07 -04:00
func (s *stepCreateImage) Run(state multistep.StateBag) multistep.StepAction {
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)
2015-06-12 00:16:43 -04:00
// We need the v2 compute client
client, err := config.computeV2Client()
if err != nil {
err = fmt.Errorf("Error initializing compute client: %s", err)
state.Put("error", err)
return multistep.ActionHalt
}
// Create the image
ui.Say(fmt.Sprintf("Creating the image: %s", config.ImageName))
2015-06-12 00:16:43 -04:00
imageId, err := servers.CreateImage(client, server.ID, servers.CreateImageOpts{
Name: config.ImageName,
Metadata: config.ImageMetadata,
2015-06-12 00:16:43 -04:00
}).ExtractImageID()
if err != nil {
err := fmt.Errorf("Error creating image: %s", err)
2013-08-31 15:37:07 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// 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)
// Wait for the image to become ready
ui.Say(fmt.Sprintf("Waiting for image %s (image id: %s) to become ready...", config.ImageName, imageId))
2015-06-12 00:16:43 -04:00
if err := WaitForImage(client, imageId); err != nil {
err := fmt.Errorf("Error waiting for image: %s", err)
2013-08-31 15:37:07 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
2013-08-31 15:37:07 -04:00
func (s *stepCreateImage) Cleanup(multistep.StateBag) {
// No cleanup...
}
// WaitForImage waits for the given Image ID to become ready.
2015-06-12 00:16:43 -04:00
func WaitForImage(client *gophercloud.ServiceClient, imageId string) error {
maxNumErrors := 10
numErrors := 0
for {
2015-06-12 00:16:43 -04:00
image, err := images.Get(client, imageId).Extract()
if err != nil {
2016-11-27 19:59:26 -05:00
errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if ok && (errCode.Actual == 500 || errCode.Actual == 404) {
numErrors++
if numErrors >= maxNumErrors {
log.Printf("[ERROR] Maximum number of errors (%d) reached; failing with: %s", numErrors, err)
return err
}
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
}
return err
}
if image.Status == "ACTIVE" {
return nil
}
log.Printf("Waiting for image creation status: %s (%d%%)", image.Status, image.Progress)
time.Sleep(2 * time.Second)
}
}