provider.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package worker
  2. import (
  3. "bytes"
  4. "errors"
  5. "html/template"
  6. "path/filepath"
  7. "time"
  8. )
  9. // mirror provider is the wrapper of mirror jobs
  10. const (
  11. _WorkingDirKey = "working_dir"
  12. _LogDirKey = "log_dir"
  13. _LogFileKey = "log_file"
  14. )
  15. // A mirrorProvider instance
  16. type mirrorProvider interface {
  17. // name
  18. Name() string
  19. Upstream() string
  20. Type() providerEnum
  21. // run mirror job in background
  22. Run() error
  23. // run mirror job in background
  24. Start() error
  25. // Wait job to finish
  26. Wait() error
  27. // terminate mirror job
  28. Terminate() error
  29. // job hooks
  30. IsRunning() bool
  31. // Cgroup
  32. Cgroup() *cgroupHook
  33. // ZFS
  34. ZFS() *zfsHook
  35. // Docker
  36. Docker() *dockerHook
  37. AddHook(hook jobHook)
  38. Hooks() []jobHook
  39. Interval() time.Duration
  40. Retry() int
  41. WorkingDir() string
  42. LogDir() string
  43. LogFile() string
  44. IsMaster() bool
  45. DataSize() string
  46. // enter context
  47. EnterContext() *Context
  48. // exit context
  49. ExitContext() *Context
  50. // return context
  51. Context() *Context
  52. }
  53. // newProvider creates a mirrorProvider instance
  54. // using a mirrorCfg and the global cfg
  55. func newMirrorProvider(mirror mirrorConfig, cfg *Config) mirrorProvider {
  56. formatLogDir := func(logDir string, m mirrorConfig) string {
  57. tmpl, err := template.New("logDirTmpl-" + m.Name).Parse(logDir)
  58. if err != nil {
  59. panic(err)
  60. }
  61. var formatedLogDir bytes.Buffer
  62. tmpl.Execute(&formatedLogDir, m)
  63. return formatedLogDir.String()
  64. }
  65. logDir := mirror.LogDir
  66. mirrorDir := mirror.MirrorDir
  67. if logDir == "" {
  68. logDir = cfg.Global.LogDir
  69. }
  70. if mirrorDir == "" {
  71. mirrorDir = filepath.Join(
  72. cfg.Global.MirrorDir, mirror.Name,
  73. )
  74. }
  75. if mirror.Interval == 0 {
  76. mirror.Interval = cfg.Global.Interval
  77. }
  78. if mirror.Retry == 0 {
  79. mirror.Retry = cfg.Global.Retry
  80. }
  81. logDir = formatLogDir(logDir, mirror)
  82. // IsMaster
  83. isMaster := true
  84. if mirror.Role == "slave" {
  85. isMaster = false
  86. } else {
  87. if mirror.Role != "" && mirror.Role != "master" {
  88. logger.Warningf("Invalid role configuration for %s", mirror.Name)
  89. }
  90. }
  91. var provider mirrorProvider
  92. switch mirror.Provider {
  93. case provCommand:
  94. pc := cmdConfig{
  95. name: mirror.Name,
  96. upstreamURL: mirror.Upstream,
  97. command: mirror.Command,
  98. workingDir: mirrorDir,
  99. failOnMatch: mirror.FailOnMatch,
  100. logDir: logDir,
  101. logFile: filepath.Join(logDir, "latest.log"),
  102. interval: time.Duration(mirror.Interval) * time.Minute,
  103. retry: mirror.Retry,
  104. env: mirror.Env,
  105. }
  106. p, err := newCmdProvider(pc)
  107. if err != nil {
  108. panic(err)
  109. }
  110. p.isMaster = isMaster
  111. provider = p
  112. case provRsync:
  113. rc := rsyncConfig{
  114. name: mirror.Name,
  115. upstreamURL: mirror.Upstream,
  116. rsyncCmd: mirror.Command,
  117. username: mirror.Username,
  118. password: mirror.Password,
  119. excludeFile: mirror.ExcludeFile,
  120. extraOptions: mirror.RsyncOptions,
  121. workingDir: mirrorDir,
  122. logDir: logDir,
  123. logFile: filepath.Join(logDir, "latest.log"),
  124. useIPv6: mirror.UseIPv6,
  125. useIPv4: mirror.UseIPv4,
  126. interval: time.Duration(mirror.Interval) * time.Minute,
  127. retry: mirror.Retry,
  128. }
  129. p, err := newRsyncProvider(rc)
  130. if err != nil {
  131. panic(err)
  132. }
  133. p.isMaster = isMaster
  134. provider = p
  135. case provTwoStageRsync:
  136. rc := twoStageRsyncConfig{
  137. name: mirror.Name,
  138. stage1Profile: mirror.Stage1Profile,
  139. upstreamURL: mirror.Upstream,
  140. rsyncCmd: mirror.Command,
  141. username: mirror.Username,
  142. password: mirror.Password,
  143. excludeFile: mirror.ExcludeFile,
  144. extraOptions: mirror.RsyncOptions,
  145. workingDir: mirrorDir,
  146. logDir: logDir,
  147. logFile: filepath.Join(logDir, "latest.log"),
  148. useIPv6: mirror.UseIPv6,
  149. interval: time.Duration(mirror.Interval) * time.Minute,
  150. retry: mirror.Retry,
  151. }
  152. p, err := newTwoStageRsyncProvider(rc)
  153. if err != nil {
  154. panic(err)
  155. }
  156. p.isMaster = isMaster
  157. provider = p
  158. default:
  159. panic(errors.New("Invalid mirror provider"))
  160. }
  161. // Add Logging Hook
  162. provider.AddHook(newLogLimiter(provider))
  163. // Add ZFS Hook
  164. if cfg.ZFS.Enable {
  165. provider.AddHook(newZfsHook(provider, cfg.ZFS.Zpool))
  166. }
  167. // Add Btrfs Snapshot Hook
  168. if cfg.BtrfsSnapshot.Enable {
  169. provider.AddHook(newBtrfsSnapshotHook(provider, cfg.BtrfsSnapshot.SnapshotPath, mirror))
  170. }
  171. // Add Docker Hook
  172. if cfg.Docker.Enable && len(mirror.DockerImage) > 0 {
  173. provider.AddHook(newDockerHook(provider, cfg.Docker, mirror))
  174. } else if cfg.Cgroup.Enable {
  175. // Add Cgroup Hook
  176. provider.AddHook(
  177. newCgroupHook(
  178. provider, cfg.Cgroup.BasePath, cfg.Cgroup.Group,
  179. cfg.Cgroup.Subsystem, mirror.MemoryLimit,
  180. ),
  181. )
  182. }
  183. addHookFromCmdList := func(cmdList []string, execOn uint8) {
  184. if execOn != execOnSuccess && execOn != execOnFailure {
  185. panic("Invalid option for exec-on")
  186. }
  187. for _, cmd := range cmdList {
  188. h, err := newExecPostHook(provider, execOn, cmd)
  189. if err != nil {
  190. logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error())
  191. panic(err)
  192. }
  193. provider.AddHook(h)
  194. }
  195. }
  196. // ExecOnSuccess hook
  197. if len(mirror.ExecOnSuccess) > 0 {
  198. addHookFromCmdList(mirror.ExecOnSuccess, execOnSuccess)
  199. } else {
  200. addHookFromCmdList(cfg.Global.ExecOnSuccess, execOnSuccess)
  201. }
  202. addHookFromCmdList(mirror.ExecOnSuccessExtra, execOnSuccess)
  203. // ExecOnFailure hook
  204. if len(mirror.ExecOnFailure) > 0 {
  205. addHookFromCmdList(mirror.ExecOnFailure, execOnFailure)
  206. } else {
  207. addHookFromCmdList(cfg.Global.ExecOnFailure, execOnFailure)
  208. }
  209. addHookFromCmdList(mirror.ExecOnFailureExtra, execOnFailure)
  210. return provider
  211. }