hashicorp / packer-plugin-azure

Packer plugin for Azure Virtual Machine Image Builders
https://www.packer.io/docs/builders/azure
Mozilla Public License 2.0
51 stars 80 forks source link

The -force option it not cleaning up the gallery image #392

Closed bnfbiz closed 6 months ago

bnfbiz commented 6 months ago

When filing a bug, please include the following headings if possible. Any example text in this template can be deleted.

Overview of the Issue

When using the -force option to packer and creating a new image in the image gallery it fails to remove the image from the gallery.

Reproduction Steps

Build the image the first time
$ packer build .
Linux.azure-arm.template: output will be in this color.

==> Linux.azure-arm.template: Running builder ...
    Linux.azure-arm.template: Creating Azure Resource Manager (ARM) client ...
==> Linux.azure-arm.template: Getting source image id for the deployment ...
==> Linux.azure-arm.template:  -> SourceImageName: '/subscriptions/88888888-4444-4444-4444-cccccccccccc/resourceGroups/rg-ede-sig/providers/Microsoft.Compute/images/initial_CentOS7.9_Core_x64_2024.02.16'
==> Linux.azure-arm.template: Using existing resource group ...
==> Linux.azure-arm.template:  -> ResourceGroupName : 'rg-ede-default'
==> Linux.azure-arm.template:  -> Location          : 'centralus'
==> Linux.azure-arm.template: Validating deployment template ...
==> Linux.azure-arm.template:  -> ResourceGroupName : 'rg-ede-default'
==> Linux.azure-arm.template:  -> DeploymentName    : 'pkrdp4tee6jjg8s'
==> Linux.azure-arm.template: Deploying deployment template ...
==> Linux.azure-arm.template:  -> ResourceGroupName : 'rg-ede-default'
==> Linux.azure-arm.template:  -> DeploymentName    : 'pkrdp4tee6jjg8s'
==> Linux.azure-arm.template: Getting the VM's IP address ...
==> Linux.azure-arm.template:  -> ResourceGroupName   : 'rg-ede-default'
==> Linux.azure-arm.template:  -> PublicIPAddressName : 'pkrip4tee6jjg8s'
==> Linux.azure-arm.template:  -> NicName             : 'pkrni4tee6jjg8s'
==> Linux.azure-arm.template:  -> Network Connection  : 'PrivateEndpoint'
==> Linux.azure-arm.template:  -> IP Address          : '10.18.0.133'
==> Linux.azure-arm.template: Waiting for SSH to become available...
==> Linux.azure-arm.template: Connected to SSH!
==> Linux.azure-arm.template: Querying the machine's properties ...
==> Linux.azure-arm.template:  -> ResourceGroupName : 'rg-ede-default'
==> Linux.azure-arm.template:  -> ComputeName       : 'pkrvm4tee6jjg8s'
==> Linux.azure-arm.template:  -> Managed OS Disk   : '/subscriptions/88888888-4444-4444-4444-cccccccccccc/resourceGroups/rg-ede-default/providers/Microsoft.Compute/disks/pkros4tee6jjg8s'
==> Linux.azure-arm.template: Querying the machine's additional disks properties ...
==> Linux.azure-arm.template:  -> ResourceGroupName : 'rg-ede-default'
==> Linux.azure-arm.template:  -> ComputeName       : 'pkrvm4tee6jjg8s'
==> Linux.azure-arm.template: Powering off machine ...
==> Linux.azure-arm.template:  -> ResourceGroupName : 'rg-ede-default'
==> Linux.azure-arm.template:  -> ComputeName       : 'pkrvm4tee6jjg8s'
==> Linux.azure-arm.template:  -> Compute ResourceGroupName : 'rg-ede-default'
==> Linux.azure-arm.template:  -> Compute Name              : 'pkrvm4tee6jjg8s'
==> Linux.azure-arm.template:  -> Compute Location          : 'centralus'
==> Linux.azure-arm.template: Generalizing machine ...
==> Linux.azure-arm.template: Capturing image ...
==> Linux.azure-arm.template:  -> Image ResourceGroupName   : 'rg-ede-sig'
==> Linux.azure-arm.template:  -> Image Name                : 'basic_CentOS7.9_Core_x64_0.0.1'
==> Linux.azure-arm.template:  -> Image Location            : 'centralus'
==> Linux.azure-arm.template: Publishing to Shared Image Gallery ...
==> Linux.azure-arm.template:  -> Source ID used for SIG publish        : '/subscriptions/88888888-4444-4444-4444-cccccccccccc/resourceGroups/rg-ede-sig/providers/Microsoft.Compute/images/basic_CentOS7.9_Core_x64_0.0.1'
==> Linux.azure-arm.template:  -> SIG publish resource group            : 'rg-ede-sig'
==> Linux.azure-arm.template:  -> SIG gallery name                      : 'sig0098centralus'
==> Linux.azure-arm.template:  -> SIG image name                        : 'basic_CentOS7.9_Core_x64'
==> Linux.azure-arm.template:  -> SIG image version                     : '0.0.1'
==> Linux.azure-arm.template:  -> SIG replication regions               : '[centralus]'
==> Linux.azure-arm.template:  -> SIG storage account type              : ''
==> Linux.azure-arm.template:  -> SIG image version endoflife date      : ''
==> Linux.azure-arm.template:  -> SIG image version exclude from latest : 'false'
==> Linux.azure-arm.template:  -> SIG replica count [1, 100]            : '1'
==> Linux.azure-arm.template:  -> Shared Gallery Image Version ID : '/subscriptions/88888888-4444-4444-4444-cccccccccccc/resourceGroups/rg-ede-sig/providers/Microsoft.Compute/galleries/sig0098centralus/images/basic_CentOS7.9_Core_x64/versions/0.0.1'
==> Linux.azure-arm.template: 
==> Linux.azure-arm.template: Deleting Virtual Machine deployment and its attatched resources...
==> Linux.azure-arm.template: Adding to deletion queue -> Microsoft.Compute/virtualMachines : 'pkrvm4tee6jjg8s'
==> Linux.azure-arm.template: Adding to deletion queue -> Microsoft.Network/networkInterfaces : 'pkrni4tee6jjg8s'
==> Linux.azure-arm.template: Waiting for deletion of all resources...
==> Linux.azure-arm.template: Attempting deletion -> Microsoft.Network/networkInterfaces : 'pkrni4tee6jjg8s'
==> Linux.azure-arm.template: Attempting deletion -> Microsoft.Compute/virtualMachines : 'pkrvm4tee6jjg8s'
==> Linux.azure-arm.template: Couldn't delete Microsoft.Network/networkInterfaces resource. Will retry.
==> Linux.azure-arm.template: Name: pkrni4tee6jjg8s
==> Linux.azure-arm.template: Attempting deletion -> Microsoft.Network/networkInterfaces : 'pkrni4tee6jjg8s'
==> Linux.azure-arm.template:  Deleting -> Microsoft.Compute/disks : '/subscriptions/88888888-4444-4444-4444-ccccccccccccresourceGroups/rg-ede-default/providers/Microsoft.Compute/disks/pkros4tee6jjg8s'
==> Linux.azure-arm.template: Removing the created Deployment object: 'pkrdp4tee6jjg8s'
==> Linux.azure-arm.template: 
==> Linux.azure-arm.template: The resource group was not created by Packer, not deleting ...
Build 'Linux.azure-arm.template' finished after 13 minutes 16 seconds.

==> Wait completed after 13 minutes 16 seconds

==> Builds finished. The artifacts of successful builds are:
--> Linux.azure-arm.template: Azure.ResourceManagement.VMImage:

OSType: Linux
ManagedImageResourceGroupName: rg-ede-sig
ManagedImageName: basic_CentOS7.9_Core_x64_0.0.1
ManagedImageId: /subscriptions/88888888-4444-4444-4444-cccccccccccc/resourceGroups/rg-ede-sig/providers/Microsoft.Compute/images/basic_CentOS7.9_Core_x64_0.0.1
ManagedImageLocation: centralus
ManagedImageSharedImageGalleryId: /subscriptions/88888888-4444-4444-4444-cccccccccccc/resourceGroups/rg-ede-sig/providers/Microsoft.Compute/galleries/sig0098centralus/images/basic_CentOS7.9_Core_x64/versions/0.0.1
SharedImageGalleryResourceGroup: rg-ede-sig
SharedImageGalleryName: sig0098centralus
SharedImageGalleryImageName: basic_CentOS7.9_Core_x64
SharedImageGalleryImageVersion: 0.0.1
SharedImageGalleryReplicatedRegions: centralus
Rebuild the image a second time (iterate on the template config)
$ packer build -force . 
Linux.azure-arm.template: output will be in this color.

==> Linux.azure-arm.template: Running builder ...
    Linux.azure-arm.template: Creating Azure Resource Manager (ARM) client ...
==> Linux.azure-arm.template: the managed image named basic_CentOS7.9_Core_x64_0.0.1 already exists, but deleting it due to -force flag
Build 'Linux.azure-arm.template' errored after 31 seconds 239 milliseconds: a gallery image version for image name:version basic_CentOS7.9_Core_x64:0.0.1 already exists in gallery sig0098centralus

==> Wait completed after 31 seconds 240 milliseconds

==> Some builds didn't complete successfully and had errors:
--> Linux.azure-arm.template: a gallery image version for image name:version basic_CentOS7.9_Core_x64:0.0.1 already exists in gallery sig0098centralus

==> Builds finished but no artifacts were created.

Plugin and Packer version

From packer version

$ packer version
Packer v1.10.2

Simplified Packer Buildfile

source-azure.pkr.hcl

source "azure-arm" "template" {
    subscription_id           = var.subscription_id
    client_id                 = var.client_id
    client_cert_path          = var.client_cert_path
    client_cert_password      = var.client_cert_pass
    polling_duration_timeout  = "30m"

    azure_tags = var.az_tags

    os_type                   ="Linux"
    os_disk_size_gb           = 50

    shared_image_gallery {
        subscription   = var.subscription_id
        resource_group = var.az_shared_library_rg
        gallery_name   = var.az_gallery_name
        image_name     = var.src_image_name
        image_version  = var.az_src_image_version
    }

    shared_image_gallery_destination {
        subscription        = var.subscription_id
        resource_group      = var.az_shared_library_rg
        gallery_name        = var.az_gallery_name
        image_name          = var.vm_prefix
        image_version       = "0.0.1"
        replication_regions = var.az_replication_regions
    }

    shared_image_gallery_timeout           = "1h"
    managed_image_name                     = "${var.vm_prefix}_0.0.1"
    managed_image_resource_group_name      = var.az_shared_library_rg
    vm_size                                = "Standard_D2_v4"
    private_virtual_network_with_public_ip = false
    virtual_network_name                   = var.az_vnet_name
    virtual_network_subnet_name            = var.az_subnet_name
    virtual_network_resource_group_name    = var.az_subnet_rg
    build_resource_group_name              = var.az_build_rg

    // Settings for the communicator
    communicator   = "ssh"
    ssh_username   = var.vm_usr
    ssh_password   = var.vm_psw
  }

vars.pkr.hcl

variable "subscription_id" {
    type = string
}

variable "client_id" {
    type = string
}

variable "client_cert_path" {
    type = string
}

variable "client_cert_pass" {
    type = string
}

variable "az_tags" {
  type        = map(string)
  description = "Tags assigned to created resources. (Optional for: base and baked)"
  default     = {
    edaas_purpose     = "Test azure builder 2.0.4"
    edaas_environment = "QA"
    edaas_user_email  = "email@someplace.com"
  }
}

variable "az_shared_library_rg" {
  type        = string
}

variable "az_gallery_name" {
  type        = string
}

variable "src_image_name" {
  type        = string
}

variable "az_src_image_version" {
  type        = string
}

variable "vm_prefix" {
  type        = string
}

variable "az_replication_regions" {
  type        = list(string)
}

variable "az_vnet_name" {
  type        = string
}

variable "az_subnet_rg" {
  type        = string
}

variable "az_build_rg" {
  type        = string
}

variable "az_subnet_name" {
  type        = string
}

variable "vm_psw" {
  type        = string
}

variable "vm_usr" {
  type        = string
}

versions.pkr.hcl

packer {
  required_plugins {
    azure = {
      version = "2.0.4"
      source  = "github.com/hashicorp/azure"
    }
  }
}

azure-vm.pkr.hcl

build {
  name = "Linux"
  sources = ["source.azure-arm.template"]

  # for this don't need provisioners
}

Operating system and Environment details

WSL Ubuntu 22.04

Log Fragments and crash.log files

Error provided above

JenGoldstrich commented 6 months ago

Hey @bnfbiz I have merged a PR https://github.com/hashicorp/packer-plugin-azure/pull/397 adding this functionality, it will be released in the coming weeks, thanks for reporting this, definitely something worth correcting!