config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package worker
  2. import (
  3. "errors"
  4. "os"
  5. "github.com/BurntSushi/toml"
  6. )
  7. type providerEnum uint8
  8. const (
  9. provRsync providerEnum = iota
  10. provTwoStageRsync
  11. provCommand
  12. )
  13. func (p *providerEnum) UnmarshalText(text []byte) error {
  14. s := string(text)
  15. switch s {
  16. case `command`:
  17. *p = provCommand
  18. case `rsync`:
  19. *p = provRsync
  20. case `two-stage-rsync`:
  21. *p = provTwoStageRsync
  22. default:
  23. return errors.New("Invalid value to provierEnum")
  24. }
  25. return nil
  26. }
  27. // Worker config options
  28. type Config struct {
  29. Global globalConfig `toml:"global"`
  30. Manager managerConfig `toml:"manager"`
  31. Server serverConfig `toml:"server"`
  32. Cgroup cgroupConfig `toml:"cgroup"`
  33. Mirrors []mirrorConfig `toml:"mirrors"`
  34. }
  35. type globalConfig struct {
  36. Name string `toml:"name"`
  37. LogDir string `toml:"log_dir"`
  38. MirrorDir string `toml:"mirror_dir"`
  39. Concurrent int `toml:"concurrent"`
  40. Interval int `toml:"interval"`
  41. }
  42. type managerConfig struct {
  43. APIBase string `toml:"api_base"`
  44. CACert string `toml:"ca_cert"`
  45. Token string `toml:"token"`
  46. }
  47. type serverConfig struct {
  48. Hostname string `toml:"hostname"`
  49. Addr string `toml:"listen_addr"`
  50. Port int `toml:"listen_port"`
  51. SSLCert string `toml:"ssl_cert"`
  52. SSLKey string `toml:"ssl_key"`
  53. }
  54. type cgroupConfig struct {
  55. Enable bool `toml:"enable"`
  56. BasePath string `toml:"base_path"`
  57. Group string `toml:"group"`
  58. }
  59. type mirrorConfig struct {
  60. Name string `toml:"name"`
  61. Provider providerEnum `toml:"provider"`
  62. Upstream string `toml:"upstream"`
  63. Interval int `toml:"interval"`
  64. MirrorDir string `toml:"mirror_dir"`
  65. LogDir string `toml:"log_dir"`
  66. Env map[string]string `toml:"env"`
  67. Role string `toml:"role"`
  68. ExecOnSuccess string `toml:"exec_on_success"`
  69. ExecOnFailure string `toml:"exec_on_failure"`
  70. Command string `toml:"command"`
  71. UseIPv6 bool `toml:"use_ipv6"`
  72. ExcludeFile string `toml:"exclude_file"`
  73. Password string `toml:"password"`
  74. Stage1Profile string `toml:"stage1_profile"`
  75. }
  76. // LoadConfig loads configuration
  77. func LoadConfig(cfgFile string) (*Config, error) {
  78. if _, err := os.Stat(cfgFile); err != nil {
  79. return nil, err
  80. }
  81. cfg := new(Config)
  82. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  83. logger.Errorf(err.Error())
  84. return nil, err
  85. }
  86. return cfg, nil
  87. }