Convert legacy Packer json template to hcl2 template (#247)
* convert legacy Packer json template to hcl2 template
This commit is contained in:
		
							
								
								
									
										1
									
								
								.github/workflows/e2e.yaml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.github/workflows/e2e.yaml
									
									
									
									
										vendored
									
									
								
							| @@ -23,6 +23,7 @@ jobs: | |||||||
|           separator: "," |           separator: "," | ||||||
|           files: "quickstart/*" |           files: "quickstart/*" | ||||||
|           files_ignore: "**/TestRecord.md" |           files_ignore: "**/TestRecord.md" | ||||||
|  |           dir_names_max_depth: 2 | ||||||
|       - name: test pr |       - name: test pr | ||||||
|         run: | |         run: | | ||||||
|           az login --identity --username $MSI_ID > /dev/null |           az login --identity --username $MSI_ID > /dev/null | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								.github/workflows/pr-check.yaml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.github/workflows/pr-check.yaml
									
									
									
									
										vendored
									
									
								
							| @@ -20,6 +20,7 @@ jobs: | |||||||
|           dir_names: "true" |           dir_names: "true" | ||||||
|           separator: "," |           separator: "," | ||||||
|           files: "quickstart/*" |           files: "quickstart/*" | ||||||
|  |           dir_names_max_depth: 2 | ||||||
|       - name: pr-check |       - name: pr-check | ||||||
|         run: | |         run: | | ||||||
|           export CHANGED_FOLDERS="${{ steps.changed-files.outputs.all_changed_files }}" |           export CHANGED_FOLDERS="${{ steps.changed-files.outputs.all_changed_files }}" | ||||||
|   | |||||||
| @@ -7,6 +7,22 @@ terraform { | |||||||
|       source  = "hashicorp/azurerm" |       source  = "hashicorp/azurerm" | ||||||
|       version = "~>2.0" |       version = "~>2.0" | ||||||
|     } |     } | ||||||
|  |     azapi = { | ||||||
|  |       source = "Azure/azapi" | ||||||
|  |       version = "~> 1.0" | ||||||
|  |     } | ||||||
|  |     local = { | ||||||
|  |       source  = "hashicorp/local" | ||||||
|  |       version = "2.4.0" | ||||||
|  |     } | ||||||
|  |     random = { | ||||||
|  |       source  = "hashicorp/random" | ||||||
|  |       version = "3.5.1" | ||||||
|  |     } | ||||||
|  |     tls = { | ||||||
|  |       source  = "hashicorp/tls" | ||||||
|  |       version = "4.0.4" | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -14,8 +30,10 @@ provider "azurerm" { | |||||||
|   features {} |   features {} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | resource "random_pet" "id" {} | ||||||
|  |  | ||||||
| resource "azurerm_resource_group" "vmss" { | resource "azurerm_resource_group" "vmss" { | ||||||
|   name     = var.resource_group_name |   name     = coalesce(var.resource_group_name, "201-vmss-packer-jumpbox-${random_pet.id.id}") | ||||||
|   location = var.location |   location = var.location | ||||||
|   tags     = var.tags |   tags     = var.tags | ||||||
| } | } | ||||||
| @@ -24,7 +42,7 @@ resource "random_string" "fqdn" { | |||||||
|   length  = 6 |   length  = 6 | ||||||
|   special = false |   special = false | ||||||
|   upper   = false |   upper   = false | ||||||
|  number  = false |   numeric = false | ||||||
| } | } | ||||||
|  |  | ||||||
| resource "azurerm_virtual_network" "vmss" { | resource "azurerm_virtual_network" "vmss" { | ||||||
| @@ -97,6 +115,31 @@ data "azurerm_image" "image" { | |||||||
|   resource_group_name = data.azurerm_resource_group.image.name |   resource_group_name = data.azurerm_resource_group.image.name | ||||||
| } | } | ||||||
|  |  | ||||||
|  | resource "azapi_resource" "ssh_public_key" { | ||||||
|  |   type      = "Microsoft.Compute/sshPublicKeys@2022-11-01" | ||||||
|  |   name      = random_pet.id.id | ||||||
|  |   location  = azurerm_resource_group.vmss.location | ||||||
|  |   parent_id = azurerm_resource_group.vmss.id | ||||||
|  | } | ||||||
|  |  | ||||||
|  | resource "azapi_resource_action" "ssh_public_key_gen" { | ||||||
|  |   type        = "Microsoft.Compute/sshPublicKeys@2022-11-01" | ||||||
|  |   resource_id = azapi_resource.ssh_public_key.id | ||||||
|  |   action      = "generateKeyPair" | ||||||
|  |   method      = "POST" | ||||||
|  |  | ||||||
|  |   response_export_values = ["publicKey", "privateKey"] | ||||||
|  | } | ||||||
|  |  | ||||||
|  | resource "random_password" "password" { | ||||||
|  |   count  = var.admin_password == null ? 1 : 0 | ||||||
|  |   length = 20 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | locals { | ||||||
|  |   admin_password = try(random_password.password[0].result, var.admin_password) | ||||||
|  | } | ||||||
|  |  | ||||||
| resource "azurerm_virtual_machine_scale_set" "vmss" { | resource "azurerm_virtual_machine_scale_set" "vmss" { | ||||||
|   name                = "vmscaleset" |   name                = "vmscaleset" | ||||||
|   location            = var.location |   location            = var.location | ||||||
| @@ -130,7 +173,7 @@ resource "azurerm_virtual_machine_scale_set" "vmss" { | |||||||
|   os_profile { |   os_profile { | ||||||
|     computer_name_prefix = "vmlab" |     computer_name_prefix = "vmlab" | ||||||
|     admin_username       = var.admin_user |     admin_username       = var.admin_user | ||||||
|     admin_password       = var.admin_password |     admin_password       = local.admin_password | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   os_profile_linux_config { |   os_profile_linux_config { | ||||||
| @@ -138,7 +181,7 @@ resource "azurerm_virtual_machine_scale_set" "vmss" { | |||||||
|  |  | ||||||
|     ssh_keys { |     ssh_keys { | ||||||
|       path     = "/home/azureuser/.ssh/authorized_keys" |       path     = "/home/azureuser/.ssh/authorized_keys" | ||||||
|       key_data = file("~/.ssh/id_rsa.pub") |       key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -205,7 +248,7 @@ resource "azurerm_virtual_machine" "jumpbox" { | |||||||
|   os_profile { |   os_profile { | ||||||
|     computer_name  = "jumpbox" |     computer_name  = "jumpbox" | ||||||
|     admin_username = var.admin_user |     admin_username = var.admin_user | ||||||
|     admin_password = var.admin_password |     admin_password = local.admin_password | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   os_profile_linux_config { |   os_profile_linux_config { | ||||||
| @@ -213,7 +256,7 @@ resource "azurerm_virtual_machine" "jumpbox" { | |||||||
|  |  | ||||||
|     ssh_keys { |     ssh_keys { | ||||||
|       path     = "/home/azureuser/.ssh/authorized_keys" |       path     = "/home/azureuser/.ssh/authorized_keys" | ||||||
|       key_data = file("~/.ssh/id_rsa.pub") |       key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -0,0 +1,6 @@ | |||||||
|  | resource "random_pet" "id" {} | ||||||
|  |  | ||||||
|  | resource "azurerm_resource_group" "image_group" { | ||||||
|  |   location = "eastus" | ||||||
|  |   name     = "packer-image-${random_pet.id.id}" | ||||||
|  | } | ||||||
| @@ -0,0 +1,3 @@ | |||||||
|  | output "resource_group_name" { | ||||||
|  |   value = azurerm_resource_group.image_group.name | ||||||
|  | } | ||||||
| @@ -0,0 +1,26 @@ | |||||||
|  | terraform { | ||||||
|  |   required_version = ">=1.2" | ||||||
|  |  | ||||||
|  |   required_providers { | ||||||
|  |     azurerm = { | ||||||
|  |       source  = "hashicorp/azurerm" | ||||||
|  |       version = "~> 3.0" | ||||||
|  |     } | ||||||
|  |     helm = { | ||||||
|  |       source  = "hashicorp/helm" | ||||||
|  |       version = "2.9.0" | ||||||
|  |     } | ||||||
|  |     random = { | ||||||
|  |       source  = "hashicorp/random" | ||||||
|  |       version = "~> 3.0" | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | provider "azurerm" { | ||||||
|  |   features { | ||||||
|  |     resource_group { | ||||||
|  |       prevent_deletion_if_contains_resources = false | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										68
									
								
								quickstart/201-vmss-packer-jumpbox/ubuntu.pkr.hcl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								quickstart/201-vmss-packer-jumpbox/ubuntu.pkr.hcl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | |||||||
|  | packer { | ||||||
|  |   required_plugins { | ||||||
|  |     azure = { | ||||||
|  |       source  = "github.com/hashicorp/azure" | ||||||
|  |       version = "~> 2" | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | variable client_id { | ||||||
|  |   type = string | ||||||
|  |   default = null | ||||||
|  | } | ||||||
|  | variable client_secret { | ||||||
|  |   type = string | ||||||
|  |   default = null | ||||||
|  | } | ||||||
|  |  | ||||||
|  | variable subscription_id { | ||||||
|  |   type = string | ||||||
|  |   default = null | ||||||
|  | } | ||||||
|  |  | ||||||
|  | variable tenant_id { | ||||||
|  |   type = string | ||||||
|  |   default = null | ||||||
|  | } | ||||||
|  |  | ||||||
|  | variable location { | ||||||
|  |   default = "eastus" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | variable "image_resource_group_name" { | ||||||
|  |   description = "Name of the resource group in which the Packer image will be created" | ||||||
|  |   default     = "myPackerImages" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | source "azure-arm" "builder" { | ||||||
|  |   client_id                         = var.client_id | ||||||
|  |   client_secret                     = var.client_secret | ||||||
|  |   image_offer                       = "UbuntuServer" | ||||||
|  |   image_publisher                   = "canonical" | ||||||
|  |   image_sku                         = "16.04-LTS" | ||||||
|  |   location                          = var.location | ||||||
|  |   managed_image_name                = "myPackerImage" | ||||||
|  |   managed_image_resource_group_name = var.image_resource_group_name | ||||||
|  |   os_type                           = "Linux" | ||||||
|  |   subscription_id                   = var.subscription_id | ||||||
|  |   tenant_id                         = var.tenant_id | ||||||
|  |   vm_size                           = "Standard_DS2_v2" | ||||||
|  |   azure_tags                        = { | ||||||
|  |     "dept" : "Engineering", | ||||||
|  |     "task" : "Image deployment", | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | build { | ||||||
|  |   sources = ["source.azure-arm.builder"] | ||||||
|  |   provisioner "shell" { | ||||||
|  |     execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'" | ||||||
|  |     inline = [ | ||||||
|  |       "apt-get update", | ||||||
|  |       "apt-get upgrade -y", | ||||||
|  |       "apt-get -y install nginx", | ||||||
|  |       "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync", | ||||||
|  |     ] | ||||||
|  |   } | ||||||
|  | } | ||||||
| @@ -10,12 +10,7 @@ variable "packer_image_name" { | |||||||
|  |  | ||||||
| variable "resource_group_name" { | variable "resource_group_name" { | ||||||
|   description = "Name of the resource group in which the Packer image  will be created" |   description = "Name of the resource group in which the Packer image  will be created" | ||||||
|    default     = "myPackerImages" |   default     = null | ||||||
| } |  | ||||||
|  |  | ||||||
| variable "resource_group_name" { |  | ||||||
|    description = "Name of the resource group in which the resources will be created" |  | ||||||
|    default     = "myResourceGroup" |  | ||||||
| } | } | ||||||
|  |  | ||||||
| variable "location" { | variable "location" { | ||||||
| @@ -43,4 +38,5 @@ variable "admin_user" { | |||||||
|  |  | ||||||
| variable "admin_password" { | variable "admin_password" { | ||||||
|   description = "Default password for admin account" |   description = "Default password for admin account" | ||||||
|  |   default     = null | ||||||
| } | } | ||||||
| @@ -2,6 +2,9 @@ package e2e | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"github.com/gruntwork-io/terratest/modules/files" | 	"github.com/gruntwork-io/terratest/modules/files" | ||||||
|  | 	"github.com/gruntwork-io/terratest/modules/packer" | ||||||
|  | 	test_structure "github.com/gruntwork-io/terratest/modules/test-structure" | ||||||
|  | 	"github.com/stretchr/testify/require" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"strings" | 	"strings" | ||||||
| @@ -11,6 +14,10 @@ import ( | |||||||
| 	"github.com/gruntwork-io/terratest/modules/terraform" | 	"github.com/gruntwork-io/terratest/modules/terraform" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | var speicalTests = map[string]func(*testing.T){ | ||||||
|  | 	"quickstart/201-vmss-packer-jumpbox": test201VmssPackerJumpbox, | ||||||
|  | } | ||||||
|  |  | ||||||
| func Test_Quickstarts(t *testing.T) { | func Test_Quickstarts(t *testing.T) { | ||||||
| 	msiId := os.Getenv("MSI_ID") | 	msiId := os.Getenv("MSI_ID") | ||||||
| 	if msiId != "" { | 	if msiId != "" { | ||||||
| @@ -25,6 +32,7 @@ func Test_Quickstarts(t *testing.T) { | |||||||
| 			t.Fatalf(err.Error()) | 			t.Fatalf(err.Error()) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	folders = removeDuplicates(folders) | ||||||
| 	for _, f := range folders { | 	for _, f := range folders { | ||||||
| 		f = strings.TrimSpace(f) | 		f = strings.TrimSpace(f) | ||||||
| 		if filepath.Dir(f) != "quickstart" { | 		if filepath.Dir(f) != "quickstart" { | ||||||
| @@ -35,11 +43,16 @@ func Test_Quickstarts(t *testing.T) { | |||||||
| 		if !files.IsExistingDir(path) { | 		if !files.IsExistingDir(path) { | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 		t.Run(f, func(t *testing.T) { | 		test, ok := speicalTests[f] | ||||||
|  | 		if !ok { | ||||||
|  | 			test = func(t *testing.T) { | ||||||
| 				helper.RunE2ETest(t, rootPath, f, terraform.Options{ | 				helper.RunE2ETest(t, rootPath, f, terraform.Options{ | ||||||
| 					Upgrade: true, | 					Upgrade: true, | ||||||
| 				}, nil) | 				}, nil) | ||||||
| 		}) | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		t.Run(f, test) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -57,3 +70,65 @@ func allExamples() ([]string, error) { | |||||||
| 	} | 	} | ||||||
| 	return r, nil | 	return r, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func test201VmssPackerJumpbox(t *testing.T) { | ||||||
|  | 	examplePath := filepath.Join("..", "..", "quickstart", "201-vmss-packer-jumpbox") | ||||||
|  | 	examplePath = test_structure.CopyTerraformFolderToTemp(t, examplePath, "") | ||||||
|  | 	defer func() { | ||||||
|  | 		_ = os.RemoveAll(examplePath) | ||||||
|  | 	}() | ||||||
|  | 	harnessPath := filepath.Join(examplePath, "packer_image_resource_group") | ||||||
|  | 	harnessOptions := &terraform.Options{ | ||||||
|  | 		TerraformDir: harnessPath, | ||||||
|  | 	} | ||||||
|  | 	defer terraform.Destroy(t, harnessOptions) | ||||||
|  | 	terraform.InitAndApply(t, harnessOptions) | ||||||
|  | 	harnessOutput := terraform.OutputAll(t, harnessOptions) | ||||||
|  | 	imageResourceGroupName := harnessOutput["resource_group_name"].(string) | ||||||
|  | 	pkrCfg := filepath.Join(examplePath, "ubuntu.pkr.hcl") | ||||||
|  | 	packerVars := map[string]string{ | ||||||
|  | 		"image_resource_group_name": imageResourceGroupName, | ||||||
|  | 	} | ||||||
|  | 	useMsi := false | ||||||
|  | 	if clientId := os.Getenv("ARM_CLIENT_ID"); clientId != "" { | ||||||
|  | 		packerVars["client_id"] = clientId | ||||||
|  | 	} | ||||||
|  | 	if identityId := os.Getenv("MSI_ID"); identityId != "" { | ||||||
|  | 		packerVars["client_id"] = identityId | ||||||
|  | 		useMsi = true | ||||||
|  | 	} | ||||||
|  | 	if clientSecret := os.Getenv("ARM_CLIENT_SECRET"); clientSecret != "" { | ||||||
|  | 		packerVars["client_secret"] = clientSecret | ||||||
|  | 	} | ||||||
|  | 	if subscriptionId := os.Getenv("ARM_SUBSCRIPTION_ID"); subscriptionId != "" { | ||||||
|  | 		packerVars["subscription_id"] = subscriptionId | ||||||
|  | 	} | ||||||
|  | 	if tenantId := os.Getenv("ARM_TENANT_ID"); !useMsi && tenantId != "" { | ||||||
|  | 		packerVars["tenant_id"] = tenantId | ||||||
|  | 	} | ||||||
|  | 	_, err := packer.BuildArtifactE(t, &packer.Options{ | ||||||
|  | 		Template:   pkrCfg, | ||||||
|  | 		Vars:       packerVars, | ||||||
|  | 		VarFiles:   nil, | ||||||
|  | 		WorkingDir: examplePath, | ||||||
|  | 	}) | ||||||
|  | 	require.NoError(t, err) | ||||||
|  | 	helper.RunE2ETest(t, examplePath, "", terraform.Options{ | ||||||
|  | 		Upgrade: true, | ||||||
|  | 		Vars: map[string]interface{}{ | ||||||
|  | 			"packer_resource_group_name": imageResourceGroupName, | ||||||
|  | 		}, | ||||||
|  | 	}, nil) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func removeDuplicates(s []string) []string { | ||||||
|  | 	m := make(map[string]struct{}) | ||||||
|  | 	result := []string{} | ||||||
|  | 	for _, item := range s { | ||||||
|  | 		if _, ok := m[item]; !ok { | ||||||
|  | 			m[item] = struct{}{} | ||||||
|  | 			result = append(result, item) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return result | ||||||
|  | } | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ go 1.19 | |||||||
| require ( | require ( | ||||||
| 	github.com/Azure/terraform-module-test-helper v0.8.0 | 	github.com/Azure/terraform-module-test-helper v0.8.0 | ||||||
| 	github.com/gruntwork-io/terratest v0.41.9 | 	github.com/gruntwork-io/terratest v0.41.9 | ||||||
|  | 	github.com/stretchr/testify v1.8.1 | ||||||
| ) | ) | ||||||
|  |  | ||||||
| require ( | require ( | ||||||
| @@ -66,7 +67,6 @@ require ( | |||||||
| 	github.com/russross/blackfriday/v2 v2.1.0 // indirect | 	github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||||||
| 	github.com/spf13/afero v1.9.3 // indirect | 	github.com/spf13/afero v1.9.3 // indirect | ||||||
| 	github.com/spf13/pflag v1.0.5 // indirect | 	github.com/spf13/pflag v1.0.5 // indirect | ||||||
| 	github.com/stretchr/testify v1.8.1 // indirect |  | ||||||
| 	github.com/tmccombs/hcl2json v0.3.3 // indirect | 	github.com/tmccombs/hcl2json v0.3.3 // indirect | ||||||
| 	github.com/ulikunitz/xz v0.5.8 // indirect | 	github.com/ulikunitz/xz v0.5.8 // indirect | ||||||
| 	github.com/urfave/cli v1.22.2 // indirect | 	github.com/urfave/cli v1.22.2 // indirect | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user