func InstallOpsAgent()

in integration_test/agents/agents.go [779:838]


func InstallOpsAgent(ctx context.Context, logger *log.Logger, vm *gce.VM, location PackageLocation) error {
	if location.packagesInGCS != "" && location.repoSuffix != "" {
		return fmt.Errorf("invalid PackageLocation: cannot provide both location.packagesInGCS and location.repoSuffix. location=%#v", location)
	}

	if location.artifactRegistryRegion != "" && location.repoSuffix == "" {
		return fmt.Errorf("invalid PackageLocation: location.artifactRegistryRegion was nonempty yet location.repoSuffix was empty. location=%#v", location)
	}

	if gce.IsOpsAgentUAPPlugin() {
		return InstallOpsAgentUAPPlugin(ctx, logger, vm, location)
	}

	if location.packagesInGCS != "" {
		return InstallPackageFromGCS(ctx, logger, vm, location.packagesInGCS)
	}

	preservedEnvironment := map[string]string{
		"REPO_SUFFIX":               location.repoSuffix,
		"REPO_CODENAME":             location.repoCodename,
		"ARTIFACT_REGISTRY_PROJECT": location.artifactRegistryProject,
		"ARTIFACT_REGISTRY_REGION":  location.artifactRegistryRegion,
	}

	if gce.IsWindows(vm.ImageSpec) {
		// Note that these commands match the ones from our public install docs
		// (https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/installation)
		// and keeping them in sync is encouraged so that we are testing the
		// same commands that our customers are running.
		if _, err := gce.RunRemotely(ctx, logger, vm, `(New-Object Net.WebClient).DownloadFile("https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.ps1", "${env:UserProfile}\add-google-cloud-ops-agent-repo.ps1")`); err != nil {
			return fmt.Errorf("InstallOpsAgent() failed to download repo script: %w", err)
		}
		runScript := func() error {
			scriptCmd := fmt.Sprintf(`%s
& "${env:UserProfile}\add-google-cloud-ops-agent-repo.ps1" -AlsoInstall`, windowsEnvironment(preservedEnvironment))
			_, err := gce.RunRemotely(ctx, logger, vm, scriptCmd)
			return err
		}
		// TODO: b/202526819 - Remove retries once the script does retries internally.
		if err := RunInstallFuncWithRetry(ctx, logger, vm, runScript); err != nil {
			return fmt.Errorf("InstallOpsAgent() failed to run repo script: %w", err)
		}
		return nil
	}

	if _, err := gce.RunRemotely(ctx,
		logger, vm, "curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh"); err != nil {
		return fmt.Errorf("InstallOpsAgent() failed to download repo script: %w", err)
	}

	runInstallScript := func() error {
		envVars := linuxEnvironment(preservedEnvironment)
		_, err := gce.RunRemotely(ctx, logger, vm, "sudo "+envVars+" bash -x add-google-cloud-ops-agent-repo.sh --also-install")
		return err
	}
	if err := RunInstallFuncWithRetry(ctx, logger, vm, runInstallScript); err != nil {
		return fmt.Errorf("InstallOpsAgent() error running repo script: %w", err)
	}
	return nil
}