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.

100 lines
3.5 KiB

13 years ago
13 years ago
12 years ago
  1. package main
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/weed/weed_server"
  5. "github.com/gorilla/mux"
  6. "net/http"
  7. "os"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func init() {
  14. cmdVolume.Run = runVolume // break init cycle
  15. cmdVolume.IsDebug = cmdVolume.Flag.Bool("debug", false, "enable debug mode")
  16. }
  17. var cmdVolume = &Command{
  18. UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
  19. Short: "start a volume server",
  20. Long: `start a volume server to provide storage spaces
  21. `,
  22. }
  23. var (
  24. vport = cmdVolume.Flag.Int("port", 8080, "http listen port")
  25. volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  26. maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
  27. ip = cmdVolume.Flag.String("ip", "localhost", "ip or server name")
  28. publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible <ip|server_name>:<port>")
  29. masterNode = cmdVolume.Flag.String("mserver", "localhost:9333", "master server location")
  30. vpulse = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than the master's setting")
  31. vReadTimeout = cmdVolume.Flag.Int("readTimeout", 3, "connection read timeout in seconds. Increase this if uploading large files.")
  32. vMaxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  33. dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  34. rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  35. volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  36. volumeWhiteList []string
  37. )
  38. func runVolume(cmd *Command, args []string) bool {
  39. if *vMaxCpu < 1 {
  40. *vMaxCpu = runtime.NumCPU()
  41. }
  42. runtime.GOMAXPROCS(*vMaxCpu)
  43. folders := strings.Split(*volumeFolders, ",")
  44. maxCountStrings := strings.Split(*maxVolumeCounts, ",")
  45. maxCounts := make([]int, 0)
  46. for _, maxString := range maxCountStrings {
  47. if max, e := strconv.Atoi(maxString); e == nil {
  48. maxCounts = append(maxCounts, max)
  49. } else {
  50. glog.Fatalf("The max specified in -max not a valid number %s", max)
  51. }
  52. }
  53. if len(folders) != len(maxCounts) {
  54. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  55. }
  56. for _, folder := range folders {
  57. fileInfo, err := os.Stat(folder)
  58. if err != nil {
  59. glog.Fatalf("No Existing Folder:%s", folder)
  60. }
  61. if !fileInfo.IsDir() {
  62. glog.Fatalf("Volume Folder should not be a file:%s", folder)
  63. }
  64. perm := fileInfo.Mode().Perm()
  65. glog.V(0).Infoln("Volume Folder", folder)
  66. glog.V(0).Infoln("Permission:", perm)
  67. }
  68. if *publicUrl == "" {
  69. *publicUrl = *ip + ":" + strconv.Itoa(*vport)
  70. }
  71. if *volumeWhiteListOption != "" {
  72. volumeWhiteList = strings.Split(*volumeWhiteListOption, ",")
  73. }
  74. r := mux.NewRouter()
  75. weed_server.NewVolumeServer(r, VERSION, *ip, *vport, *publicUrl, folders, maxCounts,
  76. *masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList,
  77. )
  78. glog.V(0).Infoln("Start Weed volume server", VERSION, "at http://"+*ip+":"+strconv.Itoa(*vport))
  79. srv := &http.Server{
  80. Addr: ":" + strconv.Itoa(*vport),
  81. Handler: r,
  82. ReadTimeout: (time.Duration(*vReadTimeout) * time.Second),
  83. }
  84. e := srv.ListenAndServe()
  85. if e != nil {
  86. glog.Fatalf("Fail to start:%s", e.Error())
  87. }
  88. return true
  89. }