builder/amazon/*: wait for AMI to be ready in common, use it instance
This commit is contained in:
parent
072d7a647a
commit
8c3281405d
|
@ -0,0 +1,25 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mitchellh/goamz/ec2"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WaitForAMI waits for the given AMI ID to become ready.
|
||||||
|
func WaitForAMI(c *ec2.EC2, imageId string) error {
|
||||||
|
for {
|
||||||
|
imageResp, err := c.Images([]string{imageId}, ec2.NewFilter())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if imageResp.Images[0].State == "available" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Image in state %s, sleeping 2s before checking again",
|
||||||
|
imageResp.Images[0].State)
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,8 +5,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
"github.com/mitchellh/goamz/ec2"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"log"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
@ -57,25 +57,13 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
|
|
||||||
// Wait for the image to become ready
|
// Wait for the image to become ready
|
||||||
ui.Say("Waiting for AMI to become ready...")
|
ui.Say("Waiting for AMI to become ready...")
|
||||||
for {
|
if err := awscommon.WaitForAMI(ec2conn, createResp.ImageId); err != nil {
|
||||||
imageResp, err := ec2conn.Images([]string{createResp.ImageId}, ec2.NewFilter())
|
err := fmt.Errorf("Error waiting for AMI: %s", err)
|
||||||
if err != nil {
|
|
||||||
err := fmt.Errorf("Error querying images: %s", err)
|
|
||||||
state["error"] = err
|
state["error"] = err
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
if imageResp.Images[0].State == "available" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Image in state %s, sleeping 2s before checking again",
|
|
||||||
imageResp.Images[0].State)
|
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
"github.com/mitchellh/goamz/ec2"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
@ -52,6 +53,15 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction
|
||||||
amis[config.Region] = registerResp.ImageId
|
amis[config.Region] = registerResp.ImageId
|
||||||
state["amis"] = amis
|
state["amis"] = amis
|
||||||
|
|
||||||
|
// Wait for the image to become ready
|
||||||
|
ui.Say("Waiting for AMI to become ready...")
|
||||||
|
if err := awscommon.WaitForAMI(ec2conn, registerResp.ImageId); err != nil {
|
||||||
|
err := fmt.Errorf("Error waiting for AMI: %s", err)
|
||||||
|
state["error"] = err
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue