rsync_provider.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package worker
  2. import (
  3. "errors"
  4. "io/ioutil"
  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. workingDir, logDir, logFile string
  14. useIPv6, useIPv4 bool
  15. interval time.Duration
  16. retry int
  17. }
  18. // An RsyncProvider provides the implementation to rsync-based syncing jobs
  19. type rsyncProvider struct {
  20. baseProvider
  21. rsyncConfig
  22. options []string
  23. dataSize string
  24. }
  25. func newRsyncProvider(c rsyncConfig) (*rsyncProvider, error) {
  26. // TODO: check config options
  27. if !strings.HasSuffix(c.upstreamURL, "/") {
  28. return nil, errors.New("rsync upstream URL should ends with /")
  29. }
  30. provider := &rsyncProvider{
  31. baseProvider: baseProvider{
  32. name: c.name,
  33. ctx: NewContext(),
  34. interval: c.interval,
  35. retry: c.retry,
  36. },
  37. rsyncConfig: c,
  38. }
  39. if c.rsyncCmd == "" {
  40. provider.rsyncCmd = "rsync"
  41. }
  42. options := []string{
  43. "-aHvh", "--no-o", "--no-g", "--stats",
  44. "--exclude", ".~tmp~/",
  45. "--delete", "--delete-after", "--delay-updates",
  46. "--safe-links", "--timeout=120", "--contimeout=120",
  47. }
  48. if c.useIPv6 {
  49. options = append(options, "-6")
  50. } else if c.useIPv4 {
  51. options = append(options, "-4")
  52. }
  53. if c.excludeFile != "" {
  54. options = append(options, "--exclude-from", c.excludeFile)
  55. }
  56. provider.options = options
  57. provider.ctx.Set(_WorkingDirKey, c.workingDir)
  58. provider.ctx.Set(_LogDirKey, c.logDir)
  59. provider.ctx.Set(_LogFileKey, c.logFile)
  60. return provider, nil
  61. }
  62. func (p *rsyncProvider) Type() providerEnum {
  63. return provRsync
  64. }
  65. func (p *rsyncProvider) Upstream() string {
  66. return p.upstreamURL
  67. }
  68. func (p *rsyncProvider) DataSize() string {
  69. return p.dataSize
  70. }
  71. func (p *rsyncProvider) Run() error {
  72. p.dataSize = ""
  73. if err := p.Start(); err != nil {
  74. return err
  75. }
  76. if err := p.Wait(); err != nil {
  77. return err
  78. }
  79. if logContent, err := ioutil.ReadFile(p.LogFile()); err == nil {
  80. p.dataSize = internal.ExtractSizeFromRsyncLog(logContent)
  81. }
  82. return nil
  83. }
  84. func (p *rsyncProvider) Start() error {
  85. p.Lock()
  86. defer p.Unlock()
  87. if p.IsRunning() {
  88. return errors.New("provider is currently running")
  89. }
  90. env := map[string]string{}
  91. if p.username != "" {
  92. env["USER"] = p.username
  93. }
  94. if p.password != "" {
  95. env["RSYNC_PASSWORD"] = p.password
  96. }
  97. command := []string{p.rsyncCmd}
  98. command = append(command, p.options...)
  99. command = append(command, p.upstreamURL, p.WorkingDir())
  100. p.cmd = newCmdJob(p, command, p.WorkingDir(), env)
  101. if err := p.prepareLogFile(false); err != nil {
  102. return err
  103. }
  104. if err := p.cmd.Start(); err != nil {
  105. return err
  106. }
  107. p.isRunning.Store(true)
  108. return nil
  109. }