You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

manager.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package private
  5. import (
  6. "context"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "time"
  11. "code.gitea.io/gitea/modules/json"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. // Shutdown calls the internal shutdown function
  15. func Shutdown(ctx context.Context) (int, string) {
  16. reqURL := setting.LocalURL + "api/internal/manager/shutdown"
  17. req := newInternalRequest(ctx, reqURL, "POST")
  18. resp, err := req.Response()
  19. if err != nil {
  20. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  21. }
  22. defer resp.Body.Close()
  23. if resp.StatusCode != http.StatusOK {
  24. return resp.StatusCode, decodeJSONError(resp).Err
  25. }
  26. return http.StatusOK, "Shutting down"
  27. }
  28. // Restart calls the internal restart function
  29. func Restart(ctx context.Context) (int, string) {
  30. reqURL := setting.LocalURL + "api/internal/manager/restart"
  31. req := newInternalRequest(ctx, reqURL, "POST")
  32. resp, err := req.Response()
  33. if err != nil {
  34. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  35. }
  36. defer resp.Body.Close()
  37. if resp.StatusCode != http.StatusOK {
  38. return resp.StatusCode, decodeJSONError(resp).Err
  39. }
  40. return http.StatusOK, "Restarting"
  41. }
  42. // FlushOptions represents the options for the flush call
  43. type FlushOptions struct {
  44. Timeout time.Duration
  45. NonBlocking bool
  46. }
  47. // FlushQueues calls the internal flush-queues function
  48. func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) (int, string) {
  49. reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
  50. req := newInternalRequest(ctx, reqURL, "POST")
  51. if timeout > 0 {
  52. req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second)
  53. }
  54. req = req.Header("Content-Type", "application/json")
  55. jsonBytes, _ := json.Marshal(FlushOptions{
  56. Timeout: timeout,
  57. NonBlocking: nonBlocking,
  58. })
  59. req.Body(jsonBytes)
  60. resp, err := req.Response()
  61. if err != nil {
  62. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  63. }
  64. defer resp.Body.Close()
  65. if resp.StatusCode != http.StatusOK {
  66. return resp.StatusCode, decodeJSONError(resp).Err
  67. }
  68. return http.StatusOK, "Flushed"
  69. }
  70. // PauseLogging pauses logging
  71. func PauseLogging(ctx context.Context) (int, string) {
  72. reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
  73. req := newInternalRequest(ctx, reqURL, "POST")
  74. resp, err := req.Response()
  75. if err != nil {
  76. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  77. }
  78. defer resp.Body.Close()
  79. if resp.StatusCode != http.StatusOK {
  80. return resp.StatusCode, decodeJSONError(resp).Err
  81. }
  82. return http.StatusOK, "Logging Paused"
  83. }
  84. // ResumeLogging resumes logging
  85. func ResumeLogging(ctx context.Context) (int, string) {
  86. reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
  87. req := newInternalRequest(ctx, reqURL, "POST")
  88. resp, err := req.Response()
  89. if err != nil {
  90. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  91. }
  92. defer resp.Body.Close()
  93. if resp.StatusCode != http.StatusOK {
  94. return resp.StatusCode, decodeJSONError(resp).Err
  95. }
  96. return http.StatusOK, "Logging Restarted"
  97. }
  98. // ReleaseReopenLogging releases and reopens logging files
  99. func ReleaseReopenLogging(ctx context.Context) (int, string) {
  100. reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
  101. req := newInternalRequest(ctx, reqURL, "POST")
  102. resp, err := req.Response()
  103. if err != nil {
  104. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  105. }
  106. defer resp.Body.Close()
  107. if resp.StatusCode != http.StatusOK {
  108. return resp.StatusCode, decodeJSONError(resp).Err
  109. }
  110. return http.StatusOK, "Logging Restarted"
  111. }
  112. // LoggerOptions represents the options for the add logger call
  113. type LoggerOptions struct {
  114. Group string
  115. Name string
  116. Mode string
  117. Config map[string]interface{}
  118. }
  119. // AddLogger adds a logger
  120. func AddLogger(ctx context.Context, group, name, mode string, config map[string]interface{}) (int, string) {
  121. reqURL := setting.LocalURL + "api/internal/manager/add-logger"
  122. req := newInternalRequest(ctx, reqURL, "POST")
  123. req = req.Header("Content-Type", "application/json")
  124. jsonBytes, _ := json.Marshal(LoggerOptions{
  125. Group: group,
  126. Name: name,
  127. Mode: mode,
  128. Config: config,
  129. })
  130. req.Body(jsonBytes)
  131. resp, err := req.Response()
  132. if err != nil {
  133. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  134. }
  135. defer resp.Body.Close()
  136. if resp.StatusCode != http.StatusOK {
  137. return resp.StatusCode, decodeJSONError(resp).Err
  138. }
  139. return http.StatusOK, "Added"
  140. }
  141. // RemoveLogger removes a logger
  142. func RemoveLogger(ctx context.Context, group, name string) (int, string) {
  143. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(group), url.PathEscape(name))
  144. req := newInternalRequest(ctx, reqURL, "POST")
  145. resp, err := req.Response()
  146. if err != nil {
  147. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  148. }
  149. defer resp.Body.Close()
  150. if resp.StatusCode != http.StatusOK {
  151. return resp.StatusCode, decodeJSONError(resp).Err
  152. }
  153. return http.StatusOK, "Removed"
  154. }