util

package
v0.0.0-...-2fc0378 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package util provides utility methods for the agent.

Index

Constants

View Source
const (
	MarkerTaskID      = "EVR_TASK_ID"
	MarkerAgentPID    = "EVR_AGENT_PID"
	MarkerInEvergreen = "IN_EVERGREEN"
)
View Source
const (

	// AgentNice is a nice value that the agent runs at by default. This makes
	// the process more important than those running at the default but is not
	// as critical as other basic system operations.
	AgentNice = -10
	// DefaultNice is the default nice value.
	DefaultNice = 0
)

Variables

View Source
var (
	ErrPSTimeout = errors.New("ps timeout")
	// ErrContainerExecUnavailable indicates the container could not be reached
	// to clean up its processes. Callers should warn rather than treat this as a
	// hard kill failure, since failing here would mask the task's own failure.
	ErrContainerExecUnavailable = errors.New("container exec unavailable")
)

Functions

func AddVariantAndParameterExpansions

func AddVariantAndParameterExpansions(expansionsAndVars *apimodels.ExpansionsAndVars, project *model.Project, variant string)

AddVariantAndParameterExpansions applies build variant and parameter level expansions to the expansions and vars map.

func GetEC2Metadata

func GetEC2Metadata(ctx context.Context) (host.HostMetadataOptions, error)

GetEC2Metadata fetches necessary EC2 metadata needed for the /hosts/{host_id}/is_up endpoint. Volume IDs are not available from the instance metadata endpoint, so they are not included.

func GetNice

func GetNice(pid int) (int, error)

GetNice returns the nice value for the process given by PID. Passing 0 refers to the current process.

func KillSpawnedProcs

func KillSpawnedProcs(ctx context.Context, key, workingDir, execUser string, logger grip.Journaler) error

KillSpawnedProcs kills processes that descend from the agent and waits for them to terminate.

func KillSpawnedProcsInContainer

func KillSpawnedProcsInContainer(ctx context.Context, containerID, execUser string) error

KillSpawnedProcsInContainer is the container-mode equivalent of killUserProcesses: it kills the processes owned by execUser inside a running isolation container, leaving the container's PID 1 intact. It returns an error matching ErrContainerExecUnavailable if the container could not be reached.

func ParallelWorkerExec

func ParallelWorkerExec[T any](ctx context.Context, name string, work []T, logger grip.Journaler, handler func(item *T) error) (int64, error)

ParallelWorkerExec processes work items in parallel using a worker pool sized to runtime.GOMAXPROCS(0). Each worker is protected by panic recovery. Individual handler errors are logged and skipped. Context cancellation is propagated as an error. Returns the number of successfully processed items.

func S3DefaultURL

func S3DefaultURL(bucket, key string) string

S3DefaultURL returns the S3 URL for the given bucket containing the object key. The style of the S3 URL is determined based on the bucket name.

func S3PathURL

func S3PathURL(bucket, key string) string

S3PathURL returns the path-style S3 URL for the given bucket containing the object key.

func S3VirtualHostedURL

func S3VirtualHostedURL(bucket, key string) string

S3VirtualHostedURL returns the virtual hosted-style S3 URL for the given bucket containing the object key.

func SetNice

func SetNice(pid, nice int) error

SetNice sets the nice for the process given by PID. This determines its relative scheduling priority for host CPU. This is only available if the current process has sufficient permissions to set the nice.

func SetOOMScoreAdj

func SetOOMScoreAdj(adj int) error

SetOOMScoreAdj writes adj to /proc/self/oom_score_adj, which is added to the kernel's OOM badness score when selecting a kill target.

func TrackProcess

func TrackProcess(ctx context.Context, key string, pid int, logger grip.Journaler)

TrackProcess is a noop by default if we don't need to do any special bookkeeping up-front.

func WrapWithContainer

func WrapWithContainer(ctx context.Context, opts *options.Create, containerID, workdir, envFileHostDir string) error

WrapWithContainer rewrites opts.Args to run the command inside the given Docker container via `docker exec`, applying workdir, environment, exec user, and nice settings. It is a no-op if containerID is empty.

If envFileHostDir is non-empty, opts.Environment is written there as an env-file readable only by the agent's user. Docker's --env-file parser does not strip surrounding quotes from values, so expansions must be set without literal surrounding quotes or they reach the container with the quotes intact.

Types

type DynamicExpansions

type DynamicExpansions struct {
	util.Expansions
	// contains filtered or unexported fields
}

DynamicExpansions wraps expansions for safe concurrent access as they are dynamically updated. It also contains logic to redact values that should not be exposed.

func NewDynamicExpansions

func NewDynamicExpansions(e util.Expansions) *DynamicExpansions

func (*DynamicExpansions) Get

func (e *DynamicExpansions) Get(key string) string

func (*DynamicExpansions) GetRedacted

func (e *DynamicExpansions) GetRedacted() []RedactInfo

GetRedacted gets the expansions that should be redacted.

func (*DynamicExpansions) Put

func (e *DynamicExpansions) Put(key, value string)

func (*DynamicExpansions) PutAndRedact

func (e *DynamicExpansions) PutAndRedact(key, value string)

PutAndRedact puts the expansions followed by marking it for redaction.

func (*DynamicExpansions) Range

func (e *DynamicExpansions) Range(op func(key, value string) bool)

Range iterates over all the expansions and calls the given function op for each expansion key and value. op returns a boolean indicating whether to continue iteration or not for each key-value pair. This is safe for concurrent access and has no guaranteed iteration order.

func (*DynamicExpansions) Redact

func (e *DynamicExpansions) Redact(key, value string)

Redact adds a key and value pair to be redacted. This should only be used for generated keys not exposed to the task.

func (*DynamicExpansions) RedactKey

func (e *DynamicExpansions) RedactKey(key string)

Redact marks the expansion with given key for redaction.

func (*DynamicExpansions) Remove

func (e *DynamicExpansions) Remove(expansion string)

Remove deletes a value from the expansions.

func (*DynamicExpansions) Update

func (e *DynamicExpansions) Update(newExpansions map[string]string)

func (*DynamicExpansions) UpdateFromYaml

func (e *DynamicExpansions) UpdateFromYaml(filename string) ([]string, error)

func (*DynamicExpansions) UpdateFromYamlAndRedact

func (e *DynamicExpansions) UpdateFromYamlAndRedact(filename string) ([]string, error)

UpdateFromYamlAndRedact updates the expansions from the given yaml file and then marks the expansions for redaction.

type RedactInfo

type RedactInfo struct {
	Key   string
	Value string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址