config.go 3.9 KB

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