2018-07-14 00:25:26 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-15 23:35:02 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-09-25 08:59:00 -04:00
|
|
|
"time"
|
2018-07-14 00:25:26 -04:00
|
|
|
|
2020-09-25 08:59:00 -04:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/imageimport"
|
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 {
|
2020-09-25 08:59:00 -04:00
|
|
|
SourceImage string
|
|
|
|
SourceImageName string
|
|
|
|
ExternalSourceImageURL string
|
|
|
|
ExternalSourceImageFormat string
|
|
|
|
SourceImageOpts images.ListOpts
|
|
|
|
SourceMostRecent bool
|
|
|
|
SourceProperties map[string]string
|
2019-04-30 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
2020-09-25 08:59:00 -04:00
|
|
|
if s.ExternalSourceImageURL != "" {
|
|
|
|
createOpts := images.CreateOpts{
|
|
|
|
Name: s.SourceImageName,
|
|
|
|
ContainerFormat: "bare",
|
|
|
|
DiskFormat: s.ExternalSourceImageFormat,
|
|
|
|
Properties: map[string]string{
|
|
|
|
"packer_external_source_image_url": s.ExternalSourceImageURL,
|
|
|
|
"packer_external_source_image_format": s.ExternalSourceImageFormat,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating image using external source image with name " + s.SourceImageName)
|
|
|
|
ui.Say("Using disk format " + s.ExternalSourceImageFormat)
|
|
|
|
image, err := images.Create(client, createOpts).Extract()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating source image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Created image with ID " + image.ID)
|
|
|
|
|
|
|
|
importOpts := imageimport.CreateOpts{
|
|
|
|
Name: imageimport.WebDownloadMethod,
|
|
|
|
URI: s.ExternalSourceImageURL,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Importing External Source Image from URL " + s.ExternalSourceImageURL)
|
|
|
|
err = imageimport.Create(client, image.ID, importOpts).ExtractErr()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error importing source image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
for image.Status != images.ImageStatusActive {
|
|
|
|
ui.Message("Image not Active, retrying in 10 seconds")
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
|
|
|
img, err := images.Get(client, image.ID).Extract()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error querying image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
image = img
|
|
|
|
}
|
|
|
|
|
|
|
|
s.SourceImage = image.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.SourceImage != "" {
|
|
|
|
state.Put("source_image", s.SourceImage)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-09-25 08:59:00 -04:00
|
|
|
if s.ExternalSourceImageURL != "" {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
client, err := config.imageV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error creating image client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Deleting temporary external source image: %s ...", s.SourceImageName))
|
|
|
|
err = images.Delete(client, s.SourceImage).ExtractErr()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error cleaning up external source image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-07-15 23:35:02 -04:00
|
|
|
}
|