-
Notifications
You must be signed in to change notification settings - Fork 1.3k
KVM: Fix agents dont reconnect post maintenance #3239
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
Merged
DaanHoogland
merged 6 commits into
apache:4.11
from
shapeblue:agentsdontreconnectpostmaintenance
May 23, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f1340d
Keep connection alive when on maintenance
nvazquez 44249fa
Refactor cancel maintenance and unit tests
nvazquez bf1a6d5
Add marvin tests
nvazquez 237ebbc
Refactor
nvazquez f794566
Changing the way we get ssh credentials
borisstoyanov a5e39ed
Add check on SSH restart and improve marvin tests
nvazquez 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
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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import javax.inject.Inject; | ||
| import javax.naming.ConfigurationException; | ||
|
|
||
| import com.cloud.utils.Pair; | ||
| import com.cloud.vm.dao.UserVmDetailsDao; | ||
| import org.apache.cloudstack.framework.config.ConfigKey; | ||
| import org.apache.commons.lang.ObjectUtils; | ||
|
|
@@ -2344,45 +2345,76 @@ private boolean doCancelMaintenance(final long hostId) { | |
| } | ||
| } | ||
|
|
||
| handleAgentIfNotConnected(host, vms_migrating); | ||
|
|
||
| try { | ||
| resourceStateTransitTo(host, ResourceState.Event.AdminCancelMaintenance, _nodeId); | ||
| _agentMgr.pullAgentOutMaintenance(hostId); | ||
| retryHostMaintenance.remove(hostId); | ||
| } catch (final NoTransitionException e) { | ||
| s_logger.debug("Cannot transmit host " + host.getId() + "to Enabled state", e); | ||
| return false; | ||
| } | ||
|
|
||
| // for kvm, need to log into kvm host, restart cloudstack-agent | ||
| if ((host.getHypervisorType() == HypervisorType.KVM && !vms_migrating) || host.getHypervisorType() == HypervisorType.LXC) { | ||
| return true; | ||
|
|
||
| final boolean sshToAgent = Boolean.parseBoolean(_configDao.getValue(Config.KvmSshToAgentEnabled.key())); | ||
| if (!sshToAgent) { | ||
| s_logger.info("Configuration tells us not to SSH into Agents. Please restart the Agent (" + hostId + ") manually"); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| _hostDao.loadDetails(host); | ||
| final String password = host.getDetail("password"); | ||
| final String username = host.getDetail("username"); | ||
| if (password == null || username == null) { | ||
| s_logger.debug("Can't find password/username"); | ||
| return false; | ||
| } | ||
| final com.trilead.ssh2.Connection connection = SSHCmdHelper.acquireAuthorizedConnection(host.getPrivateIpAddress(), 22, username, password); | ||
| if (connection == null) { | ||
| s_logger.debug("Failed to connect to host: " + host.getPrivateIpAddress()); | ||
| return false; | ||
| } | ||
| /** | ||
| * Handle agent (if available) if its not connected before cancelling maintenance. | ||
| * Agent must be connected before cancelling maintenance. | ||
| * If the host status is not Up: | ||
| * - If kvm.ssh.to.agent is true, then SSH into the host and restart the agent. | ||
| * - If kvm.shh.to.agent is false, then fail cancelling maintenance | ||
| */ | ||
| protected void handleAgentIfNotConnected(HostVO host, boolean vmsMigrating) { | ||
| final boolean isAgentOnHost = host.getHypervisorType() == HypervisorType.KVM || | ||
| host.getHypervisorType() == HypervisorType.LXC; | ||
| if (!isAgentOnHost || vmsMigrating || host.getStatus() == Status.Up) { | ||
| return; | ||
| } | ||
| final boolean sshToAgent = Boolean.parseBoolean(_configDao.getValue(KvmSshToAgentEnabled.key())); | ||
| if (sshToAgent) { | ||
| Pair<String, String> credentials = getHostCredentials(host); | ||
| connectAndRestartAgentOnHost(host, credentials.first(), credentials.second()); | ||
| } else { | ||
| throw new CloudRuntimeException("SSH access is disabled, cannot cancel maintenance mode as " + | ||
| "host agent is not connected"); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| SSHCmdHelper.SSHCmdResult result = SSHCmdHelper.sshExecuteCmdOneShot(connection, "service cloudstack-agent restart"); | ||
| s_logger.debug("cloudstack-agent restart result: " + result.toString()); | ||
| } catch (final SshException e) { | ||
| return false; | ||
| } | ||
| } | ||
| /** | ||
| * Get host credentials | ||
| * @throws CloudRuntimeException if username or password are not found | ||
| */ | ||
| protected Pair<String, String> getHostCredentials(HostVO host) { | ||
| _hostDao.loadDetails(host); | ||
| final String password = host.getDetail("password"); | ||
| final String username = host.getDetail("username"); | ||
| if (password == null || username == null) { | ||
| throw new CloudRuntimeException("SSH to agent is enabled, but username/password credentials are not found"); | ||
| } | ||
| return new Pair<>(username, password); | ||
| } | ||
|
|
||
| return true; | ||
| } catch (final NoTransitionException e) { | ||
| s_logger.debug("Cannot transmit host " + host.getId() + "to Enabled state", e); | ||
| return false; | ||
| /** | ||
| * True if agent is restarted via SSH. Assumes kvm.ssh.to.agent = true and host status is not Up | ||
| */ | ||
| protected void connectAndRestartAgentOnHost(HostVO host, String username, String password) { | ||
| final com.trilead.ssh2.Connection connection = SSHCmdHelper.acquireAuthorizedConnection( | ||
| host.getPrivateIpAddress(), 22, username, password); | ||
| if (connection == null) { | ||
| throw new CloudRuntimeException("SSH to agent is enabled, but failed to connect to host: " + host.getPrivateIpAddress()); | ||
| } | ||
| try { | ||
| SSHCmdHelper.SSHCmdResult result = SSHCmdHelper.sshExecuteCmdOneShot( | ||
| connection, "service cloudstack-agent restart"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be changed to systemctl restart cloudstack-agent || service cloudstack-agent restart |
||
| if (result.getReturnCode() != 0) { | ||
| throw new CloudRuntimeException("Could not restart agent on host " + host.getId() + " due to: " + result.getStdErr()); | ||
| } | ||
| s_logger.debug("cloudstack-agent restart result: " + result.toString()); | ||
| } catch (final SshException e) { | ||
| throw new CloudRuntimeException("SSH to agent is enabled, but agent restart failed", e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.