/
api.go
50 lines (41 loc) · 1.6 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package job
import (
"fmt"
"github.com/buildkite/agent/v3/internal/socket"
"github.com/buildkite/agent/v3/jobapi"
)
// startJobAPI starts the job API server, iff the OS of the box supports it otherwise it returns a
// noop cleanup function. It also sets the BUILDKITE_AGENT_JOB_API_SOCKET and
// BUILDKITE_AGENT_JOB_API_TOKEN environment variables
func (e *Executor) startJobAPI() (cleanup func(), err error) {
cleanup = func() {}
if !socket.Available() {
e.shell.OptionalWarningf("job-api-unavailable", `The Job API isn't available on this machine, as it's running an unsupported version of Windows.
The Job API is available on Unix agents, and agents running Windows versions after build 17063
We'll continue to run your job, but you won't be able to use the Job API`)
return cleanup, nil
}
socketPath, err := jobapi.NewSocketPath(e.ExecutorConfig.SocketsPath)
if err != nil {
return cleanup, fmt.Errorf("creating job API socket path: %v", err)
}
jobAPIOpts := []jobapi.ServerOpts{}
if e.ExecutorConfig.Debug {
jobAPIOpts = append(jobAPIOpts, jobapi.WithDebug())
}
srv, token, err := jobapi.NewServer(e.shell.Logger, socketPath, e.shell.Env, e.redactors, jobAPIOpts...)
if err != nil {
return cleanup, fmt.Errorf("creating job API server: %v", err)
}
e.shell.Env.Set("BUILDKITE_AGENT_JOB_API_SOCKET", socketPath)
e.shell.Env.Set("BUILDKITE_AGENT_JOB_API_TOKEN", token)
if err := srv.Start(); err != nil {
return cleanup, fmt.Errorf("starting Job API server: %v", err)
}
return func() {
err = srv.Stop()
if err != nil {
e.shell.Errorf("Error stopping Job API server: %v", err)
}
}, nil
}