config.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. Docker dockerConfig `toml:"docker"`
  36. Include includeConfig `toml:"include"`
  37. Mirrors []mirrorConfig `toml:"mirrors"`
  38. }
  39. type globalConfig struct {
  40. Name string `toml:"name"`
  41. LogDir string `toml:"log_dir"`
  42. MirrorDir string `toml:"mirror_dir"`
  43. Concurrent int `toml:"concurrent"`
  44. Interval int `toml:"interval"`
  45. Retry int `toml:"retry"`
  46. ExecOnSuccess []string `toml:"exec_on_success"`
  47. ExecOnFailure []string `toml:"exec_on_failure"`
  48. }
  49. type managerConfig struct {
  50. APIBase string `toml:"api_base"`
  51. // this option overrides the APIBase
  52. APIList []string `toml:"api_base_list"`
  53. CACert string `toml:"ca_cert"`
  54. // Token string `toml:"token"`
  55. }
  56. func (mc managerConfig) APIBaseList() []string {
  57. if len(mc.APIList) > 0 {
  58. return mc.APIList
  59. }
  60. return []string{mc.APIBase}
  61. }
  62. type serverConfig struct {
  63. Hostname string `toml:"hostname"`
  64. Addr string `toml:"listen_addr"`
  65. Port int `toml:"listen_port"`
  66. SSLCert string `toml:"ssl_cert"`
  67. SSLKey string `toml:"ssl_key"`
  68. }
  69. type cgroupConfig struct {
  70. Enable bool `toml:"enable"`
  71. BasePath string `toml:"base_path"`
  72. Group string `toml:"group"`
  73. Subsystem string `toml:"subsystem"`
  74. }
  75. type dockerConfig struct {
  76. Enable bool `toml:"enable"`
  77. Volumes []string `toml:"volumes"`
  78. Options []string `toml:"options"`
  79. }
  80. type zfsConfig struct {
  81. Enable bool `toml:"enable"`
  82. Zpool string `toml:"zpool"`
  83. }
  84. type includeConfig struct {
  85. IncludeMirrors string `toml:"include_mirrors"`
  86. }
  87. type includedMirrorConfig struct {
  88. Mirrors []mirrorConfig `toml:"mirrors"`
  89. }
  90. type mirrorConfig struct {
  91. Name string `toml:"name"`
  92. Provider providerEnum `toml:"provider"`
  93. Upstream string `toml:"upstream"`
  94. Interval int `toml:"interval"`
  95. Retry int `toml:"retry"`
  96. MirrorDir string `toml:"mirror_dir"`
  97. LogDir string `toml:"log_dir"`
  98. Env map[string]string `toml:"env"`
  99. Role string `toml:"role"`
  100. // These two options over-write the global options
  101. ExecOnSuccess []string `toml:"exec_on_success"`
  102. ExecOnFailure []string `toml:"exec_on_failure"`
  103. // These two options the global options
  104. ExecOnSuccessExtra []string `toml:"exec_on_success_extra"`
  105. ExecOnFailureExtra []string `toml:"exec_on_failure_extra"`
  106. Command string `toml:"command"`
  107. UseIPv6 bool `toml:"use_ipv6"`
  108. ExcludeFile string `toml:"exclude_file"`
  109. Username string `toml:"username"`
  110. Password string `toml:"password"`
  111. Stage1Profile string `toml:"stage1_profile"`
  112. MemoryLimit string `toml:"memory_limit"`
  113. DockerImage string `toml:"docker_image"`
  114. DockerVolumes []string `toml:"docker_volumes"`
  115. DockerOptions []string `toml:"docker_options"`
  116. }
  117. // LoadConfig loads configuration
  118. func LoadConfig(cfgFile string) (*Config, error) {
  119. if _, err := os.Stat(cfgFile); err != nil {
  120. return nil, err
  121. }
  122. cfg := new(Config)
  123. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  124. logger.Errorf(err.Error())
  125. return nil, err
  126. }
  127. if cfg.Include.IncludeMirrors != "" {
  128. includedFiles, err := filepath.Glob(cfg.Include.IncludeMirrors)
  129. if err != nil {
  130. logger.Errorf(err.Error())
  131. return nil, err
  132. }
  133. for _, f := range includedFiles {
  134. var incMirCfg includedMirrorConfig
  135. if _, err := toml.DecodeFile(f, &incMirCfg); err != nil {
  136. logger.Errorf(err.Error())
  137. return nil, err
  138. }
  139. cfg.Mirrors = append(cfg.Mirrors, incMirCfg.Mirrors...)
  140. }
  141. }
  142. return cfg, nil
  143. }