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.1KB

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