Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf4d6afb

Browse files
authored
feat(agent): Allow specifying log directory via flag or env (#5915)
1 parentfa5b612 commitf4d6afb

File tree

5 files changed

+114
-4
lines changed

5 files changed

+114
-4
lines changed

‎agent/agent.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const (
6060

6161
typeOptionsstruct {
6262
Filesystem afero.Fs
63+
LogDirstring
6364
TempDirstring
6465
ExchangeTokenfunc(ctx context.Context) (string,error)
6566
ClientClient
@@ -87,6 +88,12 @@ func New(options Options) io.Closer {
8788
ifoptions.TempDir=="" {
8889
options.TempDir=os.TempDir()
8990
}
91+
ifoptions.LogDir=="" {
92+
ifoptions.TempDir!=os.TempDir() {
93+
options.Logger.Debug(context.Background(),"log dir not set, using temp dir",slog.F("temp_dir",options.TempDir))
94+
}
95+
options.LogDir=options.TempDir
96+
}
9097
ifoptions.ExchangeToken==nil {
9198
options.ExchangeToken=func(ctx context.Context) (string,error) {
9299
return"",nil
@@ -102,6 +109,7 @@ func New(options Options) io.Closer {
102109
client:options.Client,
103110
exchangeToken:options.ExchangeToken,
104111
filesystem:options.Filesystem,
112+
logDir:options.LogDir,
105113
tempDir:options.TempDir,
106114
lifecycleUpdate:make(chanstruct{},1),
107115
}
@@ -114,6 +122,7 @@ type agent struct {
114122
clientClient
115123
exchangeTokenfunc(ctx context.Context) (string,error)
116124
filesystem afero.Fs
125+
logDirstring
117126
tempDirstring
118127

119128
reconnectingPTYs sync.Map
@@ -582,7 +591,7 @@ func (a *agent) runStartupScript(ctx context.Context, script string) error {
582591
}
583592

584593
a.logger.Info(ctx,"running startup script",slog.F("script",script))
585-
writer,err:=a.filesystem.OpenFile(filepath.Join(a.tempDir,"coder-startup-script.log"),os.O_CREATE|os.O_RDWR,0o600)
594+
writer,err:=a.filesystem.OpenFile(filepath.Join(a.logDir,"coder-startup-script.log"),os.O_CREATE|os.O_RDWR,0o600)
586595
iferr!=nil {
587596
returnxerrors.Errorf("open startup script log file: %w",err)
588597
}

‎cli/agent.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
funcworkspaceAgent()*cobra.Command {
3030
var (
3131
authstring
32+
logDirstring
3233
pprofAddressstring
3334
noReapbool
3435
)
@@ -55,7 +56,7 @@ func workspaceAgent() *cobra.Command {
5556
// of zombie processes.
5657
ifreaper.IsInitProcess()&&!noReap&&isLinux {
5758
logWriter:=&lumberjack.Logger{
58-
Filename:filepath.Join(os.TempDir(),"coder-agent-init.log"),
59+
Filename:filepath.Join(logDir,"coder-agent-init.log"),
5960
MaxSize:5,// MB
6061
}
6162
deferlogWriter.Close()
@@ -91,7 +92,7 @@ func workspaceAgent() *cobra.Command {
9192
godumpHandler(ctx)
9293

9394
logWriter:=&lumberjack.Logger{
94-
Filename:filepath.Join(os.TempDir(),"coder-agent.log"),
95+
Filename:filepath.Join(logDir,"coder-agent.log"),
9596
MaxSize:5,// MB
9697
}
9798
deferlogWriter.Close()
@@ -178,6 +179,7 @@ func workspaceAgent() *cobra.Command {
178179
closer:=agent.New(agent.Options{
179180
Client:client,
180181
Logger:logger,
182+
LogDir:logDir,
181183
ExchangeToken:func(ctx context.Context) (string,error) {
182184
ifexchangeToken==nil {
183185
returnclient.SDK.SessionToken(),nil
@@ -199,8 +201,9 @@ func workspaceAgent() *cobra.Command {
199201
}
200202

201203
cliflag.StringVarP(cmd.Flags(),&auth,"auth","","CODER_AGENT_AUTH","token","Specify the authentication type to use for the agent")
202-
cliflag.BoolVarP(cmd.Flags(),&noReap,"no-reap","","",false,"Do not start a process reaper.")
204+
cliflag.StringVarP(cmd.Flags(),&logDir,"log-dir","","CODER_AGENT_LOG_DIR",os.TempDir(),"Specify the location for the agent log files")
203205
cliflag.StringVarP(cmd.Flags(),&pprofAddress,"pprof-address","","CODER_AGENT_PPROF_ADDRESS","127.0.0.1:6060","The address to serve pprof.")
206+
cliflag.BoolVarP(cmd.Flags(),&noReap,"no-reap","","",false,"Do not start a process reaper.")
204207
returncmd
205208
}
206209

‎cli/agent_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cli_test
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
57
"runtime"
68
"strings"
79
"testing"
@@ -14,10 +16,70 @@ import (
1416
"github.com/coder/coder/coderd/coderdtest"
1517
"github.com/coder/coder/provisioner/echo"
1618
"github.com/coder/coder/provisionersdk/proto"
19+
"github.com/coder/coder/testutil"
1720
)
1821

1922
funcTestWorkspaceAgent(t*testing.T) {
2023
t.Parallel()
24+
25+
t.Run("LogDirectory",func(t*testing.T) {
26+
t.Parallel()
27+
28+
authToken:=uuid.NewString()
29+
client:=coderdtest.New(t,&coderdtest.Options{
30+
IncludeProvisionerDaemon:true,
31+
})
32+
user:=coderdtest.CreateFirstUser(t,client)
33+
version:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,&echo.Responses{
34+
Parse:echo.ParseComplete,
35+
ProvisionApply: []*proto.Provision_Response{{
36+
Type:&proto.Provision_Response_Complete{
37+
Complete:&proto.Provision_Complete{
38+
Resources: []*proto.Resource{{
39+
Name:"somename",
40+
Type:"someinstance",
41+
Agents: []*proto.Agent{{
42+
Id:uuid.NewString(),
43+
Name:"someagent",
44+
Auth:&proto.Agent_Token{
45+
Token:authToken,
46+
},
47+
}},
48+
}},
49+
},
50+
},
51+
}},
52+
})
53+
template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version.ID)
54+
coderdtest.AwaitTemplateVersionJob(t,client,version.ID)
55+
workspace:=coderdtest.CreateWorkspace(t,client,user.OrganizationID,template.ID)
56+
coderdtest.AwaitWorkspaceBuildJob(t,client,workspace.LatestBuild.ID)
57+
58+
logDir:=t.TempDir()
59+
cmd,_:=clitest.New(t,
60+
"agent",
61+
"--auth","token",
62+
"--agent-token",authToken,
63+
"--agent-url",client.URL.String(),
64+
"--log-dir",logDir,
65+
)
66+
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitMedium)
67+
defercancel()
68+
errC:=make(chanerror,1)
69+
gofunc() {
70+
errC<-cmd.ExecuteContext(ctx)
71+
}()
72+
coderdtest.AwaitWorkspaceAgents(t,client,workspace.ID)
73+
74+
cancel()
75+
err:=<-errC
76+
require.NoError(t,err)
77+
78+
info,err:=os.Stat(filepath.Join(logDir,"coder-agent.log"))
79+
require.NoError(t,err)
80+
require.Greater(t,info.Size(),int64(0))
81+
})
82+
2183
t.Run("Azure",func(t*testing.T) {
2284
t.Parallel()
2385
instanceID:="instanceidentifier"

‎cli/root_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ func TestCommandHelp(t *testing.T) {
5353
"CODER_CACHE_DIRECTORY":"/tmp/coder-cli-test-cache",
5454
},
5555
},
56+
{
57+
name:"coder agent --help",
58+
cmd: []string{"agent","--help"},
59+
env:map[string]string{
60+
"CODER_AGENT_LOG_DIR":"/tmp",
61+
},
62+
},
5663
}
5764

5865
root:=cli.Root(cli.AGPL())
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Usage:
2+
coder agent [flags]
3+
4+
Flags:
5+
--auth string Specify the authentication type to use for the agent.
6+
Consumes $CODER_AGENT_AUTH (default "token")
7+
-h, --help help for agent
8+
--log-dir string Specify the location for the agent log files.
9+
Consumes $CODER_AGENT_LOG_DIR (default "/tmp")
10+
--no-reap Do not start a process reaper.
11+
--pprof-address string The address to serve pprof.
12+
Consumes $CODER_AGENT_PPROF_ADDRESS (default "127.0.0.1:6060")
13+
14+
Global Flags:
15+
--global-config coder Path to the global coder config directory.
16+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
17+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
18+
Consumes $CODER_HEADER
19+
--no-feature-warning Suppress warnings about unlicensed features.
20+
Consumes $CODER_NO_FEATURE_WARNING
21+
--no-version-warning Suppress warning when client and server versions do not match.
22+
Consumes $CODER_NO_VERSION_WARNING
23+
--token string Specify an authentication token. For security reasons setting
24+
CODER_SESSION_TOKEN is preferred.
25+
Consumes $CODER_SESSION_TOKEN
26+
--url string URL to a deployment.
27+
Consumes $CODER_URL
28+
-v, --verbose Enable verbose output.
29+
Consumes $CODER_VERBOSE

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp