Search in sources :

Example 51 with VirtualMachine

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

the class ManageVirtualMachineWithDisk 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 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;
    try {
        // Creates an empty data disk to attach to the virtual machine
        //
        System.out.println("Creating an empty managed disk");
        Disk dataDisk1 = azure.disks().define(Utils.createRandomName("dsk-")).withRegion(region).withNewResourceGroup(rgName).withData().withSizeInGB(50).create();
        System.out.println("Created managed disk");
        // Prepare first creatable data disk
        //
        Creatable<Disk> dataDiskCreatable1 = azure.disks().define(Utils.createRandomName("dsk-")).withRegion(region).withExistingResourceGroup(rgName).withData().withSizeInGB(100);
        // Prepare second creatable data disk
        //
        Creatable<Disk> dataDiskCreatable2 = azure.disks().define(Utils.createRandomName("dsk-")).withRegion(region).withExistingResourceGroup(rgName).withData().withSizeInGB(50).withSku(DiskSkuTypes.STANDARD_LRS);
        //======================================================================
        // Create a Linux VM using a PIR image with managed OS and Data disks
        System.out.println("Creating a 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).withNewDataDisk(dataDiskCreatable1).withNewDataDisk(dataDiskCreatable2, 2, CachingTypes.READ_ONLY).withExistingDataDisk(dataDisk1).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
        System.out.println("Created a Linux VM with managed OS and data disks: " + linuxVM.id());
        Utils.print(linuxVM);
        //======================================================================
        // Update the virtual machine by detaching two data disks with lun 3 and 4 and adding one
        System.out.println("Updating Linux VM");
        String lun3DiskId = linuxVM.dataDisks().get(3).id();
        linuxVM.update().withoutDataDisk(3).withoutDataDisk(4).withNewDataDisk(200).apply();
        System.out.println("Updated Linux VM: " + linuxVM.id());
        Utils.print(linuxVM);
        // ======================================================================
        // Delete a managed disk
        Disk disk = azure.disks().getById(lun3DiskId);
        System.out.println("Delete managed disk: " + disk.id());
        azure.disks().deleteByResourceGroup(disk.resourceGroupName(), disk.name());
        System.out.println("Deleted managed disk");
        //======================================================================
        // Deallocate the virtual machine
        System.out.println("De-allocate Linux VM");
        linuxVM.deallocate();
        System.out.println("De-allocated Linux VM");
        //======================================================================
        // Resize the OS and Data Disks
        Disk osDisk = azure.disks().getById(linuxVM.osDiskId());
        List<Disk> dataDisks = new ArrayList<>();
        for (VirtualMachineDataDisk vmDataDisk : linuxVM.dataDisks().values()) {
            Disk dataDisk = azure.disks().getById(vmDataDisk.id());
            dataDisks.add(dataDisk);
        }
        System.out.println("Update OS disk: " + osDisk.id());
        osDisk.update().withSizeInGB(2 * osDisk.sizeInGB()).apply();
        System.out.println("OS disk updated");
        for (Disk dataDisk : dataDisks) {
            System.out.println("Update data disk: " + dataDisk.id());
            dataDisk.update().withSizeInGB(dataDisk.sizeInGB() + 10).apply();
            System.out.println("Data disk updated");
        }
        //======================================================================
        // Starting the virtual machine
        System.out.println("Starting Linux VM");
        linuxVM.start();
        System.out.println("Started Linux VM");
        Utils.print(linuxVM);
        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) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 52 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.

the class AzureDockerVMOps method deleteDockerHostAll.

public static void deleteDockerHostAll(Azure azureClient, String resourceGroup, String vmName) {
    if (azureClient == null || resourceGroup == null || vmName == null) {
        throw new AzureDockerException("Unexpected param values; Azure instance, resource group and VM name cannot be null");
    }
    VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(resourceGroup, vmName);
    if (vm == null) {
        throw new AzureDockerException(String.format("Unexpected error retrieving VM %s from Azure", vmName));
    }
    try {
        PublicIPAddress publicIp = vm.getPrimaryPublicIPAddress();
        NicIPConfiguration nicIPConfiguration = publicIp.getAssignedNetworkInterfaceIPConfiguration();
        Network vnet = nicIPConfiguration.getNetwork();
        NetworkInterface nic = vm.getPrimaryNetworkInterface();
        azureClient.virtualMachines().deleteById(vm.id());
        azureClient.networkInterfaces().deleteById(nic.id());
        azureClient.publicIPAddresses().deleteById(publicIp.id());
        azureClient.networks().deleteById(vnet.id());
    } catch (Exception e) {
        throw new AzureDockerException(String.format("Unexpected error while deleting VM %s and its associated resources", vmName));
    }
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 53 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.

the class AzureDockerVMOps method updateDockerHostVM.

public static VirtualMachine updateDockerHostVM(Azure azureClient, DockerHost dockerHost) throws AzureDockerException {
    try {
        VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
        HashMap<String, Object> protectedSettings = new HashMap<>();
        protectedSettings.put("username", dockerHost.certVault.vmUsername);
        if (dockerHost.hasPwdLogIn) {
            protectedSettings.put("password", dockerHost.certVault.vmPwd);
        }
        if (dockerHost.hasSSHLogIn) {
            protectedSettings.put("ssh_key", dockerHost.certVault.sshPubKey);
        }
        protectedSettings.put("reset_ssh", "true");
        if (vm.listExtensions().get("VMAccessForLinux") != null) {
            vm.update().updateExtension("VMAccessForLinux").withProtectedSettings(protectedSettings).parent().withoutTag("dockervault").apply();
        } else {
            vm.update().defineNewExtension("VMAccessForLinux").withPublisher("Microsoft.OSTCExtensions").withType("VMAccessForLinux").withVersion("1.4").withProtectedSettings(protectedSettings).attach().withoutTag("dockervault").apply();
        }
        return vm;
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage(), e);
    }
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 54 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.

the class DockerHostNode method loadActions.

@Override
protected void loadActions() {
    addAction(ACTION_START, ACTION_START_ICON, new NodeActionListener() {

        @Override
        public void actionPerformed(NodeActionEvent e) {
            DefaultLoader.getIdeHelper().runInBackground(null, "Starting Docker Host", false, true, "Starting Docker Host...", new Runnable() {

                @Override
                public void run() {
                    removeAllChildNodes();
                    setIconPath(DOCKERHOST_WAIT_ICON_PATH);
                    try {
                        Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerHost.sid).azureClient;
                        VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
                        if (vm != null) {
                            vm.start();
                            setIconPath(DOCKERHOST_RUN_ICON_PATH);
                            refreshDockerHostInstance(vm);
                        }
                    } catch (Exception e) {
                        DefaultLoader.getUIHelper().logError(e.getMessage(), e);
                    }
                }
            });
        }
    });
    addAction(ACTION_RESTART, ACTION_START_ICON, new RestartDockerHostAction());
    addAction(ACTION_SHUTDOWN, ACTION_SHUTDOWN_ICON, new ShutdownDockerHostAction());
    super.loadActions();
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 55 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.

the class DockerHostNode method refreshItems.

@Override
protected void refreshItems() throws AzureCmdException {
    try {
        Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerHost.sid).azureClient;
        VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
        if (vm != null) {
            refreshDockerHostInstance(vm);
        }
    } catch (Exception e) {
        DefaultLoader.getUIHelper().logError(e.getMessage(), e);
    }
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Aggregations

VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)68 ArrayList (java.util.ArrayList)21 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)20 Network (com.microsoft.azure.management.network.Network)17 Date (java.util.Date)12 Disk (com.microsoft.azure.management.compute.Disk)10 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)10 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)9 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)9 Azure (com.microsoft.azure.management.Azure)8 NetworkInterface (com.microsoft.azure.management.network.NetworkInterface)8 IOException (java.io.IOException)8 StopWatch (org.apache.commons.lang3.time.StopWatch)7 VirtualMachineDataDisk (com.microsoft.azure.management.compute.VirtualMachineDataDisk)6 HashMap (java.util.HashMap)6 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)5 JSchException (com.jcraft.jsch.JSchException)4 CloudException (com.microsoft.azure.CloudException)4 DockerHost (com.microsoft.azure.docker.model.DockerHost)4 NetworkSecurityGroup (com.microsoft.azure.management.network.NetworkSecurityGroup)4