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.8 KiB

12 years ago
12 years ago
  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/weed-fs/go/glog"
  10. "github.com/chrislusf/weed-fs/go/util"
  11. "github.com/chrislusf/weed-fs/go/weed/weed_server"
  12. )
  13. func init() {
  14. cmdVolume.Run = runVolume // break init cycle
  15. }
  16. var cmdVolume = &Command{
  17. UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
  18. Short: "start a volume server",
  19. Long: `start a volume server to provide storage spaces
  20. `,
  21. }
  22. var (
  23. vport = cmdVolume.Flag.Int("port", 8080, "http listen port")
  24. volumeSecurePort = cmdVolume.Flag.Int("port.secure", 8443, "https listen port, active when SSL certs are specified. Not ready yet.")
  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", "", "ip or server name")
  28. publicIp = cmdVolume.Flag.String("publicIp", "", "Publicly accessible <ip|server_name>")
  29. volumeBindIp = cmdVolume.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  30. masterNode = cmdVolume.Flag.String("mserver", "localhost:9333", "master server location")
  31. vpulse = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
  32. vTimeout = cmdVolume.Flag.Int("idleTimeout", 10, "connection idle seconds")
  33. vMaxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  34. dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  35. rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  36. volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  37. fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", true, "Adjust jpg orientation when uploading.")
  38. volumeWhiteList []string
  39. )
  40. func runVolume(cmd *Command, args []string) bool {
  41. if *vMaxCpu < 1 {
  42. *vMaxCpu = runtime.NumCPU()
  43. }
  44. runtime.GOMAXPROCS(*vMaxCpu)
  45. folders := strings.Split(*volumeFolders, ",")
  46. maxCountStrings := strings.Split(*maxVolumeCounts, ",")
  47. maxCounts := make([]int, 0)
  48. for _, maxString := range maxCountStrings {
  49. if max, e := strconv.Atoi(maxString); e == nil {
  50. maxCounts = append(maxCounts, max)
  51. } else {
  52. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  53. }
  54. }
  55. if len(folders) != len(maxCounts) {
  56. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  57. }
  58. for _, folder := range folders {
  59. if err := util.TestFolderWritable(folder); err != nil {
  60. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  61. }
  62. }
  63. if *publicIp == "" {
  64. if *ip == "" {
  65. *ip = "127.0.0.1"
  66. *publicIp = "localhost"
  67. } else {
  68. *publicIp = *ip
  69. }
  70. }
  71. if *volumeWhiteListOption != "" {
  72. volumeWhiteList = strings.Split(*volumeWhiteListOption, ",")
  73. }
  74. r := http.NewServeMux()
  75. volumeServer := weed_server.NewVolumeServer(r, *ip, *vport, *publicIp, folders, maxCounts,
  76. *masterNode, *vpulse, *dataCenter, *rack,
  77. volumeWhiteList,
  78. *fixJpgOrientation,
  79. )
  80. listeningAddress := *volumeBindIp + ":" + strconv.Itoa(*vport)
  81. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", listeningAddress)
  82. listener, e := util.NewListener(listeningAddress, time.Duration(*vTimeout)*time.Second)
  83. if e != nil {
  84. glog.Fatalf(e.Error())
  85. }
  86. OnInterrupt(func() {
  87. volumeServer.Shutdown()
  88. })
  89. if e := http.Serve(listener, r); e != nil {
  90. glog.Fatalf("Fail to serve:%s", e.Error())
  91. }
  92. return true
  93. }