bazel_proxy.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2023 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bazel
  15. import (
  16. "bytes"
  17. "encoding/gob"
  18. "fmt"
  19. "net"
  20. os_lib "os"
  21. "os/exec"
  22. "path/filepath"
  23. "strings"
  24. "time"
  25. )
  26. // Logs events of ProxyServer.
  27. type ServerLogger interface {
  28. Fatal(v ...interface{})
  29. Fatalf(format string, v ...interface{})
  30. Println(v ...interface{})
  31. }
  32. // CmdRequest is a request to the Bazel Proxy server.
  33. type CmdRequest struct {
  34. // Args to the Bazel command.
  35. Argv []string
  36. // Environment variables to pass to the Bazel invocation. Strings should be of
  37. // the form "KEY=VALUE".
  38. Env []string
  39. }
  40. // CmdResponse is a response from the Bazel Proxy server.
  41. type CmdResponse struct {
  42. Stdout string
  43. Stderr string
  44. ErrorString string
  45. }
  46. // ProxyClient is a client which can issue Bazel commands to the Bazel
  47. // proxy server. Requests are issued (and responses received) via a unix socket.
  48. // See ProxyServer for more details.
  49. type ProxyClient struct {
  50. outDir string
  51. }
  52. // ProxyServer is a server which runs as a background goroutine. Each
  53. // request to the server describes a Bazel command which the server should run.
  54. // The server then issues the Bazel command, and returns a response describing
  55. // the stdout/stderr of the command.
  56. // Client-server communication is done via a unix socket under the output
  57. // directory.
  58. // The server is intended to circumvent sandboxing for subprocesses of the
  59. // build. The build orchestrator (soong_ui) can launch a server to exist outside
  60. // of sandboxing, and sandboxed processes (such as soong_build) can issue
  61. // bazel commands through this socket tunnel. This allows a sandboxed process
  62. // to issue bazel requests to a bazel that resides outside of sandbox. This
  63. // is particularly useful to maintain a persistent Bazel server which lives
  64. // past the duration of a single build.
  65. // The ProxyServer will only live as long as soong_ui does; the
  66. // underlying Bazel server will live past the duration of the build.
  67. type ProxyServer struct {
  68. logger ServerLogger
  69. outDir string
  70. workspaceDir string
  71. bazeliskVersion string
  72. // The server goroutine will listen on this channel and stop handling requests
  73. // once it is written to.
  74. done chan struct{}
  75. }
  76. // NewProxyClient is a constructor for a ProxyClient.
  77. func NewProxyClient(outDir string) *ProxyClient {
  78. return &ProxyClient{
  79. outDir: outDir,
  80. }
  81. }
  82. func unixSocketPath(outDir string) string {
  83. return filepath.Join(outDir, "bazelsocket.sock")
  84. }
  85. // IssueCommand issues a request to the Bazel Proxy Server to issue a Bazel
  86. // request. Returns a response describing the output from the Bazel process
  87. // (if the Bazel process had an error, then the response will include an error).
  88. // Returns an error if there was an issue with the connection to the Bazel Proxy
  89. // server.
  90. func (b *ProxyClient) IssueCommand(req CmdRequest) (CmdResponse, error) {
  91. var resp CmdResponse
  92. var err error
  93. // Check for connections every 1 second. This is chosen to be a relatively
  94. // short timeout, because the proxy server should accept requests quite
  95. // quickly.
  96. d := net.Dialer{Timeout: 1 * time.Second}
  97. var conn net.Conn
  98. conn, err = d.Dial("unix", unixSocketPath(b.outDir))
  99. if err != nil {
  100. return resp, err
  101. }
  102. defer conn.Close()
  103. enc := gob.NewEncoder(conn)
  104. if err = enc.Encode(req); err != nil {
  105. return resp, err
  106. }
  107. dec := gob.NewDecoder(conn)
  108. err = dec.Decode(&resp)
  109. return resp, err
  110. }
  111. // NewProxyServer is a constructor for a ProxyServer.
  112. func NewProxyServer(logger ServerLogger, outDir string, workspaceDir string, bazeliskVersion string) *ProxyServer {
  113. if len(bazeliskVersion) > 0 {
  114. logger.Println("** Using Bazelisk for this build, due to env var USE_BAZEL_VERSION=" + bazeliskVersion + " **")
  115. }
  116. return &ProxyServer{
  117. logger: logger,
  118. outDir: outDir,
  119. workspaceDir: workspaceDir,
  120. done: make(chan struct{}),
  121. bazeliskVersion: bazeliskVersion,
  122. }
  123. }
  124. func ExecBazel(bazelPath string, workspaceDir string, request CmdRequest) (stdout []byte, stderr []byte, cmdErr error) {
  125. bazelCmd := exec.Command(bazelPath, request.Argv...)
  126. bazelCmd.Dir = workspaceDir
  127. bazelCmd.Env = request.Env
  128. stderrBuffer := &bytes.Buffer{}
  129. bazelCmd.Stderr = stderrBuffer
  130. if output, err := bazelCmd.Output(); err != nil {
  131. cmdErr = fmt.Errorf("bazel command failed: %s\n---command---\n%s\n---env---\n%s\n---stderr---\n%s---",
  132. err, bazelCmd, strings.Join(bazelCmd.Env, "\n"), stderrBuffer)
  133. } else {
  134. stdout = output
  135. }
  136. stderr = stderrBuffer.Bytes()
  137. return
  138. }
  139. func (b *ProxyServer) handleRequest(conn net.Conn) error {
  140. defer conn.Close()
  141. dec := gob.NewDecoder(conn)
  142. var req CmdRequest
  143. if err := dec.Decode(&req); err != nil {
  144. return fmt.Errorf("Error decoding request: %s", err)
  145. }
  146. if len(b.bazeliskVersion) > 0 {
  147. req.Env = append(req.Env, "USE_BAZEL_VERSION="+b.bazeliskVersion)
  148. }
  149. stdout, stderr, cmdErr := ExecBazel("./build/bazel/bin/bazel", b.workspaceDir, req)
  150. errorString := ""
  151. if cmdErr != nil {
  152. errorString = cmdErr.Error()
  153. }
  154. resp := CmdResponse{string(stdout), string(stderr), errorString}
  155. enc := gob.NewEncoder(conn)
  156. if err := enc.Encode(&resp); err != nil {
  157. return fmt.Errorf("Error encoding response: %s", err)
  158. }
  159. return nil
  160. }
  161. func (b *ProxyServer) listenUntilClosed(listener net.Listener) error {
  162. for {
  163. // Check for connections every 1 second. This is a blocking operation, so
  164. // if the server is closed, the goroutine will not fully close until this
  165. // deadline is reached. Thus, this deadline is short (but not too short
  166. // so that the routine churns).
  167. listener.(*net.UnixListener).SetDeadline(time.Now().Add(time.Second))
  168. conn, err := listener.Accept()
  169. select {
  170. case <-b.done:
  171. return nil
  172. default:
  173. }
  174. if err != nil {
  175. if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
  176. // Timeout is normal and expected while waiting for client to establish
  177. // a connection.
  178. continue
  179. } else {
  180. b.logger.Fatalf("Listener error: %s", err)
  181. }
  182. }
  183. err = b.handleRequest(conn)
  184. if err != nil {
  185. b.logger.Fatal(err)
  186. }
  187. }
  188. }
  189. // Start initializes the server unix socket and (in a separate goroutine)
  190. // handles requests on the socket until the server is closed. Returns an error
  191. // if a failure occurs during initialization. Will log any post-initialization
  192. // errors to the server's logger.
  193. func (b *ProxyServer) Start() error {
  194. unixSocketAddr := unixSocketPath(b.outDir)
  195. if err := os_lib.RemoveAll(unixSocketAddr); err != nil {
  196. return fmt.Errorf("couldn't remove socket '%s': %s", unixSocketAddr, err)
  197. }
  198. listener, err := net.Listen("unix", unixSocketAddr)
  199. if err != nil {
  200. return fmt.Errorf("error listening on socket '%s': %s", unixSocketAddr, err)
  201. }
  202. go b.listenUntilClosed(listener)
  203. return nil
  204. }
  205. // Close shuts down the server. This will stop the server from listening for
  206. // additional requests.
  207. func (b *ProxyServer) Close() {
  208. b.done <- struct{}{}
  209. }