2
0

config.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package worker
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "github.com/BurntSushi/toml"
  7. cgv1 "github.com/containerd/cgroups"
  8. cgv2 "github.com/containerd/cgroups/v2"
  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. ExecOnSuccess []string `toml:"exec_on_success"`
  54. ExecOnFailure []string `toml:"exec_on_failure"`
  55. }
  56. type managerConfig struct {
  57. APIBase string `toml:"api_base"`
  58. // this option overrides the APIBase
  59. APIList []string `toml:"api_base_list"`
  60. CACert string `toml:"ca_cert"`
  61. // Token string `toml:"token"`
  62. }
  63. func (mc managerConfig) APIBaseList() []string {
  64. if len(mc.APIList) > 0 {
  65. return mc.APIList
  66. }
  67. return []string{mc.APIBase}
  68. }
  69. type serverConfig struct {
  70. Hostname string `toml:"hostname"`
  71. Addr string `toml:"listen_addr"`
  72. Port int `toml:"listen_port"`
  73. SSLCert string `toml:"ssl_cert"`
  74. SSLKey string `toml:"ssl_key"`
  75. }
  76. type cgroupConfig struct {
  77. Enable bool `toml:"enable"`
  78. BasePath string `toml:"base_path"`
  79. Group string `toml:"group"`
  80. Subsystem string `toml:"subsystem"`
  81. isUnified bool
  82. cgMgrV1 cgv1.Cgroup
  83. cgMgrV2 *cgv2.Manager
  84. }
  85. type dockerConfig struct {
  86. Enable bool `toml:"enable"`
  87. Volumes []string `toml:"volumes"`
  88. Options []string `toml:"options"`
  89. }
  90. type zfsConfig struct {
  91. Enable bool `toml:"enable"`
  92. Zpool string `toml:"zpool"`
  93. }
  94. type btrfsSnapshotConfig struct {
  95. Enable bool `toml:"enable"`
  96. SnapshotPath string `toml:"snapshot_path"`
  97. }
  98. type includeConfig struct {
  99. IncludeMirrors string `toml:"include_mirrors"`
  100. }
  101. type includedMirrorConfig struct {
  102. Mirrors []mirrorConfig `toml:"mirrors"`
  103. }
  104. type MemBytes int64
  105. // Set sets the value of the MemBytes by passing a string
  106. func (m *MemBytes) Set(value string) error {
  107. val, err := units.RAMInBytes(value)
  108. *m = MemBytes(val)
  109. return err
  110. }
  111. // Type returns the type
  112. func (m *MemBytes) Type() string {
  113. return "bytes"
  114. }
  115. // Value returns the value in int64
  116. func (m *MemBytes) Value() int64 {
  117. return int64(*m)
  118. }
  119. // UnmarshalJSON is the customized unmarshaler for MemBytes
  120. func (m *MemBytes) UnmarshalText(s []byte) error {
  121. val, err := units.RAMInBytes(string(s))
  122. *m = MemBytes(val)
  123. return err
  124. }
  125. type mirrorConfig struct {
  126. Name string `toml:"name"`
  127. Provider providerEnum `toml:"provider"`
  128. Upstream string `toml:"upstream"`
  129. Interval int `toml:"interval"`
  130. Retry int `toml:"retry"`
  131. Timeout int `toml:"timeout"`
  132. MirrorDir string `toml:"mirror_dir"`
  133. MirrorSubDir string `toml:"mirror_subdir"`
  134. LogDir string `toml:"log_dir"`
  135. Env map[string]string `toml:"env"`
  136. Role string `toml:"role"`
  137. // These two options over-write the global options
  138. ExecOnSuccess []string `toml:"exec_on_success"`
  139. ExecOnFailure []string `toml:"exec_on_failure"`
  140. // These two options the global options
  141. ExecOnSuccessExtra []string `toml:"exec_on_success_extra"`
  142. ExecOnFailureExtra []string `toml:"exec_on_failure_extra"`
  143. Command string `toml:"command"`
  144. FailOnMatch string `toml:"fail_on_match"`
  145. SizePattern string `toml:"size_pattern"`
  146. UseIPv6 bool `toml:"use_ipv6"`
  147. UseIPv4 bool `toml:"use_ipv4"`
  148. ExcludeFile string `toml:"exclude_file"`
  149. Username string `toml:"username"`
  150. Password string `toml:"password"`
  151. RsyncNoTimeo bool `toml:"rsync_no_timeout"`
  152. RsyncTimeout int `toml:"rsync_timeout"`
  153. RsyncOptions []string `toml:"rsync_options"`
  154. RsyncOverride []string `toml:"rsync_override"`
  155. Stage1Profile string `toml:"stage1_profile"`
  156. MemoryLimit MemBytes `toml:"memory_limit"`
  157. DockerImage string `toml:"docker_image"`
  158. DockerVolumes []string `toml:"docker_volumes"`
  159. DockerOptions []string `toml:"docker_options"`
  160. SnapshotPath string `toml:"snapshot_path"`
  161. ChildMirrors []mirrorConfig `toml:"mirrors"`
  162. }
  163. // LoadConfig loads configuration
  164. func LoadConfig(cfgFile string) (*Config, error) {
  165. if _, err := os.Stat(cfgFile); err != nil {
  166. return nil, err
  167. }
  168. cfg := new(Config)
  169. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  170. logger.Errorf(err.Error())
  171. return nil, err
  172. }
  173. if cfg.Include.IncludeMirrors != "" {
  174. includedFiles, err := filepath.Glob(cfg.Include.IncludeMirrors)
  175. if err != nil {
  176. logger.Errorf(err.Error())
  177. return nil, err
  178. }
  179. for _, f := range includedFiles {
  180. var incMirCfg includedMirrorConfig
  181. if _, err := toml.DecodeFile(f, &incMirCfg); err != nil {
  182. logger.Errorf(err.Error())
  183. return nil, err
  184. }
  185. cfg.MirrorsConf = append(cfg.MirrorsConf, incMirCfg.Mirrors...)
  186. }
  187. }
  188. for _, m := range cfg.MirrorsConf {
  189. if err := recursiveMirrors(cfg, nil, m); err != nil {
  190. return nil, err
  191. }
  192. }
  193. return cfg, nil
  194. }
  195. func recursiveMirrors(cfg *Config, parent *mirrorConfig, mirror mirrorConfig) error {
  196. var curMir mirrorConfig
  197. if parent != nil {
  198. curMir = *parent
  199. }
  200. curMir.ChildMirrors = nil
  201. if err := mergo.Merge(&curMir, mirror, mergo.WithOverride); err != nil {
  202. return err
  203. }
  204. if mirror.ChildMirrors == nil {
  205. cfg.Mirrors = append(cfg.Mirrors, curMir)
  206. } else {
  207. for _, m := range mirror.ChildMirrors {
  208. if err := recursiveMirrors(cfg, &curMir, m); err != nil {
  209. return err
  210. }
  211. }
  212. }
  213. return nil
  214. }