2
0

config.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package worker
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "github.com/BurntSushi/toml"
  7. cgv1 "github.com/containerd/cgroups/v3/cgroup1"
  8. cgv2 "github.com/containerd/cgroups/v3/cgroup2"
  9. units "github.com/docker/go-units"
  10. "github.com/imdario/mergo"
  11. )
  12. type providerEnum uint8
  13. const (
  14. provRsync providerEnum = iota
  15. provTwoStageRsync
  16. provCommand
  17. )
  18. func (p *providerEnum) UnmarshalText(text []byte) error {
  19. s := string(text)
  20. switch s {
  21. case `command`:
  22. *p = provCommand
  23. case `rsync`:
  24. *p = provRsync
  25. case `two-stage-rsync`:
  26. *p = provTwoStageRsync
  27. default:
  28. return errors.New("Invalid value to provierEnum")
  29. }
  30. return nil
  31. }
  32. // Config represents worker config options
  33. type Config struct {
  34. Global globalConfig `toml:"global"`
  35. Manager managerConfig `toml:"manager"`
  36. Server serverConfig `toml:"server"`
  37. Cgroup cgroupConfig `toml:"cgroup"`
  38. ZFS zfsConfig `toml:"zfs"`
  39. BtrfsSnapshot btrfsSnapshotConfig `toml:"btrfs_snapshot"`
  40. Docker dockerConfig `toml:"docker"`
  41. Include includeConfig `toml:"include"`
  42. MirrorsConf []mirrorConfig `toml:"mirrors"`
  43. Mirrors []mirrorConfig
  44. }
  45. type globalConfig struct {
  46. Name string `toml:"name"`
  47. LogDir string `toml:"log_dir"`
  48. MirrorDir string `toml:"mirror_dir"`
  49. Concurrent int `toml:"concurrent"`
  50. Interval int `toml:"interval"`
  51. Retry int `toml:"retry"`
  52. Timeout int `toml:"timeout"`
  53. // appended to the options generated by rsync_provider, but before mirror-specific options
  54. RsyncOptions []string `toml:"rsync_options"`
  55. ExecOnSuccess []string `toml:"exec_on_success"`
  56. ExecOnFailure []string `toml:"exec_on_failure"`
  57. }
  58. type managerConfig struct {
  59. APIBase string `toml:"api_base"`
  60. // this option overrides the APIBase
  61. APIList []string `toml:"api_base_list"`
  62. CACert string `toml:"ca_cert"`
  63. // Token string `toml:"token"`
  64. }
  65. func (mc managerConfig) APIBaseList() []string {
  66. if len(mc.APIList) > 0 {
  67. return mc.APIList
  68. }
  69. return []string{mc.APIBase}
  70. }
  71. type serverConfig struct {
  72. Hostname string `toml:"hostname"`
  73. Addr string `toml:"listen_addr"`
  74. Port int `toml:"listen_port"`
  75. SSLCert string `toml:"ssl_cert"`
  76. SSLKey string `toml:"ssl_key"`
  77. }
  78. type cgroupConfig struct {
  79. Enable bool `toml:"enable"`
  80. BasePath string `toml:"base_path"`
  81. Group string `toml:"group"`
  82. Subsystem string `toml:"subsystem"`
  83. isUnified bool
  84. cgMgrV1 cgv1.Cgroup
  85. cgMgrV2 *cgv2.Manager
  86. }
  87. type dockerConfig struct {
  88. Enable bool `toml:"enable"`
  89. Volumes []string `toml:"volumes"`
  90. Options []string `toml:"options"`
  91. }
  92. type zfsConfig struct {
  93. Enable bool `toml:"enable"`
  94. Zpool string `toml:"zpool"`
  95. }
  96. type btrfsSnapshotConfig struct {
  97. Enable bool `toml:"enable"`
  98. SnapshotPath string `toml:"snapshot_path"`
  99. }
  100. type includeConfig struct {
  101. IncludeMirrors string `toml:"include_mirrors"`
  102. }
  103. type includedMirrorConfig struct {
  104. Mirrors []mirrorConfig `toml:"mirrors"`
  105. }
  106. type MemBytes int64
  107. // Set sets the value of the MemBytes by passing a string
  108. func (m *MemBytes) Set(value string) error {
  109. val, err := units.RAMInBytes(value)
  110. *m = MemBytes(val)
  111. return err
  112. }
  113. // Type returns the type
  114. func (m *MemBytes) Type() string {
  115. return "bytes"
  116. }
  117. // Value returns the value in int64
  118. func (m *MemBytes) Value() int64 {
  119. return int64(*m)
  120. }
  121. // UnmarshalJSON is the customized unmarshaler for MemBytes
  122. func (m *MemBytes) UnmarshalText(s []byte) error {
  123. val, err := units.RAMInBytes(string(s))
  124. *m = MemBytes(val)
  125. return err
  126. }
  127. type mirrorConfig struct {
  128. Name string `toml:"name"`
  129. Provider providerEnum `toml:"provider"`
  130. Upstream string `toml:"upstream"`
  131. Interval int `toml:"interval"`
  132. Retry int `toml:"retry"`
  133. Timeout int `toml:"timeout"`
  134. MirrorDir string `toml:"mirror_dir"`
  135. MirrorSubDir string `toml:"mirror_subdir"`
  136. LogDir string `toml:"log_dir"`
  137. Env map[string]string `toml:"env"`
  138. Role string `toml:"role"`
  139. // These two options over-write the global options
  140. ExecOnSuccess []string `toml:"exec_on_success"`
  141. ExecOnFailure []string `toml:"exec_on_failure"`
  142. // These two options are appended to the global options
  143. ExecOnSuccessExtra []string `toml:"exec_on_success_extra"`
  144. ExecOnFailureExtra []string `toml:"exec_on_failure_extra"`
  145. Command string `toml:"command"`
  146. FailOnMatch string `toml:"fail_on_match"`
  147. SizePattern string `toml:"size_pattern"`
  148. UseIPv6 bool `toml:"use_ipv6"`
  149. UseIPv4 bool `toml:"use_ipv4"`
  150. ExcludeFile string `toml:"exclude_file"`
  151. Username string `toml:"username"`
  152. Password string `toml:"password"`
  153. RsyncNoTimeo bool `toml:"rsync_no_timeout"`
  154. RsyncTimeout int `toml:"rsync_timeout"`
  155. RsyncOptions []string `toml:"rsync_options"`
  156. RsyncOverride []string `toml:"rsync_override"`
  157. RsyncOverrideOnly bool `toml:"rsync_override_only"` // only use provided overridden options if true
  158. Stage1Profile string `toml:"stage1_profile"`
  159. MemoryLimit MemBytes `toml:"memory_limit"`
  160. DockerImage string `toml:"docker_image"`
  161. DockerVolumes []string `toml:"docker_volumes"`
  162. DockerOptions []string `toml:"docker_options"`
  163. SnapshotPath string `toml:"snapshot_path"`
  164. ChildMirrors []mirrorConfig `toml:"mirrors"`
  165. }
  166. // LoadConfig loads configuration
  167. func LoadConfig(cfgFile string) (*Config, error) {
  168. if _, err := os.Stat(cfgFile); err != nil {
  169. return nil, err
  170. }
  171. cfg := new(Config)
  172. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  173. logger.Errorf(err.Error())
  174. return nil, err
  175. }
  176. if cfg.Include.IncludeMirrors != "" {
  177. includedFiles, err := filepath.Glob(cfg.Include.IncludeMirrors)
  178. if err != nil {
  179. logger.Errorf(err.Error())
  180. return nil, err
  181. }
  182. for _, f := range includedFiles {
  183. var incMirCfg includedMirrorConfig
  184. if _, err := toml.DecodeFile(f, &incMirCfg); err != nil {
  185. logger.Errorf(err.Error())
  186. return nil, err
  187. }
  188. cfg.MirrorsConf = append(cfg.MirrorsConf, incMirCfg.Mirrors...)
  189. }
  190. }
  191. for _, m := range cfg.MirrorsConf {
  192. if err := recursiveMirrors(cfg, nil, m); err != nil {
  193. return nil, err
  194. }
  195. }
  196. return cfg, nil
  197. }
  198. func recursiveMirrors(cfg *Config, parent *mirrorConfig, mirror mirrorConfig) error {
  199. var curMir mirrorConfig
  200. if parent != nil {
  201. curMir = *parent
  202. }
  203. curMir.ChildMirrors = nil
  204. if err := mergo.Merge(&curMir, mirror, mergo.WithOverride); err != nil {
  205. return err
  206. }
  207. if mirror.ChildMirrors == nil {
  208. cfg.Mirrors = append(cfg.Mirrors, curMir)
  209. } else {
  210. for _, m := range mirror.ChildMirrors {
  211. if err := recursiveMirrors(cfg, &curMir, m); err != nil {
  212. return err
  213. }
  214. }
  215. }
  216. return nil
  217. }