2019-05-31 16:02:25 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-12-17 05:25:56 -05:00
|
|
|
"log"
|
2020-04-29 16:27:33 -04:00
|
|
|
"sort"
|
2019-12-17 05:25:56 -05:00
|
|
|
|
2020-04-23 05:03:17 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
|
2019-05-31 16:02:25 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
2019-06-03 01:27:33 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
2019-05-31 16:02:25 -04: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 16:27:33 -04:00
|
|
|
ImageResourceID string
|
|
|
|
ImageOSState string
|
|
|
|
OSDiskStorageAccountType string
|
|
|
|
OSDiskCacheType string
|
|
|
|
DataDiskStorageAccountType string
|
|
|
|
DataDiskCacheType string
|
|
|
|
Location string
|
2019-05-31 16:02:25 -04: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 18:58:06 -04:00
|
|
|
diskset := state.Get(stateBagKey_Diskset).(Diskset)
|
|
|
|
diskResourceID := diskset.OS().String()
|
2019-05-31 16:02:25 -04:00
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating image %s\n using %s for os disk.",
|
|
|
|
s.ImageResourceID,
|
|
|
|
diskResourceID))
|
|
|
|
|
2020-04-29 16:27:33 -04:00
|
|
|
imageResource, err := azure.ParseResourceID(s.ImageResourceID)
|
2019-05-31 16:02:25 -04: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 01:27:33 -04:00
|
|
|
Location: to.StringPtr(s.Location),
|
2019-05-31 16:02:25 -04:00
|
|
|
ImageProperties: &compute.ImageProperties{
|
|
|
|
StorageProfile: &compute.ImageStorageProfile{
|
|
|
|
OsDisk: &compute.ImageOSDisk{
|
|
|
|
OsState: compute.OperatingSystemStateTypes(s.ImageOSState),
|
2019-06-03 04:33:31 -04:00
|
|
|
OsType: compute.Linux,
|
2019-05-31 16:02:25 -04:00
|
|
|
ManagedDisk: &compute.SubResource{
|
|
|
|
ID: &diskResourceID,
|
|
|
|
},
|
2019-06-03 04:33:31 -04:00
|
|
|
StorageAccountType: compute.StorageAccountTypes(s.OSDiskStorageAccountType),
|
|
|
|
Caching: compute.CachingTypes(s.OSDiskCacheType),
|
2019-05-31 16:02:25 -04:00
|
|
|
},
|
|
|
|
// DataDisks: nil,
|
|
|
|
// ZoneResilient: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Tags: nil,
|
|
|
|
}
|
2020-04-29 16:27:33 -04:00
|
|
|
|
|
|
|
var datadisks []compute.ImageDataDisk
|
|
|
|
for lun, resource := range diskset {
|
|
|
|
if lun != -1 {
|
2020-04-29 18:10:48 -04:00
|
|
|
ui.Say(fmt.Sprintf(" using %q for data disk (lun %d).", resource, lun))
|
|
|
|
|
2020-04-29 16:27:33 -04: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 16:02:25 -04:00
|
|
|
f, err := azcli.ImagesClient().CreateOrUpdate(
|
|
|
|
ctx,
|
2020-04-29 16:27:33 -04:00
|
|
|
imageResource.ResourceGroup,
|
|
|
|
imageResource.ResourceName,
|
2019-05-31 16:02:25 -04:00
|
|
|
image)
|
|
|
|
if err == nil {
|
2019-06-03 01:27:33 -04:00
|
|
|
log.Println("Image creation in process...")
|
2019-05-31 16:02:25 -04: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 01:27:33 -04:00
|
|
|
log.Printf("Image creation complete: %s", f.Status())
|
2019-05-31 16:02:25 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-06-03 01:27:33 -04:00
|
|
|
func (*StepCreateImage) Cleanup(bag multistep.StateBag) {} // this is the final artifact, don't delete
|