feat(agent): Allow specifying log directory via flag or env (#5915)

This commit is contained in:
Mathias Fredriksson
2023-01-30 18:39:52 +02:00
committed by GitHub
parent fa5b6125a9
commit f4d6afb01d
5 changed files with 114 additions and 4 deletions
+10 -1
View File
@@ -60,6 +60,7 @@ const (
type Options struct {
Filesystem afero.Fs
LogDir string
TempDir string
ExchangeToken func(ctx context.Context) (string, error)
Client Client
@@ -87,6 +88,12 @@ func New(options Options) io.Closer {
if options.TempDir == "" {
options.TempDir = os.TempDir()
}
if options.LogDir == "" {
if options.TempDir != os.TempDir() {
options.Logger.Debug(context.Background(), "log dir not set, using temp dir", slog.F("temp_dir", options.TempDir))
}
options.LogDir = options.TempDir
}
if options.ExchangeToken == nil {
options.ExchangeToken = func(ctx context.Context) (string, error) {
return "", nil
@@ -102,6 +109,7 @@ func New(options Options) io.Closer {
client: options.Client,
exchangeToken: options.ExchangeToken,
filesystem: options.Filesystem,
logDir: options.LogDir,
tempDir: options.TempDir,
lifecycleUpdate: make(chan struct{}, 1),
}
@@ -114,6 +122,7 @@ type agent struct {
client Client
exchangeToken func(ctx context.Context) (string, error)
filesystem afero.Fs
logDir string
tempDir string
reconnectingPTYs sync.Map
@@ -582,7 +591,7 @@ func (a *agent) runStartupScript(ctx context.Context, script string) error {
}
a.logger.Info(ctx, "running startup script", slog.F("script", script))
writer, err := a.filesystem.OpenFile(filepath.Join(a.tempDir, "coder-startup-script.log"), os.O_CREATE|os.O_RDWR, 0o600)
writer, err := a.filesystem.OpenFile(filepath.Join(a.logDir, "coder-startup-script.log"), os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return xerrors.Errorf("open startup script log file: %w", err)
}