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.

128 lines
3.3 KiB

7 years ago
7 years ago
  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. [cassandra]
  57. # CREATE TABLE filemeta (
  58. # directory varchar,
  59. # name varchar,
  60. # meta blob,
  61. # PRIMARY KEY (directory, name)
  62. # ) WITH CLUSTERING ORDER BY (name ASC);
  63. enabled = false
  64. keyspace="seaweedfs"
  65. hosts=[
  66. "localhost:9042",
  67. ]
  68. [redis]
  69. enabled = true
  70. address = "localhost:6379"
  71. password = ""
  72. db = 0
  73. `
  74. )
  75. var (
  76. Stores []FilerStore
  77. )
  78. func (f *Filer) LoadConfiguration() {
  79. // find a filer store
  80. viper.SetConfigName("filer") // name of config file (without extension)
  81. viper.AddConfigPath(".") // optionally look for config in the working directory
  82. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  83. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  84. if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
  85. glog.Fatalf("Failed to load filer.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/" +
  86. "\n\nPlease follow this example and add a filer.toml file to " +
  87. "current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/:\n" + FILER_TOML_EXAMPLE)
  88. }
  89. glog.V(0).Infof("Reading filer configuration from %s", viper.ConfigFileUsed())
  90. for _, store := range Stores {
  91. if viper.GetBool(store.GetName() + ".enabled") {
  92. viperSub := viper.Sub(store.GetName())
  93. if err := store.Initialize(viperSub); err != nil {
  94. glog.Fatalf("Failed to initialize store for %s: %+v",
  95. store.GetName(), err)
  96. }
  97. f.SetStore(store)
  98. glog.V(0).Infof("Configure filer for %s from %s", store.GetName(), viper.ConfigFileUsed())
  99. return
  100. }
  101. }
  102. println()
  103. println("Supported filer stores are:")
  104. for _, store := range Stores {
  105. println(" " + store.GetName())
  106. }
  107. println()
  108. println("Please configure a supported filer store in", viper.ConfigFileUsed())
  109. println()
  110. os.Exit(-1)
  111. }