-
-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Winget Auto-Update & Install (#1686)
* Figuring it out * Update to both instal and update the package... tests * Added the ability to both install and update the appliation using Winget. This should run on Windows Server by skipping all the checks as winget is not supported ,we still deploy to chocolaty.
- Loading branch information
Showing
7 changed files
with
305 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<LUTConfig Version="1.0"> | ||
<Repository /> | ||
<ParallelBuilds>true</ParallelBuilds> | ||
<ParallelTestRuns>true</ParallelTestRuns> | ||
<TestCaseTimeout>180000</TestCaseTimeout> | ||
</LUTConfig> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/MigrationTools.Host.Tests/Services/DetectVersionService2Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using MigrationTools.Services; | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace MigrationTools.Host.Services.Tests | ||
{ | ||
[TestClass] | ||
public class DetectVersionService2Tests | ||
{ | ||
|
||
[TestInitialize] | ||
public void Setup() | ||
{ | ||
var loggers = new LoggerConfiguration().MinimumLevel.Verbose().Enrich.FromLogContext(); | ||
loggers.WriteTo.Logger(logger => logger | ||
.WriteTo.Debug(restrictedToMinimumLevel: LogEventLevel.Verbose)); | ||
Log.Logger = loggers.CreateLogger(); | ||
Log.Logger.Information("Logger is initialized"); | ||
} | ||
|
||
|
||
[TestMethod, TestCategory("L3")] | ||
public void DetectVersionServiceTest_Initialise() | ||
{ | ||
IDetectVersionService2 dos = new DetectVersionService2(new TelemetryLoggerMock()); | ||
Assert.IsNotNull(dos); | ||
} | ||
|
||
[TestMethod, TestCategory("L3")] | ||
public void DetectVersionServiceTest_Update() | ||
{ | ||
IDetectVersionService2 dos = new DetectVersionService2(new TelemetryLoggerMock()); | ||
dos.UpdateFromSource(); | ||
|
||
Assert.IsNotNull(dos); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
src/MigrationTools.Host/Services/DetectVersionService2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.Extensions.Logging; | ||
using MigrationTools.DataContracts.Pipelines; | ||
using MigrationTools.EndpointEnrichers; | ||
using NuGet.Common; | ||
using NuGet.Configuration; | ||
using NuGet.Protocol; | ||
using NuGet.Protocol.Core.Types; | ||
using NuGet.Versioning; | ||
using Serilog; | ||
using WGetNET; | ||
using Version = System.Version; | ||
|
||
namespace MigrationTools.Host.Services | ||
{ | ||
public class DetectVersionService2 : IDetectVersionService2 | ||
{ | ||
private readonly ITelemetryLogger _Telemetry; | ||
|
||
public string PackageId { get; set; } | ||
|
||
private WinGetPackageManager packageManager; | ||
private WinGetPackage package = null; | ||
|
||
public Version RunningVersion { get; private set; } | ||
public Version AvailableVersion { get; private set; } | ||
|
||
public Version InstalledVersion { get; private set; } | ||
|
||
public bool IsPackageInstalled { get; private set; } = false; | ||
|
||
public bool IsPackageManagerInstalled { get; private set; } = false; | ||
|
||
public bool IsUpdateAvailable { | ||
get | ||
{ | ||
return (InstalledVersion < AvailableVersion); | ||
} | ||
} | ||
|
||
public bool IsRunningInDebug | ||
{ | ||
get | ||
{ | ||
return RunningVersion == new Version("0.0.0.1"); | ||
} | ||
} | ||
|
||
public bool IsNewLocalVersionAvailable | ||
{ | ||
get | ||
{ | ||
return (RunningVersion >= InstalledVersion); | ||
} | ||
} | ||
|
||
public DetectVersionService2(ITelemetryLogger telemetry) | ||
{ | ||
_Telemetry = telemetry; | ||
PackageId = "nkdAgility.AzureDevOpsMigrationTools"; | ||
|
||
RunningVersion = Assembly.GetEntryAssembly().GetName().Version; | ||
InitialiseService(); | ||
} | ||
|
||
private void InitialiseService() | ||
{ | ||
DateTime startTime = DateTime.Now; | ||
using (var bench = new Benchmark("DetectVersionService2::InitialiseService")) | ||
{ | ||
////////////////////////////////// | ||
WinGetInfo wingetInfo = new WinGetInfo(); | ||
IsPackageManagerInstalled = wingetInfo.WinGetInstalled; | ||
if (IsPackageManagerInstalled) | ||
{ | ||
Log.Debug("The Windows Package Manager is installed!"); | ||
packageManager = new WinGetPackageManager(); | ||
} | ||
try | ||
{ | ||
|
||
if (IsPackageManagerInstalled) | ||
{ | ||
Log.Debug("Searching for package!"); | ||
package = packageManager.GetInstalledPackages(PackageId).GroupBy(e => e.Id, (id, g) => g.First()).SingleOrDefault(); | ||
if (package != null) | ||
{ | ||
AvailableVersion = new Version(package.AvailableVersion); | ||
InstalledVersion = new Version(package.Version); | ||
Log.Debug("Found package with id {PackageId}", PackageId); | ||
IsPackageInstalled = true; | ||
} | ||
_Telemetry.TrackDependency(new DependencyTelemetry("PackageRepository", "winget", PackageId, AvailableVersion == null ? "nullVersion" : AvailableVersion.ToString(), startTime, bench.Elapsed, "200", IsPackageInstalled)); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Error(ex, "DetectVersionService"); | ||
IsPackageInstalled = false; | ||
_Telemetry.TrackDependency(new DependencyTelemetry("PackageRepository", "winget", PackageId, AvailableVersion == null ? "nullVersion" : AvailableVersion.ToString(), startTime, bench.Elapsed, "500", IsPackageInstalled)); | ||
} | ||
} | ||
} | ||
|
||
|
||
public void UpdateFromSource() | ||
{ | ||
using (var bench = new Benchmark("DetectVersionService2::UpdateFromSource")) | ||
{ | ||
if (IsPackageInstalled && IsUpdateAvailable) | ||
{ | ||
Log.Information("Running winget update {PackageId} from v{InstalledVersion} to v{AvailableVersion}", PackageId, InstalledVersion, AvailableVersion); | ||
System.Threading.Tasks.Task t = packageManager.UpgradePackageAsync(PackageId); | ||
while (!t.IsCompleted) | ||
{ | ||
Log.Information("Update running..."); | ||
System.Threading.Thread.Sleep(3000); | ||
} | ||
Log.Information("Update Complete..."); | ||
InitialiseService(); | ||
} | ||
else if (!IsPackageInstalled) | ||
{ | ||
Log.Information("Running winget install {PackageId} from v{InstalledVersion} to v{AvailableVersion}", PackageId, InstalledVersion, AvailableVersion); | ||
|
||
System.Threading.Tasks.Task t = packageManager.InstallPackageAsync(PackageId); | ||
while (!t.IsCompleted) | ||
{ | ||
Log.Information("Install running..."); | ||
System.Threading.Thread.Sleep(5000); | ||
} | ||
Log.Information("Install Complete..."); | ||
InitialiseService(); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
public class Benchmark : IDisposable | ||
{ | ||
private readonly Stopwatch timer = new Stopwatch(); | ||
private readonly string benchmarkName; | ||
|
||
public TimeSpan Elapsed | ||
{ | ||
get | ||
{ | ||
return timer.Elapsed; | ||
} | ||
} | ||
|
||
public Benchmark(string benchmarkName) | ||
{ | ||
this.benchmarkName = benchmarkName; | ||
timer.Start(); | ||
Log.Debug("{benchmarkName}||START", benchmarkName); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
timer.Stop(); | ||
Log.Debug("{benchmarkName}||STOP Elapsed: {timerElapsed}", benchmarkName, timer.Elapsed); | ||
} | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/MigrationTools.Host/Services/IDetectVersionService2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace MigrationTools.Host.Services | ||
{ | ||
public interface IDetectVersionService2 | ||
{ | ||
public Version AvailableVersion { get; } | ||
|
||
public Version InstalledVersion { get; } | ||
|
||
public Version RunningVersion { get; } | ||
|
||
public bool IsPackageManagerInstalled { get; } | ||
|
||
public bool IsPackageInstalled { get; } | ||
string PackageId { get; } | ||
|
||
bool IsUpdateAvailable { get; } | ||
bool IsRunningInDebug { get; } | ||
bool IsNewLocalVersionAvailable { get; } | ||
|
||
void UpdateFromSource(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters