2
0

config.go 4.6 KB

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