You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.3 KiB

6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
  1. package filer
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/spf13/viper"
  5. "os"
  6. "reflect"
  7. "strings"
  8. )
  9. var (
  10. Stores []FilerStore
  11. )
  12. func (f *Filer) LoadConfiguration(config *viper.Viper) {
  13. validateOneEnabledStore(config)
  14. // load configuration for default filer store
  15. hasDefaultStoreConfigured := false
  16. for _, store := range Stores {
  17. if config.GetBool(store.GetName() + ".enabled") {
  18. store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(FilerStore)
  19. if err := store.Initialize(config, store.GetName()+"."); err != nil {
  20. glog.Fatalf("failed to initialize store for %s: %+v", store.GetName(), err)
  21. }
  22. f.SetStore(store)
  23. glog.V(0).Infof("configured filer store to %s", store.GetName())
  24. hasDefaultStoreConfigured = true
  25. break
  26. }
  27. }
  28. if !hasDefaultStoreConfigured {
  29. println()
  30. println("Supported filer stores are:")
  31. for _, store := range Stores {
  32. println(" " + store.GetName())
  33. }
  34. os.Exit(-1)
  35. }
  36. // load path-specific filer store here
  37. // f.Store.AddPathSpecificStore(path, store)
  38. storeNames := make(map[string]FilerStore)
  39. for _, store := range Stores {
  40. storeNames[store.GetName()] = store
  41. }
  42. allKeys := config.AllKeys()
  43. for _, key := range allKeys {
  44. if !strings.HasSuffix(key, ".enabled") {
  45. continue
  46. }
  47. key = key[:len(key)-len(".enabled")]
  48. if !strings.Contains(key, ".") {
  49. continue
  50. }
  51. parts := strings.Split(key, ".")
  52. storeName, storeId := parts[0], parts[1]
  53. store, found := storeNames[storeName]
  54. if !found {
  55. continue
  56. }
  57. store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(FilerStore)
  58. if err := store.Initialize(config, key+"."); err != nil {
  59. glog.Fatalf("Failed to initialize store for %s: %+v", key, err)
  60. }
  61. location := config.GetString(key + ".location")
  62. if location == "" {
  63. glog.Errorf("path-specific filer store needs %s", key+".location")
  64. os.Exit(-1)
  65. }
  66. f.Store.AddPathSpecificStore(location, storeId, store)
  67. glog.V(0).Infof("configure filer %s for %s", store.GetName(), location)
  68. }
  69. }
  70. func validateOneEnabledStore(config *viper.Viper) {
  71. enabledStore := ""
  72. for _, store := range Stores {
  73. if config.GetBool(store.GetName() + ".enabled") {
  74. if enabledStore == "" {
  75. enabledStore = store.GetName()
  76. } else {
  77. glog.Fatalf("Filer store is enabled for both %s and %s", enabledStore, store.GetName())
  78. }
  79. }
  80. }
  81. }