func()

in cmd/ops_agent_uap_plugin/service_linux.go [73:139]


func (ps *OpsAgentPluginServer) Start(ctx context.Context, msg *pb.StartRequest) (*pb.StartResponse, error) {
	ps.mu.Lock()
	if ps.cancel != nil {
		log.Printf("The Ops Agent plugin is started already, skipping the current request")
		ps.mu.Unlock()
		return &pb.StartResponse{}, nil
	}
	log.Printf("Received a Start request: %s. Starting the Ops Agent", msg)

	pContext, cancel := context.WithCancel(context.Background())
	ps.cancel = cancel
	ps.mu.Unlock()

	pluginInstallPath, err := os.Executable()
	if err != nil {
		ps.Stop(ctx, &pb.StopRequest{Cleanup: false})
		log.Printf("Start() failed, because it cannot determine the plugin install location: %s", err)
		return nil, status.Error(13, err.Error()) // Internal
	}
	pluginInstallPath, err = filepath.EvalSymlinks(pluginInstallPath)
	if err != nil {
		ps.Stop(ctx, &pb.StopRequest{Cleanup: false})
		log.Printf("Start() failed, because it cannot determine the plugin install location: %s", err)
		return nil, status.Error(13, err.Error()) // Internal
	}
	pluginInstallDir := filepath.Dir(pluginInstallPath)

	pluginStateDir := msg.GetConfig().GetStateDirectoryPath()
	if pluginStateDir == "" {
		pluginStateDir = DefaultPluginStateDirectory
	}

	// Find existing ops agent installation, and conflicting legacy agent installation.
	foundConflictingInstallations, err := findPreExistentAgents(pContext, ps.runCommand, AgentSystemdServiceNames)
	if foundConflictingInstallations || err != nil {
		ps.Stop(ctx, &pb.StopRequest{Cleanup: false})
		log.Printf("Start() failed: %s", err)
		return nil, status.Error(9, err.Error()) // FailedPrecondition
	}

	// Receive config from the Start request and write it to the Ops Agent config file.
	if err := writeCustomConfigToFile(msg, OpsAgentConfigLocationLinux); err != nil {
		log.Printf("Start() failed: %s", err)
		ps.Stop(ctx, &pb.StopRequest{Cleanup: false})
		return nil, status.Errorf(13, "failed to write the custom Ops Agent config to file: %s", err) // Internal
	}

	// Ops Agent config validation
	if err := validateOpsAgentConfig(pContext, pluginInstallDir, pluginStateDir, ps.runCommand); err != nil {
		log.Printf("Start() failed: %s", err)
		ps.Stop(ctx, &pb.StopRequest{Cleanup: false})
		return nil, status.Errorf(9, "failed to validate Ops Agent config: %s", err) // FailedPrecondition
	}
	// Subagent config generation
	if err := generateSubagentConfigs(pContext, ps.runCommand, pluginInstallDir, pluginStateDir); err != nil {
		log.Printf("Start() failed: %s", err)
		ps.Stop(ctx, &pb.StopRequest{Cleanup: false})
		return nil, status.Errorf(9, "failed to generate subagent configs: %s", err) // FailedPrecondition
	}

	// the subagent startups
	cancelFunc := func() {
		ps.Stop(ctx, &pb.StopRequest{Cleanup: false})
	}
	go runSubagents(pContext, cancelFunc, pluginInstallDir, pluginStateDir, runSubAgentCommand, ps.runCommand)
	return &pb.StartResponse{}, nil
}