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.

109 lines
3.0 KiB

  1. package filer2
  2. import (
  3. "os"
  4. "github.com/spf13/viper"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. )
  7. const (
  8. FILER_TOML_EXAMPLE = `
  9. # A sample TOML config file for SeaweedFS filer store
  10. [memory]
  11. # local in memory, mostly for testing purpose
  12. enabled = false
  13. [leveldb]
  14. # local on disk, mostly for simple single-machine setup, fairly scalable
  15. enabled = false
  16. dir = "." # directory to store level db files
  17. [mysql]
  18. # multiple filers on shared storage, fairly scalable
  19. #
  20. # need to choose or create a database.
  21. # need to manually create a table "filemeta".
  22. #
  23. # CREATE TABLE IF NOT EXISTS filemeta (
  24. # dirhash BIGINT COMMENT 'first 64 bits of MD5 hash value of directory field',
  25. # name VARCHAR(1000) COMMENT 'directory or file name',
  26. # directory VARCHAR(4096) COMMENT 'full path to parent directory',
  27. # meta BLOB,
  28. # PRIMARY KEY (dirhash, name)
  29. # ) DEFAULT CHARSET=utf8;
  30. #
  31. enabled = true
  32. hostname = "localhost"
  33. port = 3306
  34. username = "root"
  35. password = ""
  36. database = "" # create or use an existing database
  37. connection_max_idle = 2
  38. connection_max_open = 100
  39. [postgres]
  40. # CREATE TABLE IF NOT EXISTS filemeta (
  41. # dirhash BIGINT,
  42. # name VARCHAR(1000),
  43. # directory VARCHAR(4096),
  44. # meta bytea,
  45. # PRIMARY KEY (dirhash, name)
  46. # );
  47. enabled = false
  48. hostname = "localhost"
  49. port = 5432
  50. username = "postgres"
  51. password = ""
  52. database = "" # create or use an existing database
  53. sslmode = "disable"
  54. connection_max_idle = 100
  55. connection_max_open = 100
  56. `
  57. )
  58. var (
  59. Stores []FilerStore
  60. )
  61. func (f *Filer) LoadConfiguration() {
  62. // find a filer store
  63. viper.SetConfigName("filer") // name of config file (without extension)
  64. viper.AddConfigPath(".") // optionally look for config in the working directory
  65. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  66. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  67. if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
  68. glog.Fatalf("Failed to load filer.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/" +
  69. "\n\nPlease follow this example and add a filer.toml file to " +
  70. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n" + FILER_TOML_EXAMPLE)
  71. }
  72. glog.V(0).Infof("Reading filer configuration from %s", viper.ConfigFileUsed())
  73. for _, store := range Stores {
  74. if viper.GetBool(store.GetName() + ".enabled") {
  75. viperSub := viper.Sub(store.GetName())
  76. if err := store.Initialize(viperSub); err != nil {
  77. glog.Fatalf("Failed to initialize store for %s: %+v",
  78. store.GetName(), err)
  79. }
  80. f.SetStore(store)
  81. glog.V(0).Infof("Configure filer for %s from %s", store.GetName(), viper.ConfigFileUsed())
  82. return
  83. }
  84. }
  85. println()
  86. println("Supported filer stores are:")
  87. for _, store := range Stores {
  88. println(" " + store.GetName())
  89. }
  90. println()
  91. println("Please configure a supported filer store in", viper.ConfigFileUsed())
  92. println()
  93. os.Exit(-1)
  94. }