2018-07-14 00:25:26 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-15 23:35:02 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-07-14 00:25:26 -04:00
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
|
|
|
|
"github.com/gophercloud/gophercloud/pagination"
|
2018-07-15 23:35:02 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-07-14 00:25:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepSourceImageInfo struct {
|
2018-08-19 18:21:32 -04:00
|
|
|
SourceImage string
|
|
|
|
SourceImageName string
|
|
|
|
SourceImageOpts images.ListOpts
|
|
|
|
SourceMostRecent bool
|
2019-04-30 10:34:40 -04:00
|
|
|
SourceProperties map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func PropertiesSatisfied(image *images.Image, props *map[string]string) bool {
|
|
|
|
for key, value := range *props {
|
|
|
|
if image.Properties[key] != value {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-07-14 00:25:26 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepSourceImageInfo) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2018-07-14 00:25:26 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2018-09-21 04:35:15 -04:00
|
|
|
if s.SourceImage != "" {
|
|
|
|
state.Put("source_image", s.SourceImage)
|
|
|
|
|
2018-08-22 07:37:43 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-07-20 07:51:00 -04:00
|
|
|
client, err := config.imageV2Client()
|
2019-09-16 09:26:56 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error creating image client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-07-14 00:25:26 -04:00
|
|
|
|
2018-09-21 04:35:15 -04:00
|
|
|
if s.SourceImageName != "" {
|
|
|
|
s.SourceImageOpts = images.ListOpts{
|
|
|
|
Name: s.SourceImageName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 10:34:40 -04:00
|
|
|
log.Printf("Using Image Filters %+v", s.SourceImageOpts)
|
2018-07-14 00:25:26 -04:00
|
|
|
image := &images.Image{}
|
2019-05-02 04:43:56 -04:00
|
|
|
count := 0
|
2018-08-19 18:21:32 -04:00
|
|
|
err = images.List(client, s.SourceImageOpts).EachPage(func(page pagination.Page) (bool, error) {
|
2019-04-30 10:34:40 -04:00
|
|
|
imgs, err := images.ExtractImages(page)
|
2018-07-14 00:25:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-04-30 10:34:40 -04:00
|
|
|
|
2019-05-02 04:51:40 -04:00
|
|
|
for _, img := range imgs {
|
2019-04-30 10:34:40 -04:00
|
|
|
// Check if all Properties are satisfied
|
|
|
|
if PropertiesSatisfied(&img, &s.SourceProperties) {
|
|
|
|
count++
|
2019-05-02 04:43:56 -04:00
|
|
|
if count == 1 {
|
|
|
|
// Tentatively return this result.
|
|
|
|
*image = img
|
2019-04-30 10:34:40 -04:00
|
|
|
}
|
|
|
|
// Don't iterate over entries we will never use.
|
|
|
|
if count > 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 00:25:26 -04:00
|
|
|
|
2019-04-30 10:34:40 -04:00
|
|
|
switch count {
|
2019-05-02 04:43:56 -04:00
|
|
|
case 0: // Continue looking at next page.
|
|
|
|
return true, nil
|
|
|
|
case 1: // Maybe we're done, maybe there is another result in a later page and it is an error.
|
|
|
|
if s.SourceMostRecent {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-04-30 10:34:40 -04:00
|
|
|
return true, nil
|
2019-05-02 04:43:56 -04:00
|
|
|
default: // By now we should know if getting 2+ results is an error or not.
|
2018-08-19 18:21:32 -04:00
|
|
|
if s.SourceMostRecent {
|
2018-08-21 06:46:42 -04:00
|
|
|
return false, nil
|
2018-07-20 18:09:48 -04:00
|
|
|
}
|
2018-07-15 23:35:02 -04:00
|
|
|
return false, fmt.Errorf(
|
2019-05-02 04:43:56 -04:00
|
|
|
"Your query returned more than one result. Please try a more specific search, or set most_recent to true. Search filters: %+v properties %+v",
|
|
|
|
s.SourceImageOpts, s.SourceProperties)
|
2018-07-14 00:25:26 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-08-22 07:37:43 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error querying image: %s", err)
|
2018-08-21 06:46:42 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:37:43 -04:00
|
|
|
if image.ID == "" {
|
2019-05-02 04:43:56 -04:00
|
|
|
err := fmt.Errorf("No image was found matching filters: %+v properties %+v",
|
|
|
|
s.SourceImageOpts, s.SourceProperties)
|
2018-07-14 00:25:26 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Found Image ID: %s", image.ID))
|
|
|
|
|
2018-07-20 07:51:00 -04:00
|
|
|
state.Put("source_image", image.ID)
|
2018-07-14 00:25:26 -04:00
|
|
|
return multistep.ActionContinue
|
2018-07-15 23:35:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSourceImageInfo) Cleanup(state multistep.StateBag) {
|
|
|
|
// No cleanup required for backout
|
|
|
|
}
|