Skip to content

Commit 343ec00

Browse files
sureshanapartiLocharla, Sandeep
authored andcommitted
Support for custom SSH port for KVM hosts from the host url on add host and the configuration (apache#12571)
1 parent 5b417bd commit 343ec00

7 files changed

Lines changed: 51 additions & 6 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ 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_PORT = "host.ssh.port";
63+
64+
int DEFAULT_SSH_PORT = 22;
6265

6366
/**
6467
* @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 (format: 'host: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/orchestration/src/test/java/com/cloud/agent/manager/AgentManagerImplTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.cloud.agent.api.StartupCommand;
2323
import com.cloud.agent.api.StartupRoutingCommand;
2424
import com.cloud.exception.ConnectionException;
25+
import com.cloud.host.Host;
2526
import com.cloud.host.HostVO;
2627
import com.cloud.host.Status;
2728
import com.cloud.host.dao.HostDao;
@@ -104,4 +105,36 @@ public void testGetTimeoutWithGranularTimeout() {
104105

105106
Assert.assertEquals(50, result);
106107
}
108+
109+
@Test
110+
public void testGetHostSshPortWithHostNull() {
111+
int hostSshPort = mgr.getHostSshPort(null);
112+
Assert.assertEquals(22, hostSshPort);
113+
}
114+
115+
@Test
116+
public void testGetHostSshPortWithNonKVMHost() {
117+
HostVO host = Mockito.mock(HostVO.class);
118+
Mockito.when(host.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.XenServer);
119+
int hostSshPort = mgr.getHostSshPort(host);
120+
Assert.assertEquals(22, hostSshPort);
121+
}
122+
123+
@Test
124+
public void testGetHostSshPortWithKVMHostDefaultPort() {
125+
HostVO host = Mockito.mock(HostVO.class);
126+
Mockito.when(host.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM);
127+
Mockito.when(host.getClusterId()).thenReturn(1L);
128+
int hostSshPort = mgr.getHostSshPort(host);
129+
Assert.assertEquals(22, hostSshPort);
130+
}
131+
132+
@Test
133+
public void testGetHostSshPortWithKVMHostCustomPort() {
134+
HostVO host = Mockito.mock(HostVO.class);
135+
Mockito.when(host.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM);
136+
Mockito.when(host.getDetail(Host.HOST_SSH_PORT)).thenReturn(String.valueOf(3922));
137+
int hostSshPort = mgr.getHostSshPort(host);
138+
Assert.assertEquals(3922, hostSshPort);
139+
}
107140
}

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, 22);
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_PORT, 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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,6 @@ private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long c
851851
_clusterDetailsDao.persist(cluster_cpu_detail);
852852
_clusterDetailsDao.persist(cluster_memory_detail);
853853
}
854-
855854
}
856855

857856
uri = validatedHostUrl(url, hypervisorType);
@@ -945,7 +944,6 @@ private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long c
945944
hosts.add(host);
946945
}
947946
discoverer.postDiscovery(hosts, _nodeId);
948-
949947
}
950948
logger.info("server resources successfully discovered by " + discoverer.getName());
951949
return hosts;
@@ -3873,7 +3871,7 @@ protected Ternary<String, String, String> getHostCredentials(HostVO host) {
38733871
*/
38743872
protected void connectAndRestartAgentOnHost(HostVO host, String username, String password, String privateKey) {
38753873
final com.trilead.ssh2.Connection connection = SSHCmdHelper.acquireAuthorizedConnection(
3876-
host.getPrivateIpAddress(), 22, username, password, privateKey);
3874+
host.getPrivateIpAddress(), _agentMgr.getHostSshPort(host), username, password, privateKey);
38773875
if (connection == null) {
38783876
throw new CloudRuntimeException(String.format("SSH to agent is enabled, but failed to connect to %s via IP address [%s].", host, host.getPrivateIpAddress()));
38793877
}

server/src/test/java/com/cloud/resource/ResourceManagerImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,14 @@ public void testConnectAndRestartAgentOnHostCannotRestart() throws Exception {
387387

388388
@Test
389389
public void testConnectAndRestartAgentOnHost() {
390+
when(agentManager.getHostSshPort(any())).thenReturn(22);
390391
resourceManager.connectAndRestartAgentOnHost(host, hostUsername, hostPassword, hostPrivateKey);
391392
}
392393

393394
@Test
394395
public void testHandleAgentSSHEnabledNotConnectedAgent() {
395396
when(host.getStatus()).thenReturn(Status.Disconnected);
397+
when(agentManager.getHostSshPort(any())).thenReturn(22);
396398
resourceManager.handleAgentIfNotConnected(host, false);
397399
verify(resourceManager).getHostCredentials(eq(host));
398400
verify(resourceManager).connectAndRestartAgentOnHost(eq(host), eq(hostUsername), eq(hostPassword), eq(hostPrivateKey));

utils/src/main/java/com/cloud/utils/ssh/SSHCmdHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static com.trilead.ssh2.Connection acquireAuthorizedConnection(String ip,
7777
}
7878

7979
public static com.trilead.ssh2.Connection acquireAuthorizedConnection(String ip, int port, String username, String password) {
80-
return acquireAuthorizedConnection(ip, 22, username, password, null);
80+
return acquireAuthorizedConnection(ip, port, username, password, null);
8181
}
8282

8383
public static boolean acquireAuthorizedConnectionWithPublicKey(final com.trilead.ssh2.Connection sshConnection, final String username, final String privateKey) {

0 commit comments

Comments
 (0)