Ejemplo n.º 1
0
        public UpdateResult DoRollback(Guid UpdateInstructionID, Guid RollbackUpdateBackupID)
        {
            UpdateInstruction UpdateInstruction = _UpdateRepository.GetUpdateInstructionByID(UpdateInstructionID);
            UpdateBackupEntry UpdateBackupEntry = _UpdateRepository.GetUpdateBackupEntryFromUpdateID(UpdateInstruction, RollbackUpdateBackupID);

            string AnalyticsMessageResult = "";
            var    updateResultReturn     = new UpdateResult()
            {
                ID        = UpdateInstruction.ID,
                IsSuccess = false,
                //Message = "No update found. Nothing to update!",
                UpdateInstructionID = UpdateInstruction.ID
            };

            try
            {
                Version   CurrentVersionBeforeRollback = this.GetCurrentAssemblyVersion(UpdateInstruction);
                JobStatus JobStatus = _JobService.GetQueuePosition(new JobStatus()
                {
                    ID = UpdateInstruction.ID
                });

                updateResultReturn = ExecuteRollback(UpdateInstruction, UpdateBackupEntry);

                AnalyticsMessageResult = !updateResultReturn.IsSuccess ? $"Job '{JobStatus.QueuePosition}' Failed to rollback '{UpdateInstruction.Name}' from '{CurrentVersionBeforeRollback}' to '{UpdateBackupEntry.BackupVersion}'. Alepsed time is: {this.ConvertMillisecondsToTimeString(updateResultReturn.TimeSpentMilliseconds)}.\r\nSee details:\r\n\r\n{ JsonConvert.SerializeObject(updateResultReturn.Messages, Formatting.Indented)}"
                        : $"Job '{JobStatus.QueuePosition}' rollback success for product '{UpdateInstruction.Name}' from '{CurrentVersionBeforeRollback}' to '{UpdateBackupEntry.BackupVersion}'. Alepsed time is: {this.ConvertMillisecondsToTimeString(updateResultReturn.TimeSpentMilliseconds)}.";
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                _JobService.CreateUpdateJobStatus(new JobStatus()
                {
                    ID = UpdateInstruction.ID, CurrentStatus = JobStatusTyes.Completed
                });
            }

            this.SendAnalyticsData("UpdateService.DoRollback.Results", AnalyticsMessageResult);
            return(updateResultReturn);
        }
Ejemplo n.º 2
0
        public UpdateResult ExecuteRollback(UpdateInstruction UpdateInstruction, UpdateBackupEntry UpdateBackupEntry)
        {
            //1 - Rodar linha de comando antes do rollback
            //2 - Fazer backup do diretório antigo com prefixo da versão
            //3 - Renomear Diretório do backup para o diretório atual
            //4 - Rodar linha de comano pós rollback

            var updateResultReturn = new UpdateResult()
            {
                ID = Guid.NewGuid(),
                UpdateInstructionID = UpdateInstruction.ID
            };

            Stopwatch stopWatchTimer = Stopwatch.StartNew();

            System.Version CurrentVersion  = new Version("0.0.0");
            System.Version RollbackVersion = new Version("0.0.0");
            try
            {
                updateResultReturn.AddMessage("Starting the update process", UpdateResultMessage.eMessageType.INFORMATION);
                CurrentVersion  = AssemblyManager.GetAssemblyVersion(UpdateInstruction.MainAssembly);
                RollbackVersion = AssemblyManager.GetAssemblyVersion(UpdateBackupEntry.FullPath);

                updateResultReturn.AddMessage($"Trying to rollback version {CurrentVersion} to {RollbackVersion}", UpdateResultMessage.eMessageType.INFORMATION);

                var comandLineBeforeResult = Shell.ExecuteTerminalCommand(UpdateInstruction.GetCommandLineBeforeUpdateWithReplacedParams());
                if (comandLineBeforeResult.code > 0)
                {
                    throw new Exception($"Cannot run Commandline before update, see details.\r\nStdOut: {comandLineBeforeResult.stdout}\r\nStdErr: {comandLineBeforeResult.stderr}");
                }

                updateResultReturn.AddMessage($"Command line 'CommandLineBeforeUpdate' executed.\r\nOutput code: {comandLineBeforeResult.code}\r\nStdOut: {comandLineBeforeResult.stdout}\r\nStdErr: {comandLineBeforeResult.stderr}", UpdateResultMessage.eMessageType.SUCCESS);


                //Execuando Backup
                string workingFolderBackup = this.GenerateBackupFolderFullPathName(UpdateBackupEntry.UpdateID, updateResultReturn.ID, UpdateInstruction.WorkingDirectory, CurrentVersion);
                DirectoryManager.RenameDirectory(UpdateInstruction.WorkingDirectory, workingFolderBackup);

                updateResultReturn.AddMessage($"Renamed current version folder from {UpdateInstruction.WorkingDirectory} to {workingFolderBackup}.", UpdateResultMessage.eMessageType.SUCCESS);
                //Fazendo Rollback da versão anterior para versão atual de trabalho.
                //string BackupFolder = GetBackupFolderFromUpdateID()
                DirectoryManager.RenameDirectory(UpdateBackupEntry.FullPath, UpdateInstruction.WorkingDirectory);
                updateResultReturn.AddMessage($"Rollback diretory {UpdateBackupEntry.FullPath} to {UpdateInstruction.WorkingDirectory}.", UpdateResultMessage.eMessageType.SUCCESS);


                var comandLineAfterResult = Shell.ExecuteTerminalCommand(UpdateInstruction.GetCommandLineAfterUpdateWithReplacedParams());
                if (comandLineAfterResult.code > 0)
                {
                    throw new Exception($"Cannot run Commandline after update, see details.r\nStdOut: {comandLineAfterResult.stdout}\r\nStdErr: {comandLineAfterResult.stderr}");
                }

                updateResultReturn.AddMessage($"Command line 'CommandLineBeforeUpdate' executed.\r\nOutput code: {comandLineAfterResult.code}\r\nStdOut: {comandLineAfterResult.stdout}\r\nStdErr: {comandLineAfterResult.stderr}", UpdateResultMessage.eMessageType.SUCCESS);

                stopWatchTimer.Stop();
                updateResultReturn.TimeSpentMilliseconds = stopWatchTimer.ElapsedMilliseconds;
                updateResultReturn.IsSuccess             = true;

                /*string alepsedTime = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
                 *          stopWatchTimer.Elapsed.Hours,
                 *          stopWatchTimer.Elapsed.Minutes,
                 *          stopWatchTimer.Elapsed.Seconds,
                 *          stopWatchTimer.Elapsed.Milliseconds);*/

                updateResultReturn
                .AddMessage($"Rollback '{UpdateInstruction.Name}' from '{CurrentVersion}' to '{RollbackVersion}'. Alepsed time: {this.ConvertMillisecondsToTimeString(updateResultReturn.TimeSpentMilliseconds)}",
                            updateResultReturn.IsSuccess ?
                            (updateResultReturn.Messages.Where(x => x.Type == UpdateResultMessage.eMessageType.ERROR).Count() <= 0 ? UpdateResultMessage.eMessageType.SUCCESS : UpdateResultMessage.eMessageType.WARNING) : UpdateResultMessage.eMessageType.ERROR);
            }
            catch (Exception ex)
            {
                stopWatchTimer.Stop();
                updateResultReturn.TimeSpentMilliseconds = stopWatchTimer.ElapsedMilliseconds;

                updateResultReturn
                .AddMessage($"Error updating '{UpdateInstruction.Name}' from '{CurrentVersion}' to '{RollbackVersion}' see details.\r\nDetails: " + ex.ToString(), UpdateResultMessage.eMessageType.ERROR);
                updateResultReturn.IsSuccess = false;
            }
            finally
            {
                this._UpdateRepository.WriteUpdateInstructionResult(updateResultReturn);
            }


            return(updateResultReturn);
        }