rsync_provider.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package worker
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. )
  7. type rsyncConfig struct {
  8. name string
  9. rsyncCmd string
  10. upstreamURL, password, excludeFile string
  11. workingDir, logDir, logFile string
  12. useIPv6 bool
  13. interval time.Duration
  14. }
  15. // An RsyncProvider provides the implementation to rsync-based syncing jobs
  16. type rsyncProvider struct {
  17. baseProvider
  18. rsyncConfig
  19. options []string
  20. }
  21. func newRsyncProvider(c rsyncConfig) (*rsyncProvider, error) {
  22. // TODO: check config options
  23. if !strings.HasSuffix(c.upstreamURL, "/") {
  24. return nil, errors.New("rsync upstream URL should ends with /")
  25. }
  26. provider := &rsyncProvider{
  27. baseProvider: baseProvider{
  28. name: c.name,
  29. ctx: NewContext(),
  30. interval: c.interval,
  31. },
  32. rsyncConfig: c,
  33. }
  34. if c.rsyncCmd == "" {
  35. provider.rsyncCmd = "rsync"
  36. }
  37. options := []string{
  38. "-aHvh", "--no-o", "--no-g", "--stats",
  39. "--exclude", ".~tmp~/",
  40. "--delete", "--delete-after", "--delay-updates",
  41. "--safe-links", "--timeout=120", "--contimeout=120",
  42. }
  43. if c.useIPv6 {
  44. options = append(options, "-6")
  45. }
  46. if c.excludeFile != "" {
  47. options = append(options, "--exclude-from", c.excludeFile)
  48. }
  49. provider.options = options
  50. provider.ctx.Set(_WorkingDirKey, c.workingDir)
  51. provider.ctx.Set(_LogDirKey, c.logDir)
  52. provider.ctx.Set(_LogFileKey, c.logFile)
  53. return provider, nil
  54. }
  55. func (p *rsyncProvider) Run() error {
  56. if err := p.Start(); err != nil {
  57. return err
  58. }
  59. return p.Wait()
  60. }
  61. func (p *rsyncProvider) Start() error {
  62. env := map[string]string{}
  63. if p.password != "" {
  64. env["RSYNC_PASSWORD"] = p.password
  65. }
  66. command := []string{p.rsyncCmd}
  67. command = append(command, p.options...)
  68. command = append(command, p.upstreamURL, p.WorkingDir())
  69. p.cmd = newCmdJob(command, p.WorkingDir(), env)
  70. if err := p.prepareLogFile(); err != nil {
  71. return err
  72. }
  73. if err := p.cmd.Start(); err != nil {
  74. return err
  75. }
  76. p.isRunning.Store(true)
  77. return nil
  78. }