packer-cn/builder/azure/chroot/step_create_shared_image_ve...

109 lines
3.5 KiB
Go
Raw Normal View History

2020-03-25 17:55:37 -04:00
package chroot
import (
"context"
"fmt"
"log"
"sort"
"time"
2020-03-25 17:55:37 -04:00
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
2020-03-25 17:55:37 -04:00
"github.com/Azure/go-autorest/autorest/to"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
2020-03-25 17:55:37 -04:00
"github.com/hashicorp/packer/builder/azure/common/client"
)
type StepCreateSharedImageVersion struct {
Destination SharedImageGalleryDestination
OSDiskCacheType string
DataDiskCacheType string
Location string
2020-03-25 17:55:37 -04:00
}
func (s *StepCreateSharedImageVersion) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
azcli := state.Get("azureclient").(client.AzureClientSet)
ui := state.Get("ui").(packersdk.Ui)
snapshotset := state.Get(stateBagKey_Snapshotset).(Diskset)
2020-03-25 17:55:37 -04:00
ui.Say(fmt.Sprintf("Creating image version %s\n using %q for os disk.",
2020-03-25 17:55:37 -04:00
s.Destination.ResourceID(azcli.SubscriptionID()),
snapshotset.OS()))
2020-03-25 17:55:37 -04:00
var targetRegions []compute.TargetRegion
// transform target regions to API objects
for _, tr := range s.Destination.TargetRegions {
apiObject := compute.TargetRegion{
Name: to.StringPtr(tr.Name),
RegionalReplicaCount: to.Int32Ptr(tr.ReplicaCount),
StorageAccountType: compute.StorageAccountType(tr.StorageAccountType),
}
targetRegions = append(targetRegions, apiObject)
}
imageVersion := compute.GalleryImageVersion{
Location: to.StringPtr(s.Location),
GalleryImageVersionProperties: &compute.GalleryImageVersionProperties{
StorageProfile: &compute.GalleryImageVersionStorageProfile{
OsDiskImage: &compute.GalleryOSDiskImage{
Source: &compute.GalleryArtifactVersionSource{ID: to.StringPtr(snapshotset.OS().String())},
2020-03-25 17:55:37 -04:00
HostCaching: compute.HostCaching(s.OSDiskCacheType),
},
},
PublishingProfile: &compute.GalleryImageVersionPublishingProfile{
TargetRegions: &targetRegions,
ExcludeFromLatest: to.BoolPtr(s.Destination.ExcludeFromLatest),
},
},
}
var datadisks []compute.GalleryDataDiskImage
for lun, resource := range snapshotset {
if lun != -1 {
2020-04-29 18:10:48 -04:00
ui.Say(fmt.Sprintf(" using %q for data disk (lun %d).", resource, lun))
datadisks = append(datadisks, compute.GalleryDataDiskImage{
Lun: to.Int32Ptr(lun),
Source: &compute.GalleryArtifactVersionSource{ID: to.StringPtr(resource.String())},
HostCaching: compute.HostCaching(s.DataDiskCacheType),
})
}
}
if datadisks != nil {
// sort by lun
sort.Slice(datadisks, func(i, j int) bool {
return *datadisks[i].Lun < *datadisks[j].Lun
})
imageVersion.GalleryImageVersionProperties.StorageProfile.DataDiskImages = &datadisks
}
2020-03-25 17:55:37 -04:00
f, err := azcli.GalleryImageVersionsClient().CreateOrUpdate(
ctx,
s.Destination.ResourceGroup,
s.Destination.GalleryName,
s.Destination.ImageName,
s.Destination.ImageVersion,
imageVersion)
if err == nil {
log.Println("Shared image version creation in process...")
pollClient := azcli.PollClient()
pollClient.PollingDelay = 10 * time.Second
ctx, cancel := context.WithTimeout(ctx, time.Hour*12)
defer cancel()
err = f.WaitForCompletionRef(ctx, pollClient)
2020-03-25 17:55:37 -04:00
}
if err != nil {
log.Printf("StepCreateSharedImageVersion.Run: error: %+v", err)
err := fmt.Errorf(
"error creating shared image version '%s': %v", s.Destination.ResourceID(azcli.SubscriptionID()), err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Printf("Image creation complete: %s", f.Status())
return multistep.ActionContinue
}
func (*StepCreateSharedImageVersion) Cleanup(multistep.StateBag) {}