rsync_provider.go 1.6 KB

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