@@ -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