Changes to ARM builder for publishing to SIG
This commit is contained in:
parent
fb9fd58dfb
commit
6504d3ae5c
|
@ -42,7 +42,7 @@ type AzureClient struct {
|
||||||
armStorage.AccountsClient
|
armStorage.AccountsClient
|
||||||
compute.DisksClient
|
compute.DisksClient
|
||||||
compute.SnapshotsClient
|
compute.SnapshotsClient
|
||||||
newCompute.Gallery
|
newCompute.GalleryImageVersionsClient
|
||||||
|
|
||||||
InspectorMaxLength int
|
InspectorMaxLength int
|
||||||
Template *CaptureTemplate
|
Template *CaptureTemplate
|
||||||
|
@ -204,6 +204,12 @@ func NewAzureClient(subscriptionID, resourceGroupName, storageAccountName string
|
||||||
azureClient.AccountsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
azureClient.AccountsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
||||||
azureClient.AccountsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(), azureClient.AccountsClient.UserAgent)
|
azureClient.AccountsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(), azureClient.AccountsClient.UserAgent)
|
||||||
|
|
||||||
|
azureClient.GalleryImageVersionsClient = newCompute.NewGalleryImageVersionsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
||||||
|
azureClient.GalleryImageVersionsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
||||||
|
azureClient.GalleryImageVersionsClient.RequestInspector = withInspection(maxlen)
|
||||||
|
azureClient.GalleryImageVersionsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
||||||
|
azureClient.GalleryImageVersionsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(), azureClient.GalleryImageVersionsClient.UserAgent)
|
||||||
|
|
||||||
keyVaultURL, err := url.Parse(cloud.KeyVaultEndpoint)
|
keyVaultURL, err := url.Parse(cloud.KeyVaultEndpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -198,6 +198,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
NewStepSnapshotOSDisk(azureClient, ui, b.config),
|
NewStepSnapshotOSDisk(azureClient, ui, b.config),
|
||||||
NewStepSnapshotDataDisks(azureClient, ui, b.config),
|
NewStepSnapshotDataDisks(azureClient, ui, b.config),
|
||||||
NewStepCaptureImage(azureClient, ui),
|
NewStepCaptureImage(azureClient, ui),
|
||||||
|
NewStepPublishToSharedImageGallery(azureClient, ui, b.config),
|
||||||
NewStepDeleteResourceGroup(azureClient, ui),
|
NewStepDeleteResourceGroup(azureClient, ui),
|
||||||
NewStepDeleteOSDisk(azureClient, ui),
|
NewStepDeleteOSDisk(azureClient, ui),
|
||||||
NewStepDeleteAdditionalDisks(azureClient, ui),
|
NewStepDeleteAdditionalDisks(azureClient, ui),
|
||||||
|
@ -236,6 +237,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
NewStepSnapshotOSDisk(azureClient, ui, b.config),
|
NewStepSnapshotOSDisk(azureClient, ui, b.config),
|
||||||
NewStepSnapshotDataDisks(azureClient, ui, b.config),
|
NewStepSnapshotDataDisks(azureClient, ui, b.config),
|
||||||
NewStepCaptureImage(azureClient, ui),
|
NewStepCaptureImage(azureClient, ui),
|
||||||
|
NewStepPublishToSharedImageGallery(azureClient, ui, b.config),
|
||||||
NewStepDeleteResourceGroup(azureClient, ui),
|
NewStepDeleteResourceGroup(azureClient, ui),
|
||||||
NewStepDeleteOSDisk(azureClient, ui),
|
NewStepDeleteOSDisk(azureClient, ui),
|
||||||
NewStepDeleteAdditionalDisks(azureClient, ui),
|
NewStepDeleteAdditionalDisks(azureClient, ui),
|
||||||
|
@ -369,6 +371,14 @@ func (b *Builder) configureStateBag(stateBag multistep.StateBag) {
|
||||||
stateBag.Put(constants.ArmManagedImageOSDiskSnapshotName, b.config.ManagedImageOSDiskSnapshotName)
|
stateBag.Put(constants.ArmManagedImageOSDiskSnapshotName, b.config.ManagedImageOSDiskSnapshotName)
|
||||||
stateBag.Put(constants.ArmManagedImageDataDiskSnapshotPrefix, b.config.ManagedImageDataDiskSnapshotPrefix)
|
stateBag.Put(constants.ArmManagedImageDataDiskSnapshotPrefix, b.config.ManagedImageDataDiskSnapshotPrefix)
|
||||||
stateBag.Put(constants.ArmAsyncResourceGroupDelete, b.config.AsyncResourceGroupDelete)
|
stateBag.Put(constants.ArmAsyncResourceGroupDelete, b.config.AsyncResourceGroupDelete)
|
||||||
|
if b.config.isManagedImage() && b.config.SharedGalleryDestination.SigDestinationGalleryName != "" {
|
||||||
|
stateBag.Put(constants.ArmManagedImageSigPublishSubscription, b.config.SharedGalleryDestination.SigDestinationSubscription)
|
||||||
|
stateBag.Put(constants.ArmManagedImageSigPublishResourceGroup, b.config.SharedGalleryDestination.SigDestinationResourceGroup)
|
||||||
|
stateBag.Put(constants.ArmManagedImageSharedGalleryName, b.config.SharedGalleryDestination.SigDestinationGalleryName)
|
||||||
|
stateBag.Put(constants.ArmManagedImageSharedGalleryImageName, b.config.SharedGalleryDestination.SigDestinationImageName)
|
||||||
|
stateBag.Put(constants.ArmManagedImageSharedGalleryImageVersion, b.config.SharedGalleryDestination.SigDestinationImageVersion)
|
||||||
|
stateBag.Put(constants.ArmManagedImageSharedGalleryReplicationRegions, b.config.SharedGalleryDestination.SigDestinationReplicationRegions)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameters that are only known at runtime after querying Azure.
|
// Parameters that are only known at runtime after querying Azure.
|
||||||
|
|
|
@ -0,0 +1,131 @@
|
||||||
|
package arm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
|
||||||
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
||||||
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
|
"github.com/hashicorp/packer/packer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StepPublishToSharedImageGallery struct {
|
||||||
|
client *AzureClient
|
||||||
|
publish func(ctx context.Context, miSigPubSubscription, miSigPubRg, miSIGalleryName, miSGImageName, miSGImageVersion string, miSigReplicationRegions []string, location string, tags map[string]*string) error
|
||||||
|
say func(message string)
|
||||||
|
error func(e error)
|
||||||
|
toSIG func() bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStepPublishToSharedImageGallery(client *AzureClient, ui packer.Ui, config *Config) *StepPublishToSharedImageGallery {
|
||||||
|
var step = &StepPublishToSharedImageGallery{
|
||||||
|
client: client,
|
||||||
|
say: func(message string) {
|
||||||
|
ui.Say(message)
|
||||||
|
},
|
||||||
|
error: func(e error) {
|
||||||
|
ui.Error(e.Error())
|
||||||
|
},
|
||||||
|
toSIG: func() bool {
|
||||||
|
return config.isManagedImage() && config.SharedGalleryDestination.SigDestinationGalleryName != ""
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
step.publish = step.publishToSig
|
||||||
|
return step
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepPublishToSharedImageGallery) publishToSig(ctx context.Context, miSigPubSubscription string, miSigPubRg string, miSIGalleryName string, miSGImageName string, miSGImageVersion string, miSigReplicationRegions []string, location string, tags map[string]*string) error {
|
||||||
|
|
||||||
|
var mdiID string
|
||||||
|
|
||||||
|
replicationRegions := make([]compute.TargetRegion, len(miSigReplicationRegions))
|
||||||
|
for i, v := range miSigReplicationRegions {
|
||||||
|
regionName := v
|
||||||
|
replicationRegions[i] = compute.TargetRegion{Name: ®ionName}
|
||||||
|
}
|
||||||
|
|
||||||
|
galleryImageVersion := compute.GalleryImageVersion{
|
||||||
|
Location: &location,
|
||||||
|
Tags: tags,
|
||||||
|
GalleryImageVersionProperties: &compute.GalleryImageVersionProperties{
|
||||||
|
PublishingProfile: &compute.GalleryImageVersionPublishingProfile{
|
||||||
|
Source: &compute.GalleryArtifactSource{
|
||||||
|
ManagedImage: &compute.ManagedArtifact{
|
||||||
|
ID: &mdiID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
TargetRegions: &replicationRegions,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
timeStamp := time.Now().Unix() // returns 64 bit value
|
||||||
|
versionName := "0."
|
||||||
|
// get the higher 16 bits
|
||||||
|
minorVersion := timeStamp >> 16
|
||||||
|
// get the lower 16 bits
|
||||||
|
patch := timeStamp & 0xffff
|
||||||
|
versionName += strconv.FormatInt(minorVersion, 10) + "." + strconv.FormatInt(patch, 10)
|
||||||
|
|
||||||
|
f, err := s.client.GalleryImageVersionsClient.CreateOrUpdate(ctx, miSigPubRg, miSIGalleryName, miSGImageName, versionName, galleryImageVersion)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
s.say(s.client.LastError.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = f.WaitForCompletionRef(ctx, s.client.GalleryImageVersionsClient.Client)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
s.say(s.client.LastError.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
createdSGImageVersion, err := f.Result(s.client.GalleryImageVersionsClient)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
s.say(s.client.LastError.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: compare and contrast to see if Amrita needs to add this to artifact id
|
||||||
|
s.say(fmt.Sprintf(" -> Shared Gallery Image Version ID : '%s'", *(createdSGImageVersion.ID)))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepPublishToSharedImageGallery) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction {
|
||||||
|
s.say("Publishing to Shared Image Gallery ...")
|
||||||
|
|
||||||
|
var miSigPubSubscription = stateBag.Get(constants.ArmManagedImageSigPublishSubscription).(string)
|
||||||
|
var miSigPubRg = stateBag.Get(constants.ArmManagedImageSigPublishResourceGroup).(string)
|
||||||
|
var miSIGalleryName = stateBag.Get(constants.ArmManagedImageSharedGalleryName).(string)
|
||||||
|
var miSGImageName = stateBag.Get(constants.ArmManagedImageSharedGalleryImageName).(string)
|
||||||
|
var miSGImageVersion = stateBag.Get(constants.ArmManagedImageSharedGalleryImageVersion).(string)
|
||||||
|
var location = stateBag.Get(constants.ArmLocation).(string)
|
||||||
|
var tags = stateBag.Get(constants.ArmTags).(map[string]*string)
|
||||||
|
var miSigReplicationRegions = stateBag.Get(constants.ArmManagedImageSharedGalleryReplicationRegions).([]string)
|
||||||
|
|
||||||
|
s.say(fmt.Sprintf(" -> SIG publish subscription : '%s'", miSigPubSubscription))
|
||||||
|
s.say(fmt.Sprintf(" -> SIG publish resource group : '%s'", miSigPubRg))
|
||||||
|
s.say(fmt.Sprintf(" -> SIG gallery name : '%s'", miSIGalleryName))
|
||||||
|
s.say(fmt.Sprintf(" -> SIG image name : '%s'", miSGImageName))
|
||||||
|
s.say(fmt.Sprintf(" -> SIG image version : '%s'", miSGImageVersion))
|
||||||
|
s.say(fmt.Sprintf(" -> SIG publish location : '%s'", location))
|
||||||
|
err := s.publish(ctx, miSigPubSubscription, miSigPubRg, miSIGalleryName, miSGImageName, miSGImageVersion, miSigReplicationRegions,location, tags)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
stateBag.Put(constants.Error, err)
|
||||||
|
s.error(err)
|
||||||
|
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*StepPublishToSharedImageGallery) Cleanup(multistep.StateBag) {
|
||||||
|
}
|
|
@ -34,6 +34,12 @@ const (
|
||||||
ArmManagedImageResourceGroupName string = "arm.ManagedImageResourceGroupName"
|
ArmManagedImageResourceGroupName string = "arm.ManagedImageResourceGroupName"
|
||||||
ArmManagedImageLocation string = "arm.ManagedImageLocation"
|
ArmManagedImageLocation string = "arm.ManagedImageLocation"
|
||||||
ArmManagedImageName string = "arm.ManagedImageName"
|
ArmManagedImageName string = "arm.ManagedImageName"
|
||||||
|
ArmManagedImageSigPublishSubscription string = "arm.ManagedImageSigPublishSubscription"
|
||||||
|
ArmManagedImageSigPublishResourceGroup string = "arm.ManagedImageSigPublishResourceGroup"
|
||||||
|
ArmManagedImageSharedGalleryName string = "arm.ManagedImageSharedGalleryName"
|
||||||
|
ArmManagedImageSharedGalleryImageName string = "arm.ManagedImageSharedGalleryImageName"
|
||||||
|
ArmManagedImageSharedGalleryImageVersion string = "arm.ManagedImageSharedGalleryImageVersion"
|
||||||
|
ArmManagedImageSharedGalleryReplicationRegions string = "arm.ManagedImageSharedGalleryReplicationRegions"
|
||||||
ArmAsyncResourceGroupDelete string = "arm.AsyncResourceGroupDelete"
|
ArmAsyncResourceGroupDelete string = "arm.AsyncResourceGroupDelete"
|
||||||
ArmManagedImageOSDiskSnapshotName string = "arm.ManagedImageOSDiskSnapshotName"
|
ArmManagedImageOSDiskSnapshotName string = "arm.ManagedImageOSDiskSnapshotName"
|
||||||
ArmManagedImageDataDiskSnapshotPrefix string = "arm.ManagedImageDataDiskSnapshotPrefix"
|
ArmManagedImageDataDiskSnapshotPrefix string = "arm.ManagedImageDataDiskSnapshotPrefix"
|
||||||
|
|
Loading…
Reference in New Issue