Skip to content

Commit

Permalink
Add possibility to change agent-id path with env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ttomasini committed Jun 26, 2023
1 parent 22a2b25 commit 0d22a51
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
24 changes: 12 additions & 12 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ import (
"github.com/woodpecker-ci/woodpecker/version"
)

const configPath = "/agent-id.conf"

func run(c *cli.Context) error {
agentIdConfigPath := c.String("agent-id-config-path")
hostname := c.String("hostname")
if len(hostname) == 0 {
hostname, _ = os.Hostname()
Expand Down Expand Up @@ -112,7 +111,7 @@ func run(c *cli.Context) error {
}
defer authConn.Close()

agentID := readAgentId()
agentID := readAgentId(agentIdConfigPath)
agentToken := c.String("grpc-token")
authClient := agentRpc.NewAuthGrpcClient(authConn, agentToken, agentID)
authInterceptor, err := agentRpc.NewAuthInterceptor(authClient, 30*time.Minute)
Expand Down Expand Up @@ -181,7 +180,7 @@ func run(c *cli.Context) error {
return err
}

writeAgentId(agentID)
writeAgentId(agentID, agentIdConfigPath)

labels := map[string]string{
"hostname": hostname,
Expand Down Expand Up @@ -286,29 +285,30 @@ func stringSliceAddToMap(sl []string, m map[string]string) error {
return nil
}

func readAgentId() int64 {
func readAgentId(agentIdConfigPath string) int64 {
const defaultAgentIdValue = int64(-1)

out, fileErr := os.ReadFile(configPath)
rawAgentId, fileErr := os.ReadFile(agentIdConfigPath)
if fileErr != nil {
return defaultAgentIdValue
}

value, err := strconv.ParseInt(string(out), 10, 64)
strAgentId := strings.TrimSpace(string(rawAgentId))
agentId, err := strconv.ParseInt(strAgentId, 10, 64)
if err != nil {
return defaultAgentIdValue
}

return value
return agentId
}

func writeAgentId(agentID int64) {
currentAgentId := readAgentId()
func writeAgentId(agentID int64, agentIdConfigPath string) {
currentAgentId := readAgentId(agentIdConfigPath)

if currentAgentId != agentID {
err := os.WriteFile(configPath, []byte(strconv.FormatInt(agentID, 10)), 0644)
err := os.WriteFile(agentIdConfigPath, []byte(strconv.FormatInt(agentID, 10)+"\n"), 0644)
if err != nil {
return
log.Warn().Err(err).Msgf("could not write agent-id config file to %s", agentIdConfigPath)
}
}
}
6 changes: 6 additions & 0 deletions cmd/agent/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ var flags = []cli.Flag{
Name: "hostname",
Usage: "agent hostname",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_AGENT_ID_CONFIG_PATH"},
Name: "agent-id-config-path",
Usage: "agent-id config file path",
Value: "/etc/agent-id.conf",
},
&cli.StringSliceFlag{
EnvVars: []string{"WOODPECKER_FILTER_LABELS"},
Name: "filter",
Expand Down

0 comments on commit 0d22a51

Please sign in to comment.