use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.
the class AzureDockerVMOps method waitForVirtualMachineStartup.
public static void waitForVirtualMachineStartup(Azure azureClient, DockerHost dockerHost) {
// vault is not immediately available so the next operation could fail
// add a retry policy to make sure it got created
boolean isRunning = false;
int sleepTime = 50000;
for (int sleepMs = 5000; sleepMs <= 200000; sleepMs += sleepTime) {
try {
if (!isRunning) {
VirtualMachine vm = getVM(azureClient, dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
if (vm != null && vm.powerState().toString().split("/")[1].toUpperCase().equals("RUNNING")) {
// "PowerState/running"
isRunning = true;
} else {
if (DEBUG)
System.out.format("Warning: can't connect to %s (%d sec have passed)\n", dockerHost.hostVM.dnsName, sleepMs / 1000);
try {
Thread.sleep(sleepTime);
} catch (Exception e2) {
}
}
} else {
// VM is running; try to SSH connect to it
Session session = AzureDockerSSHOps.createLoginInstance(dockerHost);
String result = AzureDockerSSHOps.executeCommand("ls -l /", session, true);
if (DEBUG)
System.out.println(result);
session.disconnect();
break;
}
} catch (Exception e) {
try {
if (DEBUG)
System.out.format("Warning: can't connect to %s (%d sec have passed)\n", dockerHost.hostVM.dnsName, sleepMs / 1000);
if (DEBUG)
System.out.println(e.getMessage());
Thread.sleep(sleepTime);
} catch (Exception e3) {
}
}
}
}
use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.
the class AzureDockerVMOps method isDeletingDockerHostAllSafe.
public static boolean isDeletingDockerHostAllSafe(Azure azureClient, String resourceGroup, String vmName) {
if (azureClient == null || resourceGroup == null || vmName == null) {
return false;
}
VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(resourceGroup, vmName);
if (vm == null) {
return false;
}
PublicIPAddress publicIp = vm.getPrimaryPublicIPAddress();
NicIPConfiguration nicIPConfiguration = publicIp.getAssignedNetworkInterfaceIPConfiguration();
Network vnet = nicIPConfiguration.getNetwork();
NetworkInterface nic = vm.getPrimaryNetworkInterface();
return nic.ipConfigurations().size() == 1 && vnet.subnets().size() == 1 && vnet.subnets().values().toArray(new Subnet[1])[0].inner().ipConfigurations().size() == 1;
}
use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.
the class AzureNewDockerWizard method createHost.
public void createHost() {
AzureDockerPreferredSettings dockerPreferredSettings = dockerManager.getDockerPreferredSettings();
if (dockerPreferredSettings == null) {
dockerPreferredSettings = new AzureDockerPreferredSettings();
}
dockerPreferredSettings.dockerApiName = newHost.apiUrl;
dockerPreferredSettings.region = newHost.hostVM.region;
dockerPreferredSettings.vmSize = newHost.hostVM.vmSize;
dockerPreferredSettings.vmOS = newHost.hostOSType.name();
dockerManager.setDockerPreferredSettings(dockerPreferredSettings);
Job createDockerHostJob = new Job("Creating Docker virtual machine " + newHost.name) {
@Override
protected IStatus run(IProgressMonitor progressMonitor) {
progressMonitor.beginTask("start task", 100);
try {
DockerHost dockerHost = newHost;
progressMonitor.subTask(String.format("Reading subscription details for Docker host %s ...", dockerHost.apiUrl));
progressMonitor.worked(5);
Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerHost.sid).azureClient;
if (progressMonitor.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 0) {
progressMonitor.done();
return Status.CANCEL_STATUS;
}
}
progressMonitor.subTask(String.format("Creating new virtual machine %s ...", dockerHost.name));
progressMonitor.worked(10);
if (AzureDockerUtils.DEBUG)
System.out.println("Creating new virtual machine: " + new Date().toString());
AzureDockerVMOps.createDockerHostVM(azureClient, dockerHost);
if (AzureDockerUtils.DEBUG)
System.out.println("Done creating new virtual machine: " + new Date().toString());
if (progressMonitor.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 0) {
progressMonitor.done();
return Status.CANCEL_STATUS;
}
}
progressMonitor.subTask("Getting the new Docker virtual machines details...");
progressMonitor.worked(5);
if (AzureDockerUtils.DEBUG)
System.out.println("Getting the new Docker hosts details: " + new Date().toString());
VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
if (vm != null) {
DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
if (updatedHost != null) {
dockerHost.hostVM = updatedHost.hostVM;
dockerHost.apiUrl = updatedHost.apiUrl;
}
}
if (AzureDockerUtils.DEBUG)
System.out.println("Done getting the new Docker hosts details: " + new Date().toString());
if (progressMonitor.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 0) {
progressMonitor.done();
return Status.CANCEL_STATUS;
}
}
progressMonitor.subTask(String.format("Waiting for virtual machine %s to be up...", dockerHost.name));
progressMonitor.worked(55);
if (AzureDockerUtils.DEBUG)
System.out.println("Waiting for virtual machine to be up: " + new Date().toString());
AzureDockerVMOps.waitForVirtualMachineStartup(azureClient, dockerHost);
if (AzureDockerUtils.DEBUG)
System.out.println("Done Waiting for virtual machine to be up: " + new Date().toString());
if (progressMonitor.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 0) {
progressMonitor.done();
return Status.CANCEL_STATUS;
}
}
progressMonitor.subTask(String.format("Configuring Docker service for %s ...", dockerHost.apiUrl));
progressMonitor.worked(15);
if (AzureDockerUtils.DEBUG)
System.out.println("Configuring Docker host: " + new Date().toString());
AzureDockerVMOps.installDocker(dockerHost);
if (AzureDockerUtils.DEBUG)
System.out.println("Done configuring Docker host: " + new Date().toString());
if (AzureDockerUtils.DEBUG)
System.out.println("Finished setting up Docker host");
if (progressMonitor.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 0) {
progressMonitor.done();
return Status.CANCEL_STATUS;
}
}
if (dockerHost.certVault != null && dockerHost.certVault.hostName != null) {
AzureDockerUIResources.createDockerKeyVault(dockerHost, dockerManager);
}
progressMonitor.subTask("Refreshing the Docker virtual machines details...");
progressMonitor.worked(5);
if (AzureDockerUtils.DEBUG)
System.out.println("Refreshing Docker hosts details: " + new Date().toString());
vm = azureClient.virtualMachines().getByResourceGroup(dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
if (vm != null) {
DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
if (updatedHost != null) {
updatedHost.sid = dockerHost.sid;
updatedHost.hostVM.sid = dockerHost.hostVM.sid;
if (updatedHost.certVault == null) {
updatedHost.certVault = dockerHost.certVault;
updatedHost.hasPwdLogIn = dockerHost.hasPwdLogIn;
updatedHost.hasSSHLogIn = dockerHost.hasSSHLogIn;
updatedHost.isTLSSecured = dockerHost.isTLSSecured;
}
dockerManager.addDockerHostDetails(updatedHost);
if (AzureUIRefreshCore.listeners != null) {
AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.ADD, updatedHost));
}
}
}
if (AzureDockerUtils.DEBUG)
System.out.println("Done refreshing Docker hosts details: " + new Date().toString());
if (progressMonitor.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 0) {
progressMonitor.done();
return Status.CANCEL_STATUS;
}
}
progressMonitor.done();
return Status.OK_STATUS;
} catch (Exception e) {
String msg = "An error occurred while attempting to create Docker host." + "\n" + e.getMessage();
log.log(Level.SEVERE, "createHost: " + msg, e);
e.printStackTrace();
return Status.CANCEL_STATUS;
}
// progressMonitor.subTask("");
// progressMonitor.worked(1);
// if (progressMonitor.isCanceled()) {
// if (displayWarningOnCreateHostCancelAction() == 0) {
// progressMonitor.done();
// return Status.CANCEL_STATUS;
// }
// }
//
// for (int i = 0; i < 10; i++) {
// try {
// Thread.sleep(3000);
// progressMonitor.subTask("doing " + i);
// // Report that 10 units are done
// progressMonitor.worked(10);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
// }
}
};
createDockerHostJob.schedule();
// DefaultLoader.getIdeHelper().runInBackground(null, "Creating Docker virtual machine " + newHost.name + "...", false, true, "Creating Docker virtual machine " + newHost.name + "...", new Runnable() {
// @Override
// public void run() {
// DefaultLoader.getIdeHelper().invokeLater(new Runnable() {
// @Override
// public void run() {
//
// }
// });
// }
// });
}
use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.
the class AzureNewDockerWizardDialog method create.
public void create() {
DockerHost dockerHost = model.getDockerHost();
AzureDockerPreferredSettings dockerPreferredSettings = model.getDockerManager().getDockerPreferredSettings();
if (dockerPreferredSettings == null) {
dockerPreferredSettings = new AzureDockerPreferredSettings();
}
dockerPreferredSettings.dockerApiName = dockerHost.apiUrl;
dockerPreferredSettings.region = dockerHost.hostVM.region;
dockerPreferredSettings.vmSize = dockerHost.hostVM.vmSize;
dockerPreferredSettings.vmOS = dockerHost.hostOSType.name();
model.getDockerManager().setDockerPreferredSettings(dockerPreferredSettings);
ProgressManager.getInstance().run(new Task.Backgroundable(model.getProject(), "Creating Docker Host on Azure...", true) {
@Override
public void run(ProgressIndicator progressIndicator) {
try {
progressIndicator.setFraction(.05);
progressIndicator.setText2(String.format("Reading subscription details for Docker host %s ...", dockerHost.apiUrl));
AzureDockerHostsManager dockerManager = model.getDockerManager();
Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerHost.sid).azureClient;
if (progressIndicator.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 1) {
return;
}
}
progressIndicator.setFraction(.10);
progressIndicator.setText2(String.format("Creating new virtual machine %s ...", dockerHost.name));
if (AzureDockerUtils.DEBUG)
System.out.println("Creating new virtual machine: " + new Date().toString());
AzureDockerVMOps.createDockerHostVM(azureClient, dockerHost);
if (AzureDockerUtils.DEBUG)
System.out.println("Done creating new virtual machine: " + new Date().toString());
if (progressIndicator.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 1) {
return;
}
}
progressIndicator.setFraction(.60);
progressIndicator.setIndeterminate(true);
progressIndicator.setText2("Getting the new Docker virtual machines details...");
if (AzureDockerUtils.DEBUG)
System.out.println("Getting the new Docker virtual machines details: " + new Date().toString());
// dockerManager.refreshDockerHostDetails();
VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
if (vm != null) {
DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
if (updatedHost != null) {
dockerHost.hostVM = updatedHost.hostVM;
dockerHost.apiUrl = updatedHost.apiUrl;
}
}
if (AzureDockerUtils.DEBUG)
System.out.println("Done getting the new Docker virtual machines details: " + new Date().toString());
if (progressIndicator.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 1) {
return;
}
}
progressIndicator.setFraction(.65);
progressIndicator.setText2(String.format("Waiting for virtual machine %s to be up...", dockerHost.name));
if (AzureDockerUtils.DEBUG)
System.out.println("Waiting for virtual machine to be up: " + new Date().toString());
AzureDockerVMOps.waitForVirtualMachineStartup(azureClient, dockerHost);
if (AzureDockerUtils.DEBUG)
System.out.println("Done Waiting for virtual machine to be up: " + new Date().toString());
if (progressIndicator.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 1) {
return;
}
}
progressIndicator.setFraction(.75);
progressIndicator.setText2(String.format("Configuring Docker service for %s ...", dockerHost.apiUrl));
if (AzureDockerUtils.DEBUG)
System.out.println("Configuring Docker host: " + new Date().toString());
AzureDockerVMOps.installDocker(dockerHost);
if (AzureDockerUtils.DEBUG)
System.out.println("Done configuring Docker host: " + new Date().toString());
if (AzureDockerUtils.DEBUG)
System.out.println("Finished setting up Docker host");
if (progressIndicator.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 1) {
return;
}
}
if (dockerHost.certVault != null && dockerHost.certVault.hostName != null) {
AzureDockerUIResources.createDockerKeyVault(model.getProject(), dockerHost, dockerManager);
}
progressIndicator.setFraction(.90);
progressIndicator.setIndeterminate(true);
progressIndicator.setText2("Refreshing the Docker virtual machines details...");
if (AzureDockerUtils.DEBUG)
System.out.println("Refreshing Docker hosts details: " + new Date().toString());
// dockerManager.refreshDockerHostDetails();
vm = azureClient.virtualMachines().getByResourceGroup(dockerHost.hostVM.resourceGroupName, dockerHost.hostVM.name);
if (vm != null) {
DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
if (updatedHost != null) {
updatedHost.sid = dockerHost.sid;
updatedHost.hostVM.sid = dockerHost.hostVM.sid;
if (updatedHost.certVault == null) {
updatedHost.certVault = dockerHost.certVault;
updatedHost.hasPwdLogIn = dockerHost.hasPwdLogIn;
updatedHost.hasSSHLogIn = dockerHost.hasSSHLogIn;
updatedHost.isTLSSecured = dockerHost.isTLSSecured;
}
dockerManager.addDockerHostDetails(updatedHost);
if (AzureUIRefreshCore.listeners != null) {
AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.ADD, updatedHost));
}
}
}
if (AzureDockerUtils.DEBUG)
System.out.println("Done refreshing Docker hosts details: " + new Date().toString());
if (progressIndicator.isCanceled()) {
if (displayWarningOnCreateHostCancelAction() == 1) {
return;
}
}
progressIndicator.setFraction(1);
progressIndicator.setIndeterminate(true);
} catch (Exception e) {
String msg = "An error occurred while attempting to create Docker host." + "\n" + e.getMessage();
LOGGER.error("Failed to Create Docker Host", e);
PluginUtil.displayErrorDialogInAWTAndLog("Failed to Create Docker Host", msg, e);
}
}
});
}
use of com.microsoft.azure.management.compute.VirtualMachine in project cloudbreak by hortonworks.
the class AzureMetadataCollector method collect.
@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
CloudResource resource = azureUtils.getTemplateResource(resources);
List<CloudVmMetaDataStatus> results = new ArrayList<>();
List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
String resourceName = resource.getName();
Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> azureUtils.getPrivateInstanceId(resourceName, from.getGroupName(), Long.toString(from.getPrivateId())));
try {
for (Entry<String, InstanceTemplate> instance : templateMap.entrySet()) {
AzureClient azureClient = authenticatedContext.getParameter(AzureClient.class);
VirtualMachine vm = azureClient.getVirtualMachine(resourceName, instance.getKey());
String subnetId = vm.getPrimaryNetworkInterface().primaryIPConfiguration().subnetName();
String privateIp = null;
String publicIp = null;
Integer faultDomainCount = azureClient.getFaultDomainNumber(resourceName, vm.name());
String platform = authenticatedContext.getCloudContext().getPlatform().value();
String location = authenticatedContext.getCloudContext().getLocation().getRegion().value();
String hostgroupNm = instance.getValue().getGroupName();
StringBuilder localityIndicatorBuilder = new StringBuilder();
localityIndicatorBuilder.append(LOCALITY_SEPARATOR).append(platform).append(LOCALITY_SEPARATOR).append(location).append(LOCALITY_SEPARATOR).append(resourceName).append(LOCALITY_SEPARATOR).append(hostgroupNm).append(LOCALITY_SEPARATOR).append(faultDomainCount);
AzureUtils.removeBlankSpace(localityIndicatorBuilder);
List<String> networkInterfaceIdList = vm.networkInterfaceIds();
for (String networkInterfaceId : networkInterfaceIdList) {
NetworkInterface networkInterface = azureClient.getNetworkInterfaceById(networkInterfaceId);
privateIp = networkInterface.primaryPrivateIP();
PublicIPAddress publicIpAddress = networkInterface.primaryIPConfiguration().getPublicIPAddress();
List<LoadBalancerBackend> backends = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerBackends();
List<LoadBalancerInboundNatRule> inboundNatRules = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerInboundNatRules();
if (!backends.isEmpty() || !inboundNatRules.isEmpty()) {
publicIp = azureClient.getLoadBalancerIps(resource.getName(), azureUtils.getLoadBalancerId(resource.getName())).get(0);
}
if (publicIpAddress != null && publicIpAddress.ipAddress() != null) {
publicIp = publicIpAddress.ipAddress();
}
}
String instanceId = instance.getKey();
CloudInstanceMetaData md = new CloudInstanceMetaData(privateIp, publicIp, faultDomainCount == null ? null : localityIndicatorBuilder.toString());
InstanceTemplate template = templateMap.get(instanceId);
if (template != null) {
Map<String, Object> params = new HashMap<>(1);
params.put(CloudInstance.SUBNET_ID, subnetId);
// TODO use shhkey here
CloudInstance cloudInstance = new CloudInstance(instanceId, template, null, params);
CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
results.add(new CloudVmMetaDataStatus(status, md));
}
}
} catch (RuntimeException e) {
throw new CloudConnectorException(e.getMessage(), e);
}
return results;
}
Aggregations