-
Notifications
You must be signed in to change notification settings - Fork 1.2k
extension: improve host vm power reporting #11619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shwstppr
wants to merge
2
commits into
apache:4.22
Choose a base branch
from
shapeblue:improve-ext-powerreport
base: 4.22
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,7 @@ | |
| import com.cloud.agent.api.StopAnswer; | ||
| import com.cloud.agent.api.StopCommand; | ||
| import com.cloud.agent.api.to.VirtualMachineTO; | ||
| import com.cloud.host.Host; | ||
| import com.cloud.host.HostVO; | ||
| import com.cloud.host.dao.HostDao; | ||
| import com.cloud.hypervisor.ExternalProvisioner; | ||
|
|
@@ -128,7 +129,7 @@ public class ExternalPathPayloadProvisioner extends ManagerBase implements Exter | |
| private ExecutorService payloadCleanupExecutor; | ||
| private ScheduledExecutorService payloadCleanupScheduler; | ||
| private static final List<String> TRIVIAL_ACTIONS = Arrays.asList( | ||
| "status" | ||
| "status", "statuses" | ||
| ); | ||
|
|
||
| @Override | ||
|
|
@@ -456,7 +457,7 @@ public StopAnswer expungeInstance(String hostName, String extensionName, String | |
| @Override | ||
| public Map<String, HostVmStateReportEntry> getHostVmStateReport(long hostId, String extensionName, | ||
| String extensionRelativePath) { | ||
| final Map<String, HostVmStateReportEntry> vmStates = new HashMap<>(); | ||
| Map<String, HostVmStateReportEntry> vmStates = new HashMap<>(); | ||
| String extensionPath = getExtensionCheckedPath(extensionName, extensionRelativePath); | ||
| if (StringUtils.isEmpty(extensionPath)) { | ||
| return vmStates; | ||
|
|
@@ -466,14 +467,20 @@ public Map<String, HostVmStateReportEntry> getHostVmStateReport(long hostId, Str | |
| logger.error("Host with ID: {} not found", hostId); | ||
| return vmStates; | ||
| } | ||
| Map<String, Map<String, String>> accessDetails = | ||
| extensionsManager.getExternalAccessDetails(host, null); | ||
| vmStates = getVmPowerStates(host, accessDetails, extensionName, extensionPath); | ||
| if (vmStates != null) { | ||
| logger.debug("Found {} VMs on the host {}", vmStates.size(), host); | ||
| return vmStates; | ||
| } | ||
| vmStates = new HashMap<>(); | ||
| List<UserVmVO> allVms = _uservmDao.listByHostId(hostId); | ||
| allVms.addAll(_uservmDao.listByLastHostId(hostId)); | ||
| if (CollectionUtils.isEmpty(allVms)) { | ||
| logger.debug("No VMs found for the {}", host); | ||
| return vmStates; | ||
| } | ||
| Map<String, Map<String, String>> accessDetails = | ||
| extensionsManager.getExternalAccessDetails(host, null); | ||
| for (UserVmVO vm: allVms) { | ||
| VirtualMachine.PowerState powerState = getVmPowerState(vm, accessDetails, extensionName, extensionPath); | ||
| vmStates.put(vm.getInstanceName(), new HostVmStateReportEntry(powerState, "host-" + hostId)); | ||
|
|
@@ -714,7 +721,7 @@ protected VirtualMachine.PowerState parsePowerStateFromResponse(UserVmVO userVmV | |
| return getPowerStateFromString(response); | ||
| } | ||
| try { | ||
| JsonObject jsonObj = new JsonParser().parse(response).getAsJsonObject(); | ||
| JsonObject jsonObj = JsonParser.parseString(response).getAsJsonObject(); | ||
|
||
| String powerState = jsonObj.has("power_state") ? jsonObj.get("power_state").getAsString() : null; | ||
| return getPowerStateFromString(powerState); | ||
| } catch (Exception e) { | ||
|
|
@@ -724,7 +731,7 @@ protected VirtualMachine.PowerState parsePowerStateFromResponse(UserVmVO userVmV | |
| } | ||
| } | ||
|
|
||
| private VirtualMachine.PowerState getVmPowerState(UserVmVO userVmVO, Map<String, Map<String, String>> accessDetails, | ||
| protected VirtualMachine.PowerState getVmPowerState(UserVmVO userVmVO, Map<String, Map<String, String>> accessDetails, | ||
| String extensionName, String extensionPath) { | ||
| VirtualMachineTO virtualMachineTO = getVirtualMachineTO(userVmVO); | ||
| accessDetails.put(ApiConstants.VIRTUAL_MACHINE, virtualMachineTO.getExternalDetails()); | ||
|
|
@@ -740,6 +747,46 @@ private VirtualMachine.PowerState getVmPowerState(UserVmVO userVmVO, Map<String, | |
| } | ||
| return parsePowerStateFromResponse(userVmVO, result.second()); | ||
| } | ||
|
|
||
| protected Map<String, HostVmStateReportEntry> getVmPowerStates(Host host, | ||
| Map<String, Map<String, String>> accessDetails, String extensionName, String extensionPath) { | ||
| Map<String, Object> modifiedDetails = loadAccessDetails(accessDetails, null); | ||
| logger.debug("Trying to get VM power statuses from the external system for {}", host); | ||
| Pair<Boolean, String> result = getInstanceStatusesOnExternalSystem(extensionName, extensionPath, | ||
| host.getName(), modifiedDetails, AgentManager.Wait.value()); | ||
| if (!result.first()) { | ||
| logger.warn("Failure response received while trying to fetch the power statuses for {} : {}", | ||
| host, result.second()); | ||
| return null; | ||
| } | ||
| if (StringUtils.isBlank(result.second())) { | ||
| logger.warn("Empty response while trying to fetch VM power statuses for host: {}", host); | ||
| return null; | ||
| } | ||
| try { | ||
| JsonObject jsonObj = JsonParser.parseString(result.second()).getAsJsonObject(); | ||
| if (!jsonObj.has("status") || !"success".equalsIgnoreCase(jsonObj.get("status").getAsString())) { | ||
| logger.warn("Invalid status in response while trying to fetch VM power statuses for host: {}: {}", | ||
| host, result.second()); | ||
| return null; | ||
| } | ||
| if (!jsonObj.has("power_state") || !jsonObj.get("power_state").isJsonObject()) { | ||
| logger.warn("Missing or invalid power_state in response for host: {}: {}", host, result.second()); | ||
| return null; | ||
| } | ||
| JsonObject powerStates = jsonObj.getAsJsonObject("power_state"); | ||
| Map<String, HostVmStateReportEntry> states = new HashMap<>(); | ||
| for (Map.Entry<String, com.google.gson.JsonElement> entry : powerStates.entrySet()) { | ||
| VirtualMachine.PowerState powerState = getPowerStateFromString(entry.getValue().getAsString()); | ||
| states.put(entry.getKey(), new HostVmStateReportEntry(powerState, "host-" + host.getId())); | ||
| } | ||
| return states; | ||
| } catch (Exception e) { | ||
| logger.warn("Failed to parse VM power statuses response for host: {}: {}", host, e.getMessage()); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public Pair<Boolean, String> prepareExternalProvisioningInternal(String extensionName, String filename, | ||
| String vmUUID, Map<String, Object> accessDetails, int wait) { | ||
| return executeExternalCommand(extensionName, "prepare", accessDetails, wait, | ||
|
|
@@ -783,6 +830,12 @@ public Pair<Boolean, String> getInstanceStatusOnExternalSystem(String extensionN | |
| String.format("Failed to get the instance power status %s on external system", vmUUID), filename); | ||
| } | ||
|
|
||
| public Pair<Boolean, String> getInstanceStatusesOnExternalSystem(String extensionName, String filename, | ||
| String hostName, Map<String, Object> accessDetails, int wait) { | ||
| return executeExternalCommand(extensionName, "statuses", accessDetails, wait, | ||
| String.format("Failed to get the %s instances power status on external system", hostName), filename); | ||
| } | ||
|
|
||
| public Pair<Boolean, String> getInstanceConsoleOnExternalSystem(String extensionName, String filename, | ||
| String vmUUID, Map<String, Object> accessDetails, int wait) { | ||
| return executeExternalCommand(extensionName, "getconsole", accessDetails, wait, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation is changed from 4 to 3 spaces.