// rollbackInstallation rollback installation to the source version func rollbackInstallation(mgr *updateManager, log log.T, context *UpdateContext) (err error) { if err = mgr.uninstall(mgr, log, context.Current.TargetVersion, context); err != nil { // Fail the rollback process as a result of target version cannot be uninstalled message := updateutil.BuildMessage( err, "failed to uninstall %v %v", context.Current.PackageName, context.Current.TargetVersion) return mgr.failed(context, log, updateutil.ErrorUninstallFailed, message, false) } if err = mgr.install(mgr, log, context.Current.SourceVersion, context); err != nil { // Fail the rollback process as a result of source version cannot be reinstalled message := updateutil.BuildMessage( err, "failed to install %v %v", context.Current.PackageName, context.Current.SourceVersion) return mgr.failed(context, log, updateutil.ErrorInstallFailed, message, false) } if err = mgr.inProgress(context, log, RolledBack); err != nil { return err } return mgr.verify(mgr, log, context, true) }
// verifyInstallation checks installation result, verifies if agent is running func verifyInstallation(mgr *updateManager, log log.T, context *UpdateContext, isRollback bool) (err error) { // Check if agent is running var isRunning = false var instanceContext *updateutil.InstanceContext if instanceContext, err = mgr.util.CreateInstanceContext(log); err != nil { return mgr.failed(context, log, updateutil.ErrorEnvironmentIssue, err.Error(), false) } log.Infof("Initiating update health check") isRunning, err = mgr.util.IsServiceRunning(log, instanceContext) if err != nil || !isRunning { if !isRollback { message := updateutil.BuildMessage(err, "failed to update %v to %v, %v", context.Current.PackageName, context.Current.TargetVersion, "failed to start the agent") context.Current.AppendError(log, message) context.Current.AppendInfo( log, "Initiating rollback %v to %v", context.Current.PackageName, context.Current.SourceVersion) // Update state to rollback if err = mgr.inProgress(context, log, Rollback); err != nil { return err } return mgr.rollback(mgr, log, context) } message := updateutil.BuildMessage(err, "failed to rollback %v to %v, %v", context.Current.PackageName, context.Current.SourceVersion, "failed to start the agent") // Rolled back, but service cannot start, Update failed. return mgr.failed(context, log, updateutil.ErrorCannotStartService, message, false) } log.Infof("%v is running", context.Current.PackageName) if !isRollback { return mgr.succeeded(context, log) } message := fmt.Sprintf("rolledback %v to %v", context.Current.PackageName, context.Current.SourceVersion) log.Infof("message is %v", message) return mgr.failed(context, log, updateutil.ErrorCannotStartService, message, false) }
// proceedUpdate starts update process func proceedUpdate(mgr *updateManager, log log.T, context *UpdateContext) (err error) { log.Infof( "Attemping to upgrade from %v to %v", context.Current.SourceVersion, context.Current.TargetVersion) // Uninstall only when the target version is lower than the source version if context.Current.RequiresUninstall { if err = mgr.uninstall(mgr, log, context.Current.SourceVersion, context); err != nil { message := updateutil.BuildMessage( err, "failed to uninstall %v %v", context.Current.PackageName, context.Current.SourceVersion) return mgr.failed(context, log, updateutil.ErrorUninstallFailed, message, true) } } if err = mgr.install(mgr, log, context.Current.TargetVersion, context); err != nil { // Install target failed with err // log the error and initiating rollback to the source version message := updateutil.BuildMessage(err, "failed to install %v %v", context.Current.PackageName, context.Current.TargetVersion) context.Current.AppendError(log, message) context.Current.AppendInfo( log, "Initiating rollback %v to %v", context.Current.PackageName, context.Current.SourceVersion) // Update state to Rollback to indicate updater has initiated the rollback process if err = mgr.inProgress(context, log, Rollback); err != nil { return err } // Rollback return mgr.rollback(mgr, log, context) } // Update state to installed to indicate there is no error occur during installation // Updater has installed the new version and started the verify process if err = mgr.inProgress(context, log, Installed); err != nil { return err } // verify target version return mgr.verify(mgr, log, context, false) }
// prepareInstallationPackages downloads artifacts from public s3 storage func prepareInstallationPackages(mgr *updateManager, log log.T, context *UpdateContext) (err error) { log.Infof("Initiating download %v", context.Current.PackageName) var instanceContext *updateutil.InstanceContext updateDownload := "" if instanceContext, err = mgr.util.CreateInstanceContext(log); err != nil { return mgr.failed(context, log, updateutil.ErrorEnvironmentIssue, err.Error(), false) } if err = validateUpdateVersion(log, context.Current, instanceContext); err != nil { return mgr.failed(context, log, updateutil.ErrorEnvironmentIssue, err.Error(), true) } if updateDownload, err = mgr.util.CreateUpdateDownloadFolder(); err != nil { message := updateutil.BuildMessage( err, "failed to create download folder %v %v", context.Current.PackageName, context.Current.TargetVersion) return mgr.failed(context, log, updateutil.ErrorEnvironmentIssue, message, true) } // Download source downloadInput := artifact.DownloadInput{ SourceURL: context.Current.SourceLocation, SourceHashValue: context.Current.SourceHash, SourceHashType: updateutil.HashType, DestinationDirectory: updateDownload, } if err = mgr.download(mgr, log, downloadInput, context, context.Current.SourceVersion); err != nil { return mgr.failed(context, log, updateutil.ErrorInvalidPackage, err.Error(), true) } // Download target downloadInput = artifact.DownloadInput{ SourceURL: context.Current.TargetLocation, SourceHashValue: context.Current.TargetHash, SourceHashType: updateutil.HashType, DestinationDirectory: updateDownload, } if err = mgr.download(mgr, log, downloadInput, context, context.Current.TargetVersion); err != nil { return mgr.failed(context, log, updateutil.ErrorInvalidPackage, err.Error(), true) } // Update stdout context.Current.AppendInfo( log, "Initiating %v update to %v", context.Current.PackageName, context.Current.TargetVersion) // Update state to Staged if err = mgr.inProgress(context, log, Staged); err != nil { return err } // Process update return mgr.update(mgr, log, context) }