util.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package internal
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "os/exec"
  12. "regexp"
  13. "time"
  14. )
  15. var rsyncExitValues = map[int]string{
  16. 0: "Success",
  17. 1: "Syntax or usage error",
  18. 2: "Protocol incompatibility",
  19. 3: "Errors selecting input/output files, dirs",
  20. 4: "Requested action not supported: an attempt was made to manipulate 64-bit files on a platform that cannot support them; or an option was specified that is supported by the client and not by the server.",
  21. 5: "Error starting client-server protocol",
  22. 6: "Daemon unable to append to log-file",
  23. 10: "Error in socket I/O",
  24. 11: "Error in file I/O",
  25. 12: "Error in rsync protocol data stream",
  26. 13: "Errors with program diagnostics",
  27. 14: "Error in IPC code",
  28. 20: "Received SIGUSR1 or SIGINT",
  29. 21: "Some error returned by waitpid()",
  30. 22: "Error allocating core memory buffers",
  31. 23: "Partial transfer due to error",
  32. 24: "Partial transfer due to vanished source files",
  33. 25: "The --max-delete limit stopped deletions",
  34. 30: "Timeout in data send/receive",
  35. 35: "Timeout waiting for daemon connection",
  36. }
  37. // GetTLSConfig generate tls.Config from CAFile
  38. func GetTLSConfig(CAFile string) (*tls.Config, error) {
  39. caCert, err := ioutil.ReadFile(CAFile)
  40. if err != nil {
  41. return nil, err
  42. }
  43. caCertPool := x509.NewCertPool()
  44. if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
  45. return nil, errors.New("Failed to add CA to pool")
  46. }
  47. tlsConfig := &tls.Config{
  48. RootCAs: caCertPool,
  49. }
  50. tlsConfig.BuildNameToCertificate()
  51. return tlsConfig, nil
  52. }
  53. // CreateHTTPClient returns a http.Client
  54. func CreateHTTPClient(CAFile string) (*http.Client, error) {
  55. var tlsConfig *tls.Config
  56. var err error
  57. if CAFile != "" {
  58. tlsConfig, err = GetTLSConfig(CAFile)
  59. if err != nil {
  60. return nil, err
  61. }
  62. }
  63. tr := &http.Transport{
  64. MaxIdleConnsPerHost: 20,
  65. TLSClientConfig: tlsConfig,
  66. }
  67. return &http.Client{
  68. Transport: tr,
  69. Timeout: 5 * time.Second,
  70. }, nil
  71. }
  72. // PostJSON posts json object to url
  73. func PostJSON(url string, obj interface{}, client *http.Client) (*http.Response, error) {
  74. if client == nil {
  75. client, _ = CreateHTTPClient("")
  76. }
  77. b := new(bytes.Buffer)
  78. if err := json.NewEncoder(b).Encode(obj); err != nil {
  79. return nil, err
  80. }
  81. return client.Post(url, "application/json; charset=utf-8", b)
  82. }
  83. // GetJSON gets a json response from url
  84. func GetJSON(url string, obj interface{}, client *http.Client) (*http.Response, error) {
  85. if client == nil {
  86. client, _ = CreateHTTPClient("")
  87. }
  88. resp, err := client.Get(url)
  89. if err != nil {
  90. return resp, err
  91. }
  92. if resp.StatusCode != http.StatusOK {
  93. return resp, errors.New("HTTP status code is not 200")
  94. }
  95. defer resp.Body.Close()
  96. body, err := ioutil.ReadAll(resp.Body)
  97. if err != nil {
  98. return resp, err
  99. }
  100. return resp, json.Unmarshal(body, obj)
  101. }
  102. // FindAllSubmatchInFile calls re.FindAllSubmatch to find matches in given file
  103. func FindAllSubmatchInFile(fileName string, re *regexp.Regexp) (matches [][][]byte, err error) {
  104. if fileName == "/dev/null" {
  105. err = errors.New("Invalid log file")
  106. return
  107. }
  108. if content, err := ioutil.ReadFile(fileName); err == nil {
  109. matches = re.FindAllSubmatch(content, -1)
  110. // fmt.Printf("FindAllSubmatchInFile: %q\n", matches)
  111. }
  112. return
  113. }
  114. // ExtractSizeFromLog uses a regexp to extract the size from log files
  115. func ExtractSizeFromLog(logFile string, re *regexp.Regexp) string {
  116. matches, _ := FindAllSubmatchInFile(logFile, re)
  117. if matches == nil || len(matches) == 0 {
  118. return ""
  119. }
  120. // return the first capture group of the last occurrence
  121. return string(matches[len(matches)-1][1])
  122. }
  123. // ExtractSizeFromRsyncLog extracts the size from rsync logs
  124. func ExtractSizeFromRsyncLog(logFile string) string {
  125. // (?m) flag enables multi-line mode
  126. re := regexp.MustCompile(`(?m)^Total file size: ([0-9\.]+[KMGTP]?) bytes`)
  127. return ExtractSizeFromLog(logFile, re)
  128. }
  129. // TranslateRsyncErrorCode translates the exit code of rsync to a message
  130. func TranslateRsyncErrorCode(cmdErr error) (exitCode int, msg string) {
  131. if exiterr, ok := cmdErr.(*exec.ExitError); ok {
  132. exitCode = exiterr.ExitCode()
  133. strerr, valid := rsyncExitValues[exitCode]
  134. if valid {
  135. msg = fmt.Sprintf("rsync error: %s", strerr)
  136. }
  137. }
  138. return
  139. }