config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  66. type dockerConfig struct {
  67. Enable bool `toml:"enable"`
  68. Volumes []string `toml:"volumes"`
  69. Options []string `toml:"options"`
  70. }
  71. type zfsConfig struct {
  72. Enable bool `toml:"enable"`
  73. Zpool string `toml:"zpool"`
  74. }
  75. type includeConfig struct {
  76. IncludeMirrors string `toml:"include_mirrors"`
  77. }
  78. type includedMirrorConfig struct {
  79. Mirrors []mirrorConfig `toml:"mirrors"`
  80. }
  81. type mirrorConfig struct {
  82. Name string `toml:"name"`
  83. Provider providerEnum `toml:"provider"`
  84. Upstream string `toml:"upstream"`
  85. Interval int `toml:"interval"`
  86. MirrorDir string `toml:"mirror_dir"`
  87. LogDir string `toml:"log_dir"`
  88. Env map[string]string `toml:"env"`
  89. Role string `toml:"role"`
  90. // These two options over-write the global options
  91. ExecOnSuccess []string `toml:"exec_on_success"`
  92. ExecOnFailure []string `toml:"exec_on_failure"`
  93. // These two options the global options
  94. ExecOnSuccessExtra []string `toml:"exec_on_success_extra"`
  95. ExecOnFailureExtra []string `toml:"exec_on_failure_extra"`
  96. Command string `toml:"command"`
  97. UseIPv6 bool `toml:"use_ipv6"`
  98. ExcludeFile string `toml:"exclude_file"`
  99. Username string `toml:"username"`
  100. Password string `toml:"password"`
  101. Stage1Profile string `toml:"stage1_profile"`
  102. DockerImage string `toml:"docker_image"`
  103. DockerVolumes []string `toml:"docker_volumes"`
  104. DockerOptions []string `toml:"docker_options"`
  105. }
  106. // LoadConfig loads configuration
  107. func LoadConfig(cfgFile string) (*Config, error) {
  108. if _, err := os.Stat(cfgFile); err != nil {
  109. return nil, err
  110. }
  111. cfg := new(Config)
  112. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  113. logger.Errorf(err.Error())
  114. return nil, err
  115. }
  116. if cfg.Include.IncludeMirrors != "" {
  117. includedFiles, err := filepath.Glob(cfg.Include.IncludeMirrors)
  118. if err != nil {
  119. logger.Errorf(err.Error())
  120. return nil, err
  121. }
  122. for _, f := range includedFiles {
  123. var incMirCfg includedMirrorConfig
  124. if _, err := toml.DecodeFile(f, &incMirCfg); err != nil {
  125. logger.Errorf(err.Error())
  126. return nil, err
  127. }
  128. cfg.Mirrors = append(cfg.Mirrors, incMirCfg.Mirrors...)
  129. }
  130. }
  131. return cfg, nil
  132. }