2
0

provider.go 5.2 KB

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