2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"time"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/gophercloud-fork-40444fb"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateImage struct{}
|
|
|
|
|
2013-08-31 15:37:07 -04:00
|
|
|
func (s *stepCreateImage) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
csp := state.Get("csp").(gophercloud.CloudServersProvider)
|
2015-05-27 16:02:57 -04:00
|
|
|
config := state.Get("config").(Config)
|
2013-08-31 15:37:07 -04:00
|
|
|
server := state.Get("server").(*gophercloud.Server)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
// Create the image
|
|
|
|
ui.Say(fmt.Sprintf("Creating the image: %s", config.ImageName))
|
|
|
|
createOpts := gophercloud.CreateImage{
|
|
|
|
Name: config.ImageName,
|
|
|
|
}
|
|
|
|
imageId, err := csp.CreateImage(server.Id, createOpts)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the Image ID in the state
|
|
|
|
ui.Say(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
|
|
|
|
ui.Say("Waiting for image to become ready...")
|
2013-08-28 01:08:18 -04:00
|
|
|
if err := WaitForImage(csp, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2013-08-28 01:08:18 -04:00
|
|
|
func WaitForImage(csp gophercloud.CloudServersProvider, imageId string) error {
|
2013-08-27 00:57:23 -04:00
|
|
|
for {
|
|
|
|
image, err := csp.ImageById(imageId)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|