diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-12-05 11:30:45 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-12-05 11:30:45 +0200 |
| commit | cd467ed8a6afec8dcf50d8dc71c75d9d445f7489 (patch) | |
| tree | 0adc1655c1d11c434061993d47ef77e461284f7f /Software/Visual_Studio/PPC/Tango.PPC.Common | |
| parent | e8c9c0b649f31bf5170be409cdf6925aa9fc11b6 (diff) | |
| download | Tango-cd467ed8a6afec8dcf50d8dc71c75d9d445f7489.tar.gz Tango-cd467ed8a6afec8dcf50d8dc71c75d9d445f7489.zip | |
Working on PPC packages...
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common')
12 files changed, 183 insertions, 11 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs index 7c67ac1a3..1474eaa04 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs @@ -61,6 +61,11 @@ namespace Tango.PPC.Common.Application bool IsInTechnicianMode { get; } /// <summary> + /// Gets a value indicating whether an update has occurred before the application started. + /// </summary> + bool IsAfterUpdate { get; } + + /// <summary> /// Shutdown the application. /// </summary> void ShutDown(); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/IMachineUpdateManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/IMachineUpdateManager.cs index 85d61d4cc..299e18e57 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/IMachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/IMachineUpdateManager.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.PMR.Synchronization; +using Tango.PPC.Common.UpdatePackages; using Tango.PPC.Common.Web; namespace Tango.PPC.Common.MachineUpdate @@ -64,5 +65,17 @@ namespace Tango.PPC.Common.MachineUpdate /// <param name="filePath">The file path.</param> /// <returns></returns> Task<UpdatePackageFile> GetUpdatePackageFileInfo(String filePath); + + /// <summary> + /// Checks whether any post update packages needs to be installed. + /// </summary> + /// <returns></returns> + Task<bool> PostUpdatePackagesRequired(); + + /// <summary> + /// Runs all post update packages. + /// </summary> + /// <returns></returns> + Task<PackageRunnerResult> RunPostUpdatePackages(); } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs index ae3e093be..33a6d04e6 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs @@ -19,6 +19,7 @@ using Tango.Integration.Upgrade; using Tango.PMR.Synchronization; using Tango.PPC.Common.Application; using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Navigation; using Tango.PPC.Common.UpdatePackages; using Tango.PPC.Common.Web; using Tango.Settings; @@ -33,6 +34,7 @@ namespace Tango.PPC.Common.MachineUpdate private IPPCApplicationManager _app_manager; private IMachineProvider _machineProvider; private IPackageRunner _packageRunner; + private INavigationManager _navigationManager; private PPCWebClient _client; #region Events @@ -66,7 +68,7 @@ namespace Tango.PPC.Common.MachineUpdate /// Initializes a new instance of the <see cref="MachineUpdateManager"/> class. /// </summary> /// <param name="applicationManager">The application manager.</param> - public MachineUpdateManager(PPCWebClient ppcWebClient, IPPCApplicationManager applicationManager, IMachineProvider machineProvider, IPackageRunner packageRunner) + public MachineUpdateManager(PPCWebClient ppcWebClient, IPPCApplicationManager applicationManager, IMachineProvider machineProvider, IPackageRunner packageRunner, INavigationManager navigationManager) { _client = ppcWebClient; _machineProvider = machineProvider; @@ -81,7 +83,7 @@ namespace Tango.PPC.Common.MachineUpdate private void _packageRunner_PackageStateChanged(object sender, PackageStateChangedEventArgs e) { - UpdateProgress("Preparing", e.PackageName); + UpdateProgress(e.PackageType == PackageType.Pre ? "Preparing" : "Finalizing", e.PackageName); } #endregion @@ -833,6 +835,26 @@ namespace Tango.PPC.Common.MachineUpdate }); } + /// <summary> + /// Checks whether any post update packages needs to be installed. + /// </summary> + /// <returns></returns> + public Task<bool> PostUpdatePackagesRequired() + { + String packagesFolder = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "packages"); + return _packageRunner.IsPackageInstallationRequired(PackageType.Post, packagesFolder); + } + + /// <summary> + /// Runs all post update packages. + /// </summary> + /// <returns></returns> + public Task<PackageRunnerResult> RunPostUpdatePackages() + { + String packagesFolder = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "packages"); + return _packageRunner.Run(PackageType.Post, packagesFolder); + } + #endregion #region Protected Methods diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs index d8a1eec56..643908e87 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs @@ -23,5 +23,6 @@ namespace Tango.PPC.Common.Navigation ShutdownView, RestartingSystemView, EmergencyView, + RestartingView, } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj index 892269c03..671cac4f1 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj @@ -162,6 +162,7 @@ <Compile Include="UpdatePackages\PackageContext.cs" /> <Compile Include="UpdatePackages\PackageInstallation.cs" /> <Compile Include="UpdatePackages\PackageInstallationState.cs" /> + <Compile Include="UpdatePackages\PackageRunnerResult.cs" /> <Compile Include="UpdatePackages\PackageStateChangedEventArgs.cs" /> <Compile Include="UpdatePackages\PackagesFile.cs" /> <Compile Include="UpdatePackages\PackageType.cs" /> diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs index 882896f04..e1323916b 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs @@ -6,9 +6,14 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; using Tango.Core; +using Tango.Core.DI; using Tango.Core.Helpers; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Notifications; namespace Tango.PPC.Common.UpdatePackages { @@ -87,11 +92,16 @@ namespace Tango.PPC.Common.UpdatePackages File.WriteAllText(_configFile, json); } - public Task Run(PackageType type, String packagesFolder) + public Task<PackageRunnerResult> Run(PackageType type, String packagesFolder) { - return Task.Factory.StartNew(() => + return Task.Factory.StartNew<PackageRunnerResult>(() => { + PackageRunnerResult result = new PackageRunnerResult(); + PackageContext context = new PackageContext(); + context.ApplicationManager = TangoIOC.Default.GetInstance<IPPCApplicationManager>(); + context.MachineProvider = TangoIOC.Default.GetInstance<IMachineProvider>(); + context.NotificationProvider = TangoIOC.Default.GetInstance<INotificationProvider>(); LogManager.Log($"Running {type}-update packages..."); @@ -163,19 +173,27 @@ namespace Tango.PPC.Common.UpdatePackages { try { - OnPackageRuns(att.Name, installedPackage.State); + OnPackageRuns(att.Name, installedPackage.State, installedPackage.Type); + installedPackage.InstallationDate = DateTime.Now; packageInstance.Run(context).GetAwaiter().GetResult(); installedPackage.State = PackageInstallationState.Installed; installedPackage.FailedReason = null; - OnPackageRuns(att.Name, installedPackage.State); + OnPackageRuns(att.Name, installedPackage.State, installedPackage.Type); LogManager.Log("Package installed successfully."); + + if (att.RequiresRestart) + { + result.RestartRequired = true; + } + + Thread.Sleep(2000); } catch (Exception ex) { LogManager.Log(ex, "Package installation failed."); installedPackage.State = PackageInstallationState.Failed; installedPackage.FailedReason = ex.FlattenMessage(); - OnPackageRuns(att.Name, installedPackage.State); + OnPackageRuns(att.Name, installedPackage.State, installedPackage.Type); continue; } } @@ -210,15 +228,97 @@ namespace Tango.PPC.Common.UpdatePackages { LogManager.Log(ex, "Error saving packages file!"); } + + return result; + }); + } + + public Task<bool> IsPackageInstallationRequired(PackageType type, String packagesFolder) + { + return Task.Factory.StartNew<bool>(() => + { + LogManager.Log("Checking if any package installation is required..."); + + _packagesFile = GetPackagesFile().Result; + + //Get all packages in folder. + foreach (var packageFile in Directory.GetFiles(packagesFolder, "*.dll")) + { + LogManager.Log($"Loading assembly '{Path.GetFileName(packageFile)}'..."); + + Assembly asm; + + //Load assembly and investigate for types based on package type. + try + { + asm = Assembly.LoadFile(packageFile); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error loading assembly!"); + continue; + } + + try + { + foreach (var packageType in asm.GetTypes().Where( + x => typeof(IPPCPackage).IsAssignableFrom(x) && + x.GetCustomAttribute<PPCPackageAttribute>() != null && + x.GetCustomAttribute<PPCPackageAttribute>().Type == type)) + { + LogManager.Log($"Checking package '{packageType.FullName}'..."); + + try + { + //Getting installed package from file. + var installedPackage = _packagesFile.PackageInstallations.SingleOrDefault(x => x.PackageName == packageType.FullName); + + //Check if requires installation. + if (installedPackage == null || installedPackage.State != PackageInstallationState.Installed) + { + if (installedPackage == null) + { + LogManager.Log("Package was never installed."); + return true; + } + else + { + LogManager.Log($"Package installation state is '{installedPackage.State}' due to {installedPackage.FailedReason}"); + return true; + } + } + else + { + LogManager.Log("Package is already installed."); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error in handling the package!"); + continue; + } + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error investigating assembly!"); + continue; + } + } + + LogManager.Log("No packages to install."); + + return false; }); } - protected virtual void OnPackageRuns(String packageName, PackageInstallationState state) + protected virtual void OnPackageRuns(String packageName, PackageInstallationState state, PackageType type) { PackageStateChanged?.Invoke(this, new PackageStateChangedEventArgs() { PackageName = packageName, State = state, + PackageType = type, }); } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs index 03c583dca..8a3490f8d 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs @@ -10,6 +10,7 @@ namespace Tango.PPC.Common.UpdatePackages { event EventHandler<PackageStateChangedEventArgs> PackageStateChanged; Task<PackagesFile> GetPackagesFile(); - Task Run(PackageType type, String packagesFolder); + Task<PackageRunnerResult> Run(PackageType type, String packagesFolder); + Task<bool> IsPackageInstallationRequired(PackageType type, String packagesFolder); } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs index 7ae4ea52d..3186b57b1 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs @@ -13,10 +13,13 @@ namespace Tango.PPC.Common.UpdatePackages public PackageType Type { get; set; } - public PPCPackageAttribute(PackageType type, String name) + public bool RequiresRestart { get; set; } + + public PPCPackageAttribute(PackageType type, String name, bool requiresRestart) { Type = type; Name = name; + RequiresRestart = requiresRestart; } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs index cf96ff026..4cfe49af8 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs @@ -3,11 +3,16 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Notifications; namespace Tango.PPC.Common.UpdatePackages { public class PackageContext { - + public IPPCApplicationManager ApplicationManager { get; set; } + public IMachineProvider MachineProvider { get; set; } + public INotificationProvider NotificationProvider { get; set; } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs index bcffb1b6e..bf00915bd 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs @@ -12,8 +12,15 @@ namespace Tango.PPC.Common.UpdatePackages public PackageType Type { get; set; } + public DateTime InstallationDate { get; set; } + public PackageInstallationState State { get; set; } public String FailedReason { get; set; } + + public PackageInstallation() + { + InstallationDate = DateTime.Now; + } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageRunnerResult.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageRunnerResult.cs new file mode 100644 index 000000000..55f3a490a --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageRunnerResult.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.UpdatePackages +{ + public class PackageRunnerResult + { + public bool RestartRequired { get; set; } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs index 62eb00e5e..9c8cbe2c0 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs @@ -10,5 +10,6 @@ namespace Tango.PPC.Common.UpdatePackages { public PackageInstallationState State { get; set; } public String PackageName { get; set; } + public PackageType PackageType { get; set; } } } |
