Create and use context for Builder.Run
This commit is contained in:
parent
cf94086b5f
commit
07d8c71a2d
|
@ -24,9 +24,10 @@ import (
|
|||
)
|
||||
|
||||
type Builder struct {
|
||||
config *Config
|
||||
stateBag multistep.StateBag
|
||||
runner multistep.Runner
|
||||
config *Config
|
||||
stateBag multistep.StateBag
|
||||
runner multistep.Runner
|
||||
ctxCancel context.CancelFunc
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -53,6 +54,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|||
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
||||
ui.Say("Running builder ...")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
b.ctxCancel = cancel
|
||||
defer cancel()
|
||||
|
||||
if err := newConfigRetriever().FillParameters(b.config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -87,7 +92,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||
}
|
||||
|
||||
if b.config.isManagedImage() {
|
||||
group, err := azureClient.GroupsClient.Get(context.TODO(), b.config.ManagedImageResourceGroupName)
|
||||
group, err := azureClient.GroupsClient.Get(ctx, b.config.ManagedImageResourceGroupName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Cannot locate the managed image resource group %s.", b.config.ManagedImageResourceGroupName)
|
||||
}
|
||||
|
@ -95,13 +100,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||
b.config.manageImageLocation = *group.Location
|
||||
|
||||
// If a managed image already exists it cannot be overwritten.
|
||||
_, err = azureClient.ImagesClient.Get(context.TODO(), b.config.ManagedImageResourceGroupName, b.config.ManagedImageName, "")
|
||||
_, err = azureClient.ImagesClient.Get(ctx, b.config.ManagedImageResourceGroupName, b.config.ManagedImageName, "")
|
||||
if err == nil {
|
||||
if b.config.PackerForce {
|
||||
ui.Say(fmt.Sprintf("the managed image named %s already exists, but deleting it due to -force flag", b.config.ManagedImageName))
|
||||
f, err := azureClient.ImagesClient.Delete(context.TODO(), b.config.ManagedImageResourceGroupName, b.config.ManagedImageName)
|
||||
f, err := azureClient.ImagesClient.Delete(ctx, b.config.ManagedImageResourceGroupName, b.config.ManagedImageName)
|
||||
if err == nil {
|
||||
err = f.WaitForCompletion(context.TODO(), azureClient.ImagesClient.Client)
|
||||
err = f.WaitForCompletion(ctx, azureClient.ImagesClient.Client)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to delete the managed image named %s : %s", b.config.ManagedImageName, azureClient.LastError.Error())
|
||||
|
@ -113,7 +118,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||
}
|
||||
|
||||
if b.config.BuildResourceGroupName != "" {
|
||||
group, err := azureClient.GroupsClient.Get(context.TODO(), b.config.BuildResourceGroupName)
|
||||
group, err := azureClient.GroupsClient.Get(ctx, b.config.BuildResourceGroupName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Cannot locate the existing build resource resource group %s.", b.config.BuildResourceGroupName)
|
||||
}
|
||||
|
@ -122,7 +127,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||
}
|
||||
|
||||
if b.config.StorageAccount != "" {
|
||||
account, err := b.getBlobAccount(azureClient, b.config.ResourceGroupName, b.config.StorageAccount)
|
||||
account, err := b.getBlobAccount(ctx, azureClient, b.config.ResourceGroupName, b.config.StorageAccount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -284,6 +289,10 @@ func (b *Builder) isPrivateNetworkCommunication() bool {
|
|||
}
|
||||
|
||||
func (b *Builder) Cancel() {
|
||||
if b.ctxCancel != nil {
|
||||
log.Printf("Cancelling Azure builder...")
|
||||
b.ctxCancel()
|
||||
}
|
||||
if b.runner != nil {
|
||||
log.Println("Cancelling the step runner...")
|
||||
b.runner.Cancel()
|
||||
|
@ -298,8 +307,8 @@ func canonicalizeLocation(location string) string {
|
|||
return strings.Replace(location, " ", "", -1)
|
||||
}
|
||||
|
||||
func (b *Builder) getBlobAccount(client *AzureClient, resourceGroupName string, storageAccountName string) (*armstorage.Account, error) {
|
||||
account, err := client.AccountsClient.GetProperties(context.TODO(), resourceGroupName, storageAccountName)
|
||||
func (b *Builder) getBlobAccount(ctx context.Context, client *AzureClient, resourceGroupName string, storageAccountName string) (*armstorage.Account, error) {
|
||||
account, err := client.AccountsClient.GetProperties(ctx, resourceGroupName, storageAccountName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue