rsync_provider.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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) Run() error {
  48. if err := p.Start(); err != nil {
  49. return err
  50. }
  51. return p.Wait()
  52. }
  53. func (p *rsyncProvider) Start() error {
  54. env := map[string]string{}
  55. if p.password != "" {
  56. env["RSYNC_PASSWORD"] = p.password
  57. }
  58. command := []string{p.rsyncCmd}
  59. command = append(command, p.options...)
  60. command = append(command, p.upstreamURL, p.WorkingDir())
  61. p.cmd = newCmdJob(command, p.WorkingDir(), env)
  62. if err := p.setLogFile(); err != nil {
  63. return err
  64. }
  65. return p.cmd.Start()
  66. }