func()

in agentendpoint/patch_task.go [167:226]


func (r *patchTask) rebootIfNeeded(ctx context.Context, prePatch bool) error {
	var reboot bool
	var err error
	if r.Task.GetPatchConfig().GetRebootConfig() == agentendpointpb.PatchConfig_ALWAYS && !prePatch && r.PostPatchRebootCount == 0 {
		reboot = true
		clog.Infof(ctx, "PatchConfig RebootConfig set to %s.", agentendpointpb.PatchConfig_ALWAYS)
	} else {
		reboot, err = systemRebootRequired(ctx)
		if err != nil {
			return fmt.Errorf("error checking if a system reboot is required: %v", err)
		}
		if reboot {
			clog.Infof(ctx, "System indicates a reboot is required.")
			totalRebootCount := r.PrePatchRebootCount + r.PostPatchRebootCount
			if totalRebootCount >= totalRebootCountLimit {
				clog.Infof(ctx, "Detected abnormal number of reboots for a single patch task (%d). Not rebooting to prevent a possible boot loop", totalRebootCount)
				return nil
			}
		} else {
			clog.Infof(ctx, "System indicates a reboot is not required.")
		}
	}

	if !reboot {
		return nil
	}

	if r.Task.GetPatchConfig().GetRebootConfig() == agentendpointpb.PatchConfig_NEVER {
		clog.Infof(ctx, "Skipping reboot because of PatchConfig RebootConfig set to %s.", agentendpointpb.PatchConfig_NEVER)
		return nil
	}

	if err := r.reportContinuingState(ctx, agentendpointpb.ApplyPatchesTaskProgress_REBOOTING); err != nil {
		return err
	}

	if r.Task.GetDryRun() {
		clog.Infof(ctx, "Dry run - not rebooting for ApplyPatchesTask")
		return nil
	}

	if prePatch {
		r.PrePatchRebootCount++
	} else {
		r.PostPatchRebootCount++
	}

	if err := r.saveState(); err != nil {
		return fmt.Errorf("error saving state: %v", err)
	}
	if err := rebootSystem(); err != nil {
		return fmt.Errorf("failed to reboot system: %v", err)
	}

	// Reboot can take a bit, pause here so other activities don't start.
	for {
		clog.Debugf(ctx, "Waiting for system reboot.")
		time.Sleep(1 * time.Minute)
	}
}