private string GetInstallationDirectory(UpdateStartupContext startupContext) { Logger.Debug("Using process ID to find installation directory: {0}", startupContext.ProcessId); var exeFileInfo = new FileInfo(_processProvider.GetProcessById(startupContext.ProcessId).StartPath); Logger.Debug("Executable location: {0}", exeFileInfo.FullName); return(exeFileInfo.DirectoryName); }
public void Start(UpdateStartupContext ctx) { var dector = new DetectApplicationType(); var processId = _processProvider.FindProcessByName(ProcessProvider.OmbiProcessName)?.FirstOrDefault()?.Id ?? -1; // Log if process is -1 var dir = CreateTempPath(); TempPath = Path.Combine(dir.FullName, "OmbiUpdate.zip"); using (var client = new WebClient()) { client.DownloadProgressChanged += (s, e) => { Console.WriteLine($"{e.ProgressPercentage}%"); }; client.DownloadFile(ctx.DownloadPath, TempPath); } var appType = dector.GetAppType(); _processProvider.FindProcessByName(ProcessProvider.OmbiProcessName); var installationFolder = GetInstallationDirectory(ctx); var terminator = new TerminateOmbi(new ServiceProvider(_processProvider), _processProvider); if (OsInfo.IsWindows) { terminator.Terminate(processId); } try { BackupCurrentVersion(); EmptyInstallationFolder(); using (var archive = ZipFile.OpenRead(TempPath)) { foreach (var entry in archive.Entries) { var fullname = string.Empty; if (entry.FullName.Contains("Release/")) // Don't extract the release folder, we are already in there { fullname = entry.FullName.Replace("Release/", string.Empty); } if (entry.Name.Contains("UpdateService")) { fullname = entry.FullName.Replace("UpdateService", "UpdateService_New"); } var fullPath = Path.Combine(PathUp(Path.GetDirectoryName(Application.ExecutablePath), 1), fullname); if (string.IsNullOrEmpty(entry.Name)) { Directory.CreateDirectory(fullPath); } else { if (entry.Name.Contains("Updater")) { continue; } entry.ExtractToFile(fullPath, true); Console.WriteLine("Restored {0}", entry.FullName); } } } // Need to install here } catch (Exception e) { Console.WriteLine(e); RestoreBackup(); throw; } finally { var startOmbi = new StartOmbi(new ServiceProvider(_processProvider), _processProvider); if (OsInfo.IsWindows) { startOmbi.Start(appType, installationFolder); } else { terminator.Terminate(processId); Logger.Info("Waiting for external auto-restart."); for (int i = 0; i < 5; i++) { System.Threading.Thread.Sleep(1000); if (_processProvider.Exists(ProcessProvider.OmbiProcessName)) { Logger.Info("Ombi was restarted by external process."); break; } } if (!_processProvider.Exists(ProcessProvider.OmbiProcessName)) { startOmbi.Start(appType, installationFolder, ctx.StartupArgs); } } } }