2
0

config.go 5.5 KB

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