2020-03-25 14:24:18 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/client"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-03-25 14:24:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepResolvePlatformImageVersion resolves the exact PIR version when the version is 'latest'
|
|
|
|
type StepResolvePlatformImageVersion struct {
|
|
|
|
*client.PlatformImage
|
|
|
|
Location string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run retrieves all available versions of a PIR image and stores the latest in the PlatformImage
|
|
|
|
func (pi *StepResolvePlatformImageVersion) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-03-25 14:24:18 -04:00
|
|
|
|
|
|
|
if strings.EqualFold(pi.Version, "latest") {
|
|
|
|
azcli := state.Get("azureclient").(client.AzureClientSet)
|
|
|
|
|
|
|
|
vmi, err := azcli.VirtualMachineImagesClient().GetLatest(ctx, pi.Publisher, pi.Offer, pi.Sku, pi.Location)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("StepResolvePlatformImageVersion.Run: error: %+v", err)
|
|
|
|
err := fmt.Errorf("error retieving latest version of %q: %v", pi.URN(), err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
pi.Version = to.String(vmi.Name)
|
|
|
|
ui.Say("Resolved latest version of source image: " + pi.Version)
|
|
|
|
} else {
|
|
|
|
ui.Say("Nothing to do, version is not 'latest'")
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepResolvePlatformImageVersion) Cleanup(multistep.StateBag) {}
|