provider.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. overriddenOptions: mirror.RsyncOverride,
  122. workingDir: mirrorDir,
  123. logDir: logDir,
  124. logFile: filepath.Join(logDir, "latest.log"),
  125. useIPv6: mirror.UseIPv6,
  126. useIPv4: mirror.UseIPv4,
  127. interval: time.Duration(mirror.Interval) * time.Minute,
  128. retry: mirror.Retry,
  129. }
  130. p, err := newRsyncProvider(rc)
  131. if err != nil {
  132. panic(err)
  133. }
  134. p.isMaster = isMaster
  135. provider = p
  136. case provTwoStageRsync:
  137. rc := twoStageRsyncConfig{
  138. name: mirror.Name,
  139. stage1Profile: mirror.Stage1Profile,
  140. upstreamURL: mirror.Upstream,
  141. rsyncCmd: mirror.Command,
  142. username: mirror.Username,
  143. password: mirror.Password,
  144. excludeFile: mirror.ExcludeFile,
  145. extraOptions: mirror.RsyncOptions,
  146. workingDir: mirrorDir,
  147. logDir: logDir,
  148. logFile: filepath.Join(logDir, "latest.log"),
  149. useIPv6: mirror.UseIPv6,
  150. interval: time.Duration(mirror.Interval) * time.Minute,
  151. retry: mirror.Retry,
  152. }
  153. p, err := newTwoStageRsyncProvider(rc)
  154. if err != nil {
  155. panic(err)
  156. }
  157. p.isMaster = isMaster
  158. provider = p
  159. default:
  160. panic(errors.New("Invalid mirror provider"))
  161. }
  162. // Add Logging Hook
  163. provider.AddHook(newLogLimiter(provider))
  164. // Add ZFS Hook
  165. if cfg.ZFS.Enable {
  166. provider.AddHook(newZfsHook(provider, cfg.ZFS.Zpool))
  167. }
  168. // Add Btrfs Snapshot Hook
  169. if cfg.BtrfsSnapshot.Enable {
  170. provider.AddHook(newBtrfsSnapshotHook(provider, cfg.BtrfsSnapshot.SnapshotPath, mirror))
  171. }
  172. // Add Docker Hook
  173. if cfg.Docker.Enable && len(mirror.DockerImage) > 0 {
  174. provider.AddHook(newDockerHook(provider, cfg.Docker, mirror))
  175. } else if cfg.Cgroup.Enable {
  176. // Add Cgroup Hook
  177. provider.AddHook(
  178. newCgroupHook(
  179. provider, cfg.Cgroup.BasePath, cfg.Cgroup.Group,
  180. cfg.Cgroup.Subsystem, mirror.MemoryLimit,
  181. ),
  182. )
  183. }
  184. addHookFromCmdList := func(cmdList []string, execOn uint8) {
  185. if execOn != execOnSuccess && execOn != execOnFailure {
  186. panic("Invalid option for exec-on")
  187. }
  188. for _, cmd := range cmdList {
  189. h, err := newExecPostHook(provider, execOn, cmd)
  190. if err != nil {
  191. logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error())
  192. panic(err)
  193. }
  194. provider.AddHook(h)
  195. }
  196. }
  197. // ExecOnSuccess hook
  198. if len(mirror.ExecOnSuccess) > 0 {
  199. addHookFromCmdList(mirror.ExecOnSuccess, execOnSuccess)
  200. } else {
  201. addHookFromCmdList(cfg.Global.ExecOnSuccess, execOnSuccess)
  202. }
  203. addHookFromCmdList(mirror.ExecOnSuccessExtra, execOnSuccess)
  204. // ExecOnFailure hook
  205. if len(mirror.ExecOnFailure) > 0 {
  206. addHookFromCmdList(mirror.ExecOnFailure, execOnFailure)
  207. } else {
  208. addHookFromCmdList(cfg.Global.ExecOnFailure, execOnFailure)
  209. }
  210. addHookFromCmdList(mirror.ExecOnFailureExtra, execOnFailure)
  211. return provider
  212. }