Search in sources :

Example 1 with VirtualMachineDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineDataDisk in project azure-sdk-for-java by Azure.

the class ManageManagedDisks method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    final Region region = Region.US_EAST;
    final String rgName = Utils.createRandomName("rgCOMV");
    final String userName = "tirekicker";
    final String sshkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
    try {
        // ::==Create a VM
        // Create a virtual machine with an implicit Managed OS disk and explicit Managed data disk
        System.out.println("Creating VM [with an implicit Managed OS disk and explicit Managed data disk]");
        final String linuxVM1Name = SdkContext.randomResourceName("vm" + "-", 18);
        final String linuxVM1Pip = SdkContext.randomResourceName("pip" + "-", 18);
        VirtualMachine linuxVM1 = azure.virtualMachines().define(linuxVM1Name).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM1Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshkey).withNewDataDisk(100).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created VM [with an implicit Managed OS disk and explicit Managed data disk]");
        // Creation is simplified with implicit creation of managed disks without specifying all the disk details. You will notice that you do not require storage accounts
        // ::== Update the VM
        // Create a VMSS with implicit managed OS disks and explicit managed data disks
        System.out.println("Creating VMSS [with implicit managed OS disks and explicit managed data disks]");
        final String vmScaleSetName = SdkContext.randomResourceName("vmss" + "-", 18);
        VirtualMachineScaleSet vmScaleSet = azure.virtualMachineScaleSets().define(vmScaleSetName).withRegion(region).withExistingResourceGroup(rgName).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D5_V2).withExistingPrimaryNetworkSubnet(prepareNetwork(azure, region, rgName), "subnet1").withExistingPrimaryInternetFacingLoadBalancer(prepareLoadBalancer(azure, region, rgName)).withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername("tirekicker").withSsh(sshkey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withNewDataDisk(100, 2, CachingTypes.READ_ONLY).withCapacity(3).create();
        System.out.println("Created VMSS [with implicit managed OS disks and explicit managed data disks]");
        // Create an empty disk and attach to a VM (Manage Virtual Machine With Disk)
        System.out.println("Creating empty data disk [to attach to a VM]");
        final String diskName = SdkContext.randomResourceName("dsk" + "-", 18);
        Disk dataDisk = azure.disks().define(diskName).withRegion(region).withExistingResourceGroup(rgName).withData().withSizeInGB(50).create();
        System.out.println("Created empty data disk [to attach to a VM]");
        System.out.println("Creating VM [with new managed data disks and disk attached]");
        final String linuxVM2Name = SdkContext.randomResourceName("vm" + "-", 10);
        final String linuxVM2Pip = SdkContext.randomResourceName("pip" + "-", 18);
        VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVM2Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM2Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshkey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withExistingDataDisk(dataDisk).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created VM [with new managed data disks and disk attached]");
        // Update a VM
        System.out.println("Updating VM [by detaching a disk and adding empty disk]");
        linuxVM2.update().withoutDataDisk(2).withNewDataDisk(200).apply();
        System.out.println("Updated VM [by detaching a disk and adding empty disk]");
        // Create a VM from an image (Create Virtual Machine Using Custom Image from VM)
        System.out.println("Preparing specialized virtual machine with un-managed disk");
        final VirtualMachine linuxVM = prepareSpecializedUnmanagedVirtualMachine(azure, region, rgName);
        System.out.println("Prepared specialized virtual machine with un-managed disk");
        System.out.println("Creating custom image from specialized virtual machine");
        final String customImageName = SdkContext.randomResourceName("cimg" + "-", 10);
        VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(rgName).fromVirtualMachine(// from a deallocated and generalized VM
        linuxVM).create();
        System.out.println("Created custom image from specialized virtual machine");
        System.out.println("Creating VM [from custom image]");
        final String linuxVM3Name = SdkContext.randomResourceName("vm" + "-", 10);
        VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVM3Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withSsh(sshkey).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created VM [from custom image]");
        // Create a VM from a VHD (Create Virtual Machine Using Specialized VHD)
        final String linuxVMName4 = SdkContext.randomResourceName("vm" + "-", 10);
        final String specializedVhd = linuxVM.osUnmanagedDiskVhdUri();
        azure.virtualMachines().deleteById(linuxVM.id());
        System.out.println("Creating VM [by attaching un-managed disk]");
        VirtualMachine linuxVM4 = azure.virtualMachines().define(linuxVMName4).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSUnmanagedDisk(specializedVhd, OperatingSystemTypes.LINUX).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created VM [by attaching un-managed disk]");
        // Create a Snapshot (Create Virtual Machine using specialized disks from snapshot)
        System.out.println("Preparing specialized virtual machine with managed disks");
        final VirtualMachine linuxVM5 = prepareSpecializedManagedVirtualMachine(azure, region, rgName);
        Disk osDisk = azure.disks().getById(linuxVM5.osDiskId());
        List<Disk> dataDisks = new ArrayList<>();
        for (VirtualMachineDataDisk disk : linuxVM5.dataDisks().values()) {
            Disk d = azure.disks().getById(disk.id());
            dataDisks.add(d);
        }
        System.out.println("Prepared specialized virtual machine with managed disks");
        System.out.println("Deleting VM: " + linuxVM5.id());
        azure.virtualMachines().deleteById(linuxVM5.id());
        System.out.println("Deleted the VM: " + linuxVM5.id());
        System.out.println("Creating snapshot [from managed OS disk]");
        // Create a managed snapshot for an OS disk
        final String managedOSSnapshotName = SdkContext.randomResourceName("snp" + "-", 10);
        Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromDisk(osDisk).create();
        System.out.println("Created snapshot [from managed OS disk]");
        System.out.println("Creating managed OS disk [from snapshot]");
        // Create a managed disk from the managed snapshot for the OS disk
        final String managedNewOSDiskName = SdkContext.randomResourceName("dsk" + "-", 10);
        Disk newOSDisk = azure.disks().define(managedNewOSDiskName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromSnapshot(osSnapshot).withSizeInGB(100).create();
        System.out.println("Created managed OS disk [from snapshot]");
        System.out.println("Creating managed data snapshot [from managed data disk]");
        // Create a managed snapshot for a data disk
        final String managedDataDiskSnapshotName = SdkContext.randomResourceName("dsk" + "-", 10);
        Snapshot dataSnapshot = azure.snapshots().define(managedDataDiskSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withDataFromDisk(dataDisks.get(0)).withSku(DiskSkuTypes.STANDARD_LRS).create();
        System.out.println("Created managed data snapshot [from managed data disk]");
        System.out.println("Creating managed data disk [from managed snapshot]");
        // Create a managed disk from the managed snapshot for the data disk
        final String managedNewDataDiskName = SdkContext.randomResourceName("dsk" + "-", 10);
        Disk newDataDisk = azure.disks().define(managedNewDataDiskName).withRegion(region).withExistingResourceGroup(rgName).withData().fromSnapshot(dataSnapshot).create();
        System.out.println("Created managed data disk [from managed snapshot]");
        System.out.println("Creating VM [with specialized OS managed disk]");
        final String linuxVM6Name = SdkContext.randomResourceName("vm" + "-", 10);
        VirtualMachine linuxVM6 = azure.virtualMachines().define(linuxVM6Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSDisk(newOSDisk, OperatingSystemTypes.LINUX).withExistingDataDisk(newDataDisk).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created VM [with specialized OS managed disk]");
        // ::== Migrate a VM to managed disks with a single reboot
        System.out.println("Creating VM [with un-managed disk for migration]");
        final String linuxVM7Name = SdkContext.randomResourceName("vm" + "-", 10);
        final String linuxVM7Pip = SdkContext.randomResourceName("pip" + "-", 18);
        VirtualMachine linuxVM7 = azure.virtualMachines().define(linuxVM7Name).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM7Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername("tirekicker").withSsh(sshkey).withUnmanagedDisks().withNewUnmanagedDataDisk(100).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created VM [with un-managed disk for migration]");
        System.out.println("De-allocating VM :" + linuxVM7.id());
        linuxVM7.deallocate();
        System.out.println("De-allocated VM :" + linuxVM7.id());
        System.out.println("Migrating VM");
        linuxVM7.convertToManaged();
        System.out.println("Migrated VM");
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().deleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Snapshot(com.microsoft.azure.management.compute.Snapshot) VirtualMachineCustomImage(com.microsoft.azure.management.compute.VirtualMachineCustomImage) ArrayList(java.util.ArrayList) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) VirtualMachineScaleSet(com.microsoft.azure.management.compute.VirtualMachineScaleSet) VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Disk(com.microsoft.azure.management.compute.Disk) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 2 with VirtualMachineDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineDataDisk in project azure-sdk-for-java by Azure.

the class CreateVirtualMachineUsingCustomImageFromVM method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    final String linuxVMName1 = Utils.createRandomName("VM1");
    final String linuxVMName2 = Utils.createRandomName("VM2");
    final String linuxVMName3 = Utils.createRandomName("VM3");
    final String customImageName = Utils.createRandomName("img");
    final String rgName = Utils.createRandomName("rgCOMV");
    final String publicIpDnsLabel = Utils.createRandomName("pip");
    final String userName = "tirekicker";
    final String password = "12NewPA$$w0rd!";
    final Region region = Region.US_WEST_CENTRAL;
    final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
    final String apacheInstallCommand = "bash install_apache.sh";
    List<String> apacheInstallScriptUris = new ArrayList<>();
    apacheInstallScriptUris.add(apacheInstallScript);
    try {
        //=============================================================
        // Create a Linux VM using a PIR image with un-managed OS and data disks and customize virtual
        // machine using custom script extension
        System.out.println("Creating a un-managed Linux VM");
        VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName1).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIpDnsLabel).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withUnmanagedDisks().defineUnmanagedDataDisk("disk-1").withNewVhd(100).withLun(1).attach().defineUnmanagedDataDisk("disk-2").withNewVhd(50).withLun(2).attach().defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", apacheInstallScriptUris).withPublicSetting("commandToExecute", apacheInstallCommand).attach().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created a Linux VM with un-managed OS and data disks: " + linuxVM.id());
        Utils.print(linuxVM);
        // De-provision the virtual machine
        deprovisionAgentInLinuxVM(linuxVM.getPrimaryPublicIPAddress().fqdn(), 22, userName, password);
        //=============================================================
        // Deallocate the virtual machine
        System.out.println("Deallocate VM: " + linuxVM.id());
        linuxVM.deallocate();
        System.out.println("De-allocated VM: " + linuxVM.id() + "; state = " + linuxVM.powerState());
        //=============================================================
        // Generalize the virtual machine
        System.out.println("Generalize VM: " + linuxVM.id());
        linuxVM.generalize();
        System.out.println("Generalized VM: " + linuxVM.id());
        //=============================================================
        // Capture the virtual machine to get a 'Generalized image' with Apache
        System.out.println("Capturing VM as custom image: " + linuxVM.id());
        VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(rgName).fromVirtualMachine(linuxVM).create();
        System.out.println("Captured VM: " + linuxVM.id());
        Utils.print(virtualMachineCustomImage);
        //=============================================================
        // Create a Linux VM using custom image
        System.out.println("Creating a Linux VM using custom image - " + virtualMachineCustomImage.id());
        VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        Utils.print(linuxVM2);
        //=============================================================
        // Create another Linux VM using custom image and configure the data disks from image and
        // add another data disk
        System.out.println("Creating another Linux VM with additional data disks using custom image - " + virtualMachineCustomImage.id());
        VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVMName3).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withNewDataDiskFromImage(1, 200, // TODO: Naming needs to be finalized
        CachingTypes.READ_WRITE).withNewDataDiskFromImage(2, 100, CachingTypes.READ_ONLY).withNewDataDisk(50).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        Utils.print(linuxVM3);
        // Getting the SAS URIs requires virtual machines to be de-allocated
        // [Access not permitted because'disk' is currently attached to running VM]
        //
        System.out.println("De-allocating the virtual machine - " + linuxVM3.id());
        linuxVM3.deallocate();
        //=============================================================
        // Get the readonly SAS URI to the OS and data disks
        System.out.println("Getting OS and data disks SAS Uris");
        // OS Disk SAS Uri
        Disk osDisk = azure.disks().getById(linuxVM3.osDiskId());
        String osDiskSasUri = osDisk.grantAccess(24 * 60);
        System.out.println("OS disk SAS Uri: " + osDiskSasUri);
        // Data disks SAS Uri
        for (VirtualMachineDataDisk disk : linuxVM3.dataDisks().values()) {
            Disk dataDisk = azure.disks().getById(disk.id());
            String dataDiskSasUri = dataDisk.grantAccess(24 * 60);
            System.out.println(String.format("Data disk (lun: %d) SAS Uri: %s", disk.lun(), dataDiskSasUri));
        }
        //=============================================================
        // Deleting the custom image
        System.out.println("Deleting custom Image: " + virtualMachineCustomImage.id());
        azure.virtualMachineCustomImages().deleteById(virtualMachineCustomImage.id());
        System.out.println("Deleted custom image");
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().deleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) VirtualMachineCustomImage(com.microsoft.azure.management.compute.VirtualMachineCustomImage) ArrayList(java.util.ArrayList) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Disk(com.microsoft.azure.management.compute.Disk) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 3 with VirtualMachineDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineDataDisk in project azure-sdk-for-java by Azure.

the class CreateVirtualMachineUsingSpecializedDiskFromSnapshot method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    final String linuxVMName1 = Utils.createRandomName("VM1");
    final String linuxVMName2 = Utils.createRandomName("VM2");
    final String managedOSSnapshotName = Utils.createRandomName("ss-os-");
    final String managedDataDiskSnapshotPrefix = Utils.createRandomName("ss-data-");
    final String managedNewOSDiskName = Utils.createRandomName("ds-os-nw-");
    final String managedNewDataDiskNamePrefix = Utils.createRandomName("ds-data-nw-");
    final String rgName = Utils.createRandomName("rgCOMV");
    final String publicIpDnsLabel = Utils.createRandomName("pip");
    final String userName = "tirekicker";
    final String password = "12NewPA$$w0rd!";
    final Region region = Region.US_WEST_CENTRAL;
    final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
    final String apacheInstallCommand = "bash install_apache.sh";
    List<String> apacheInstallScriptUris = new ArrayList<>();
    apacheInstallScriptUris.add(apacheInstallScript);
    try {
        //=============================================================
        // Create a Linux VM using a PIR image with managed OS and data disks and customize virtual
        // machine using custom script extension
        System.out.println("Creating a un-managed Linux VM");
        VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName1).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIpDnsLabel).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", apacheInstallScriptUris).withPublicSetting("commandToExecute", apacheInstallCommand).attach().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created a Linux VM with managed OS and data disks: " + linuxVM.id());
        Utils.print(linuxVM);
        // Gets the specialized managed OS and Data disks of the virtual machine
        //
        Disk osDisk = azure.disks().getById(linuxVM.osDiskId());
        List<Disk> dataDisks = new ArrayList<>();
        for (VirtualMachineDataDisk disk : linuxVM.dataDisks().values()) {
            Disk dataDisk = azure.disks().getById(disk.id());
            dataDisks.add(dataDisk);
        }
        //=============================================================
        // Delete the virtual machine
        System.out.println("Deleting VM: " + linuxVM.id());
        azure.virtualMachines().deleteById(linuxVM.id());
        System.out.println("Deleted the VM");
        //=============================================================
        // Create Snapshot from the OS managed disk
        System.out.println(String.format("Creating managed snapshot from the managed disk (holding specialized OS): %s ", osDisk.id()));
        Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromDisk(osDisk).create();
        System.out.println("Created managed snapshot holding OS: " + osSnapshot.id());
        // Utils.print(osSnapshot); TODO
        //=============================================================
        // Create Managed snapshot from the Data managed disks
        List<Snapshot> dataSnapshots = new ArrayList<>();
        int i = 0;
        for (Disk dataDisk : dataDisks) {
            System.out.println(String.format("Creating managed snapshot from the managed disk (holding data): %s ", dataDisk.id()));
            Snapshot dataSnapshot = azure.snapshots().define(managedDataDiskSnapshotPrefix + "-" + i).withRegion(region).withExistingResourceGroup(rgName).withDataFromDisk(dataDisk).withSku(DiskSkuTypes.STANDARD_LRS).create();
            dataSnapshots.add(dataSnapshot);
            System.out.println("Created managed snapshot holding data: " + dataSnapshot.id());
            // Utils.print(dataDisk); TODO
            i++;
        }
        //=============================================================
        // Create Managed disk from the specialized OS snapshot
        System.out.println(String.format("Creating managed disk from the snapshot holding OS: %s ", osSnapshot.id()));
        Disk newOSDisk = azure.disks().define(managedNewOSDiskName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromSnapshot(osSnapshot).withSizeInGB(100).create();
        System.out.println("Created managed disk holding OS: " + osDisk.id());
        // Utils.print(osDisk); TODO
        //=============================================================
        // Create Managed disks from the data snapshots
        List<Disk> newDataDisks = new ArrayList<>();
        i = 0;
        for (Snapshot dataSnapshot : dataSnapshots) {
            System.out.println(String.format("Creating managed disk from the Data snapshot: %s ", dataSnapshot.id()));
            Disk dataDisk = azure.disks().define(managedNewDataDiskNamePrefix + "-" + i).withRegion(region).withExistingResourceGroup(rgName).withData().fromSnapshot(dataSnapshot).create();
            newDataDisks.add(dataDisk);
            System.out.println("Created managed disk holding data: " + dataDisk.id());
            // Utils.print(dataDisk); TODO
            i++;
        }
        //
        //=============================================================
        // Create a Linux VM by attaching the managed disks
        System.out.println("Creating a Linux VM using specialized OS and data disks");
        VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSDisk(newOSDisk, OperatingSystemTypes.LINUX).withExistingDataDisk(newDataDisks.get(0)).withExistingDataDisk(newDataDisks.get(1), 1, CachingTypes.READ_WRITE).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        Utils.print(linuxVM2);
        //=============================================================
        //
        System.out.println("Deleting OS snapshot - " + osSnapshot.id());
        azure.snapshots().deleteById(osSnapshot.id());
        System.out.println("Deleted OS snapshot");
        for (Snapshot dataSnapshot : dataSnapshots) {
            System.out.println("Deleting data snapshot - " + dataSnapshot.id());
            azure.snapshots().deleteById(dataSnapshot.id());
            System.out.println("Deleted data snapshot");
        }
        // Getting the SAS URIs requires virtual machines to be de-allocated
        // [Access not permitted because'disk' is currently attached to running VM]
        //
        System.out.println("De-allocating the virtual machine - " + linuxVM2.id());
        linuxVM2.deallocate();
        //=============================================================
        // Get the readonly SAS URI to the OS and data disks
        System.out.println("Getting OS and data disks SAS Uris");
        // OS Disk SAS Uri
        osDisk = azure.disks().getById(linuxVM2.osDiskId());
        String osDiskSasUri = osDisk.grantAccess(24 * 60);
        System.out.println("OS disk SAS Uri: " + osDiskSasUri);
        // Data disks SAS Uri
        for (VirtualMachineDataDisk disk : linuxVM2.dataDisks().values()) {
            Disk dataDisk = azure.disks().getById(disk.id());
            String dataDiskSasUri = dataDisk.grantAccess(24 * 60);
            System.out.println(String.format("Data disk (lun: %d) SAS Uri: %s", disk.lun(), dataDiskSasUri));
        }
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Snapshot(com.microsoft.azure.management.compute.Snapshot) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Disk(com.microsoft.azure.management.compute.Disk) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 4 with VirtualMachineDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineDataDisk in project azure-sdk-for-java by Azure.

the class CreateVirtualMachineUsingSpecializedDiskFromVhd method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    final String linuxVMName1 = Utils.createRandomName("VM1");
    final String linuxVMName2 = Utils.createRandomName("VM2");
    final String managedOSDiskName = Utils.createRandomName("ds-os-");
    final String managedDataDiskNamePrefix = Utils.createRandomName("ds-data-");
    final String rgName = Utils.createRandomName("rgCOMV");
    final String publicIpDnsLabel = Utils.createRandomName("pip");
    final String userName = "tirekicker";
    final String password = "12NewPA$$w0rd!";
    final Region region = Region.US_WEST_CENTRAL;
    final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
    final String apacheInstallCommand = "bash install_apache.sh";
    List<String> apacheInstallScriptUris = new ArrayList<>();
    apacheInstallScriptUris.add(apacheInstallScript);
    try {
        //=============================================================
        // Create a Linux VM using an image from PIR (Platform Image Repository)
        System.out.println("Creating a un-managed Linux VM");
        VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName1).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIpDnsLabel).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withUnmanagedDisks().defineUnmanagedDataDisk("disk-1").withNewVhd(50).withLun(1).attach().defineUnmanagedDataDisk("disk-2").withNewVhd(50).withLun(2).attach().defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", apacheInstallScriptUris).withPublicSetting("commandToExecute", apacheInstallCommand).attach().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created a Linux VM with un-managed OS and data disks: " + linuxVM.id());
        Utils.print(linuxVM);
        // Gets the specialized OS and Data disk VHDs of the virtual machine
        //
        String specializedOSVhdUri = linuxVM.osUnmanagedDiskVhdUri();
        List<String> dataVhdUris = new ArrayList<>();
        for (VirtualMachineUnmanagedDataDisk dataDisk : linuxVM.unmanagedDataDisks().values()) {
            dataVhdUris.add(dataDisk.vhdUri());
        }
        //=============================================================
        // Delete the virtual machine
        System.out.println("Deleting VM: " + linuxVM.id());
        azure.virtualMachines().deleteById(linuxVM.id());
        System.out.println("Deleted the VM");
        //=============================================================
        // Create Managed disk from the specialized OS VHD
        System.out.println(String.format("Creating managed disk from the specialized OS VHD: %s ", specializedOSVhdUri));
        Disk osDisk = azure.disks().define(managedOSDiskName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromVhd(specializedOSVhdUri).withSizeInGB(100).create();
        System.out.println("Created managed disk holding OS: " + osDisk.id());
        // Utils.print(osDisk); TODO
        //=============================================================
        // Create Managed disks from the Data VHDs
        List<Disk> dataDisks = new ArrayList<>();
        int i = 0;
        for (String dataVhdUri : dataVhdUris) {
            System.out.println(String.format("Creating managed disk from the Data VHD: %s ", dataVhdUri));
            Disk dataDisk = azure.disks().define(managedDataDiskNamePrefix + "-" + i).withRegion(region).withExistingResourceGroup(rgName).withData().fromVhd(dataVhdUri).withSizeInGB(150).withSku(DiskSkuTypes.STANDARD_LRS).create();
            dataDisks.add(dataDisk);
            System.out.println("Created managed disk holding data: " + dataDisk.id());
            // Utils.print(dataDisk); TODO
            i++;
        }
        //=============================================================
        // Create a Linux VM by attaching the disks
        System.out.println("Creating a Linux VM using specialized OS and data disks");
        VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSDisk(osDisk, OperatingSystemTypes.LINUX).withExistingDataDisk(dataDisks.get(0)).withExistingDataDisk(dataDisks.get(1), 1, CachingTypes.READ_WRITE).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        Utils.print(linuxVM2);
        List<String> dataDiskIds = new ArrayList<>();
        for (VirtualMachineDataDisk disk : linuxVM2.dataDisks().values()) {
            dataDiskIds.add(disk.id());
        }
        //=============================================================
        // Detach the data disks from the virtual machine
        System.out.println("Updating VM by detaching the data disks");
        linuxVM2.update().withoutDataDisk(0).withoutDataDisk(1).apply();
        Utils.print(linuxVM2);
        //=============================================================
        // Get the readonly SAS URI to the data disks
        System.out.println("Getting data disks SAS Uris");
        for (String diskId : dataDiskIds) {
            Disk dataDisk = azure.disks().getById(diskId);
            String dataDiskSasUri = dataDisk.grantAccess(24 * 60);
            System.out.println(String.format("Data disk SAS Uri: %s", dataDiskSasUri));
        }
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) ArrayList(java.util.ArrayList) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Disk(com.microsoft.azure.management.compute.Disk) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) VirtualMachineUnmanagedDataDisk(com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)

Example 5 with VirtualMachineDataDisk

use of com.microsoft.azure.management.compute.VirtualMachineDataDisk in project azure-sdk-for-java by Azure.

the class CreateVirtualMachineUsingCustomImageFromVHD method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    final String linuxVMName1 = SdkContext.randomResourceName("VM1", 10);
    final String linuxVMName2 = SdkContext.randomResourceName("VM2", 10);
    final String linuxVMName3 = SdkContext.randomResourceName("VM3", 10);
    final String customImageName = SdkContext.randomResourceName("img", 10);
    final String rgName = SdkContext.randomResourceName("rgCOMV", 15);
    final String publicIPDnsLabel = SdkContext.randomResourceName("pip", 10);
    final String userName = "tirekicker";
    final String password = "12NewPA$$w0rd!";
    final Region region = Region.US_WEST_CENTRAL;
    final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
    final String apacheInstallCommand = "bash install_apache.sh";
    List<String> apacheInstallScriptUris = new ArrayList<>();
    apacheInstallScriptUris.add(apacheInstallScript);
    try {
        //=============================================================
        // Create a Linux VM using a PIR image with un-managed OS and data disks and customize virtual
        // machine using custom script extension
        System.out.println("Creating a un-managed Linux VM");
        VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName1).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIPDnsLabel).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withUnmanagedDisks().defineUnmanagedDataDisk("disk-1").withNewVhd(100).withLun(1).attach().defineUnmanagedDataDisk("disk-2").withNewVhd(50).withLun(2).attach().defineUnmanagedDataDisk("disk-3").withNewVhd(60).withLun(3).attach().defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", apacheInstallScriptUris).withPublicSetting("commandToExecute", apacheInstallCommand).attach().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created a Linux VM with un-managed OS and data disks: " + linuxVM.id());
        Utils.print(linuxVM);
        // De-provision the virtual machine
        deprovisionAgentInLinuxVM(linuxVM.getPrimaryPublicIPAddress().fqdn(), 22, userName, password);
        //=============================================================
        // Deallocate the virtual machine
        System.out.println("Deallocate VM: " + linuxVM.id());
        linuxVM.deallocate();
        System.out.println("De-allocated VM: " + linuxVM.id() + "; state = " + linuxVM.powerState());
        //=============================================================
        // Generalize the virtual machine
        System.out.println("Generalize VM: " + linuxVM.id());
        linuxVM.generalize();
        System.out.println("Generalized VM: " + linuxVM.id());
        //=============================================================
        // Create a virtual machine custom image from OS and data disks of the virtual machine
        System.out.println("Creating virtual machine custom image from un-managed disk VHDs: " + linuxVM.id());
        VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromVhd(linuxVM.osUnmanagedDiskVhdUri(), OperatingSystemStateTypes.GENERALIZED).defineDataDiskImage().withLun(linuxVM.unmanagedDataDisks().get(1).lun()).fromVhd(linuxVM.unmanagedDataDisks().get(1).vhdUri()).attach().defineDataDiskImage().withLun(linuxVM.unmanagedDataDisks().get(2).lun()).fromVhd(linuxVM.unmanagedDataDisks().get(2).vhdUri()).attach().defineDataDiskImage().withLun(linuxVM.unmanagedDataDisks().get(3).lun()).fromVhd(linuxVM.unmanagedDataDisks().get(3).vhdUri()).withDiskCaching(CachingTypes.READ_ONLY).attach().create();
        System.out.println("Created custom image");
        Utils.print(virtualMachineCustomImage);
        //=============================================================
        // Create a Linux VM using custom image
        System.out.println("Creating a Linux VM using custom image: " + virtualMachineCustomImage.id());
        VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created Linux VM");
        Utils.print(linuxVM2);
        //=============================================================
        // Create another Linux VM using custom image and configure the data disks from image and
        // add another data disk
        VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVMName3).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).withNewDataDiskFromImage(1, 200, CachingTypes.READ_WRITE).withNewDataDiskFromImage(2, 100, CachingTypes.READ_ONLY).withNewDataDiskFromImage(3, 100, CachingTypes.READ_WRITE).withNewDataDisk(50).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        Utils.print(linuxVM3);
        // Getting the SAS URIs requires virtual machines to be de-allocated
        // [Access not permitted because'disk' is currently attached to running VM]
        //
        System.out.println("De-allocating the virtual machine - " + linuxVM3.id());
        linuxVM3.deallocate();
        //=============================================================
        // Get the readonly SAS URI to the OS and data disks
        System.out.println("Getting OS and data disks SAS Uris");
        // OS Disk SAS Uri
        Disk osDisk = azure.disks().getById(linuxVM3.osDiskId());
        String osDiskSasUri = osDisk.grantAccess(24 * 60);
        System.out.println("OS disk SAS Uri: " + osDiskSasUri);
        // Data Disks SAS Uri
        for (VirtualMachineDataDisk disk : linuxVM3.dataDisks().values()) {
            Disk dataDisk = azure.disks().getById(disk.id());
            String dataDiskSasUri = dataDisk.grantAccess(24 * 60);
            System.out.println(String.format("Data disk (lun: %d) SAS Uri: %s", disk.lun(), dataDiskSasUri));
        }
        //=============================================================
        // Deleting the custom image
        System.out.println("Deleting custom Image: " + virtualMachineCustomImage.id());
        azure.virtualMachineCustomImages().deleteById(virtualMachineCustomImage.id());
        System.out.println("Deleted custom image");
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) VirtualMachineCustomImage(com.microsoft.azure.management.compute.VirtualMachineCustomImage) ArrayList(java.util.ArrayList) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) VirtualMachineDataDisk(com.microsoft.azure.management.compute.VirtualMachineDataDisk) Disk(com.microsoft.azure.management.compute.Disk) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Aggregations

Disk (com.microsoft.azure.management.compute.Disk)6 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)6 VirtualMachineDataDisk (com.microsoft.azure.management.compute.VirtualMachineDataDisk)6 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)6 ArrayList (java.util.ArrayList)6 JSchException (com.jcraft.jsch.JSchException)3 VirtualMachineCustomImage (com.microsoft.azure.management.compute.VirtualMachineCustomImage)3 IOException (java.io.IOException)3 Snapshot (com.microsoft.azure.management.compute.Snapshot)2 VirtualMachineScaleSet (com.microsoft.azure.management.compute.VirtualMachineScaleSet)1 VirtualMachineUnmanagedDataDisk (com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk)1