Files
Packer-Cn/builder/azure/chroot/step_create_image.go
T

113 lines
3.3 KiB
Go
Raw Normal View History

2019-05-31 20:02:25 +00:00
package chroot
import (
"context"
"fmt"
2019-12-17 11:25:56 +01:00
"log"
2020-04-29 20:27:33 +00:00
"sort"
2019-12-17 11:25:56 +01:00
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
2019-05-31 20:02:25 +00:00
"github.com/Azure/go-autorest/autorest/azure"
2019-06-03 05:27:33 +00:00
"github.com/Azure/go-autorest/autorest/to"
2019-05-31 20:02:25 +00:00
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
var _ multistep.Step = &StepCreateImage{}
type StepCreateImage struct {
2020-04-29 20:27:33 +00:00
ImageResourceID string
ImageOSState string
OSDiskStorageAccountType string
OSDiskCacheType string
DataDiskStorageAccountType string
DataDiskCacheType string
Location string
2019-05-31 20:02:25 +00:00
}
func (s *StepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
azcli := state.Get("azureclient").(client.AzureClientSet)
ui := state.Get("ui").(packer.Ui)
2020-04-15 22:58:06 +00:00
diskset := state.Get(stateBagKey_Diskset).(Diskset)
diskResourceID := diskset.OS().String()
2019-05-31 20:02:25 +00:00
ui.Say(fmt.Sprintf("Creating image %s\n using %s for os disk.",
s.ImageResourceID,
diskResourceID))
2020-04-29 20:27:33 +00:00
imageResource, err := azure.ParseResourceID(s.ImageResourceID)
2019-05-31 20:02:25 +00:00
if err != nil {
log.Printf("StepCreateImage.Run: error: %+v", err)
err := fmt.Errorf(
"error parsing image resource id '%s': %v", s.ImageResourceID, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
image := compute.Image{
2019-06-03 05:27:33 +00:00
Location: to.StringPtr(s.Location),
2019-05-31 20:02:25 +00:00
ImageProperties: &compute.ImageProperties{
StorageProfile: &compute.ImageStorageProfile{
OsDisk: &compute.ImageOSDisk{
OsState: compute.OperatingSystemStateTypes(s.ImageOSState),
2019-06-03 08:33:31 +00:00
OsType: compute.Linux,
2019-05-31 20:02:25 +00:00
ManagedDisk: &compute.SubResource{
ID: &diskResourceID,
},
2019-06-03 08:33:31 +00:00
StorageAccountType: compute.StorageAccountTypes(s.OSDiskStorageAccountType),
Caching: compute.CachingTypes(s.OSDiskCacheType),
2019-05-31 20:02:25 +00:00
},
// DataDisks: nil,
// ZoneResilient: nil,
},
},
// Tags: nil,
}
2020-04-29 20:27:33 +00:00
var datadisks []compute.ImageDataDisk
for lun, resource := range diskset {
if lun != -1 {
2020-04-29 22:10:48 +00:00
ui.Say(fmt.Sprintf(" using %q for data disk (lun %d).", resource, lun))
2020-04-29 20:27:33 +00:00
datadisks = append(datadisks, compute.ImageDataDisk{
Lun: to.Int32Ptr(lun),
ManagedDisk: &compute.SubResource{ID: to.StringPtr(resource.String())},
StorageAccountType: compute.StorageAccountTypes(s.DataDiskStorageAccountType),
Caching: compute.CachingTypes(s.DataDiskCacheType),
})
}
}
if datadisks != nil {
sort.Slice(datadisks, func(i, j int) bool {
return *datadisks[i].Lun < *datadisks[j].Lun
})
image.ImageProperties.StorageProfile.DataDisks = &datadisks
}
2019-05-31 20:02:25 +00:00
f, err := azcli.ImagesClient().CreateOrUpdate(
ctx,
2020-04-29 20:27:33 +00:00
imageResource.ResourceGroup,
imageResource.ResourceName,
2019-05-31 20:02:25 +00:00
image)
if err == nil {
2019-06-03 05:27:33 +00:00
log.Println("Image creation in process...")
2019-05-31 20:02:25 +00:00
err = f.WaitForCompletionRef(ctx, azcli.PollClient())
}
if err != nil {
log.Printf("StepCreateImage.Run: error: %+v", err)
err := fmt.Errorf(
"error creating image '%s': %v", s.ImageResourceID, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
2019-06-03 05:27:33 +00:00
log.Printf("Image creation complete: %s", f.Status())
2019-05-31 20:02:25 +00:00
return multistep.ActionContinue
}
2019-06-03 05:27:33 +00:00
func (*StepCreateImage) Cleanup(bag multistep.StateBag) {} // this is the final artifact, don't delete