2
0

config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. Include includeConfig `toml:"include"`
  35. Mirrors []mirrorConfig `toml:"mirrors"`
  36. }
  37. type globalConfig struct {
  38. Name string `toml:"name"`
  39. LogDir string `toml:"log_dir"`
  40. MirrorDir string `toml:"mirror_dir"`
  41. Concurrent int `toml:"concurrent"`
  42. Interval int `toml:"interval"`
  43. }
  44. type managerConfig struct {
  45. APIBase string `toml:"api_base"`
  46. CACert string `toml:"ca_cert"`
  47. Token string `toml:"token"`
  48. }
  49. type serverConfig struct {
  50. Hostname string `toml:"hostname"`
  51. Addr string `toml:"listen_addr"`
  52. Port int `toml:"listen_port"`
  53. SSLCert string `toml:"ssl_cert"`
  54. SSLKey string `toml:"ssl_key"`
  55. }
  56. type cgroupConfig struct {
  57. Enable bool `toml:"enable"`
  58. BasePath string `toml:"base_path"`
  59. Group string `toml:"group"`
  60. }
  61. type includeConfig struct {
  62. IncludeMirrors string `toml:"include_mirrors"`
  63. }
  64. type includedMirrorConfig struct {
  65. Mirrors []mirrorConfig `toml:"mirrors"`
  66. }
  67. type mirrorConfig struct {
  68. Name string `toml:"name"`
  69. Provider providerEnum `toml:"provider"`
  70. Upstream string `toml:"upstream"`
  71. Interval int `toml:"interval"`
  72. MirrorDir string `toml:"mirror_dir"`
  73. LogDir string `toml:"log_dir"`
  74. Env map[string]string `toml:"env"`
  75. Role string `toml:"role"`
  76. ExecOnSuccess string `toml:"exec_on_success"`
  77. ExecOnFailure string `toml:"exec_on_failure"`
  78. Command string `toml:"command"`
  79. UseIPv6 bool `toml:"use_ipv6"`
  80. ExcludeFile string `toml:"exclude_file"`
  81. Password string `toml:"password"`
  82. Stage1Profile string `toml:"stage1_profile"`
  83. }
  84. // LoadConfig loads configuration
  85. func LoadConfig(cfgFile string) (*Config, error) {
  86. if _, err := os.Stat(cfgFile); err != nil {
  87. return nil, err
  88. }
  89. cfg := new(Config)
  90. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  91. logger.Errorf(err.Error())
  92. return nil, err
  93. }
  94. if cfg.Include.IncludeMirrors != "" {
  95. includedFiles, err := filepath.Glob(cfg.Include.IncludeMirrors)
  96. if err != nil {
  97. logger.Errorf(err.Error())
  98. return nil, err
  99. }
  100. for _, f := range includedFiles {
  101. var incMirCfg includedMirrorConfig
  102. if _, err := toml.DecodeFile(f, &incMirCfg); err != nil {
  103. logger.Errorf(err.Error())
  104. return nil, err
  105. }
  106. cfg.Mirrors = append(cfg.Mirrors, incMirCfg.Mirrors...)
  107. }
  108. }
  109. return cfg, nil
  110. }