func()

in cmd/core_plugin/snapshot/snapshot_linux.go [201:255]


func (s *snapshotClient) handleRequest(ctx context.Context, request *sspb.SnapshotRequest) error {
	type snapshotOperation struct {
		cache          *lru.Handle[int32]
		scriptFileName string
		name           string
	}

	operationConfigs := map[sspb.OperationType]*snapshotOperation{
		sspb.OperationType_PRE_SNAPSHOT: &snapshotOperation{
			cache:          s.seenPreOperationIDS,
			scriptFileName: "pre.sh",
			name:           "pre",
		},
		sspb.OperationType_POST_SNAPSHOT: &snapshotOperation{
			cache:          s.seenPostOperationIDS,
			scriptFileName: "post.sh",
			name:           "post",
		},
	}

	// Determine if we know how to handle the operation type.
	config, found := operationConfigs[request.GetType()]
	if !found {
		return fmt.Errorf("unhandled operation type %q", request.GetType())
	}

	// Have we seen this operation ID before?
	if _, found := config.cache.Get(request.GetOperationId()); found {
		return fmt.Errorf("duplicate %s snapshot request operation id %d", config.name, request.GetOperationId())
	}

	galog.Infof("Handling snapshot request type: %q, operation id: %d.", config.name, request.GetOperationId())

	// Mark the operation ID as seen and avoid repeated execution.
	config.cache.Put(request.GetOperationId(), true)
	scriptPath := filepath.Join(s.options.scriptDir, config.scriptFileName)

	// Trigger the execution of the script.
	exitCode, errCode := s.runScript(ctx, scriptPath, request.GetDiskList())

	response := &sspb.SnapshotResponse{
		OperationId:       request.GetOperationId(),
		Type:              request.GetType(),
		ScriptsReturnCode: int32(exitCode),
		AgentReturnCode:   errCode,
	}

	// Send the response back to the snapshot service.
	if err := s.sendResponse(ctx, response); err != nil {
		return fmt.Errorf("failed to send snapshot response: %w", err)
	}

	galog.Debugf("Successfully handled snapshot request.")
	return nil
}