logs.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package docker
  2. /*
  3. logs.go
  4. WebSocket streaming of long-running docker output (container logs now;
  5. compose logs reuse streamCommandToWS in the compose phase). The upgrade
  6. happens inside the already-auth-checked handler body, exactly like
  7. src/cast.go's arozcast websocket. When the client disconnects, the spawned
  8. docker process is killed so no orphaned `docker logs -f` lingers.
  9. */
  10. import (
  11. "io"
  12. "net/http"
  13. "os/exec"
  14. "github.com/gorilla/websocket"
  15. "imuslab.com/arozos/mod/utils"
  16. )
  17. // dockerWsUpgrader upgrades HTTP requests to websockets for streaming endpoints.
  18. var dockerWsUpgrader = websocket.Upgrader{
  19. ReadBufferSize: 4096,
  20. WriteBufferSize: 4096,
  21. CheckOrigin: func(r *http.Request) bool { return true },
  22. }
  23. // streamCommandToWS runs cmd, streams its merged stdout+stderr to the websocket
  24. // as text frames, and kills the process when the client disconnects. It blocks
  25. // until the command exits or the client goes away.
  26. func streamCommandToWS(conn *websocket.Conn, cmd *exec.Cmd) {
  27. pr, pw := io.Pipe()
  28. cmd.Stdout = pw
  29. cmd.Stderr = pw
  30. if err := cmd.Start(); err != nil {
  31. conn.WriteMessage(websocket.TextMessage, []byte("Failed to start: "+err.Error()))
  32. pw.Close()
  33. return
  34. }
  35. //Close the pipe writer once the process exits so the read loop below ends.
  36. go func() {
  37. cmd.Wait()
  38. pw.Close()
  39. }()
  40. //Reader goroutine: any read error (or client close) means the client is
  41. //gone — kill the docker process to stop the stream.
  42. go func() {
  43. for {
  44. if _, _, err := conn.ReadMessage(); err != nil {
  45. if cmd.Process != nil {
  46. cmd.Process.Kill()
  47. }
  48. return
  49. }
  50. }
  51. }()
  52. buf := make([]byte, 4096)
  53. for {
  54. n, err := pr.Read(buf)
  55. if n > 0 {
  56. if werr := conn.WriteMessage(websocket.TextMessage, buf[:n]); werr != nil {
  57. //Client write failed — stop the process and bail.
  58. if cmd.Process != nil {
  59. cmd.Process.Kill()
  60. }
  61. return
  62. }
  63. }
  64. if err != nil {
  65. return
  66. }
  67. }
  68. }
  69. // HandleContainerLogs streams the tail of a container's logs over a websocket.
  70. func (d *DockerManager) HandleContainerLogs(w http.ResponseWriter, r *http.Request) {
  71. ref, err := utils.GetPara(r, "id")
  72. if err != nil {
  73. http.Error(w, "missing container id", http.StatusBadRequest)
  74. return
  75. }
  76. if err := validateContainerRef(ref); err != nil {
  77. http.Error(w, err.Error(), http.StatusBadRequest)
  78. return
  79. }
  80. conn, err := dockerWsUpgrader.Upgrade(w, r, nil)
  81. if err != nil {
  82. return
  83. }
  84. defer conn.Close()
  85. cmd := exec.Command("docker", "logs", "-f", "--tail", "200", ref)
  86. streamCommandToWS(conn, cmd)
  87. }