Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 55 additions & 30 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,7 @@ func ReadAgentWorkspaceInfo(
return false, nil, err
}

if errors.Is(err, ErrFindAgentHomeDir) {
log.Debugf(
"agent home folder not found: agentFolder=%s, context=%s, workspaceId=%s",
agentFolder,
context,
id,
)
}

if errors.Is(err, os.ErrPermission) {
log.Debugf(
"permission denied reading workspace info: agentFolder=%s, context=%s, workspaceId=%s",
agentFolder,
context,
id,
)
}
logWorkspaceInfoReadError(err, agentFolder, context, id)

// check if we need to become root
log.Debug("checking if root privileges are required")
Expand All @@ -147,6 +131,26 @@ func ReadAgentWorkspaceInfo(
return false, workspaceInfo, nil
}

func logWorkspaceInfoReadError(err error, agentFolder, context, id string) {
if errors.Is(err, ErrFindAgentHomeDir) {
log.Debugf(
"agent home folder not found: agentFolder=%s, context=%s, workspaceId=%s",
agentFolder,
context,
id,
)
}

if errors.Is(err, os.ErrPermission) {
log.Debugf(
"permission denied reading workspace info: agentFolder=%s, context=%s, workspaceId=%s",
agentFolder,
context,
id,
)
}
}

func WorkspaceInfo(
workspaceInfoEncoded string,
) (bool, *provider2.AgentWorkspaceInfo, error) {
Expand Down Expand Up @@ -203,16 +207,28 @@ func decodeWorkspaceInfoAndWrite(

resolveContentFolder(workspaceInfo, workspaceDir)

if writeInfo {
if err := writeWorkspaceInfo(workspaceConfig, workspaceInfo); err != nil {
return false, nil, fmt.Errorf("write workspace info: %w", err)
}
if err := persistWorkspaceInfo(writeInfo, workspaceConfig, workspaceInfo); err != nil {
return false, nil, err
}

workspaceInfo.Origin = workspaceDir
return false, workspaceInfo, nil
}

func persistWorkspaceInfo(
writeInfo bool,
workspaceConfig string,
workspaceInfo *provider2.AgentWorkspaceInfo,
) error {
if !writeInfo {
return nil
}
if err := writeWorkspaceInfo(workspaceConfig, workspaceInfo); err != nil {
return fmt.Errorf("write workspace info: %w", err)
}
return nil
}

// handleStaleWorkspace deletes a workspace whose persisted UID no longer
// matches the incoming one, then recreates the workspace dir. Returns the
// (possibly new) workspaceDir.
Expand Down Expand Up @@ -325,10 +341,14 @@ func writeWorkspaceInfo(file string, workspaceInfo *provider2.AgentWorkspaceInfo
return nil
}

func rootNotRequired(workspaceInfo *provider2.AgentWorkspaceInfo) bool {
return runtime.GOOS != "linux" || os.Getuid() == 0 ||
(workspaceInfo != nil && workspaceInfo.Agent.Local == config.BoolTrue)
}

func rerunAsRoot(workspaceInfo *provider2.AgentWorkspaceInfo) (bool, error) {
// check if root is required
if runtime.GOOS != "linux" || os.Getuid() == 0 ||
(workspaceInfo != nil && workspaceInfo.Agent.Local == config.BoolTrue) {
if rootNotRequired(workspaceInfo) {
return false, nil
}

Expand Down Expand Up @@ -426,6 +446,17 @@ func Tunnel(ctx context.Context, opts TunnelOptions) error {
return opts.Exec(ctx, user, command, opts.Stdin, opts.Stdout, opts.Stderr)
}

func applyDockerEnv(cmd *exec.Cmd, envs map[string]string) {
if len(envs) == 0 {
return
}
newEnvs := os.Environ()
for k, v := range envs {
newEnvs = append(newEnvs, k+"="+v)
}
cmd.Env = newEnvs
}

func dockerReachable(dockerOverride string, envs map[string]string) (bool, error) {
docker := "docker"
if dockerOverride != "" {
Expand All @@ -442,13 +473,7 @@ func dockerReachable(dockerOverride string, envs map[string]string) (bool, error
}

cmd := exec.Command(docker, "ps")
if len(envs) > 0 {
newEnvs := os.Environ()
for k, v := range envs {
newEnvs = append(newEnvs, k+"="+v)
}
cmd.Env = newEnvs
}
applyDockerEnv(cmd, envs)

_, err := cmd.CombinedOutput()
if err != nil {
Expand Down
15 changes: 10 additions & 5 deletions pkg/agent/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,17 @@ func (s *HTTPDownloadSource) streamAndCache(
return
}

if err := os.Rename(tmpPath, cachePath); err == nil {
success = true
if s.Version != "" && s.Cache != nil {
s.Cache.WriteVersion(arch, s.Version)
}
success = s.finalizeCache(arch, tmpPath, cachePath)
}

func (s *HTTPDownloadSource) finalizeCache(arch, tmpPath, cachePath string) bool {
if err := os.Rename(tmpPath, cachePath); err != nil {
return false
}
if s.Version != "" && s.Cache != nil {
s.Cache.WriteVersion(arch, s.Version)
}
return true
}

func (s *HTTPDownloadSource) createTempFile(
Expand Down
38 changes: 23 additions & 15 deletions pkg/agent/delivery/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ func NewAgentDelivery(opts FactoryOptions) AgentDelivery {
return legacyShellDelivery(opts, "custom driver")

case driverType == provider.KubernetesDriver:
if opts.PodExec == nil {
return legacyShellDelivery(opts, "kubernetes pod exec unavailable")
}
log.Debugf("using kubernetes-native delivery (exec stream)")
return &KubernetesDelivery{Exec: opts.PodExec}
return kubernetesDelivery(opts)

case driverType == provider.AppleDriver:
// Shell delivery launches the agent in one exec, which keeps the VM
Expand All @@ -47,22 +43,34 @@ func NewAgentDelivery(opts FactoryOptions) AgentDelivery {
return remoteDockerDelivery(opts)

case driverType == "" || driverType == provider.DockerDriver:
if isDockerLocal(opts.DockerCommand) {
log.Debugf("using local docker delivery (named volume)")
return &LocalDockerDelivery{
DockerCommand: opts.DockerCommand,
Environment: opts.DockerEnv,
HelperImage: opts.HelperImage,
}
}
log.Debugf("using remote docker delivery for non-local docker daemon")
return remoteDockerDelivery(opts)
return dockerDelivery(opts)

default:
return legacyShellDelivery(opts, fmt.Sprintf("driver: %s", driverType))
}
}

func kubernetesDelivery(opts FactoryOptions) AgentDelivery {
if opts.PodExec == nil {
return legacyShellDelivery(opts, "kubernetes pod exec unavailable")
}
log.Debugf("using kubernetes-native delivery (exec stream)")
return &KubernetesDelivery{Exec: opts.PodExec}
}

func dockerDelivery(opts FactoryOptions) AgentDelivery {
if isDockerLocal(opts.DockerCommand) {
log.Debugf("using local docker delivery (named volume)")
return &LocalDockerDelivery{
DockerCommand: opts.DockerCommand,
Environment: opts.DockerEnv,
HelperImage: opts.HelperImage,
}
}
log.Debugf("using remote docker delivery for non-local docker daemon")
return remoteDockerDelivery(opts)
}

func remoteDockerDelivery(opts FactoryOptions) AgentDelivery {
return &RemoteDockerDelivery{
DockerCommand: opts.DockerCommand,
Expand Down
Loading
Loading