config.go 3.3 KB

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