github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/template_factory.go (about) 1 package arm 2 3 import ( 4 "encoding/json" 5 6 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute" 7 "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources" 8 9 "fmt" 10 11 "github.com/hashicorp/packer/builder/azure/common/constants" 12 "github.com/hashicorp/packer/builder/azure/common/template" 13 ) 14 15 type templateFactoryFunc func(*Config) (*resources.Deployment, error) 16 17 func GetKeyVaultDeployment(config *Config) (*resources.Deployment, error) { 18 params := &template.TemplateParameters{ 19 KeyVaultName: &template.TemplateParameter{Value: config.tmpKeyVaultName}, 20 KeyVaultSecretValue: &template.TemplateParameter{Value: config.winrmCertificate}, 21 ObjectId: &template.TemplateParameter{Value: config.ObjectID}, 22 TenantId: &template.TemplateParameter{Value: config.TenantID}, 23 } 24 25 builder, _ := template.NewTemplateBuilder(template.KeyVault) 26 builder.SetTags(&config.AzureTags) 27 28 doc, _ := builder.ToJSON() 29 return createDeploymentParameters(*doc, params) 30 } 31 32 func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error) { 33 params := &template.TemplateParameters{ 34 AdminUsername: &template.TemplateParameter{Value: config.UserName}, 35 AdminPassword: &template.TemplateParameter{Value: config.Password}, 36 DnsNameForPublicIP: &template.TemplateParameter{Value: config.tmpComputeName}, 37 NicName: &template.TemplateParameter{Value: config.tmpNicName}, 38 OSDiskName: &template.TemplateParameter{Value: config.tmpOSDiskName}, 39 PublicIPAddressName: &template.TemplateParameter{Value: config.tmpPublicIPAddressName}, 40 SubnetName: &template.TemplateParameter{Value: config.tmpSubnetName}, 41 StorageAccountBlobEndpoint: &template.TemplateParameter{Value: config.storageAccountBlobEndpoint}, 42 VirtualNetworkName: &template.TemplateParameter{Value: config.tmpVirtualNetworkName}, 43 VMSize: &template.TemplateParameter{Value: config.VMSize}, 44 VMName: &template.TemplateParameter{Value: config.tmpComputeName}, 45 } 46 47 builder, err := template.NewTemplateBuilder(template.BasicTemplate) 48 if err != nil { 49 return nil, err 50 } 51 osType := compute.Linux 52 53 switch config.OSType { 54 case constants.Target_Linux: 55 builder.BuildLinux(config.sshAuthorizedKey) 56 case constants.Target_Windows: 57 osType = compute.Windows 58 builder.BuildWindows(config.tmpKeyVaultName, config.tmpWinRMCertificateUrl) 59 } 60 61 if config.ImageUrl != "" { 62 builder.SetImageUrl(config.ImageUrl, osType) 63 } else if config.CustomManagedImageName != "" { 64 builder.SetManagedDiskUrl(config.customManagedImageID, config.managedImageStorageAccountType) 65 } else if config.ManagedImageName != "" && config.ImagePublisher != "" { 66 imageID := fmt.Sprintf("/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/ArtifactTypes/vmimage/offers/%s/skus/%s/versions/%s", 67 config.SubscriptionID, 68 config.Location, 69 config.ImagePublisher, 70 config.ImageOffer, 71 config.ImageSku, 72 config.ImageVersion) 73 74 builder.SetManagedMarketplaceImage(config.Location, config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion, imageID, config.managedImageStorageAccountType) 75 } else if config.SharedGallery.Subscription != "" { 76 imageID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/galleries/%s/images/%s", 77 config.SharedGallery.Subscription, 78 config.SharedGallery.ResourceGroup, 79 config.SharedGallery.GalleryName, 80 config.SharedGallery.ImageName) 81 if config.SharedGallery.ImageVersion != "" { 82 imageID += fmt.Sprintf("/versions/%s", 83 config.SharedGallery.ImageVersion) 84 } 85 86 builder.SetSharedGalleryImage(config.Location, imageID) 87 } else { 88 builder.SetMarketPlaceImage(config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion) 89 } 90 91 if config.OSDiskSizeGB > 0 { 92 builder.SetOSDiskSizeGB(config.OSDiskSizeGB) 93 } 94 95 if len(config.AdditionalDiskSize) > 0 { 96 builder.SetAdditionalDisks(config.AdditionalDiskSize, config.CustomManagedImageName != "" || (config.ManagedImageName != "" && config.ImagePublisher != "")) 97 } 98 99 if config.customData != "" { 100 builder.SetCustomData(config.customData) 101 } 102 103 if config.PlanInfo.PlanName != "" { 104 builder.SetPlanInfo(config.PlanInfo.PlanName, config.PlanInfo.PlanProduct, config.PlanInfo.PlanPublisher, config.PlanInfo.PlanPromotionCode) 105 } 106 107 if config.VirtualNetworkName != "" && DefaultPrivateVirtualNetworkWithPublicIp != config.PrivateVirtualNetworkWithPublicIp { 108 builder.SetPrivateVirtualNetworkWithPublicIp( 109 config.VirtualNetworkResourceGroupName, 110 config.VirtualNetworkName, 111 config.VirtualNetworkSubnetName) 112 } else if config.VirtualNetworkName != "" { 113 builder.SetVirtualNetwork( 114 config.VirtualNetworkResourceGroupName, 115 config.VirtualNetworkName, 116 config.VirtualNetworkSubnetName) 117 } 118 119 builder.SetTags(&config.AzureTags) 120 doc, _ := builder.ToJSON() 121 return createDeploymentParameters(*doc, params) 122 } 123 124 func createDeploymentParameters(doc string, parameters *template.TemplateParameters) (*resources.Deployment, error) { 125 var template map[string]interface{} 126 err := json.Unmarshal(([]byte)(doc), &template) 127 if err != nil { 128 return nil, err 129 } 130 131 bs, err := json.Marshal(*parameters) 132 if err != nil { 133 return nil, err 134 } 135 136 var templateParameters map[string]interface{} 137 err = json.Unmarshal(bs, &templateParameters) 138 if err != nil { 139 return nil, err 140 } 141 142 return &resources.Deployment{ 143 Properties: &resources.DeploymentProperties{ 144 Mode: resources.Incremental, 145 Template: &template, 146 Parameters: &templateParameters, 147 }, 148 }, nil 149 }