provider.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // enter context
  46. EnterContext() *Context
  47. // exit context
  48. ExitContext() *Context
  49. // return context
  50. Context() *Context
  51. }
  52. // newProvider creates a mirrorProvider instance
  53. // using a mirrorCfg and the global cfg
  54. func newMirrorProvider(mirror mirrorConfig, cfg *Config) mirrorProvider {
  55. formatLogDir := func(logDir string, m mirrorConfig) string {
  56. tmpl, err := template.New("logDirTmpl-" + m.Name).Parse(logDir)
  57. if err != nil {
  58. panic(err)
  59. }
  60. var formatedLogDir bytes.Buffer
  61. tmpl.Execute(&formatedLogDir, m)
  62. return formatedLogDir.String()
  63. }
  64. logDir := mirror.LogDir
  65. mirrorDir := mirror.MirrorDir
  66. if logDir == "" {
  67. logDir = cfg.Global.LogDir
  68. }
  69. if mirrorDir == "" {
  70. mirrorDir = filepath.Join(
  71. cfg.Global.MirrorDir, mirror.Name,
  72. )
  73. }
  74. if mirror.Interval == 0 {
  75. mirror.Interval = cfg.Global.Interval
  76. }
  77. if mirror.Retry == 0 {
  78. mirror.Retry = cfg.Global.Retry
  79. }
  80. logDir = formatLogDir(logDir, mirror)
  81. // IsMaster
  82. isMaster := true
  83. if mirror.Role == "slave" {
  84. isMaster = false
  85. } else {
  86. if mirror.Role != "" && mirror.Role != "master" {
  87. logger.Warningf("Invalid role configuration for %s", mirror.Name)
  88. }
  89. }
  90. var provider mirrorProvider
  91. switch mirror.Provider {
  92. case provCommand:
  93. pc := cmdConfig{
  94. name: mirror.Name,
  95. upstreamURL: mirror.Upstream,
  96. command: mirror.Command,
  97. workingDir: mirrorDir,
  98. logDir: logDir,
  99. logFile: filepath.Join(logDir, "latest.log"),
  100. interval: time.Duration(mirror.Interval) * time.Minute,
  101. retry: mirror.Retry,
  102. env: mirror.Env,
  103. }
  104. p, err := newCmdProvider(pc)
  105. p.isMaster = isMaster
  106. if err != nil {
  107. panic(err)
  108. }
  109. provider = p
  110. case provRsync:
  111. rc := rsyncConfig{
  112. name: mirror.Name,
  113. upstreamURL: mirror.Upstream,
  114. rsyncCmd: mirror.Command,
  115. username: mirror.Username,
  116. password: mirror.Password,
  117. excludeFile: mirror.ExcludeFile,
  118. workingDir: mirrorDir,
  119. logDir: logDir,
  120. logFile: filepath.Join(logDir, "latest.log"),
  121. useIPv6: mirror.UseIPv6,
  122. interval: time.Duration(mirror.Interval) * time.Minute,
  123. retry: mirror.Retry,
  124. }
  125. p, err := newRsyncProvider(rc)
  126. p.isMaster = isMaster
  127. if err != nil {
  128. panic(err)
  129. }
  130. provider = p
  131. case provTwoStageRsync:
  132. rc := twoStageRsyncConfig{
  133. name: mirror.Name,
  134. stage1Profile: mirror.Stage1Profile,
  135. upstreamURL: mirror.Upstream,
  136. rsyncCmd: mirror.Command,
  137. username: mirror.Username,
  138. password: mirror.Password,
  139. excludeFile: mirror.ExcludeFile,
  140. workingDir: mirrorDir,
  141. logDir: logDir,
  142. logFile: filepath.Join(logDir, "latest.log"),
  143. useIPv6: mirror.UseIPv6,
  144. interval: time.Duration(mirror.Interval) * time.Minute,
  145. retry: mirror.Retry,
  146. }
  147. p, err := newTwoStageRsyncProvider(rc)
  148. p.isMaster = isMaster
  149. if err != nil {
  150. panic(err)
  151. }
  152. provider = p
  153. default:
  154. panic(errors.New("Invalid mirror provider"))
  155. }
  156. // Add Logging Hook
  157. provider.AddHook(newLogLimiter(provider))
  158. // Add ZFS Hook
  159. if cfg.ZFS.Enable {
  160. provider.AddHook(newZfsHook(provider, cfg.ZFS.Zpool))
  161. }
  162. // Add Docker Hook
  163. if cfg.Docker.Enable && len(mirror.DockerImage) > 0 {
  164. provider.AddHook(newDockerHook(provider, cfg.Docker, mirror))
  165. } else if cfg.Cgroup.Enable {
  166. // Add Cgroup Hook
  167. provider.AddHook(
  168. newCgroupHook(
  169. provider, cfg.Cgroup.BasePath, cfg.Cgroup.Group,
  170. cfg.Cgroup.Subsystem, mirror.MemoryLimit,
  171. ),
  172. )
  173. }
  174. addHookFromCmdList := func(cmdList []string, execOn uint8) {
  175. if execOn != execOnSuccess && execOn != execOnFailure {
  176. panic("Invalid option for exec-on")
  177. }
  178. for _, cmd := range cmdList {
  179. h, err := newExecPostHook(provider, execOn, cmd)
  180. if err != nil {
  181. logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error())
  182. panic(err)
  183. }
  184. provider.AddHook(h)
  185. }
  186. }
  187. // ExecOnSuccess hook
  188. if len(mirror.ExecOnSuccess) > 0 {
  189. addHookFromCmdList(mirror.ExecOnSuccess, execOnSuccess)
  190. } else {
  191. addHookFromCmdList(cfg.Global.ExecOnSuccess, execOnSuccess)
  192. }
  193. addHookFromCmdList(mirror.ExecOnSuccessExtra, execOnSuccess)
  194. // ExecOnFailure hook
  195. if len(mirror.ExecOnFailure) > 0 {
  196. addHookFromCmdList(mirror.ExecOnFailure, execOnFailure)
  197. } else {
  198. addHookFromCmdList(cfg.Global.ExecOnFailure, execOnFailure)
  199. }
  200. addHookFromCmdList(mirror.ExecOnFailureExtra, execOnFailure)
  201. return provider
  202. }