Skip to content

feat: add agent exec pkg #15577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 25, 2024
Prev Previous commit
Next Next commit
pr changes
  • Loading branch information
sreya committed Nov 21, 2024
commit 5076cf08d3c342c74c44ace2bfe89db78469d467
20 changes: 8 additions & 12 deletions agent/agentexec/cli_unix.go → agent/agentexec/cli_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func CLI() error {
// We lock the OS thread here to avoid a race conditino where the nice priority
// we get is on a different thread from the one we set it on.
runtime.LockOSThread()
// Nop on success but we do it anyway in case of an error.
defer runtime.UnlockOSThread()

var (
fs = flag.NewFlagSet("agent-exec", flag.ExitOnError)
Expand All @@ -42,18 +44,12 @@ func CLI() error {
return xerrors.Errorf("parse flags: %w", err)
}

if runtime.GOOS != "linux" {
return xerrors.Errorf("agent-exec is only supported on Linux")
}

// Get everything after "coder agent-exec --"
args := execArgs(os.Args)
if len(args) == 0 {
return xerrors.Errorf("no exec command provided %+v", os.Args)
}

pid := os.Getpid()

if *nice == unset {
// If an explicit nice score isn't set, we use the default.
*nice, err = defaultNiceScore()
Expand All @@ -75,7 +71,7 @@ func CLI() error {
return xerrors.Errorf("set nice score: %w", err)
}

err = writeOOMScoreAdj(pid, *oom)
err = writeOOMScoreAdj(*oom)
if err != nil {
return xerrors.Errorf("set oom score: %w", err)
}
Expand Down Expand Up @@ -104,7 +100,7 @@ func defaultNiceScore() (int, error) {
}

func defaultOOMScore() (int, error) {
score, err := oomScoreAdj(os.Getpid())
score, err := oomScoreAdj()
if err != nil {
return 0, xerrors.Errorf("get oom score: %w", err)
}
Expand All @@ -126,16 +122,16 @@ func defaultOOMScore() (int, error) {
return 998, nil
}

func oomScoreAdj(pid int) (int, error) {
scoreStr, err := os.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid))
func oomScoreAdj() (int, error) {
scoreStr, err := os.ReadFile("/proc/self/oom_score_adj")
if err != nil {
return 0, xerrors.Errorf("read oom_score_adj: %w", err)
}
return strconv.Atoi(strings.TrimSpace(string(scoreStr)))
}

func writeOOMScoreAdj(pid int, score int) error {
return os.WriteFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid), []byte(fmt.Sprintf("%d", score)), 0o600)
func writeOOMScoreAdj(score int) error {
return os.WriteFile("/proc/self/oom_score_adj", []byte(fmt.Sprintf("%d", score)), 0o600)
}

// execArgs returns the arguments to pass to syscall.Exec after the "--" delimiter.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion agent/agentexec/cli_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package agentexec
import "golang.org/x/xerrors"

func CLI() error {
return xerrors.Errorf("agent-exec is only supported on Linux")
return xerrors.New("agent-exec is only supported on Linux")
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build linux
// +build linux

package main

import (
Expand Down
43 changes: 43 additions & 0 deletions agent/agentexec/main_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package agentexec_test

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
)

var TestBin string

func TestMain(m *testing.M) {
code := func() int {
// We generate a unique directory per test invocation to avoid collisions between two
// processes attempting to create the same temp file.
dir := genDir()
defer os.RemoveAll(dir)
TestBin = buildBinary(dir)
return m.Run()
}()

os.Exit(code)
}

func buildBinary(dir string) string {
path := filepath.Join(dir, "agent-test")
out, err := exec.Command("go", "build", "-o", path, "./cmdtest").CombinedOutput()
mustf(err, "build binary: %s", out)
return path
}

func mustf(err error, msg string, args ...any) {
if err != nil {
panic(fmt.Sprintf(msg, args...))
}
}

func genDir() string {
dir, err := os.MkdirTemp(os.TempDir(), "agentexec")
mustf(err, "create temp dir: %v", err)
return dir
}
27 changes: 0 additions & 27 deletions agent/agentexec/main_test.go

This file was deleted.

Loading