Skip to content

Commit 39df705

Browse files
authored
Merge pull request #137 from michaelw777/fix-add-wait-to-image-creation
fix: add wait to image creation
2 parents 019eba0 + d951f8d commit 39df705

5 files changed

Lines changed: 20 additions & 2 deletions

File tree

.web-docs/components/builder/openstack/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ builder.
317317

318318
- `use_floating_ip` (bool) - *Deprecated* use `floating_ip` or `floating_ip_pool` instead.
319319

320+
- `image_creation_wait` (int) - Additional wait time in seconds after the image reaches `active` status (default: 0).
321+
320322
<!-- End of code generated from the comments of the RunConfig struct in builder/openstack/run_config.go; -->
321323

322324

builder/openstack/builder.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/openstack/run_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ type RunConfig struct {
177177
OpenstackProvider string `mapstructure:"openstack_provider"`
178178
// *Deprecated* use `floating_ip` or `floating_ip_pool` instead.
179179
UseFloatingIp bool `mapstructure:"use_floating_ip" required:"false"`
180+
// Additional wait time in seconds after the image reaches `active` status (default: 0).
181+
ImageCreationWait int `mapstructure:"image_creation_wait" required:"false"`
180182

181183
sourceImageOpts images.ListOpts
182184
}

builder/openstack/step_create_image.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
105105

106106
// Wait for the image to become ready
107107
ui.Say(fmt.Sprintf("Waiting for image %s (image id: %s) to become ready...", config.ImageName, imageId))
108-
if err := WaitForImage(ctx, imageClient, imageId); err != nil {
108+
if err := WaitForImage(ctx, imageClient, imageId, config.ImageCreationWait); err != nil {
109109
err := fmt.Errorf("Error waiting for image: %s", err)
110110
state.Put("error", err)
111111
ui.Error(err.Error())
@@ -120,7 +120,7 @@ func (s *stepCreateImage) Cleanup(multistep.StateBag) {
120120
}
121121

122122
// WaitForImage waits for the given Image ID to become ready.
123-
func WaitForImage(ctx context.Context, client *gophercloud.ServiceClient, imageId string) error {
123+
func WaitForImage(ctx context.Context, client *gophercloud.ServiceClient, imageId string, imageCreationWait int) error {
124124
maxNumErrors := 10
125125
numErrors := 0
126126

@@ -146,6 +146,16 @@ func WaitForImage(ctx context.Context, client *gophercloud.ServiceClient, imageI
146146
}
147147

148148
if image.Status == "active" {
149+
if imageCreationWait > 0 {
150+
log.Printf("Additional wait (%d seconds) after image status has changed to active...", imageCreationWait)
151+
select {
152+
case <-time.After(time.Duration(imageCreationWait) * time.Second):
153+
// Timer finished, safe to continue
154+
case <-ctx.Done():
155+
// User cancelled during the wait, abort immediately
156+
return ctx.Err()
157+
}
158+
}
149159
return nil
150160
}
151161

docs-partials/builder/openstack/RunConfig-not-required.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,6 @@
9696

9797
- `use_floating_ip` (bool) - *Deprecated* use `floating_ip` or `floating_ip_pool` instead.
9898

99+
- `image_creation_wait` (int) - Additional wait time in seconds after the image reaches `active` status (default: 0).
100+
99101
<!-- End of code generated from the comments of the RunConfig struct in builder/openstack/run_config.go; -->

0 commit comments

Comments
 (0)