|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.compute.samples; |
| 5 | + |
| 6 | +import com.azure.core.credential.TokenCredential; |
| 7 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 8 | +import com.azure.core.management.AzureEnvironment; |
| 9 | +import com.azure.core.management.Region; |
| 10 | +import com.azure.core.management.profile.AzureProfile; |
| 11 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 12 | +import com.azure.resourcemanager.AzureResourceManager; |
| 13 | +import com.azure.resourcemanager.compute.models.CachingTypes; |
| 14 | +import com.azure.resourcemanager.compute.models.Gallery; |
| 15 | +import com.azure.resourcemanager.compute.models.GalleryDataDiskImage; |
| 16 | +import com.azure.resourcemanager.compute.models.GalleryImage; |
| 17 | +import com.azure.resourcemanager.compute.models.GalleryImageVersion; |
| 18 | +import com.azure.resourcemanager.compute.models.HyperVGeneration; |
| 19 | +import com.azure.resourcemanager.compute.models.HyperVGenerationTypes; |
| 20 | +import com.azure.resourcemanager.compute.models.KnownWindowsVirtualMachineImage; |
| 21 | +import com.azure.resourcemanager.compute.models.OperatingSystemStateTypes; |
| 22 | +import com.azure.resourcemanager.compute.models.RunCommandInput; |
| 23 | +import com.azure.resourcemanager.compute.models.VirtualMachine; |
| 24 | +import com.azure.resourcemanager.compute.models.VirtualMachineCustomImage; |
| 25 | +import com.azure.resourcemanager.compute.models.VirtualMachineDataDisk; |
| 26 | +import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes; |
| 27 | +import com.azure.resourcemanager.resources.models.ResourceGroup; |
| 28 | +import com.azure.resourcemanager.samples.Utils; |
| 29 | + |
| 30 | +import java.util.Arrays; |
| 31 | + |
| 32 | +/** |
| 33 | + * Azure Compute sample for creating virtual machine with TrustedLaunch from gallery image. |
| 34 | + * - Create a managed virtual machine with TrustedLaunch from PIR windows image with two empty data disks |
| 35 | + * - Prepare virtual machine for generalization |
| 36 | + * - Deallocate virtual machine |
| 37 | + * - Generalize virtual machine |
| 38 | + * - Create a virtual machine custom image from the created virtual machine |
| 39 | + * - Create a gallery to hold gallery images |
| 40 | + * - Create a gallery image in the gallery with hypervisor generation 2 and with TrustedLaunch feature |
| 41 | + * - Create a gallery image version from the virtual machine custom image |
| 42 | + * - Create a virtual machine with TrustedLaunch from the gallery image version |
| 43 | + */ |
| 44 | +public class CreateVirtualMachineWithTrustedLaunchFromGalleryImage { |
| 45 | + /** |
| 46 | + * Main function which runs the actual sample. |
| 47 | + * @param azure instance of the azure client |
| 48 | + * @return true if sample runs successfully |
| 49 | + */ |
| 50 | + public static boolean runSample(AzureResourceManager azure) { |
| 51 | + final String rgName = Utils.randomResourceName(azure, "rg", 15); |
| 52 | + final Region region = Region.US_WEST; |
| 53 | + final String newRgName = Utils.randomResourceName(azure, "rg", 15); |
| 54 | + final Region newRegion = Region.US_WEST2; |
| 55 | + |
| 56 | + ResourceGroup resourceGroup = null; |
| 57 | + ResourceGroup newResourceGroup = null; |
| 58 | + try { |
| 59 | + resourceGroup = azure.resourceGroups() |
| 60 | + .define(rgName) |
| 61 | + .withRegion(region) |
| 62 | + .create(); |
| 63 | + newResourceGroup = azure.resourceGroups() |
| 64 | + .define(newRgName) |
| 65 | + .withRegion(newRegion) |
| 66 | + .create(); |
| 67 | + |
| 68 | + //============================================================= |
| 69 | + // Create a managed virtual machine with TrustedLaunch from PIR image with two empty data disks |
| 70 | + final String trustedVmName = Utils.randomResourceName(azure, "vm", 15); |
| 71 | + VirtualMachine trustedVMFromPirImage = azure |
| 72 | + .virtualMachines() |
| 73 | + .define(trustedVmName) |
| 74 | + .withRegion(resourceGroup.region()) |
| 75 | + .withExistingResourceGroup(resourceGroup) |
| 76 | + .withNewPrimaryNetwork("10.0.0.0/28") |
| 77 | + .withPrimaryPrivateIPAddressDynamic() |
| 78 | + .withoutPrimaryPublicIPAddress() |
| 79 | + .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2016_DATACENTER_GEN2) |
| 80 | + .withAdminUsername("azureuser") |
| 81 | + .withAdminPassword(Utils.password()) |
| 82 | + .withNewDataDisk(10) |
| 83 | + .withNewDataDisk(10, 2, CachingTypes.READ_WRITE) |
| 84 | + .withSize(VirtualMachineSizeTypes.STANDARD_DS1_V2) |
| 85 | + // Trusted Launch options start |
| 86 | + .withTrustedLaunch() |
| 87 | + .withSecureBoot() |
| 88 | + .withVTpm() |
| 89 | + // Trusted Launch options end |
| 90 | + .create(); |
| 91 | + |
| 92 | + Utils.print(trustedVMFromPirImage); |
| 93 | + |
| 94 | + //============================================================= |
| 95 | + // Prepare virtual machine for generalization |
| 96 | + prepareWindowsVMForGeneralization(trustedVMFromPirImage); |
| 97 | + |
| 98 | + //============================================================= |
| 99 | + // Deallocate virtual machine |
| 100 | + trustedVMFromPirImage.deallocate(); |
| 101 | + |
| 102 | + //============================================================= |
| 103 | + // Generalize virtual machine |
| 104 | + trustedVMFromPirImage.generalize(); |
| 105 | + |
| 106 | + //============================================================= |
| 107 | + // Create a virtual machine custom image |
| 108 | + final String imageName = Utils.randomResourceName(azure, "img", 20); |
| 109 | + VirtualMachineCustomImage.DefinitionStages.WithCreateAndDataDiskImageOSDiskSettings creatableDisk = |
| 110 | + azure.virtualMachineCustomImages() |
| 111 | + .define(imageName) |
| 112 | + .withRegion(resourceGroup.region()) |
| 113 | + .withExistingResourceGroup(resourceGroup) |
| 114 | + .withHyperVGeneration(HyperVGenerationTypes.V2) |
| 115 | + .withWindowsFromDisk(trustedVMFromPirImage.osDiskId(), OperatingSystemStateTypes.GENERALIZED) |
| 116 | + .withOSDiskCaching(trustedVMFromPirImage.osDiskCachingType()); |
| 117 | + |
| 118 | + for (VirtualMachineDataDisk dataDisk : trustedVMFromPirImage.dataDisks().values()) { |
| 119 | + creatableDisk.defineDataDiskImage() |
| 120 | + .withLun(dataDisk.lun()) |
| 121 | + .fromManagedDisk(dataDisk.id()) |
| 122 | + .withDiskCaching(dataDisk.cachingType()) |
| 123 | + .withDiskSizeInGB(dataDisk.size()) |
| 124 | + .attach(); |
| 125 | + } |
| 126 | + |
| 127 | + VirtualMachineCustomImage customImage = creatableDisk.create(); |
| 128 | + Utils.print(customImage); |
| 129 | + |
| 130 | + //============================================================= |
| 131 | + // Create a gallery to hold gallery images |
| 132 | + final String galleryName = Utils.randomResourceName(azure, "jsim", 15); |
| 133 | + Gallery gallery = azure |
| 134 | + .galleries() |
| 135 | + .define(galleryName) |
| 136 | + .withRegion(Region.US_WEST_CENTRAL) |
| 137 | + .withExistingResourceGroup(resourceGroup) |
| 138 | + .withDescription("java's image gallery") |
| 139 | + .create(); |
| 140 | + |
| 141 | + //============================================================= |
| 142 | + // Create a gallery image in the gallery with hypervisor generation 2 and with TrustedLaunch feature |
| 143 | + final String galleryImageName = "SampleImages"; |
| 144 | + GalleryImage galleryImage = azure |
| 145 | + .galleryImages() |
| 146 | + .define(galleryImageName) |
| 147 | + .withExistingGallery(gallery) |
| 148 | + .withLocation(resourceGroup.region()) |
| 149 | + .withIdentifier("JavaSDKTeam", "JDK", "Jdk-9") |
| 150 | + .withGeneralizedWindows() |
| 151 | + .withHyperVGeneration(HyperVGeneration.V2) |
| 152 | + .withTrustedLaunch() |
| 153 | + .create(); |
| 154 | + |
| 155 | + //============================================================= |
| 156 | + // Create a gallery image version from the virtual machine custom image |
| 157 | + final String versionName = "0.0.1"; |
| 158 | + GalleryImageVersion imageVersion = azure |
| 159 | + .galleryImageVersions() |
| 160 | + .define(versionName) |
| 161 | + .withExistingImage(resourceGroup.name(), gallery.name(), galleryImage.name()) |
| 162 | + .withLocation(resourceGroup.region()) |
| 163 | + .withSourceCustomImage(customImage) |
| 164 | + .withRegionAvailability(newRegion, 1) |
| 165 | + .create(); |
| 166 | + |
| 167 | + //============================================================= |
| 168 | + // Create a virtual machine with TrustedLaunch from the gallery image version |
| 169 | + final String vmFromImageName = Utils.randomResourceName(azure, "tlvm", 15); |
| 170 | + VirtualMachine.DefinitionStages.WithManagedCreate withManagedCreate = |
| 171 | + azure |
| 172 | + .virtualMachines() |
| 173 | + .define(vmFromImageName) |
| 174 | + .withRegion(newRegion) |
| 175 | + .withExistingResourceGroup(newResourceGroup) |
| 176 | + .withNewPrimaryNetwork("10.0.1.0/28") |
| 177 | + .withPrimaryPrivateIPAddressDynamic() |
| 178 | + .withoutPrimaryPublicIPAddress() |
| 179 | + .withGeneralizedWindowsGalleryImageVersion(imageVersion.id()) |
| 180 | + .withAdminUsername("jvuser") |
| 181 | + .withAdminPassword(Utils.password()); |
| 182 | + |
| 183 | + for (GalleryDataDiskImage ddi : imageVersion.storageProfile().dataDiskImages()) { |
| 184 | + withManagedCreate.withNewDataDiskFromImage( |
| 185 | + ddi.lun(), |
| 186 | + ddi.sizeInGB() + 1, |
| 187 | + ddi.hostCaching() == null ? null : CachingTypes.fromString(ddi.hostCaching().toString())); |
| 188 | + } |
| 189 | + |
| 190 | + VirtualMachine trustedVMFromGalleryImage = withManagedCreate |
| 191 | + .withSize(VirtualMachineSizeTypes.STANDARD_DS1_V2) |
| 192 | + // gallery images with 'TrustedLaunch` feature can only create VMs with 'TrustedLaunch' feature |
| 193 | + .withTrustedLaunch() |
| 194 | + .withSecureBoot() |
| 195 | + .withVTpm() |
| 196 | + .create(); |
| 197 | + Utils.print(trustedVMFromGalleryImage); |
| 198 | + |
| 199 | + return true; |
| 200 | + } catch (Exception e) { |
| 201 | + e.printStackTrace(); |
| 202 | + return false; |
| 203 | + } finally { |
| 204 | + if (resourceGroup != null) { |
| 205 | + azure.resourceGroups().beginDeleteByName(rgName); |
| 206 | + } |
| 207 | + if (newResourceGroup != null) { |
| 208 | + azure.resourceGroups().beginDeleteByName(newRgName); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + //https://learn.microsoft.com/en-us/azure/virtual-machines/generalize#windows |
| 214 | + private static void prepareWindowsVMForGeneralization(VirtualMachine virtualMachine) { |
| 215 | + System.out.println("Trying to de-provision"); |
| 216 | + virtualMachine.manager() |
| 217 | + .serviceClient() |
| 218 | + .getVirtualMachines() |
| 219 | + .beginRunCommand( |
| 220 | + virtualMachine.resourceGroupName(), virtualMachine.name(), |
| 221 | + new RunCommandInput() |
| 222 | + .withCommandId("RunPowerShellScript") |
| 223 | + .withScript(Arrays.asList( |
| 224 | + "Remove-Item 'C:\\Windows\\Panther' -Recurse", |
| 225 | + "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /mode:vm /shutdown" |
| 226 | + ))) |
| 227 | + .waitForCompletion(); |
| 228 | + System.out.println("De-provision finished"); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Main entry point. |
| 233 | + * @param args the parameters |
| 234 | + */ |
| 235 | + public static void main(String[] args) { |
| 236 | + try { |
| 237 | + //============================================================= |
| 238 | + // Authenticate |
| 239 | + |
| 240 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 241 | + final TokenCredential credential = new DefaultAzureCredentialBuilder() |
| 242 | + .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint()) |
| 243 | + .build(); |
| 244 | + |
| 245 | + AzureResourceManager azureResourceManager = AzureResourceManager |
| 246 | + .configure() |
| 247 | + .withLogLevel(HttpLogDetailLevel.BASIC) |
| 248 | + .authenticate(credential, profile) |
| 249 | + .withDefaultSubscription(); |
| 250 | + |
| 251 | + // Print selected subscription |
| 252 | + System.out.println("Selected subscription: " + azureResourceManager.subscriptionId()); |
| 253 | + |
| 254 | + runSample(azureResourceManager); |
| 255 | + } catch (Exception e) { |
| 256 | + System.out.println(e.getMessage()); |
| 257 | + e.printStackTrace(); |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments