2
0

config.go 4.1 KB

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