provider.go 4.9 KB

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