private NativeMethods.IUpdate GetUpdateToDownload(NativeMethods.IUpdateCollection updateCollection) { try { NativeMethods.IUpdate updateToDownload = null; if (updateCollection.Count > 0) { foreach (NativeMethods.IUpdate update in updateCollection) { if (updateToDownload != null) { Trace.WriteWarning(TraceType, "GetUpdateToDownload: Update collection has more then one update: {0}", update.Identity.UpdateID); } else { updateToDownload = update; } } } // Taking the first one return(updateToDownload); } catch (Exception ex) { Trace.WriteError(TraceType, "GetUpdateToDownload: Error : {0}", ex); } return(null); }
public async Task <ClusterUpgradeCommandParameter> DownloadWindowsUpdate( NativeMethods.IUpdate update, TimeSpan timeout, CancellationToken token) { if (update == null) { return(null); } if (!update.IsDownloaded) { var downloadOperation = DownloadUpdateAsync(updateSession, update); await AwaitForOperationToComplete(downloadOperation, timeout, token, "Download"); } if (update.IsDownloaded) { string dir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); var info = new DirectoryInfo(dir); if (!info.Exists) { info.Create(); } update.CopyFromCache(dir, false); var cabFilePath = Directory.GetFiles(dir, "*.cab").FirstOrDefault(); if (cabFilePath != null) { var cabVersion = CabFileOperations.GetCabVersion(cabFilePath); return(new ClusterUpgradeCommandParameter() { CodeFilePath = cabFilePath, CodeVersion = cabVersion }); } } return(null); }
private Task <NativeMethods.IDownloadResult> DownloadUpdateAsync(NativeMethods.IUpdateSession3 updateSession, NativeMethods.IUpdate update) { NativeMethods.IUpdateDownloader updateDownloader = updateSession.CreateUpdateDownloader(); updateDownloader.Updates = (NativeMethods.IUpdateCollection) new NativeMethods.UpdateCollection(); updateDownloader.Updates.Add(update); TaskCompletionSource <NativeMethods.IDownloadResult> tcs = new TaskCompletionSource <NativeMethods.IDownloadResult>(); var job = updateDownloader.BeginDownload( new DownloadProgressCallback(), new DownloadCompletedCallback(updateDownloader, tcs), null); return(tcs.Task); }
// Return true if upgrade started other wise false private async Task <bool> OnGettingUpdateCollectionAsync( NativeMethods.IUpdateCollection updateCollection, FabricUpgradeProgress upgradeProgress, CancellationToken token) { ClusterUpgradeCommandParameter commandParameter = null; string updateId = string.Empty; if (updateCollection != null) { NativeMethods.IUpdate updateToDownload = this.GetUpdateToDownload(updateCollection); if (updateToDownload != null) { Trace.WriteInfo(TraceType, "OnGettingUpdateCollectionAsync: Update to download: {0}", updateToDownload.Identity.UpdateID); commandParameter = await this.packageRetriever.DownloadWindowsUpdate(updateToDownload, this.windowsUpdateApiTimeout, token); updateId = updateToDownload.Identity.UpdateID; } else { Trace.WriteInfo(TraceType, "OnGettingUpdateCollectionAsync: No update found."); } } else { Trace.WriteInfo(TraceType, "OnGettingUpdateCollectionAsync: update collection is null."); if (this.testMode && Directory.Exists(this.testSrcDir)) { var srcCabFile = Directory.GetFiles(this.testSrcDir, "*.cab", SearchOption.TopDirectoryOnly).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(srcCabFile)) { Trace.WriteWarning(TraceType, "OnGettingUpdateCollectionAsync: Test cab file {0}", srcCabFile); var dir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); var info = new DirectoryInfo(dir); if (!info.Exists) { info.Create(); } updateId = Guid.NewGuid().ToString(); var destCabFile = Path.Combine(dir, "test.cab"); File.Copy(srcCabFile, destCabFile); Trace.WriteWarning(TraceType, "OnGettingUpdateCollectionAsync: Test dest file {0}", destCabFile); var cabVersion = CabFileOperations.GetCabVersion(destCabFile); Trace.WriteWarning(TraceType, "OnGettingUpdateCollectionAsync: Cab version {0}", cabVersion); commandParameter = new ClusterUpgradeCommandParameter() { CodeFilePath = destCabFile, CodeVersion = cabVersion }; } } } if (commandParameter == null) { return(false); } var updatedCommandParameter = await this.UpdateReplicaStoreBeforeUpgradeStartAsync( commandParameter, upgradeProgress, updateId, token); var upgradeTask = await this.commandProcessor.ClusterUpgradeAsync(updatedCommandParameter, TimeSpan.MaxValue, token).ContinueWith( (task) => { if (commandParameter != null) { DeleteFileDirectory(commandParameter.CodeFilePath); DeleteFileDirectory(commandParameter.ConfigFilePath); } return(task); }); await upgradeTask; return(true); }