Skip to content

Commit 30a3939

Browse files
nvazquezDaanHoogland
authored andcommitted
Refactor and fix upload certificate error message even though operation is successful
1 parent e1df782 commit 30a3939

3 files changed

Lines changed: 54 additions & 27 deletions

File tree

api/src/org/apache/cloudstack/api/command/admin/direct/download/UploadTemplateDirectDownloadCertificate.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,20 @@ public void execute() throws ResourceUnavailableException, InsufficientCapacityE
6767
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Currently supporting KVM hosts only");
6868
}
6969

70+
SuccessResponse response = new SuccessResponse(getCommandName());
7071
try {
71-
directDownloadManager.uploadCertificateToHosts(certificate, name);;
72-
setResponseObject(new SuccessResponse(getCommandName()));
72+
LOG.debug("Uploading certificate " + name + " to agents for Direct Download");
73+
boolean result = directDownloadManager.uploadCertificateToHosts(certificate, name, hypervisor);
74+
response.setSuccess(result);
75+
setResponseObject(response);
7376
} catch (Exception e) {
7477
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
7578
}
7679
}
7780

7881
@Override
7982
public String getCommandName() {
80-
return UploadTemplateDirectDownloadCertificate.APINAME;
83+
return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX;
8184
}
8285

8386
@Override

framework/direct-download/src/org/apache/cloudstack/framework/agent/direct/download/DirectDownloadService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ public interface DirectDownloadService {
2727
/**
2828
* Upload client certificate to each running host
2929
*/
30-
boolean uploadCertificateToHosts(String certificateCer, String certificateName);
30+
boolean uploadCertificateToHosts(String certificateCer, String certificateName, String hypervisor);
3131
}

server/src/org/apache/cloudstack/direct/download/DirectDownloadManagerImpl.java

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,7 @@ public void downloadTemplate(long templateId, long poolId, long hostId) {
211211
DownloadProtocol protocol = getProtocolFromUrl(url);
212212
DirectDownloadCommand cmd = getDirectDownloadCommandFromProtocol(protocol, url, templateId, to, checksum, headers);
213213

214-
boolean downloaded = false;
215-
int retry = 3;
216-
Long[] hostsToRetry = getHostsToRetryOn(host.getClusterId(), host.getDataCenterId(), host.getHypervisorType(), hostId);
217-
int hostIndex = 0;
218-
Answer answer = null;
219-
Long hostToSendDownloadCmd = hostsToRetry[hostIndex];
220-
while (!downloaded && retry > 0) {
221-
s_logger.debug("Sending Direct download command to host " + hostToSendDownloadCmd);
222-
answer = agentManager.easySend(hostToSendDownloadCmd, cmd);
223-
downloaded = answer != null && answer.getResult();
224-
hostToSendDownloadCmd = hostsToRetry[(hostIndex + 1) % hostsToRetry.length];
225-
retry --;
226-
}
227-
if (!downloaded) {
228-
throw new CloudRuntimeException("Template " + templateId + " could not be downloaded on pool " + poolId + ", failing after trying on several hosts");
229-
}
214+
Answer answer = sendDirectDownloadCommand(cmd, templateId, poolId, host);
230215

231216
VMTemplateStoragePoolVO sPoolRef = vmTemplatePoolDao.findByPoolTemplate(poolId, templateId);
232217
if (sPoolRef == null) {
@@ -245,6 +230,35 @@ public void downloadTemplate(long templateId, long poolId, long hostId) {
245230
}
246231
}
247232

233+
/**
234+
* Send direct download command for downloading template with ID templateId on storage pool with ID poolId.<br/>
235+
* At first, cmd is sent to host, in case of failure it will retry on other hosts before failing
236+
* @param cmd direct download command
237+
* @param templateId template id
238+
* @param poolId pool id
239+
* @param host first host to which send the command
240+
* @return download answer from any host which could handle cmd
241+
*/
242+
private Answer sendDirectDownloadCommand(DirectDownloadCommand cmd, long templateId, long poolId, HostVO host) {
243+
boolean downloaded = false;
244+
int retry = 3;
245+
Long[] hostsToRetry = getHostsToRetryOn(host.getClusterId(), host.getDataCenterId(), host.getHypervisorType(), host.getId());
246+
int hostIndex = 0;
247+
Answer answer = null;
248+
Long hostToSendDownloadCmd = hostsToRetry[hostIndex];
249+
while (!downloaded && retry > 0) {
250+
s_logger.debug("Sending Direct download command to host " + hostToSendDownloadCmd);
251+
answer = agentManager.easySend(hostToSendDownloadCmd, cmd);
252+
downloaded = answer != null && answer.getResult();
253+
hostToSendDownloadCmd = hostsToRetry[(hostIndex + 1) % hostsToRetry.length];
254+
retry --;
255+
}
256+
if (!downloaded) {
257+
throw new CloudRuntimeException("Template " + templateId + " could not be downloaded on pool " + poolId + ", failing after trying on several hosts");
258+
}
259+
return answer;
260+
}
261+
248262
/**
249263
* Return DirectDownloadCommand according to the protocol
250264
* @param protocol
@@ -268,16 +282,26 @@ private DirectDownloadCommand getDirectDownloadCommandFromProtocol(DownloadProto
268282
}
269283
}
270284

271-
@Override
272-
public boolean uploadCertificateToHosts(String certificateCer, String certificateName) {
273-
List<HostVO> hosts = hostDao.listAllHostsByType(Host.Type.Routing)
285+
/**
286+
* Return the list of running hosts to which upload certificates for Direct Download
287+
*/
288+
private List<HostVO> getRunningHostsToUploadCertificate(HypervisorType hypervisorType) {
289+
return hostDao.listAllHostsByType(Host.Type.Routing)
274290
.stream()
275291
.filter(x -> x.getStatus().equals(Status.Up) &&
276-
x.getHypervisorType().equals(HypervisorType.KVM))
292+
x.getHypervisorType().equals(hypervisorType))
277293
.collect(Collectors.toList());
278-
for (HostVO host : hosts) {
279-
if (!uploadCertificate(certificateCer, certificateName, host.getId())) {
280-
throw new CloudRuntimeException("Uploading certificate " + certificateName + " failed on host: " + host.getId());
294+
}
295+
296+
@Override
297+
public boolean uploadCertificateToHosts(String certificateCer, String certificateName, String hypervisor) {
298+
HypervisorType hypervisorType = HypervisorType.getType(hypervisor);
299+
List<HostVO> hosts = getRunningHostsToUploadCertificate(hypervisorType);
300+
if (CollectionUtils.isNotEmpty(hosts)) {
301+
for (HostVO host : hosts) {
302+
if (!uploadCertificate(certificateCer, certificateName, host.getId())) {
303+
throw new CloudRuntimeException("Uploading certificate " + certificateName + " failed on host: " + host.getId());
304+
}
281305
}
282306
}
283307
return true;

0 commit comments

Comments
 (0)