Skip to content

Commit 5f4bc91

Browse files
Pick discovery/ssh port from host url while adding KVM host (and use that port if passed, other fall back to config 'kvm.host.discovery.ssh.port')
1 parent 2e49b0c commit 5f4bc91

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

api/src/main/java/com/cloud/host/Host.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static String[] toStrings(Host.Type... types) {
5959
String HOST_INSTANCE_CONVERSION = "host.instance.conversion";
6060
String HOST_OVFTOOL_VERSION = "host.ovftool.version";
6161
String HOST_VIRTV2V_VERSION = "host.virtv2v.version";
62+
String HOST_SSH_POST = "host.ssh.port";
6263

6364
/**
6465
* @return name of the machine.

api/src/main/java/org/apache/cloudstack/api/command/admin/host/AddHostCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public class AddHostCmd extends BaseCmd {
6060
@Parameter(name = ApiConstants.POD_ID, type = CommandType.UUID, entityType = PodResponse.class, required = true, description = "The Pod ID for the host")
6161
private Long podId;
6262

63-
@Parameter(name = ApiConstants.URL, type = CommandType.STRING, required = true, description = "The host URL")
63+
@Parameter(name = ApiConstants.URL, type = CommandType.STRING, required = true, description = "The host URL, optionally add ssh port for KVM hosts," +
64+
" otherwise falls back to the port defined at the config 'kvm.host.discovery.ssh.port'")
6465
private String url;
6566

6667
@Parameter(name = ApiConstants.ZONE_ID, type = CommandType.UUID, entityType = ZoneResponse.class, required = true, description = "The Zone ID for the host")

engine/components-api/src/main/java/com/cloud/agent/AgentManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public interface AgentManager {
5555
"For example: DhcpEntryCommand=600, SavePasswordCommand=300, VmDataCommand=300", false);
5656

5757
ConfigKey<Integer> KVMHostDiscoverySshPort = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Integer.class,
58-
"kvm.host.discovery.ssh.port", "22", "SSH port used for KVM host discovery and any other operations on host (using SSH). Please note that this is applicable for all the KVM hosts added to this CloudStack deployment, so ensure all hosts are accessible on this port", true);
58+
"kvm.host.discovery.ssh.port", "22", "SSH port used for KVM host discovery and any other operations on host (using SSH)." +
59+
" Please note that this is applicable when port is not defined through host url while adding the KVM host.", true, ConfigKey.Scope.Cluster);
5960

6061
enum TapAgentsAction {
6162
Add, Del, Contains,

plugins/backup/networker/src/main/java/org/apache/cloudstack/backup/NetworkerBackupProvider.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.cloud.agent.AgentManager;
2020
import com.cloud.dc.dao.ClusterDao;
21+
import com.cloud.host.Host;
2122
import com.cloud.host.HostVO;
2223
import com.cloud.host.Status;
2324
import com.cloud.host.dao.HostDao;
@@ -28,6 +29,7 @@
2829
import com.cloud.storage.dao.StoragePoolHostDao;
2930
import com.cloud.storage.dao.VolumeDao;
3031
import com.cloud.utils.Pair;
32+
import com.cloud.utils.StringUtils;
3133
import com.cloud.utils.Ternary;
3234
import com.cloud.utils.component.AdapterBase;
3335
import com.cloud.utils.db.Transaction;
@@ -230,8 +232,13 @@ private String executeBackupCommand(HostVO host, String username, String passwor
230232
String nstRegex = "\\bcompleted savetime=([0-9]{10})";
231233
Pattern saveTimePattern = Pattern.compile(nstRegex);
232234

235+
if (host == null) {
236+
LOG.warn("Unable to take backup, host is null");
237+
return null;
238+
}
239+
233240
try {
234-
Pair<Boolean, String> response = SshHelper.sshExecute(host.getPrivateIpAddress(), AgentManager.KVMHostDiscoverySshPort.value(),
241+
Pair<Boolean, String> response = SshHelper.sshExecute(host.getPrivateIpAddress(), getHostSshPort(host),
235242
username, null, password, command, 120000, 120000, 3600000);
236243
if (!response.first()) {
237244
LOG.error(String.format("Backup Script failed on HYPERVISOR %s due to: %s", host, response.second()));
@@ -250,9 +257,13 @@ private String executeBackupCommand(HostVO host, String username, String passwor
250257
return null;
251258
}
252259
private boolean executeRestoreCommand(HostVO host, String username, String password, String command) {
260+
if (host == null) {
261+
LOG.warn("Unable to restore backup, host is null");
262+
return false;
263+
}
253264

254265
try {
255-
Pair<Boolean, String> response = SshHelper.sshExecute(host.getPrivateIpAddress(), AgentManager.KVMHostDiscoverySshPort.value(),
266+
Pair<Boolean, String> response = SshHelper.sshExecute(host.getPrivateIpAddress(), getHostSshPort(host),
256267
username, null, password, command, 120000, 120000, 3600000);
257268

258269
if (!response.first()) {
@@ -267,6 +278,23 @@ private boolean executeRestoreCommand(HostVO host, String username, String passw
267278
return false;
268279
}
269280

281+
private int getHostSshPort(HostVO host) {
282+
if (host == null) {
283+
return AgentManager.KVMHostDiscoverySshPort.value();
284+
}
285+
286+
hostDao.loadDetails(host);
287+
String hostPort = host.getDetail(Host.HOST_SSH_POST);
288+
int sshPort;
289+
if (StringUtils.isBlank(hostPort)) {
290+
sshPort = AgentManager.KVMHostDiscoverySshPort.valueIn(host.getClusterId());
291+
} else {
292+
sshPort = Integer.parseInt(hostPort);
293+
}
294+
295+
return sshPort;
296+
}
297+
270298
private NetworkerClient getClient(final Long zoneId) {
271299
try {
272300
return new NetworkerClient(NetworkerUrl.valueIn(zoneId), NetworkerUsername.valueIn(zoneId), NetworkerPassword.valueIn(zoneId),

server/src/main/java/com/cloud/hypervisor/kvm/discoverer/LibvirtServerDiscoverer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,12 @@ private void setupAgentSecurity(final Connection sshConnection, final String age
272272
}
273273
}
274274

275-
sshConnection = new Connection(agentIp, AgentManager.KVMHostDiscoverySshPort.value());
275+
int port = uri.getPort();
276+
if (port <= 0) {
277+
port = AgentManager.KVMHostDiscoverySshPort.valueIn(clusterId);
278+
}
279+
280+
sshConnection = new Connection(agentIp, port);
276281

277282
sshConnection.connect(null, 60000, 60000);
278283

@@ -380,6 +385,9 @@ private void setupAgentSecurity(final Connection sshConnection, final String age
380385
Map<String, String> hostDetails = connectedHost.getDetails();
381386
hostDetails.put("password", password);
382387
hostDetails.put("username", username);
388+
if (uri.getPort() > 0) {
389+
hostDetails.put(Host.HOST_SSH_POST, String.valueOf(uri.getPort()));
390+
}
383391
_hostDao.saveDetails(connectedHost);
384392
return resources;
385393
} catch (DiscoveredWithErrorException e) {

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,6 @@ private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long c
776776
_clusterDetailsDao.persist(cluster_cpu_detail);
777777
_clusterDetailsDao.persist(cluster_memory_detail);
778778
}
779-
780779
}
781780

782781
try {
@@ -871,7 +870,6 @@ private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long c
871870
hosts.add(host);
872871
}
873872
discoverer.postDiscovery(hosts, _nodeId);
874-
875873
}
876874
logger.info("server resources successfully discovered by " + discoverer.getName());
877875
return hosts;
@@ -2949,7 +2947,7 @@ protected Ternary<String, String, String> getHostCredentials(HostVO host) {
29492947
*/
29502948
protected void connectAndRestartAgentOnHost(HostVO host, String username, String password, String privateKey) {
29512949
final com.trilead.ssh2.Connection connection = SSHCmdHelper.acquireAuthorizedConnection(
2952-
host.getPrivateIpAddress(), AgentManager.KVMHostDiscoverySshPort.value(), username, password, privateKey);
2950+
host.getPrivateIpAddress(), getHostSshPort(host), username, password, privateKey);
29532951
if (connection == null) {
29542952
throw new CloudRuntimeException(String.format("SSH to agent is enabled, but failed to connect to %s via IP address [%s].", host, host.getPrivateIpAddress()));
29552953
}
@@ -2965,6 +2963,23 @@ protected void connectAndRestartAgentOnHost(HostVO host, String username, String
29652963
}
29662964
}
29672965

2966+
private int getHostSshPort(HostVO host) {
2967+
if (host == null) {
2968+
return AgentManager.KVMHostDiscoverySshPort.value();
2969+
}
2970+
2971+
_hostDao.loadDetails(host);
2972+
String hostPort = host.getDetail(Host.HOST_SSH_POST);
2973+
int sshPort;
2974+
if (StringUtils.isBlank(hostPort)) {
2975+
sshPort = AgentManager.KVMHostDiscoverySshPort.valueIn(host.getClusterId());
2976+
} else {
2977+
sshPort = Integer.parseInt(hostPort);
2978+
}
2979+
2980+
return sshPort;
2981+
}
2982+
29682983
public boolean cancelMaintenance(final long hostId) {
29692984
try {
29702985
final Boolean result = propagateResourceEvent(hostId, ResourceState.Event.AdminCancelMaintenance);

0 commit comments

Comments
 (0)