Improve error messaging / logs when listing VMs on the remote KVM host (for import) (#9204)

This commit is contained in:
Suresh Kumar Anaparti 2024-06-11 18:18:21 +05:30 committed by GitHub
parent 6edcf32c5e
commit 2e3f76ec03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 22 deletions

View File

@ -50,9 +50,7 @@ public final class LibvirtGetRemoteVmsCommandWrapper extends CommandWrapper<GetR
@Override
public Answer execute(final GetRemoteVmsCommand command, final LibvirtComputingResource libvirtComputingResource) {
String result = null;
String hypervisorURI = "qemu+tcp://" + command.getRemoteIp() +
"/system";
String hypervisorURI = "qemu+tcp://" + command.getRemoteIp() + "/system";
HashMap<String, UnmanagedInstanceTO> unmanagedInstances = new HashMap<>();
try {
Connect conn = LibvirtConnection.getConnection(hypervisorURI);
@ -61,26 +59,27 @@ public final class LibvirtGetRemoteVmsCommandWrapper extends CommandWrapper<GetR
final Domain domain = libvirtComputingResource.getDomain(conn, name);
final DomainInfo.DomainState ps = domain.getInfo().state;
final VirtualMachine.PowerState state = libvirtComputingResource.convertToPowerState(ps);
s_logger.debug("VM " + domain.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
s_logger.debug("VM " + domain.getName() + " - powerstate: " + ps + ", state: " + state.toString());
if (state == VirtualMachine.PowerState.PowerOff) {
try {
UnmanagedInstanceTO instance = getUnmanagedInstance(libvirtComputingResource, domain, conn);
unmanagedInstances.put(instance.getName(), instance);
} catch (Exception e) {
s_logger.error("Error while fetching instance details", e);
s_logger.error("Couldn't fetch VM " + domain.getName() + " details, due to: " + e.getMessage(), e);
}
}
domain.free();
}
s_logger.debug("Found Vms: "+ unmanagedInstances.size());
return new GetRemoteVmsAnswer(command, "", unmanagedInstances);
s_logger.debug("Found " + unmanagedInstances.size() + " stopped VMs on host " + command.getRemoteIp());
return new GetRemoteVmsAnswer(command, "", unmanagedInstances);
} catch (final LibvirtException e) {
s_logger.error("Error while listing stopped Vms on remote host: "+ e.getMessage());
return new Answer(command, false, result);
s_logger.error("Failed to list stopped VMs on remote host " + command.getRemoteIp() + ", due to: " + e.getMessage(), e);
if (e.getMessage().toLowerCase().contains("connection refused")) {
return new Answer(command, false, "Unable to connect to remote host " + command.getRemoteIp() + ", please check the libvirtd tcp connectivity and retry");
}
return new Answer(command, false, "Unable to list stopped VMs on remote host " + command.getRemoteIp() + ", due to: " + e.getMessage());
}
}
@ -106,8 +105,8 @@ public final class LibvirtGetRemoteVmsCommandWrapper extends CommandWrapper<GetR
return instance;
} catch (Exception e) {
s_logger.debug("Unable to retrieve unmanaged instance info. ", e);
throw new CloudRuntimeException("Unable to retrieve unmanaged instance info. " + e.getMessage());
s_logger.debug("Unable to retrieve unmanaged instance info, due to: " + e.getMessage(), e);
throw new CloudRuntimeException("Unable to retrieve unmanaged instance info, due to: " + e.getMessage());
}
}
@ -119,7 +118,6 @@ public final class LibvirtGetRemoteVmsCommandWrapper extends CommandWrapper<GetR
return UnmanagedInstanceTO.PowerState.PowerOff;
default:
return UnmanagedInstanceTO.PowerState.PowerUnknown;
}
}
@ -166,7 +164,6 @@ public final class LibvirtGetRemoteVmsCommandWrapper extends CommandWrapper<GetR
disk.setLabel(diskDef.getDiskLabel());
disk.setController(diskDef.getBusType().toString());
Pair<String, String> sourceHostPath = getSourceHostPath(libvirtComputingResource, diskDef.getSourcePath());
if (sourceHostPath != null) {
disk.setDatastoreHost(sourceHostPath.first());

View File

@ -2046,10 +2046,10 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
throw new InvalidParameterValueException("Username need to be provided.");
}
HashMap<String, UnmanagedInstanceTO> instancesMap = getRemoteVms(zoneId, remoteUrl, cmd.getUsername(), cmd.getPassword());
HashMap<String, UnmanagedInstanceTO> instancesMap = getRemoteVmsOnKVMHost(zoneId, remoteUrl, cmd.getUsername(), cmd.getPassword());
unmanagedInstanceTO = instancesMap.get(cmd.getName());
if (unmanagedInstanceTO == null) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Vm with name: %s not found on remote host", instanceName));
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM with name: %s not found on remote host %s", instanceName, remoteUrl));
}
}
@ -2460,7 +2460,7 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
}
List<UnmanagedInstanceResponse> responses = new ArrayList<>();
HashMap<String, UnmanagedInstanceTO> vmMap = getRemoteVms(zoneId, cmd.getHost(), cmd.getUsername(), cmd.getPassword());
HashMap<String, UnmanagedInstanceTO> vmMap = getRemoteVmsOnKVMHost(zoneId, cmd.getHost(), cmd.getUsername(), cmd.getPassword());
for (String key : vmMap.keySet()) {
UnmanagedInstanceTO instance = vmMap.get(key);
if (StringUtils.isNotEmpty(keyword) &&
@ -2475,17 +2475,17 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
return listResponses;
}
private HashMap<String, UnmanagedInstanceTO> getRemoteVms(long zoneId, String remoteUrl, String username, String password) {
private HashMap<String, UnmanagedInstanceTO> getRemoteVmsOnKVMHost(long zoneId, String remoteHostUrl, String username, String password) {
//ToDo: add option to list one Vm by name
List<HostVO> hosts = resourceManager.listAllUpAndEnabledHostsInOneZoneByHypervisor(Hypervisor.HypervisorType.KVM, zoneId);
if(hosts.size() < 1) {
throw new CloudRuntimeException("No hosts available for Vm Import");
throw new CloudRuntimeException("No hosts available for VM import");
}
HostVO host = hosts.get(0);
GetRemoteVmsCommand getRemoteVmsCommand = new GetRemoteVmsCommand(remoteUrl, username, password);
GetRemoteVmsCommand getRemoteVmsCommand = new GetRemoteVmsCommand(remoteHostUrl, username, password);
Answer answer = agentManager.easySend(host.getId(), getRemoteVmsCommand);
if (!(answer instanceof GetRemoteVmsAnswer)) {
throw new CloudRuntimeException("Error while listing remote Vms");
throw new CloudRuntimeException("Failed to list VMs, due to: " + answer.getDetails());
}
GetRemoteVmsAnswer getRemoteVmsAnswer = (GetRemoteVmsAnswer) answer;
return getRemoteVmsAnswer.getUnmanagedInstances();