rsync_provider.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package worker
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/tuna/tunasync/internal"
  8. )
  9. type rsyncConfig struct {
  10. name string
  11. rsyncCmd string
  12. upstreamURL, username, password, excludeFile string
  13. extraOptions []string
  14. overriddenOptions []string
  15. rsyncNeverTimeout bool
  16. rsyncTimeoutValue int
  17. rsyncEnv map[string]string
  18. workingDir, logDir, logFile string
  19. useIPv6, useIPv4 bool
  20. interval time.Duration
  21. retry int
  22. timeout time.Duration
  23. }
  24. // An RsyncProvider provides the implementation to rsync-based syncing jobs
  25. type rsyncProvider struct {
  26. baseProvider
  27. rsyncConfig
  28. options []string
  29. dataSize string
  30. }
  31. func newRsyncProvider(c rsyncConfig) (*rsyncProvider, error) {
  32. // TODO: check config options
  33. if !strings.HasSuffix(c.upstreamURL, "/") {
  34. return nil, errors.New("rsync upstream URL should ends with /")
  35. }
  36. if c.retry == 0 {
  37. c.retry = defaultMaxRetry
  38. }
  39. provider := &rsyncProvider{
  40. baseProvider: baseProvider{
  41. name: c.name,
  42. ctx: NewContext(),
  43. interval: c.interval,
  44. retry: c.retry,
  45. timeout: c.timeout,
  46. },
  47. rsyncConfig: c,
  48. }
  49. if c.rsyncCmd == "" {
  50. provider.rsyncCmd = "rsync"
  51. }
  52. if c.rsyncEnv == nil {
  53. provider.rsyncEnv = map[string]string{}
  54. }
  55. if c.username != "" {
  56. provider.rsyncEnv["USER"] = c.username
  57. }
  58. if c.password != "" {
  59. provider.rsyncEnv["RSYNC_PASSWORD"] = c.password
  60. }
  61. options := []string{
  62. "-aHvh", "--no-o", "--no-g", "--stats",
  63. "--filter", "risk .~tmp~/", "--exclude", ".~tmp~/",
  64. "--delete", "--delete-after", "--delay-updates",
  65. "--safe-links",
  66. }
  67. if c.overriddenOptions != nil {
  68. options = c.overriddenOptions
  69. }
  70. if !c.rsyncNeverTimeout {
  71. timeo := 120
  72. if c.rsyncTimeoutValue > 0 {
  73. timeo = c.rsyncTimeoutValue
  74. }
  75. options = append(options, fmt.Sprintf("--timeout=%d", timeo))
  76. }
  77. if c.useIPv6 {
  78. options = append(options, "-6")
  79. } else if c.useIPv4 {
  80. options = append(options, "-4")
  81. }
  82. if c.excludeFile != "" {
  83. options = append(options, "--exclude-from", c.excludeFile)
  84. }
  85. if c.extraOptions != nil {
  86. options = append(options, c.extraOptions...)
  87. }
  88. provider.options = options
  89. provider.ctx.Set(_WorkingDirKey, c.workingDir)
  90. provider.ctx.Set(_LogDirKey, c.logDir)
  91. provider.ctx.Set(_LogFileKey, c.logFile)
  92. return provider, nil
  93. }
  94. func (p *rsyncProvider) Type() providerEnum {
  95. return provRsync
  96. }
  97. func (p *rsyncProvider) Upstream() string {
  98. return p.upstreamURL
  99. }
  100. func (p *rsyncProvider) DataSize() string {
  101. return p.dataSize
  102. }
  103. func (p *rsyncProvider) Run(started chan empty) error {
  104. p.dataSize = ""
  105. defer p.closeLogFile()
  106. if err := p.Start(); err != nil {
  107. return err
  108. }
  109. started <- empty{}
  110. if err := p.Wait(); err != nil {
  111. code, msg := internal.TranslateRsyncErrorCode(err)
  112. if code != 0 {
  113. logger.Debug("Rsync exitcode %d (%s)", code, msg)
  114. if p.logFileFd != nil {
  115. p.logFileFd.WriteString(msg + "\n")
  116. }
  117. }
  118. return err
  119. }
  120. p.dataSize = internal.ExtractSizeFromRsyncLog(p.LogFile())
  121. return nil
  122. }
  123. func (p *rsyncProvider) Start() error {
  124. p.Lock()
  125. defer p.Unlock()
  126. if p.IsRunning() {
  127. return errors.New("provider is currently running")
  128. }
  129. command := []string{p.rsyncCmd}
  130. command = append(command, p.options...)
  131. command = append(command, p.upstreamURL, p.WorkingDir())
  132. p.cmd = newCmdJob(p, command, p.WorkingDir(), p.rsyncEnv)
  133. if err := p.prepareLogFile(false); err != nil {
  134. return err
  135. }
  136. if err := p.cmd.Start(); err != nil {
  137. return err
  138. }
  139. p.isRunning.Store(true)
  140. logger.Debugf("set isRunning to true: %s", p.Name())
  141. return nil
  142. }