2
0

config.go 3.5 KB

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